Skip to content

Commit

Permalink
add BaseAttrib.audit() method
Browse files Browse the repository at this point in the history
Destroys ATTRIB and ATTDEF entities without
a "tag" attribute.
  • Loading branch information
mozman committed Nov 8, 2024
1 parent 0b87729 commit b14aa29
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/ezdxf/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class AuditError(IntEnum):
INVALID_CREASE_VALUE_COUNT = 224
INVALID_ELLIPSE_RATIO = 225
INVALID_HATCH_BOUNDARY_PATH = 226
TAG_ATTRIBUTE_MISSING = 227


REQUIRED_ROOT_DICT_ENTRIES = ("ACAD_GROUP", "ACAD_PLOTSTYLENAME")
Expand All @@ -111,7 +112,7 @@ def __init__(
self.message: str = message # error message
self.data: Any = data # additional data as an arbitrary object

# pylint: disable=too-many-public-methods

class Auditor:
def __init__(self, doc: Drawing) -> None:
assert doc is not None and doc.rootdict is not None and doc.entitydb is not None
Expand Down
14 changes: 13 additions & 1 deletion src/ezdxf/entities/attrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from typing import TYPE_CHECKING, Optional
from typing_extensions import Self
import copy

from ezdxf.audit import Auditor, AuditError
from ezdxf.lldxf import validator
from ezdxf.math import NULLVEC, Vec3, Z_AXIS, OCS, Matrix44
from ezdxf.lldxf.attributes import (
Expand Down Expand Up @@ -40,7 +42,6 @@
if TYPE_CHECKING:
from ezdxf.lldxf.tagwriter import AbstractTagWriter
from ezdxf.lldxf.tags import Tags
from ezdxf.entities import DXFEntity
from ezdxf import xref


Expand Down Expand Up @@ -81,6 +82,7 @@
# ezdxf stores the last group code 280 as "lock_position" attribute and does
# not export a version tag for any DXF version.
# Tag string (cannot contain spaces):
# Mandatory by AutoCAD!
"tag": DXFAttr(
2,
default="",
Expand Down Expand Up @@ -402,6 +404,16 @@ def transform(self, m: Matrix44) -> Self:
self.post_transform(m)
return self

def audit(self, auditor: Auditor) -> None:
"""Validity check."""
super().audit(auditor)
if not self.dxf.hasattr("tag"):
auditor.fixed_error(
code=AuditError.TAG_ATTRIBUTE_MISSING,
message=f'Missing mandatory "tag" attribute, entity {str(self)} deleted.',
)
auditor.trash(self)


def _update_content_from_mtext(text: Text, mtext: MText) -> None:
content = mtext.plain_text(split=True, fast=True)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_02_dxf_graphics/test_208_attrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ezdxf.lldxf.const import DXF12, DXF2000
from ezdxf.lldxf.tagwriter import TagCollector, basic_tags_from_text
from ezdxf.math import Matrix44
from ezdxf.audit import Auditor

TEST_CLASS = Attrib
TEST_TYPE = "ATTRIB"
Expand Down Expand Up @@ -295,6 +296,19 @@ def test_version_without_lock_position():
assert attrib.dxf.lock_position == 7


def test_audit_destroys_attrib_without_tag_attribute(doc):
# Unbound entities cannot be audited!
attrib = Attrib.new()
doc.modelspace().add_entity(attrib)

auditor = Auditor(doc)
attrib.audit(auditor)
auditor.empty_trashcan()

assert len(auditor.fixes) == 1
assert attrib.is_alive is False


LOCK_POSITION_AND_VERSION = """0
ATTRIB
5
Expand Down
3 changes: 2 additions & 1 deletion tests/test_04_dxf_high_level_structs/test_424_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ def test_remove_standalone_attrib_entities_from_blocks():
# The model space is just a BLOCK!
doc = ezdxf.new()
msp = doc.modelspace()
msp.add_entity(Attrib())
# Missing tag is a different issue!
msp.add_entity(Attrib.new(dxfattribs={"tag": "test"}))
auditor = doc.audit()
assert len(list(msp)) == 0
assert len(auditor.fixes) == 1
Expand Down

0 comments on commit b14aa29

Please sign in to comment.