From f54a1a163d7f0cd26ac986c8bcbdbf0eff1e83e0 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Tue, 5 Oct 2021 10:46:11 +1100 Subject: [PATCH] Changing spatial container now guards against invalid containment operations --- src/blenderbim/blenderbim/tool/container.py | 46 +++++++++ src/blenderbim/test/tool/test_container.py | 102 ++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 src/blenderbim/blenderbim/tool/container.py create mode 100644 src/blenderbim/test/tool/test_container.py diff --git a/src/blenderbim/blenderbim/tool/container.py b/src/blenderbim/blenderbim/tool/container.py new file mode 100644 index 0000000000..b270c98e89 --- /dev/null +++ b/src/blenderbim/blenderbim/tool/container.py @@ -0,0 +1,46 @@ +import bpy +import blenderbim.core.tool +import blenderbim.tool as tool + +class Container(blenderbim.core.tool.Container): + @classmethod + def can_contain(cls, structure_obj, element_obj): + structure = tool.Ifc.get_entity(structure_obj) + element = tool.Ifc.get_entity(element_obj) + if not structure or not element: + return False + if tool.Ifc.get_schema() == "IFC2X3": + if not structure.is_a("IfcSpatialStructureElement"): + return False + else: + if not structure.is_a("IfcSpatialElement"): + return False + if not element.is_a("IfcElement"): + return False + return True + + @classmethod + def enable_editing(cls, obj): + obj.BIMObjectSpatialProperties.is_editing = True + + @classmethod + def disable_editing(cls, obj): + obj.BIMObjectSpatialProperties.is_editing = False + + @classmethod + def import_containers(cls, parent=None): + props = bpy.context.scene.BIMSpatialProperties + props.containers.clear() + + if not parent: + parent = tool.Ifc.get().by_type("IfcProject")[0] + + props.active_container_id = parent.id() + + for rel in parent.IsDecomposedBy or []: + for element in rel.RelatedObjects: + new = props.containers.add() + new.name = element.Name or "Unnamed" + new.long_name = element.LongName or "" + new.has_decomposition = bool(element.IsDecomposedBy) + new.ifc_definition_id = element.id() diff --git a/src/blenderbim/test/tool/test_container.py b/src/blenderbim/test/tool/test_container.py new file mode 100644 index 0000000000..f4b883114a --- /dev/null +++ b/src/blenderbim/test/tool/test_container.py @@ -0,0 +1,102 @@ +import bpy +import ifcopenshell +import blenderbim.core.tool +import blenderbim.tool as tool +from blenderbim.tool.container import Container as subject +from test.bim.bootstrap import NewFile + + +class TestImplementsTool(NewFile): + def test_run(self): + assert isinstance(subject(), blenderbim.core.tool.Container) + + +class TestCanContain(NewFile): + def test_a_spatial_element_can_contain_an_element(self): + ifc = ifcopenshell.file() + tool.Ifc.set(ifc) + structure = ifc.createIfcSite() + structure_obj = bpy.data.objects.new("Object", None) + tool.Ifc.link(structure, structure_obj) + element = ifc.createIfcWall() + element_obj = bpy.data.objects.new("Object", None) + tool.Ifc.link(element, element_obj) + assert subject.can_contain(structure_obj, element_obj) is True + + def test_a_spatial_element_can_contain_an_element_ifc2x3(self): + ifc = ifcopenshell.file(schema="IFC2X3") + tool.Ifc.set(ifc) + structure = ifc.createIfcSite() + structure_obj = bpy.data.objects.new("Object", None) + tool.Ifc.link(structure, structure_obj) + element = ifc.createIfcWall() + element_obj = bpy.data.objects.new("Object", None) + tool.Ifc.link(element, element_obj) + assert subject.can_contain(structure_obj, element_obj) is True + + def test_unlinked_elements_cannot_contain_anything(self): + structure_obj = bpy.data.objects.new("Object", None) + element_obj = bpy.data.objects.new("Object", None) + assert subject.can_contain(structure_obj, element_obj) is False + + def test_a_non_spatial_element_cannot_contain_anything(self): + ifc = ifcopenshell.file() + tool.Ifc.set(ifc) + structure = ifc.createIfcWall() + structure_obj = bpy.data.objects.new("Object", None) + tool.Ifc.link(structure, structure_obj) + element = ifc.createIfcWall() + element_obj = bpy.data.objects.new("Object", None) + tool.Ifc.link(element, element_obj) + assert subject.can_contain(structure_obj, element_obj) is False + + def test_a_non_element_cannot_be_contained(self): + ifc = ifcopenshell.file() + tool.Ifc.set(ifc) + structure = ifc.createIfcSite() + structure_obj = bpy.data.objects.new("Object", None) + tool.Ifc.link(structure, structure_obj) + element = ifc.createIfcTask() + element_obj = bpy.data.objects.new("Object", None) + tool.Ifc.link(element, element_obj) + assert subject.can_contain(structure_obj, element_obj) is False + + +class TestEnableEditing(NewFile): + def test_run(self): + obj = bpy.data.objects.new("Object", None) + subject.enable_editing(obj) + assert obj.BIMObjectSpatialProperties.is_editing is True + + +class TestDisableEditing(NewFile): + def test_run(self): + obj = bpy.data.objects.new("Object", None) + subject.enable_editing(obj) + subject.disable_editing(obj) + assert obj.BIMObjectSpatialProperties.is_editing is False + + +class TestImportContainers(NewFile): + def test_run(self): + bpy.ops.bim.create_project() + subject.import_containers() + props = bpy.context.scene.BIMSpatialProperties + assert len(props.containers) == 1 + assert props.containers[0].name == "My Site" + assert props.containers[0].long_name == "" + assert props.containers[0].has_decomposition is True + assert props.containers[0].ifc_definition_id == tool.Ifc.get().by_type("IfcSite")[0].id() + assert props.active_container_id == tool.Ifc.get().by_type("IfcProject")[0].id() + + def test_importing_with_a_specified_parent(self): + bpy.ops.bim.create_project() + site = tool.Ifc.get().by_type("IfcSite")[0] + subject.import_containers(site) + props = bpy.context.scene.BIMSpatialProperties + assert len(props.containers) == 1 + assert props.containers[0].name == "My Building" + assert props.containers[0].long_name == "" + assert props.containers[0].has_decomposition is True + assert props.containers[0].ifc_definition_id == tool.Ifc.get().by_type("IfcBuilding")[0].id() + assert props.active_container_id == site.id()