-
-
Notifications
You must be signed in to change notification settings - Fork 901
Expand file tree
/
Copy pathtest_render.py
More file actions
355 lines (294 loc) · 15.5 KB
/
test_render.py
File metadata and controls
355 lines (294 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# This file was generated with the assistance of an AI coding tool.
import os
import subprocess
import sys
import tempfile
import ifcopenshell
import ifcopenshell.api.aggregate
import ifcopenshell.api.context
import ifcopenshell.api.geometry
import ifcopenshell.api.owner.settings
import ifcopenshell.api.project
import ifcopenshell.api.root
import ifcopenshell.api.spatial
import ifcopenshell.api.unit
import ifcopenshell.guid
import numpy as np
import pytest
from ifcquery.render import _make_profile_occurrence, _make_type_occurrence, render
try:
import pyvista # noqa: F401
HAS_PYVISTA = True
except ImportError:
HAS_PYVISTA = False
pytestmark = pytest.mark.skipif(not HAS_PYVISTA, reason="pyvista not installed")
PNG_MAGIC = b"\x89PNG"
@pytest.fixture
def model_with_geometry():
"""Create an IFC4 model with walls that have geometric representations."""
f = ifcopenshell.api.project.create_file()
ifcopenshell.api.owner.settings.get_user = lambda ifc: (ifc.by_type("IfcPersonAndOrganization") or [None])[0]
ifcopenshell.api.owner.settings.get_application = lambda ifc: (ifc.by_type("IfcApplication") or [None])[0]
project = ifcopenshell.api.root.create_entity(f, ifc_class="IfcProject", name="TestProject")
ifcopenshell.api.unit.assign_unit(f)
site = ifcopenshell.api.root.create_entity(f, ifc_class="IfcSite", name="TestSite")
building = ifcopenshell.api.root.create_entity(f, ifc_class="IfcBuilding", name="TestBuilding")
storey = ifcopenshell.api.root.create_entity(f, ifc_class="IfcBuildingStorey", name="Ground Floor")
ifcopenshell.api.aggregate.assign_object(f, products=[site], relating_object=project)
ifcopenshell.api.aggregate.assign_object(f, products=[building], relating_object=site)
ifcopenshell.api.aggregate.assign_object(f, products=[storey], relating_object=building)
model_ctx = ifcopenshell.api.context.add_context(f, context_type="Model")
body = ifcopenshell.api.context.add_context(
f, context_type="Model", context_identifier="Body", target_view="MODEL_VIEW", parent=model_ctx
)
wall1 = ifcopenshell.api.root.create_entity(f, ifc_class="IfcWall", name="Wall001")
rep1 = ifcopenshell.api.geometry.add_wall_representation(f, context=body, length=5, height=3, thickness=0.2)
ifcopenshell.api.geometry.assign_representation(f, product=wall1, representation=rep1)
ifcopenshell.api.spatial.assign_container(f, products=[wall1], relating_structure=storey)
wall2 = ifcopenshell.api.root.create_entity(f, ifc_class="IfcWall", name="Wall002")
rep2 = ifcopenshell.api.geometry.add_wall_representation(f, context=body, length=4, height=3, thickness=0.2)
ifcopenshell.api.geometry.assign_representation(f, product=wall2, representation=rep2)
ifcopenshell.api.spatial.assign_container(f, products=[wall2], relating_structure=storey)
matrix2 = np.eye(4)
matrix2[1, 3] = 3.0
ifcopenshell.api.geometry.edit_object_placement(f, product=wall2, matrix=matrix2)
return f
@pytest.fixture
def library_with_type():
"""IFC4 library file: a WallType with a RepresentationMap but no instances."""
f = ifcopenshell.api.project.create_file()
ifcopenshell.api.owner.settings.get_user = lambda ifc: (ifc.by_type("IfcPersonAndOrganization") or [None])[0]
ifcopenshell.api.owner.settings.get_application = lambda ifc: (ifc.by_type("IfcApplication") or [None])[0]
project = ifcopenshell.api.root.create_entity(f, ifc_class="IfcProject", name="LibProject")
ifcopenshell.api.unit.assign_unit(f)
model_ctx = ifcopenshell.api.context.add_context(f, context_type="Model")
body = ifcopenshell.api.context.add_context(
f, context_type="Model", context_identifier="Body", target_view="MODEL_VIEW", parent=model_ctx
)
# Build the shape representation and wrap it in an IfcRepresentationMap.
shape_rep = ifcopenshell.api.geometry.add_wall_representation(f, context=body, length=3, height=2.5, thickness=0.2)
origin = f.create_entity("IfcCartesianPoint", Coordinates=(0.0, 0.0, 0.0))
z_dir = f.create_entity("IfcDirection", DirectionRatios=(0.0, 0.0, 1.0))
x_dir = f.create_entity("IfcDirection", DirectionRatios=(1.0, 0.0, 0.0))
map_origin = f.create_entity("IfcAxis2Placement3D", Location=origin, Axis=z_dir, RefDirection=x_dir)
rep_map = f.create_entity("IfcRepresentationMap", MappingOrigin=map_origin, MappedRepresentation=shape_rep)
wall_type = ifcopenshell.api.root.create_entity(f, ifc_class="IfcWallType", name="LibWallType")
wall_type.RepresentationMaps = [rep_map]
return f, wall_type
@pytest.fixture
def library_with_profile_type():
"""IFC4 library: a BeamType with an IfcMaterialProfileSet but no RepresentationMaps."""
f = ifcopenshell.api.project.create_file()
ifcopenshell.api.owner.settings.get_user = lambda ifc: (ifc.by_type("IfcPersonAndOrganization") or [None])[0]
ifcopenshell.api.owner.settings.get_application = lambda ifc: (ifc.by_type("IfcApplication") or [None])[0]
project = ifcopenshell.api.root.create_entity(f, ifc_class="IfcProject", name="ProfileLibProject")
ifcopenshell.api.unit.assign_unit(f)
model_ctx = ifcopenshell.api.context.add_context(f, context_type="Model")
ifcopenshell.api.context.add_context(
f, context_type="Model", context_identifier="Body", target_view="MODEL_VIEW", parent=model_ctx
)
# Rectangular profile 0.2m x 0.3m
profile = f.create_entity(
"IfcRectangleProfileDef",
ProfileType="AREA",
ProfileName="200x300",
XDim=0.2,
YDim=0.3,
)
material = f.create_entity("IfcMaterial", Name="Steel")
mat_profile = f.create_entity("IfcMaterialProfile", Material=material, Profile=profile)
profile_set = f.create_entity("IfcMaterialProfileSet", MaterialProfiles=[mat_profile])
beam_type = ifcopenshell.api.root.create_entity(f, ifc_class="IfcBeamType", name="200x300 Steel Beam")
rel = f.create_entity(
"IfcRelAssociatesMaterial",
GlobalId=ifcopenshell.guid.new(),
RelatedObjects=[beam_type],
RelatingMaterial=profile_set,
)
return f, beam_type
class TestRenderBasic:
def test_returns_png_bytes(self, model_with_geometry):
result = render(model_with_geometry)
assert isinstance(result, bytes)
assert result[:4] == PNG_MAGIC
def test_iso_view(self, model_with_geometry):
result = render(model_with_geometry, view="iso")
assert result[:4] == PNG_MAGIC
def test_top_view(self, model_with_geometry):
result = render(model_with_geometry, view="top")
assert result[:4] == PNG_MAGIC
def test_south_view(self, model_with_geometry):
result = render(model_with_geometry, view="south")
assert result[:4] == PNG_MAGIC
def test_unknown_view_falls_back_to_iso(self, model_with_geometry):
# Unknown view strings fall through to isometric
result = render(model_with_geometry, view="diagonal")
assert result[:4] == PNG_MAGIC
class TestRenderSelector:
def test_selector_restricts_elements(self, model_with_geometry):
result = render(model_with_geometry, selector="IfcWall")
assert result[:4] == PNG_MAGIC
def test_selector_no_match_raises(self, model_with_geometry):
with pytest.raises(ValueError, match="matched no elements"):
render(model_with_geometry, selector="IfcDoor")
class TestRenderHighlight:
def test_highlight_single_element(self, model_with_geometry):
wall = model_with_geometry.by_type("IfcWall")[0]
result = render(model_with_geometry, element_ids=[wall.id()])
assert result[:4] == PNG_MAGIC
def test_highlight_multiple_elements(self, model_with_geometry):
walls = model_with_geometry.by_type("IfcWall")
result = render(model_with_geometry, element_ids=[w.id() for w in walls])
assert result[:4] == PNG_MAGIC
class TestRenderTypes:
def test_render_type_by_selector(self, library_with_type):
"""Selecting a type class renders its RepresentationMap geometry."""
model, wall_type = library_with_type
result = render(model, selector="IfcWallType")
assert result[:4] == PNG_MAGIC
def test_render_type_by_element_id(self, library_with_type):
"""Passing a type step-ID via element_ids renders it highlighted."""
model, wall_type = library_with_type
result = render(model, element_ids=[wall_type.id()])
assert result[:4] == PNG_MAGIC
def test_original_model_unmodified(self, library_with_type):
"""Rendering a type must not add entities to the original model."""
model, wall_type = library_with_type
entity_count_before = len(list(model))
render(model, selector="IfcWallType")
assert len(list(model)) == entity_count_before
def test_make_type_occurrence_no_rep_maps(self, library_with_type):
"""_make_type_occurrence returns None for a type with no RepresentationMaps."""
model, _ = library_with_type
bare_type = ifcopenshell.api.root.create_entity(model, ifc_class="IfcWallType", name="Bare")
assert _make_type_occurrence(model, bare_type) is None
def test_type_without_rep_maps_raises(self):
"""Selecting a type that has no RepresentationMaps raises ValueError."""
f = ifcopenshell.api.project.create_file()
ifcopenshell.api.owner.settings.get_user = lambda ifc: (ifc.by_type("IfcPersonAndOrganization") or [None])[0]
ifcopenshell.api.owner.settings.get_application = lambda ifc: (ifc.by_type("IfcApplication") or [None])[0]
ifcopenshell.api.root.create_entity(f, ifc_class="IfcProject", name="P")
ifcopenshell.api.unit.assign_unit(f)
ifcopenshell.api.root.create_entity(f, ifc_class="IfcWallType", name="Bare")
with pytest.raises(ValueError):
render(f, selector="IfcWallType")
class TestRenderProfileTypes:
def test_render_profile_type_by_element_id(self, library_with_profile_type):
"""A type with only a material profile set renders via temporary extrusion."""
model, beam_type = library_with_profile_type
result = render(model, element_ids=[beam_type.id()])
assert result[:4] == PNG_MAGIC
def test_make_profile_occurrence_creates_occurrence(self, library_with_profile_type):
"""_make_profile_occurrence returns an occurrence entity for a profile-set type."""
model, beam_type = library_with_profile_type
occ = _make_profile_occurrence(model, beam_type)
assert occ is not None
def test_make_profile_occurrence_no_profile_returns_none(self, library_with_type):
"""_make_profile_occurrence returns None when type has no material profile set."""
model, wall_type = library_with_type
# wall_type has RepresentationMaps but no material profile set
occ = _make_profile_occurrence(model, wall_type)
assert occ is None
def test_original_model_unmodified_for_profile_type(self, library_with_profile_type):
"""Rendering a profile-based type does not modify the original model."""
model, beam_type = library_with_profile_type
entity_count_before = len(list(model))
render(model, element_ids=[beam_type.id()])
assert len(list(model)) == entity_count_before
class TestRenderNoGeometry:
def test_no_geometry_raises(self):
"""A model without geometry representations raises ValueError."""
f = ifcopenshell.api.project.create_file()
ifcopenshell.api.owner.settings.get_user = lambda ifc: (ifc.by_type("IfcPersonAndOrganization") or [None])[0]
ifcopenshell.api.owner.settings.get_application = lambda ifc: (ifc.by_type("IfcApplication") or [None])[0]
project = ifcopenshell.api.root.create_entity(f, ifc_class="IfcProject", name="P")
ifcopenshell.api.unit.assign_unit(f)
site = ifcopenshell.api.root.create_entity(f, ifc_class="IfcSite", name="S")
building = ifcopenshell.api.root.create_entity(f, ifc_class="IfcBuilding", name="B")
storey = ifcopenshell.api.root.create_entity(f, ifc_class="IfcBuildingStorey", name="GF")
ifcopenshell.api.aggregate.assign_object(f, products=[site], relating_object=project)
ifcopenshell.api.aggregate.assign_object(f, products=[building], relating_object=site)
ifcopenshell.api.aggregate.assign_object(f, products=[storey], relating_object=building)
wall = ifcopenshell.api.root.create_entity(f, ifc_class="IfcWall", name="Wallless")
ifcopenshell.api.spatial.assign_container(f, products=[wall], relating_structure=storey)
with pytest.raises(ValueError, match="No renderable geometry"):
render(f)
class TestCLI:
@staticmethod
def _ifc_path(model):
f = tempfile.NamedTemporaryFile(suffix=".ifc", delete=False)
model.write(f.name)
f.close()
return f.name
def test_render_writes_png(self, model_with_geometry):
ifc_path = self._ifc_path(model_with_geometry)
out_path = ifc_path.replace(".ifc", "_out.png")
try:
result = subprocess.run(
[sys.executable, "-m", "ifcquery", ifc_path, "render", "-o", out_path],
capture_output=True,
text=True,
)
assert result.returncode == 0, result.stderr
assert os.path.exists(out_path)
with open(out_path, "rb") as f:
assert f.read(4) == PNG_MAGIC
finally:
for path in (ifc_path, out_path):
try:
os.unlink(path)
except OSError:
pass
def test_render_default_output_path(self, model_with_geometry):
ifc_path = self._ifc_path(model_with_geometry)
expected_png = ifc_path.replace(".ifc", ".png")
try:
result = subprocess.run(
[sys.executable, "-m", "ifcquery", ifc_path, "render"],
capture_output=True,
text=True,
)
assert result.returncode == 0, result.stderr
assert os.path.exists(expected_png)
finally:
for path in (ifc_path, expected_png):
try:
os.unlink(path)
except OSError:
pass
def test_render_with_selector(self, model_with_geometry):
ifc_path = self._ifc_path(model_with_geometry)
out_path = ifc_path.replace(".ifc", "_sel.png")
try:
result = subprocess.run(
[sys.executable, "-m", "ifcquery", ifc_path, "render", "-o", out_path, "--selector", "IfcWall"],
capture_output=True,
text=True,
)
assert result.returncode == 0, result.stderr
with open(out_path, "rb") as f:
assert f.read(4) == PNG_MAGIC
finally:
for path in (ifc_path, out_path):
try:
os.unlink(path)
except OSError:
pass
def test_render_with_view(self, model_with_geometry):
ifc_path = self._ifc_path(model_with_geometry)
out_path = ifc_path.replace(".ifc", "_top.png")
try:
result = subprocess.run(
[sys.executable, "-m", "ifcquery", ifc_path, "render", "-o", out_path, "--view", "top"],
capture_output=True,
text=True,
)
assert result.returncode == 0, result.stderr
with open(out_path, "rb") as f:
assert f.read(4) == PNG_MAGIC
finally:
for path in (ifc_path, out_path):
try:
os.unlink(path)
except OSError:
pass