diff --git a/src/ifcopenshell-python/ifcopenshell/util/system.py b/src/ifcopenshell-python/ifcopenshell/util/system.py index e1517be6b8..c703c1fe08 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/system.py +++ b/src/ifcopenshell-python/ifcopenshell/util/system.py @@ -34,3 +34,12 @@ def get_element_systems(element): ]: results.append(rel.RelatingGroup) return results + + +def get_ports(element): + results = [] + for rel in getattr(element, "IsNestedBy", []) or []: + results.extend([o for o in rel.RelatedObjects if o.is_a("IfcDistributionPort")]) + for rel in element.HasPorts or []: + results.append(rel.RelatingPort) + return results diff --git a/src/ifcopenshell-python/test/util/test_system.py b/src/ifcopenshell-python/test/util/test_system.py index 5d998cd904..3ec569e970 100644 --- a/src/ifcopenshell-python/test/util/test_system.py +++ b/src/ifcopenshell-python/test/util/test_system.py @@ -45,3 +45,19 @@ class TestGetElementSystems(test.bootstrap.IFC4): "system.assign_system", self.file, product=element, system=self.file.createIfcStructuralAnalysisModel() ) assert not subject.get_element_systems(element) + + +class TestGetPorts(test.bootstrap.IFC4): + def test_run(self): + port = self.file.createIfcDistributionPort() + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcChiller") + ifcopenshell.api.run("system.assign_port", self.file, element=element, port=port) + assert subject.get_ports(element) == [port] + + +class TestGetPortsIFC2X3(test.bootstrap.IFC2X3): + def test_run(self): + port = self.file.createIfcDistributionPort() + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcFlowSegment") + ifcopenshell.api.run("system.assign_port", self.file, element=element, port=port) + assert subject.get_ports(element) == [port]