Debian Codegraph @

src/bonsai/test/bim/bootstrap.py

raw
1 # Bonsai - OpenBIM Blender Add-on
2 # Copyright (C) 2021 Dion Moult <[email protected]>
3 #
4 # This file is part of Bonsai.
5 #
6 # Bonsai is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Bonsai is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Bonsai. If not, see <http://www.gnu.org/licenses/>.
18
19
20 import os
21 import re
22 import bpy
23 import pytest
24 import webbrowser
25 import bonsai.bim.handler
26 import bonsai.tool as tool
27 import ifcopenshell
28 import ifcopenshell.util.element
29 import ifcopenshell.util.representation
30 from bonsai.bim.ifc import IfcStore
31 from mathutils import Vector
32
33 # Monkey-patch webbrowser opening since we want to test headlessly
34 webbrowser.open = lambda x: True
35
36
37 variables = {"cwd": os.getcwd(), "ifc": "IfcStore.get_file()"}
38
39
40 class NewFile:
41 @pytest.fixture(autouse=True)
42 def setup(self):
43 IfcStore.purge()
44 bpy.ops.wm.read_homefile(app_template="")
45 if bpy.data.objects:
46 bpy.data.batch_remove(bpy.data.objects)
47 bpy.ops.outliner.orphans_purge(do_local_ids=True, do_linked_ids=True, do_recursive=True)
48 bonsai.bim.handler.load_post(None)
49
50
51 class NewIfc:
52 @pytest.fixture(autouse=True)
53 def setup(self):
54 IfcStore.purge()
55 bpy.ops.wm.read_homefile(app_template="")
56 bpy.data.batch_remove(bpy.data.objects)
57 bpy.ops.outliner.orphans_purge(do_local_ids=True, do_linked_ids=True, do_recursive=True)
58 bonsai.bim.handler.load_post(None)
59 bpy.ops.bim.create_project()
60
61
62 class NewIfc4X3:
63 @pytest.fixture(autouse=True)
64 def setup(self):
65 IfcStore.purge()
66 bpy.ops.wm.read_homefile(app_template="")
67 bpy.data.batch_remove(bpy.data.objects)
68 bpy.ops.outliner.orphans_purge(do_local_ids=True, do_linked_ids=True, do_recursive=True)
69 bonsai.bim.handler.load_post(None)
70 props = tool.Project.get_project_props()
71 props.export_schema = "IFC4X3_ADD2"
72 bpy.ops.bim.create_project()
73
74
75 def scenario(function):
76 def subfunction(self):
77 run(function(self))
78
79 return subfunction
80
81
82 def scenario_debug(function):
83 def subfunction(self):
84 run_debug(function(self))
85
86 return subfunction
87
88
89 def an_empty_ifc_project():
90 bpy.ops.bim.create_project()
91
92
93 def i_add_a_cube():
94 bpy.ops.mesh.primitive_cube_add()
95
96
97 def i_add_a_cube_of_size_size_at_location(size, location):
98 bpy.ops.mesh.primitive_cube_add(size=float(size), location=[float(co) for co in location.split(",")])
99
100
101 def the_object_name_is_selected(name):
102 i_deselect_all_objects()
103 additionally_the_object_name_is_selected(name)
104
105
106 def additionally_the_object_name_is_selected(name):
107 obj = bpy.context.scene.objects.get(name)
108 if not obj:
109 assert False, 'The object "{name}" could not be selected'
110 bpy.context.view_layer.objects.active = obj
111 obj.select_set(True)
112
113
114 def i_deselect_all_objects():
115 bpy.context.view_layer.objects.active = None
116 bpy.ops.object.select_all(action="DESELECT")
117
118
119 def i_am_on_frame_number(number):
120 bpy.context.scene.frame_set(int(number))
121
122
123 def i_set_prop_to_value(prop, value):
124 try:
125 eval(f"bpy.context.{prop}")
126 except:
127 assert False, "Property does not exist"
128 try:
129 exec(f'bpy.context.{prop} = "{value}"')
130 except:
131 exec(f"bpy.context.{prop} = {value}")
132
133
134 def prop_is_value(prop, value):
135 is_value = False
136 try:
137 exec(f'assert bpy.context.{prop} == "{value}"')
138 is_value = True
139 except:
140 try:
141 exec(f"assert bpy.context.{prop} == {value}")
142 is_value = True
143 except:
144 try:
145 exec(f"assert list(bpy.context.{prop}) == {value}")
146 is_value = True
147 except:
148 pass
149 if not is_value:
150 actual_value = eval(f"bpy.context.{prop}")
151 assert False, f"Value is {actual_value}"
152
153
154 def i_enable_prop(prop):
155 exec(f"bpy.context.{prop} = True")
156
157
158 def i_press_operator(operator):
159 if "(" in operator:
160 exec(f"bpy.ops.{operator}")
161 else:
162 exec(f"bpy.ops.{operator}()")
163
164
165 def i_rename_the_object_name1_to_name2(name1, name2):
166 the_object_name_exists(name1).name = name2
167
168
169 def the_object_name_exists(name):
170 obj = bpy.data.objects.get(name)
171 if not obj:
172 assert False, f'The object "{name}" does not exist'
173 return obj
174
175
176 def an_ifc_file_exists():
177 ifc = tool.Ifc.get()
178 if not ifc:
179 assert False, "No IFC file is available"
180 return ifc
181
182
183 def an_ifc_file_does_not_exist():
184 ifc = tool.Ifc.get()
185 if ifc:
186 assert False, "An IFC is available"
187
188
189 def the_object_name_does_not_exist(name):
190 assert bpy.data.objects.get(name) is None, "Object exists"
191
192
193 def the_object_name_is_an_ifc_class(name, ifc_class):
194 ifc = an_ifc_file_exists()
195 element = ifc.by_id(tool.Blender.get_ifc_definition_id(the_object_name_exists(name)))
196 assert element.is_a(ifc_class), f'Object "{name}" is an {element.is_a()}'
197
198
199 def the_object_name_is_not_an_ifc_element(name):
200 id = tool.Blender.get_ifc_definition_id(the_object_name_exists(name))
201 assert id == 0, f"The ID is {id}"
202
203
204 def the_object_name_is_in_the_collection_collection(name, collection):
205 assert collection in [c.name for c in the_object_name_exists(name).users_collection]
206
207
208 def the_object_name_is_not_in_the_collection_collection(name, collection):
209 assert collection not in [c.name for c in the_object_name_exists(name).users_collection]
210
211
212 def the_object_name_has_a_body_of_value(name, value):
213 assert the_object_name_exists(name).data.body == value
214
215
216 def the_collection_name1_is_in_the_collection_name2(name1, name2):
217 assert bpy.data.collections.get(name2).children.get(name1)
218
219
220 def the_collection_name1_is_not_in_the_collection_name2(name1, name2):
221 assert not bpy.data.collections.get(name2).children.get(name1)
222
223
224 def the_object_name_is_placed_in_the_collection_collection(name, collection):
225 obj = the_object_name_exists(name)
226 [c.objects.unlink(obj) for c in obj.users_collection]
227 bpy.data.collections.get(collection).objects.link(obj)
228
229
230 def the_object_name_has_a_type_representation_of_context(name, type, context):
231 ifc = an_ifc_file_exists()
232 element = ifc.by_id(tool.Blender.get_ifc_definition_id(the_object_name_exists(name)))
233 context, subcontext, target_view = context.split("/")
234 assert ifcopenshell.util.representation.get_representation(
235 element, context, subcontext or None, target_view or None
236 )
237
238
239 def the_object_name_is_contained_in_container_name(name, container_name):
240 ifc = an_ifc_file_exists()
241 element = ifc.by_id(tool.Blender.get_ifc_definition_id(the_object_name_exists(name)))
242 container = ifcopenshell.util.element.get_container(element)
243 if not container:
244 assert False, f'Object "{name}" is not in any container'
245 assert container.Name == container_name, f'Object "{name}" is in {container}'
246
247
248 def i_duplicate_the_selected_objects():
249 bpy.ops.object.duplicate_move()
250 bonsai.bim.handler.active_object_callback()
251
252
253 def i_delete_the_selected_objects():
254 bpy.ops.object.delete()
255 bonsai.bim.handler.active_object_callback()
256
257
258 def the_object_name1_and_name2_are_different_elements(name1, name2):
259 ifc = an_ifc_file_exists()
260 element1 = ifc.by_id(tool.Blender.get_ifc_definition_id(the_object_name_exists(name1)))
261 element2 = ifc.by_id(tool.Blender.get_ifc_definition_id(the_object_name_exists(name2)))
262 assert element1 != element2, f"Objects {name1} and {name2} have same elements {element1} and {element2}"
263
264
265 def the_file_name_should_contain_value(name, value):
266 with open(name, "r") as f:
267 assert value in f.read()
268
269
270 def the_object_name1_has_a_boolean_difference_by_name2(name1, name2):
271 obj = the_object_name_exists(name1)
272 for modifier in obj.modifiers:
273 if modifier.type == "BOOLEAN" and modifier.object and modifier.object.name == name2:
274 return True
275 assert False, "No boolean found"
276
277
278 def the_object_name1_has_no_boolean_difference_by_name2(name1, name2):
279 obj = the_object_name_exists(name1)
280 for modifier in obj.modifiers:
281 if modifier.type == "BOOLEAN" and modifier.object and modifier.object.name == name2:
282 assert False, "A boolean was found"
283
284
285 def the_object_name_is_voided_by_void(name, void):
286 ifc = tool.Ifc.get()
287 element = ifc.by_id(tool.Blender.get_ifc_definition_id(the_object_name_exists(name)))
288 for rel in element.HasOpenings:
289 if rel.RelatedOpeningElement.Name == void:
290 return True
291 assert False, "No void found"
292
293
294 def the_object_name_is_not_voided_by_void(name, void):
295 ifc = tool.Ifc.get()
296 element = ifc.by_id(tool.Blender.get_ifc_definition_id(the_object_name_exists(name)))
297 for rel in element.HasOpenings:
298 if rel.RelatedOpeningElement.Name == void:
299 assert False, "A void was found"
300
301
302 def the_object_name_is_not_voided(name):
303 ifc = tool.Ifc.get()
304 element = ifc.by_id(tool.Blender.get_ifc_definition_id(the_object_name_exists(name)))
305 if any(element.HasOpenings):
306 assert False, "An opening was found"
307
308
309 def the_object_name_is_not_a_void(name):
310 ifc = tool.Ifc.get()
311 element = ifc.by_id(tool.Blender.get_ifc_definition_id(the_object_name_exists(name)))
312 if any(element.VoidsElements):
313 assert False, "A void was found"
314
315
316 def the_void_name_is_filled_by_filling(name, filling):
317 ifc = tool.Ifc.get()
318 element = ifc.by_id(tool.Blender.get_ifc_definition_id(the_object_name_exists(name)))
319 if any(rel.RelatedBuildingElement.Name == filling for rel in element.HasFillings):
320 return True
321 assert False, "No filling found"
322
323
324 def the_void_name_is_not_filled_by_filling(name, filling):
325 ifc = tool.Ifc.get()
326 element = ifc.by_id(tool.Blender.get_ifc_definition_id(the_object_name_exists(name)))
327 if any(rel.RelatedBuildingElement.Name == filling for rel in element.HasFillings):
328 assert False, "A filling was found"
329
330
331 def the_object_name_is_not_a_filling(name):
332 ifc = tool.Ifc.get()
333 element = ifc.by_id(tool.Blender.get_ifc_definition_id(the_object_name_exists(name)))
334 if any(element.FillsVoids):
335 assert False, "A filling was found"
336
337
338 def the_object_name_should_display_as_mode(name, mode):
339 assert the_object_name_exists(name).display_type == mode
340
341
342 def the_object_name_has_number_vertices(name, number):
343 total = len(the_object_name_exists(name).data.vertices)
344 assert total == int(number), f"We found {total} vertices"
345
346
347 def the_object_name_is_at_location(name, location):
348 obj_location = the_object_name_exists(name).location
349 assert (
350 obj_location - Vector([float(co) for co in location.split(",")])
351 ).length < 0.1, f"Object is at {obj_location}"
352
353
354 def the_variable_key_is_value(key, value):
355 variables[key] = eval(value)
356
357
358 definitions = {
359 'the variable "(.*)" is "(.*)"': the_variable_key_is_value,
360 "an empty IFC project": an_empty_ifc_project,
361 "I add a cube": i_add_a_cube,
362 'I add a cube of size "([0-9]+)" at "(.*)"': i_add_a_cube_of_size_size_at_location,
363 'the object "(.*)" is selected': the_object_name_is_selected,
364 'additionally the object "(.*)" is selected': additionally_the_object_name_is_selected,
365 "I deselect all objects": i_deselect_all_objects,
366 'I am on frame "([0-9]+)"': i_am_on_frame_number,
367 'I set "(.*)" to "(.*)"': i_set_prop_to_value,
368 '"(.*)" is "(.*)"': prop_is_value,
369 'I enable "(.*)"': i_enable_prop,
370 'I press "(.*)"': i_press_operator,
371 'I rename the object "(.*)" to "(.*)"': i_rename_the_object_name1_to_name2,
372 'the object "(.*)" exists': the_object_name_exists,
373 'the object "(.*)" does not exist': the_object_name_does_not_exist,
374 'the object "(.*)" is an "(.*)"': the_object_name_is_an_ifc_class,
375 'the object "(.*)" is not an IFC element': the_object_name_is_not_an_ifc_element,
376 'the object "(.*)" is in the collection "(.*)"': the_object_name_is_in_the_collection_collection,
377 'the object "(.*)" is not in the collection "(.*)"': the_object_name_is_not_in_the_collection_collection,
378 'the object "(.*)" has a body of "(.*)"': the_object_name_has_a_body_of_value,
379 'the collection "(.*)" is in the collection "(.*)"': the_collection_name1_is_in_the_collection_name2,
380 'the collection "(.*)" is not in the collection "(.*)"': the_collection_name1_is_not_in_the_collection_name2,
381 "an IFC file exists": an_ifc_file_exists,
382 "an IFC file does not exist": an_ifc_file_does_not_exist,
383 'the object "(.*)" has a "(.*)" representation of "(.*)"': the_object_name_has_a_type_representation_of_context,
384 'the object "(.*)" is placed in the collection "(.*)"': the_object_name_is_placed_in_the_collection_collection,
385 'the object "(.*)" is contained in "(.*)"': the_object_name_is_contained_in_container_name,
386 "I duplicate the selected objects": i_duplicate_the_selected_objects,
387 "I delete the selected objects": i_delete_the_selected_objects,
388 'the object "(.*)" and "(.*)" are different elements': the_object_name1_and_name2_are_different_elements,
389 'the file "(.*)" should contain "(.*)"': the_file_name_should_contain_value,
390 'the object "(.*)" has a boolean difference by "(.*)"': the_object_name1_has_a_boolean_difference_by_name2,
391 'the object "(.*)" has no boolean difference by "(.*)"': the_object_name1_has_no_boolean_difference_by_name2,
392 'the object "(.*)" is voided by "(.*)"': the_object_name_is_voided_by_void,
393 'the object "(.*)" is not voided by "(.*)"': the_object_name_is_not_voided_by_void,
394 'the object "(.*)" is not a void': the_object_name_is_not_a_void,
395 'the object "(.*)" is not voided': the_object_name_is_not_voided,
396 'the object "(.*)" should display as "(.*)"': the_object_name_should_display_as_mode,
397 'the object "(.*)" has "([0-9]+)" vertices': the_object_name_has_number_vertices,
398 'the object "(.*)" is at "(.*)"': the_object_name_is_at_location,
399 "nothing interesting happens": lambda: None,
400 'the void "(.*)" is filled by "(.*)"': the_void_name_is_filled_by_filling,
401 'the void "(.*)" is not filled by "(.*)"': the_void_name_is_not_filled_by_filling,
402 'the object "(.*)" is not a filling': the_object_name_is_not_a_filling,
403 }
404
405
406 # Super lightweight Gherkin implementation
407 def run(scenario):
408 keywords = ["Given", "When", "Then", "And", "But"]
409 for line in scenario.split("\n"):
410 for key, value in variables.items():
411 line = line.replace("{" + key + "}", str(value))
412 for keyword in keywords:
413 line = line.replace(keyword, "")
414 line = line.strip()
415 if not line:
416 continue
417 match = None
418 for definition, callback in definitions.items():
419 match = re.search("^" + definition + "$", line)
420 if match:
421 try:
422 callback(*match.groups())
423 except AssertionError as e:
424 assert False, f"Failed: {line}, with error: {e}"
425 break
426 if not match:
427 assert False, f"Definition not implemented: {line}"
428 return True
429
430
431 def run_debug(scenario, blend_filepath=None):
432 try:
433 result = run(scenario)
434 except Exception as e:
435 if blend_filepath:
436 bpy.ops.wm.save_as_mainfile(filepath=blend_filepath)
437 assert False, e
438 if blend_filepath:
439 bpy.ops.wm.save_as_mainfile(filepath=blend_filepath)
440 return result