Skip to content

Commit f335635

Browse files
committed
add r12_strict.clean() function
1 parent 8dfa850 commit f335635

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

integration_tests/test_r12strict.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,28 @@ def test_translated_block_content_attributes(doc: Drawing):
174174
assert entity.dxf.linetype == "MY_LTYPE"
175175

176176

177+
def test_clean_xdata_from_table_entries():
178+
doc = ezdxf.new("R12")
179+
doc.appids.new("MY_ID")
180+
layer = doc.layers.new("MY_LAYER")
181+
layer.set_xdata("MY_ID", [(1000, "Test")])
182+
183+
r12strict.clean(doc)
184+
assert layer.xdata is None, "expected XDATA to be deleted"
185+
186+
187+
def test_remove_legacy_blocks():
188+
doc = ezdxf.new("R12")
189+
doc.blocks.new("$Model_Space")
190+
doc.blocks.new("$Paper_Space")
191+
192+
r12strict.clean(doc)
193+
# block names are case-insensitive
194+
assert "$MODEL_SPACE" not in doc.blocks
195+
assert "$PAPER_SPACE" not in doc.blocks
196+
assert doc.block_records.has_entry("$Model_Space") is False
197+
assert doc.block_records.has_entry("$Paper_Space") is False
198+
199+
177200
if __name__ == "__main__":
178201
pytest.main([__file__])

src/ezdxf/r12strict.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ezdxf.document import Drawing
1010
from ezdxf.sections.table import Table
1111

12-
__all__ = ["translate_names", "purify", "R12NameTranslator"]
12+
__all__ = ["translate_names", "clean", "R12NameTranslator"]
1313

1414

1515
def translate_names(doc: Drawing) -> None:
@@ -35,15 +35,41 @@ def translate_names(doc: Drawing) -> None:
3535
_R12StrictRename(doc).execute()
3636

3737

38-
def purify(doc: Drawing) -> None:
39-
"""Remove or destroy all features and entity types that are not supported by DXF
40-
version R12.
38+
def clean(doc: Drawing) -> None:
39+
"""Remove all features that are not supported for DXF version R12 by Autodesk
40+
products.
4141
"""
4242
if doc.dxfversion != const.DXF12:
4343
raise const.DXFVersionError(
4444
f"expected DXF document version R12, got: {doc.acad_release}"
4545
)
46-
raise NotImplementedError()
46+
_remove_table_xdata(doc.appids)
47+
_remove_table_xdata(doc.linetypes)
48+
_remove_table_xdata(doc.layers)
49+
_remove_table_xdata(doc.styles)
50+
_remove_table_xdata(doc.dimstyles)
51+
_remove_table_xdata(doc.ucs)
52+
_remove_table_xdata(doc.views)
53+
_remove_table_xdata(doc.viewports)
54+
_remove_legacy_blocks(doc)
55+
56+
57+
def _remove_table_xdata(table: Table) -> None:
58+
"""Autodesk products do not accept XDATA in table entries for DXF R12."""
59+
for entry in list(table):
60+
entry.xdata = None
61+
62+
63+
def _remove_legacy_blocks(doc: Drawing) -> None:
64+
"""Due to bad conversion some DXF files contain after loading the blocks
65+
"$MODEL_SPACE" and "$PAPER_SPACE", delete them (no content) because they will
66+
clash with the translated layout names of "*Model_Space" and "*Paper_Space".
67+
"""
68+
for name in ("$MODEL_SPACE", "$PAPER_SPACE"):
69+
try:
70+
doc.blocks.delete_block(name, safe=False)
71+
except const.DXFKeyError:
72+
pass
4773

4874

4975
class R12NameTranslator:

0 commit comments

Comments
 (0)