From c105b8f3a74b0588e1c2cac6d1da7871f5dd58ab Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Tue, 5 Oct 2021 16:00:26 +1100 Subject: [PATCH] Fix bug where assigning a spatial container didn't work with non local placements --- src/blenderbim/blenderbim/bim/ifc.py | 6 +++++- src/blenderbim/blenderbim/bim/module/root/operator.py | 1 - .../blenderbim/bim/module/spatial/operator.py | 1 - src/blenderbim/blenderbim/core/spatial.py | 3 +-- src/blenderbim/test/core/test_spatial.py | 8 +++----- .../ifcopenshell/api/spatial/assign_container.py | 3 ++- .../test/api/spatial/test_assign_container.py | 11 +++++++++++ 7 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/ifc.py b/src/blenderbim/blenderbim/bim/ifc.py index 8bed6f5e7c..efe16b6fe3 100644 --- a/src/blenderbim/blenderbim/bim/ifc.py +++ b/src/blenderbim/blenderbim/bim/ifc.py @@ -248,7 +248,11 @@ class IfcStore: operator, rollback=lambda d: IfcStore.get_file().undo(), commit=lambda d: IfcStore.get_file().redo() ) IfcStore.end_transaction(operator) - context.view_layer.objects.active = active_object + try: + active_object.name + context.view_layer.objects.active = active_object + except: + pass return result diff --git a/src/blenderbim/blenderbim/bim/module/root/operator.py b/src/blenderbim/blenderbim/bim/module/root/operator.py index dfb368dc55..5f274062a7 100644 --- a/src/blenderbim/blenderbim/bim/module/root/operator.py +++ b/src/blenderbim/blenderbim/bim/module/root/operator.py @@ -242,7 +242,6 @@ class AssignClass(bpy.types.Operator): tool.Ifc, tool.Collector, tool.Container, - tool.Surveyor, structure_obj=spatial_obj, element_obj=obj, ) diff --git a/src/blenderbim/blenderbim/bim/module/spatial/operator.py b/src/blenderbim/blenderbim/bim/module/spatial/operator.py index b8e56ac9e6..2c867e4515 100644 --- a/src/blenderbim/blenderbim/bim/module/spatial/operator.py +++ b/src/blenderbim/blenderbim/bim/module/spatial/operator.py @@ -46,7 +46,6 @@ class AssignContainer(bpy.types.Operator, Operator): tool.Ifc, tool.Collector, tool.Container, - tool.Surveyor, structure_obj=structure_obj, element_obj=element_obj, ) diff --git a/src/blenderbim/blenderbim/core/spatial.py b/src/blenderbim/blenderbim/core/spatial.py index bb51abdae3..d54b35b26d 100644 --- a/src/blenderbim/blenderbim/core/spatial.py +++ b/src/blenderbim/blenderbim/core/spatial.py @@ -19,7 +19,7 @@ import blenderbim.core -def assign_container(ifc, collector, container, surveyor, structure_obj=None, element_obj=None): +def assign_container(ifc, collector, container, structure_obj=None, element_obj=None): if not container.can_contain(structure_obj, element_obj): return rel = ifc.run( @@ -27,7 +27,6 @@ def assign_container(ifc, collector, container, surveyor, structure_obj=None, el product=ifc.get_entity(element_obj), relating_structure=ifc.get_entity(structure_obj), ) - blenderbim.core.geometry.edit_object_placement(ifc, surveyor, obj=element_obj) container.disable_editing(element_obj) collector.assign(element_obj) return rel diff --git a/src/blenderbim/test/core/test_spatial.py b/src/blenderbim/test/core/test_spatial.py index 6dffb3a85a..d0c768d683 100644 --- a/src/blenderbim/test/core/test_spatial.py +++ b/src/blenderbim/test/core/test_spatial.py @@ -17,24 +17,22 @@ # along with BlenderBIM Add-on. If not, see . import blenderbim.core.spatial as subject -from test.core.bootstrap import ifc, collector, container, surveyor +from test.core.bootstrap import ifc, collector, container class TestAssignContainer: - def test_run(self, ifc, collector, container, surveyor): + def test_run(self, ifc, collector, container): container.can_contain("structure_obj", "element_obj").should_be_called().will_return(True) ifc.get_entity("structure_obj").should_be_called().will_return("structure") ifc.get_entity("element_obj").should_be_called().will_return("element") ifc.run( "spatial.assign_container", product="element", relating_structure="structure" ).should_be_called().will_return("rel") - surveyor.get_absolute_matrix("element_obj").should_be_called().will_return("matrix") - ifc.run("geometry.edit_object_placement", product="element", matrix="matrix").should_be_called() container.disable_editing("element_obj").should_be_called() collector.assign("element_obj").should_be_called() assert ( subject.assign_container( - ifc, collector, container, surveyor, structure_obj="structure_obj", element_obj="element_obj" + ifc, collector, container, structure_obj="structure_obj", element_obj="element_obj" ) == "rel" ) diff --git a/src/ifcopenshell-python/ifcopenshell/api/spatial/assign_container.py b/src/ifcopenshell-python/ifcopenshell/api/spatial/assign_container.py index 40c8744b94..f502762933 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/spatial/assign_container.py +++ b/src/ifcopenshell-python/ifcopenshell/api/spatial/assign_container.py @@ -45,7 +45,8 @@ class Usecase: } ) - if getattr(self.settings["product"], "ObjectPlacement", None): + placement = getattr(self.settings["product"], "ObjectPlacement", None) + if placement and placement.is_a("IfcLocalPlacement"): ifcopenshell.api.run( "geometry.edit_object_placement", self.file, diff --git a/src/ifcopenshell-python/test/api/spatial/test_assign_container.py b/src/ifcopenshell-python/test/api/spatial/test_assign_container.py index a91857a32c..bcf0052b0b 100644 --- a/src/ifcopenshell-python/test/api/spatial/test_assign_container.py +++ b/src/ifcopenshell-python/test/api/spatial/test_assign_container.py @@ -76,4 +76,15 @@ class TestEditObjectPlacement(test.bootstrap.IFC4): "geometry.edit_object_placement", self.file, product=subelement, matrix=matrix1.copy(), is_si=False ) ifcopenshell.api.run("spatial.assign_container", self.file, product=subelement, relating_structure=element2) + assert subelement.ObjectPlacement.PlacementRelTo.PlacesObject[0] == element2 assert numpy.array_equal(ifcopenshell.util.placement.get_local_placement(subelement.ObjectPlacement), matrix1) + + def test_not_updating_placement_if_placement_is_not_relative(self): + ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject") + ifcopenshell.api.run("unit.assign_unit", self.file) + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding") + subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + placement = self.file.createIfcGridPlacement() + subelement.ObjectPlacement = placement + ifcopenshell.api.run("spatial.assign_container", self.file, product=subelement, relating_structure=element) + assert subelement.ObjectPlacement == placement