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,24 +86,40 @@ 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]
inverse = ifcopenshell.util.element.copy(self.file, inverse)
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")]
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]
inverse = ifcopenshell.util.element.copy(self.file, inverse)
if inverse.is_a("IfcRelNests"):
inverse.RelatingObject = to_element
inverse.RelatedObjects = new_ports
for port in new_ports:
ifcopenshell.api.run("system.unassign_port", self.file, element=from_element, port=port)
matrix = ifcopenshell.util.placement.get_local_placement(port.ObjectPlacement)
ifcopenshell.api.run(
"geometry.edit_object_placement",
self.file,
product=port,
matrix=matrix,
is_si=False,
should_transform_children=False,
)
else:
inverse.RelatedElement = to_element
inverse.RelatingPort = new_ports[0]
for port in new_ports:
ifcopenshell.api.run("system.unassign_port", self.file, element=from_element, port=port)
matrix = ifcopenshell.util.placement.get_local_placement(port.ObjectPlacement)
ifcopenshell.api.run(
"geometry.edit_object_placement",
self.file,
product=port,
matrix=matrix,
is_si=False,
should_transform_children=False,
)
elif inverse.is_a("IfcRelAggregates") and inverse.RelatingObject == from_element:
continue
elif inverse.is_a("IfcRelContainedInSpatialStructure") and inverse.RelatingStructure == from_element:
@@ -232,3 +232,15 @@ class TestCopyClass(test.bootstrap.IFC4):
new = ifcopenshell.api.run("root.copy_class", self.file, product=element)
assert len(self.file.by_type("IfcRelAssignsToGroup")) == 1
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]