Debian Codegraph @

tests/test_03_dxf_layouts/test_303_auto_block_references.py

raw symbols SCIP
1 # Copyright (c) 2011-2024, Manfred Moitzi
2 # License: MIT License
3 import pytest
4 import ezdxf
5 from ezdxf.lldxf import const
6 from ezdxf.entities import MText, Insert
7
8
9 @pytest.fixture
10 def doc():
11 return ezdxf.new()
12
13
14 @pytest.fixture
15 def block(doc):
16 block = doc.blocks.new("TEST")
17 block.add_attdef("TAG1", (0, 0), text="default text1")
18 block.add_attdef("TAG2", (0, 5), text="default text2")
19 return block
20
21
22 def test_auto_blockref(doc, block):
23 values = {"TAG1": "text1", "TAG2": "text2"}
24 msp = doc.modelspace()
25 blockref = msp.add_auto_blockref("TEST", (0, 0), values)
26 autoblock = doc.blocks[blockref.dxf.name]
27 entities = list(autoblock)
28 insert = entities[0]
29 assert isinstance(insert, Insert) is True
30
31 attrib1, attrib2 = insert.attribs
32 assert attrib1.dxftype() == "ATTRIB"
33 assert attrib1.dxf.tag == "TAG1"
34 assert attrib1.dxf.text == "text1"
35 assert attrib1.dxf.attribute_type == const.ATTRIB_TYPE_SINGLE_LINE
36
37 assert attrib2.dxftype() == "ATTRIB"
38 assert attrib2.dxf.tag == "TAG2"
39 assert attrib2.dxf.text == "text2"
40 assert attrib2.dxf.attribute_type == const.ATTRIB_TYPE_SINGLE_LINE
41
42
43 def test_add_auto_attribs(doc, block):
44 values = {"TAG1": "text1", "TAG2": "text2"}
45 msp = doc.modelspace()
46 blockref = msp.add_blockref("TEST", (0, 0)).add_auto_attribs(values)
47 assert blockref.dxftype() == "INSERT"
48
49 attrib1, attrib2 = blockref.attribs
50 assert attrib1.dxftype() == "ATTRIB"
51 assert attrib1.dxf.tag == "TAG1"
52 assert attrib1.dxf.text == "text1"
53 assert attrib1.dxf.attribute_type == const.ATTRIB_TYPE_SINGLE_LINE
54
55 assert attrib2.dxftype() == "ATTRIB"
56 assert attrib2.dxf.tag == "TAG2"
57 assert attrib2.dxf.text == "text2"
58 assert attrib2.dxf.attribute_type == const.ATTRIB_TYPE_SINGLE_LINE
59
60
61 def test_add_auto_attribs_with_default_text(doc, block):
62 msp = doc.modelspace()
63 blockref = msp.add_blockref("TEST", (0, 0)).add_auto_attribs({})
64 attrib1, attrib2 = blockref.attribs
65 assert attrib1.dxf.text == "default text1"
66 assert attrib2.dxf.text == "default text2"
67
68
69 def test_add_auto_attribs_with_missing_tag():
70 doc = ezdxf.new()
71 msp = doc.modelspace()
72 blocks = doc.blocks.new("TEST")
73 attdef = blocks.add_attdef("TAG1", (0, 0))
74 attdef.dxf.discard("tag") # simulate erroneous DXF input
75
76 blockref = msp.add_blockref("TEST", (0, 0)).add_auto_attribs({"TAG1": "Test"})
77 assert blockref.has_attrib("TAG1") is False
78
79
80 def test_add_auto_attribs_with_missing_location():
81 doc = ezdxf.new()
82 msp = doc.modelspace()
83 blocks = doc.blocks.new("TEST")
84 attdef = blocks.add_attdef("TAG1", (0, 0))
85 attdef.dxf.discard("insert") # simulate erroneous DXF input
86
87 blockref = msp.add_blockref("TEST", (0, 0)).add_auto_attribs({"TAG1": "Test"})
88 assert blockref.has_attrib("TAG1") is False
89
90
91 def test_has_attdef(block):
92 assert block.has_attdef("TAG1") is True
93 assert block.has_attdef("TAG_Z") is False
94
95
96 def test_get_attdef(block):
97 attdef = block.get_attdef("TAG1")
98 assert attdef.dxf.tag == "TAG1"
99 assert block.get_attdef("TAG1_Z") is None
100
101
102 def test_get_attdef_text(block):
103 block.add_attdef("TAGX", insert=(0, 0), text="PRESET_TEXT")
104 text = block.get_attdef_text("TAGX")
105 assert text == "PRESET_TEXT"
106
107
108 def test_attdef_getter_properties(block):
109 attrib = block.get_attdef("TAG1")
110
111 assert attrib.is_const is False
112 assert attrib.is_invisible is False
113 assert attrib.is_verify is False
114 assert attrib.is_preset is False
115
116
117 def test_attdef_setter_properties(block):
118 attrib = block.get_attdef("TAG1")
119
120 assert attrib.is_const is False
121 attrib.is_const = True
122 assert attrib.is_const is True
123
124 assert attrib.is_invisible is False
125 attrib.is_invisible = True
126 assert attrib.is_invisible is True
127
128 assert attrib.is_verify is False
129 attrib.is_verify = True
130 assert attrib.is_verify is True
131
132 assert attrib.is_preset is False
133 attrib.is_preset = True
134 assert attrib.is_preset is True
135
136
137 class TestMultiLineAttribs:
138 @pytest.fixture
139 def doc(self):
140 return ezdxf.new()
141
142 @pytest.fixture
143 def block(self, doc):
144 block = doc.blocks.new("TEST")
145 attdef = block.add_attdef("TAG1")
146 # Note: \n is converted to \P automatically at DXF export
147 mtext = MText.new(dxfattribs={"insert": (1, 1), "text": "default\ntext"})
148 attdef.embed_mtext(mtext)
149 return block
150
151 def test_has_multi_line_attdef(self, block):
152 attdef = block.get_attdef("TAG1")
153 assert attdef is not None
154 assert attdef.has_embedded_mtext_entity is True
155 assert attdef.dxf.attribute_type == const.ATTDEF_TYPE_MULTI_LINE
156
157 def test_multi_line_attdef_properties(self, block):
158 attdef = block.get_attdef("TAG1")
159 assert attdef is not None
160 assert attdef.dxf.insert == (1, 1)
161 assert attdef.dxf.text == "default", "expected just first line"
162
163 mtext = attdef.virtual_mtext_entity()
164 assert isinstance(mtext, MText) is True
165
166 assert mtext.dxf.insert == (1, 1)
167 assert mtext.dxf.text == "default\ntext", "expected all lines"
168
169 def test_add_auto_multi_line_attrib(self, doc, block):
170 msp = doc.modelspace()
171 insert = msp.add_blockref(block.name, insert=(2, 3))
172 assert insert is not None
173
174 insert.add_auto_attribs({"TAG1": "MultiLine\nAttribute"})
175
176 attrib = insert.attribs[0]
177 assert attrib is not None
178
179 assert attrib.dxf.insert == (3, 4)
180 assert attrib.dxf.text == "MultiLine", "expected just first line"
181 assert attrib.has_embedded_mtext_entity is True
182 assert attrib.dxf.attribute_type == const.ATTRIB_TYPE_MULTI_LINE
183
184 mtext = attrib.virtual_mtext_entity()
185 assert isinstance(mtext, MText) is True
186
187 assert mtext.dxf.insert == (3, 4)
188 assert mtext.dxf.text == "MultiLine\nAttribute", "expected all lines"