From 05ca6aea77090f0866194f2a01c669adbf8c4310 Mon Sep 17 00:00:00 2001 From: mozman Date: Fri, 8 Nov 2024 17:31:49 +0100 Subject: [PATCH] add BaseAttrib.discard_mtext() method --- docs/source/blocks/attdef.rst | 2 ++ docs/source/blocks/attrib.rst | 1 + src/ezdxf/entities/attrib.py | 11 ++++++++++- tests/test_02_dxf_graphics/test_208_attrib.py | 11 +++++++++-- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/docs/source/blocks/attdef.rst b/docs/source/blocks/attdef.rst index f081a4804..da14b037e 100644 --- a/docs/source/blocks/attdef.rst +++ b/docs/source/blocks/attdef.rst @@ -60,4 +60,6 @@ Inherited DXF attributes :ref:`Common graphical DXF attributes` .. automethod:: embed_mtext + .. automethod:: discard_mtext + .. _DXF Reference: http://help.autodesk.com/view/OARX/2018/ENU/?guid=GUID-F0EA099B-6F88-4BCC-BEC7-247BA64838A4 \ No newline at end of file diff --git a/docs/source/blocks/attrib.rst b/docs/source/blocks/attrib.rst index e82b011ff..e2fe50fe2 100644 --- a/docs/source/blocks/attrib.rst +++ b/docs/source/blocks/attrib.rst @@ -54,5 +54,6 @@ Inherited DXF attributes :ref:`Common graphical DXF attributes` .. automethod:: embed_mtext + .. automethod:: discard_mtext .. _DXF Reference: http://help.autodesk.com/view/OARX/2018/ENU/?guid=GUID-7DD8B495-C3F8-48CD-A766-14F9D7D0DD9B \ No newline at end of file diff --git a/src/ezdxf/entities/attrib.py b/src/ezdxf/entities/attrib.py index 2693be77f..b672f6f96 100644 --- a/src/ezdxf/entities/attrib.py +++ b/src/ezdxf/entities/attrib.py @@ -366,7 +366,7 @@ def set_mtext(self, mtext: MText, graphic_properties=True) -> None: def embed_mtext(self, mtext: MText, graphic_properties=True) -> None: """Set multi-line properties from a :class:`MText` entity and destroy the - source entity afterwards. + source entity afterward. The multi-line ATTRIB/ATTDEF entity requires DXF R2018, otherwise an ordinary single line ATTRIB/ATTDEF entity will be exported. @@ -380,6 +380,15 @@ def embed_mtext(self, mtext: MText, graphic_properties=True) -> None: self.set_mtext(mtext, graphic_properties) mtext.destroy() + def discard_mtext(self) -> None: + """Discard multi-line feature. + + The embedded MTEXT will be removed and the ATTRIB/ATTDEF will be converted to a + single-line attribute. + """ + self._embedded_mtext = None + self.dxf.attribute_type = const.ATTRIB_TYPE_SINGLE_LINE + def register_resources(self, registry: xref.Registry) -> None: """Register required resources to the resource registry.""" super().register_resources(registry) diff --git a/tests/test_02_dxf_graphics/test_208_attrib.py b/tests/test_02_dxf_graphics/test_208_attrib.py index a43afe884..79d50dd03 100644 --- a/tests/test_02_dxf_graphics/test_208_attrib.py +++ b/tests/test_02_dxf_graphics/test_208_attrib.py @@ -6,7 +6,7 @@ import ezdxf from ezdxf.entities import Attrib, MText from ezdxf.entities.attrib import EmbeddedMText, EmbeddedMTextNS -from ezdxf.lldxf.const import DXF12, DXF2000 +from ezdxf.lldxf import const from ezdxf.lldxf.tagwriter import TagCollector, basic_tags_from_text from ezdxf.math import Matrix44 from ezdxf.audit import Auditor @@ -159,7 +159,9 @@ def test_load_from_text(entity): assert entity.dxf.insert == (0, 0, 0) -@pytest.mark.parametrize("txt,ver", [(ENTITY_R2000, DXF2000), (ENTITY_R12, DXF12)]) +@pytest.mark.parametrize( + "txt,ver", [(ENTITY_R2000, const.DXF2000), (ENTITY_R12, const.DXF12)] +) def test_write_dxf(txt, ver): expected = basic_tags_from_text(txt) attdef = TEST_CLASS.from_text(txt) @@ -237,6 +239,11 @@ def test_set_mtext(self): attrib.set_mtext(mtext) assert attrib.has_embedded_mtext_entity is True + def test_discard_mtext(self, attrib): + attrib.discard_mtext() + assert attrib.has_embedded_mtext_entity is False + assert attrib.dxf.attribute_type == const.ATTRIB_TYPE_SINGLE_LINE + def test_transformation_of_embedded_mtext(self): attrib = Attrib.new(dxfattribs={"color": 3, "text": "TEST"}) mtext = MText.new(dxfattribs={"color": 3})