diff --git a/src/ifcopenshell-python/ifcopenshell/util/element.py b/src/ifcopenshell-python/ifcopenshell/util/element.py index fb7bde9a2d..b8ef0711d5 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/element.py +++ b/src/ifcopenshell-python/ifcopenshell/util/element.py @@ -935,7 +935,7 @@ def get_container( return container container = get_aggregate(container) elif parent := get_parent(element): - return get_container(parent, should_get_direct) + return get_container(parent, should_get_direct, ifc_class) def get_referenced_structures(element: ifcopenshell.entity_instance) -> list[ifcopenshell.entity_instance]: diff --git a/src/ifcopenshell-python/test/util/test_element.py b/src/ifcopenshell-python/test/util/test_element.py index 1ab002f7b0..f40cb295da 100644 --- a/src/ifcopenshell-python/test/util/test_element.py +++ b/src/ifcopenshell-python/test/util/test_element.py @@ -793,6 +793,26 @@ class TestGetContainerIFC4(test.bootstrap.IFC4): ifcopenshell.api.aggregate.assign_object(self.file, products=[subelement], relating_object=element) assert subject.get_container(subelement, should_get_direct=True) is None + def test_getting_the_specific_spatial_container_of_an_element(self): + element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall") + building = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding") + storey = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuildingStorey") + ifcopenshell.api.aggregate.assign_object(self.file, products=[storey], relating_object=building) + ifcopenshell.api.spatial.assign_container(self.file, products=[element], relating_structure=storey) + assert subject.get_container(element, ifc_class="IfcBuilding") == building + assert subject.get_container(element, ifc_class="IfcSite") == None + + def test_getting_the_specific_spatial_container_of_an_element_indirectly(self): + element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcElementAssembly") + subelement = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall") + ifcopenshell.api.aggregate.assign_object(self.file, products=[subelement], relating_object=element) + building = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding") + storey = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuildingStorey") + ifcopenshell.api.aggregate.assign_object(self.file, products=[storey], relating_object=building) + ifcopenshell.api.spatial.assign_container(self.file, products=[element], relating_structure=storey) + assert subject.get_container(subelement, ifc_class="IfcBuilding") == building + assert subject.get_container(subelement, ifc_class="IfcSite") == None + class TestGetReferencedStructures(test.bootstrap.IFC4): def test_getting_references_of_an_element(self):