From ff76850a5489db82857a3f5d6664a0d4ddaddc71 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Mon, 15 Apr 2024 17:12:16 +0500 Subject: [PATCH] ifcopenshell.util.element.get_structure_referenced_elements --- .../ifcopenshell/util/element.py | 21 +++++++++++++++++++ .../test/util/test_element.py | 16 ++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/ifcopenshell-python/ifcopenshell/util/element.py b/src/ifcopenshell-python/ifcopenshell/util/element.py index 5a97494737..f8b8791787 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/element.py +++ b/src/ifcopenshell-python/ifcopenshell/util/element.py @@ -904,6 +904,27 @@ def get_referenced_structures(element: ifcopenshell.entity_instance) -> list[ifc return [r.RelatingStructure for r in getattr(element, "ReferencedInStructures", [])] +def get_structure_referenced_elements(structure: ifcopenshell.entity_instance) -> set[ifcopenshell.entity_instance]: + """Retreives a set of elements referenced by a structure + + :param structure: IfcSpatialElement + :type element: ifcopenshell.entity_instance.entity_instance + :return: A set of referenced elements, IfcSpatialReferenceSelect + :rtype: set[ifcopenshell.entity_instance.entity_instance] + + Example: + + .. code:: python + + element = file.by_type("IfcBuildingStorey")[0] + print(ifcopenshell.util.element.get_structure_referenced_elements(element)) + """ + referenced = set() + for rel in structure.ReferencesElements: + referenced.update(rel.RelatedElements) + return referenced + + def get_decomposition(element: ifcopenshell.entity_instance, is_recursive=True) -> list[ifcopenshell.entity_instance]: """ Retrieves all subelements of an element based on the spatial decomposition diff --git a/src/ifcopenshell-python/test/util/test_element.py b/src/ifcopenshell-python/test/util/test_element.py index 97363f8017..e90130e28e 100644 --- a/src/ifcopenshell-python/test/util/test_element.py +++ b/src/ifcopenshell-python/test/util/test_element.py @@ -726,6 +726,22 @@ class TestGetReferencedStructures(test.bootstrap.IFC4): assert subject.get_referenced_structures(element) == [building, building2] +class TestGetStructureReferencedElements(test.bootstrap.IFC4): + def test_getting_references_of_an_element(self): + building = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding") + assert subject.get_structure_referenced_elements(building) == set() + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + ifcopenshell.api.run("spatial.reference_structure", self.file, product=element, relating_structure=building) + assert subject.get_structure_referenced_elements(building) == {element} + element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + ifcopenshell.api.run("spatial.reference_structure", self.file, product=element2, relating_structure=building) + assert subject.get_structure_referenced_elements(building) == {element, element2} + + +class TestGetStructureReferencedElementsIFC2X3(test.bootstrap.IFC2X3, TestGetStructureReferencedElements): + pass + + class TestGetDecompositionIFC4(test.bootstrap.IFC4): def test_getting_decomposed_subelements_of_an_element(self): element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcElementAssembly")