From fee390c7edbf1c4c0e9e69120a69edea49734b75 Mon Sep 17 00:00:00 2001 From: Massimo Fabbro Date: Wed, 27 Sep 2023 21:43:42 +0200 Subject: [PATCH] refactor create spaces from walls --- .../blenderbim/bim/module/model/space.py | 27 ++++++++++ src/blenderbim/blenderbim/core/spatial.py | 49 ++++--------------- src/blenderbim/blenderbim/core/tool.py | 4 +- src/blenderbim/blenderbim/tool/spatial.py | 9 ++++ .../test/bim/feature/spatial.feature | 15 ------ 5 files changed, 49 insertions(+), 55 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/model/space.py b/src/blenderbim/blenderbim/bim/module/model/space.py index 5f0e8edaf3..2ae6135205 100644 --- a/src/blenderbim/blenderbim/bim/module/model/space.py +++ b/src/blenderbim/blenderbim/bim/module/model/space.py @@ -68,6 +68,13 @@ class GenerateSpace(bpy.types.Operator, tool.Ifc.Operator): bpy.context.window_manager.popup_menu(msg, title="Error", icon="ERROR") return +# core.generate_space(tool.Ifc, tool.Spatial) + + + + + + active_obj = bpy.context.active_object element = None if bpy.context.selected_objects and active_obj: @@ -233,6 +240,26 @@ class GenerateSpacesFromWalls(bpy.types.Operator, tool.Ifc.Operator): # walls (i.e. prismatic) in the active object storey. # In order to run, the active object must be a wall and # there must be selected walls + + active_obj = bpy.context.active_object + element = tool.Ifc.get_entity(active_obj) + container = tool.Spatial.get_container(element) + + if not active_obj: + self.report({"ERROR"}, "No active object. Please select a wall") + return + + element = tool.Ifc.get_entity(active_obj) + if element and not element.is_a("IfcWall"): + return self.report({"ERROR"}, "The active object is not a wall. Please select a wall.") + + if not container: + self.report({"ERROR"}, "The wall is not contained.") + + if not bpy.context.selected_objects: + self.report({"ERROR"}, "No selected objects found. Please select walls.") + return + core.generate_spaces_from_walls(tool.Ifc, tool.Spatial, tool.Collector) class ToggleSpaceVisibility(bpy.types.Operator, tool.Ifc.Operator): diff --git a/src/blenderbim/blenderbim/core/spatial.py b/src/blenderbim/blenderbim/core/spatial.py index 0e75438933..e5bbefa127 100644 --- a/src/blenderbim/blenderbim/core/spatial.py +++ b/src/blenderbim/blenderbim/core/spatial.py @@ -127,36 +127,14 @@ def select_decomposed_elements(spatial): if container: spatial.select_products(spatial.get_decomposed_elements(container)) - -# HERE STARTS SPATIAL TOOL +#HERE STARTS SPATIAL TOOL +def generate_space(ifc, spatial): + pass def generate_spaces_from_walls(ifc, spatial, collector): - import bpy - - active_obj = bpy.context.active_object - element = ifc.get_entity(active_obj) - container = spatial.get_container(element) - - if not active_obj: - self.report({"ERROR"}, "No active object. Please select a wall") - return - - element = ifc.get_entity(active_obj) - if element and not element.is_a("IfcWall"): - return self.report({"ERROR"}, "The active object is not a wall. Please select a wall.") - - if not container: - self.report({"ERROR"}, "The wall is not contained.") - - if not bpy.context.selected_objects: - self.report({"ERROR"}, "No selected objects found. Please select walls.") - return - - x, y, z = active_obj.matrix_world.translation.xyz - mat = active_obj.matrix_world - h = active_obj.dimensions.z - selected_objects = bpy.context.selected_objects + z = spatial.get_active_obj_z() + h = spatial.get_active_obj_height() union = spatial.get_union_shape_from_selected_objects() @@ -166,23 +144,16 @@ def generate_spaces_from_walls(ifc, spatial, collector): bm = spatial.get_bmesh_from_polygon(poly, h) name = "Space" + str(i) - mesh = bpy.data.meshes.new(name=name) - bm.to_mesh(mesh) - bm.free() - obj = bpy.data.objects.new(name, mesh) - obj.matrix_world = mat + obj = spatial.get_named_obj_from_bmesh(name, bmesh = bm) spatial.set_obj_origin_to_bboxcenter(obj) + spatial.traslate_obj_to_z_location(obj, z) - if z != 0: - obj.location = obj.location + Vector((0, 0, z)) - - bpy.context.view_layer.active_layer_collection.collection.objects.link(obj) - bpy.ops.bim.assign_class(obj=obj.name, ifc_class="IfcSpace") - container_obj = ifc.get_object(container) - blenderbim.core.spatial.assign_container(ifc, collector, spatial, structure_obj=container_obj, element_obj=obj) + spatial.link_obj_to_active_collection(obj) + spatial.assign_ifcspace_class_to_obj(obj) + spatial.assign_container_to_obj(obj) def toggle_space_visibility(ifc, spatial): model = ifc.get() diff --git a/src/blenderbim/blenderbim/core/tool.py b/src/blenderbim/blenderbim/core/tool.py index 756f2a4192..272d50bddc 100644 --- a/src/blenderbim/blenderbim/core/tool.py +++ b/src/blenderbim/blenderbim/core/tool.py @@ -820,12 +820,14 @@ class Spatial: def get_bmesh_from_polygon(cls, poly, h): pass def get_named_obj_from_bmesh(cls, name, bmesh): pass def set_obj_origin_to_bboxcenter(cls, obj): pass - def get_active_obj_z(cls, obj): pass + def get_active_obj_z(cls): pass + def get_active_obj_height(cls): pass def traslate_obj_to_z_location(cls, obj): pass def link_obj_to_active_collection(cls, obj): pass def get_2d_vertices_from_obj(cls, obj): pass def assign_swept_area_outer_curve_from_2d_vertices(cls, obj, vertices): pass def get_body_representation(cls, obj): pass + def assign_ifcspace_class_to_obj(cls, obj): pass def assign_type_to_obj(cls, obj): pass def regen_obj_representation(cls, ifc, geometry, obj, body): pass def toggle_spaces_visibility_wired_and_textured(cls, spaces): pass diff --git a/src/blenderbim/blenderbim/tool/spatial.py b/src/blenderbim/blenderbim/tool/spatial.py index f838703059..efa5d659a0 100644 --- a/src/blenderbim/blenderbim/tool/spatial.py +++ b/src/blenderbim/blenderbim/tool/spatial.py @@ -427,6 +427,11 @@ class Spatial(blenderbim.core.tool.Spatial): x, y, z = bpy.context.active_object.matrix_world.translation.xyz return z + @classmethod + def get_active_obj_height(cls): + height = bpy.context.active_object.dimensions.z + return height + @classmethod def traslate_obj_to_z_location(cls, obj, z): if z != 0: @@ -468,6 +473,10 @@ class Spatial(blenderbim.core.tool.Spatial): body = ifcopenshell.util.representation.get_representation(element, "Model", "Body", "MODEL_VIEW") return body + @classmethod + def assign_ifcspace_class_to_obj(cls, obj): + bpy.ops.bim.assign_class(obj=obj.name, ifc_class="IfcSpace") + @classmethod def assign_type_to_obj(cls, obj): relating_type_id = bpy.context.scene.BIMModelProperties.relating_type_id diff --git a/src/blenderbim/test/bim/feature/spatial.feature b/src/blenderbim/test/bim/feature/spatial.feature index 0eedf8552a..b400efbec6 100644 --- a/src/blenderbim/test/bim/feature/spatial.feature +++ b/src/blenderbim/test/bim/feature/spatial.feature @@ -21,13 +21,9 @@ Scenario: Assign container Given an empty IFC project And I add a cube And the object "Cube" is selected -<<<<<<< HEAD - And I press "bim.assign_class(ifc_class='IfcWall', predefined_type='SOLIDWALL')" -======= And I set "scene.BIMRootProperties.ifc_product" to "IfcElement" And I set "scene.BIMRootProperties.ifc_class" to "IfcWall" And I press "bim.assign_class" ->>>>>>> b2f95bfa6 (change tests due to new class assignment workflow ref commit 808ff54) And the object "IfcWall/Cube" is selected And I press "bim.enable_editing_container" And the variable "site" is "tool.Ifc.get().by_type('IfcSite')[0].id()" @@ -120,17 +116,6 @@ Scenario: Execute generate spaces from walls When I press "bim.generate_spaces_from_walls" Then nothing happens -Scenario: Execute generate flooring coverings from walls - Given an empty IFC project - And I load the demo construction library - And I set "scene.BIMModelProperties.ifc_class" to "IfcWallType" - And the variable "element_type" is "[e for e in {ifc}.by_type('IfcWallType') if e.Name == 'WAL100'][0].id()" - And I set "scene.BIMModelProperties.relating_type_id" to "{element_type}" - And I press "bim.add_constr_type_instance" - And the object "IfcWall/Wall" is selected - When I press "bim.generate_flooring_coverings_from_walls" - Then nothing happens - Scenario: Execute toggle space visibility Given an empty IFC project And I add a cube