From 933383926717707c8a7a57ac8d7b00c34dfe5eb1 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Tue, 3 May 2022 18:05:27 +1000 Subject: [PATCH] Copying an element now also copies its distribution ports --- .../ifcopenshell/api/root/copy_class.py | 11 +++++++++++ .../test/api/root/test_copy_class.py | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/ifcopenshell-python/ifcopenshell/api/root/copy_class.py b/src/ifcopenshell-python/ifcopenshell/api/root/copy_class.py index 1021998af4..acb57a9979 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/root/copy_class.py +++ b/src/ifcopenshell-python/ifcopenshell/api/root/copy_class.py @@ -45,6 +45,17 @@ class Usecase: inverse.RelatedObjects = [to_element] pset = ifcopenshell.util.element.copy_deep(self.file, inverse.RelatingPropertyDefinition) inverse.RelatingPropertyDefinition = pset + elif inverse.is_a("IfcRelNests") and inverse.RelatingObject == from_element: + ports = [e for e in inverse.RelatedObjects if e.is_a("IfcDistributionPort")] + if ports: + new_ports = [ifcopenshell.api.run("root.copy_class", self.file, product=p) for p in ports] + [ + ifcopenshell.api.run("system.unassign_port", self.file, element=from_element, port=p) + for p in new_ports + ] + inverse = ifcopenshell.util.element.copy(self.file, inverse) + inverse.RelatingObject = to_element + inverse.RelatedObjects = new_ports elif inverse.is_a("IfcRelAggregates") and inverse.RelatingObject == from_element: continue elif inverse.is_a("IfcRelContainedInSpatialStructure") and inverse.RelatingStructure == from_element: diff --git a/src/ifcopenshell-python/test/api/root/test_copy_class.py b/src/ifcopenshell-python/test/api/root/test_copy_class.py index 39198260d6..fb67ac3214 100644 --- a/src/ifcopenshell-python/test/api/root/test_copy_class.py +++ b/src/ifcopenshell-python/test/api/root/test_copy_class.py @@ -149,3 +149,13 @@ class TestCopyClass(test.bootstrap.IFC4): ifcopenshell.api.run("type.assign_type", self.file, related_object=element, relating_type=type) new = ifcopenshell.api.run("root.copy_class", self.file, product=type) assert not new.Types + + def test_copying_distribution_ports(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcChiller") + port = ifcopenshell.api.run("system.add_port", self.file) + ifcopenshell.api.run("system.assign_port", self.file, element=element, port=port) + new = ifcopenshell.api.run("root.copy_class", self.file, product=element) + new_ports = ifcopenshell.util.system.get_ports(new) + assert port not in new_ports + assert new_ports[0].is_a("IfcDistributionPort") + assert ifcopenshell.util.system.get_ports(element) == [port]