root.copy_class to support copying ports in ifc2x3

This commit is contained in:
Andrej730
2023-07-14 15:47:36 +05:00
parent 6c88d55cb8
commit 38dd5a218c
2 changed files with 44 additions and 16 deletions
@@ -86,13 +86,29 @@ class Usecase:
inverse.RelatedObjects = [to_element] inverse.RelatedObjects = [to_element]
pset = ifcopenshell.util.element.copy_deep(self.file, inverse.RelatingPropertyDefinition) pset = ifcopenshell.util.element.copy_deep(self.file, inverse.RelatingPropertyDefinition)
inverse.RelatingPropertyDefinition = pset inverse.RelatingPropertyDefinition = pset
elif inverse.is_a("IfcRelNests") and inverse.RelatingObject == from_element: elif (
inverse.is_a("IfcRelNests")
and inverse.RelatingObject == from_element
or inverse.is_a("IfcRelConnectsPortToElement")
and inverse.RelatedElement == from_element
):
# IfcRelConnectsPortToElement was used in IFC2X3
if inverse.is_a("IfcRelNests"):
ports = [e for e in inverse.RelatedObjects if e.is_a("IfcDistributionPort")] ports = [e for e in inverse.RelatedObjects if e.is_a("IfcDistributionPort")]
if ports: else: # IfcRelConnectsPortToElement
ports = [inverse.RelatingPort]
if not ports:
continue
new_ports = [ifcopenshell.api.run("root.copy_class", self.file, product=p) for p in ports] new_ports = [ifcopenshell.api.run("root.copy_class", self.file, product=p) for p in ports]
inverse = ifcopenshell.util.element.copy(self.file, inverse) inverse = ifcopenshell.util.element.copy(self.file, inverse)
if inverse.is_a("IfcRelNests"):
inverse.RelatingObject = to_element inverse.RelatingObject = to_element
inverse.RelatedObjects = new_ports inverse.RelatedObjects = new_ports
else:
inverse.RelatedElement = to_element
inverse.RelatingPort = new_ports[0]
for port in new_ports: for port in new_ports:
ifcopenshell.api.run("system.unassign_port", self.file, element=from_element, port=port) ifcopenshell.api.run("system.unassign_port", self.file, element=from_element, port=port)
matrix = ifcopenshell.util.placement.get_local_placement(port.ObjectPlacement) matrix = ifcopenshell.util.placement.get_local_placement(port.ObjectPlacement)
@@ -232,3 +232,15 @@ class TestCopyClass(test.bootstrap.IFC4):
new = ifcopenshell.api.run("root.copy_class", self.file, product=element) new = ifcopenshell.api.run("root.copy_class", self.file, product=element)
assert len(self.file.by_type("IfcRelAssignsToGroup")) == 1 assert len(self.file.by_type("IfcRelAssignsToGroup")) == 1
assert new.HasAssignments[0].RelatingGroup == group assert new.HasAssignments[0].RelatingGroup == group
class TestCopyClassIFC2X3(test.bootstrap.IFC2X3):
def test_copying_distribution_ports_bug(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcFlowTerminal")
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]