nest api to maintain order of related objects #4698

order of objects in .RelatedObjects is important (e.g. for cost items, it's the order of their appearance), so we should maintain it and cannot use sets for .RelatedObjects
This commit is contained in:
Andrej730
2024-05-20 16:24:35 +05:00
parent fb88499197
commit 678dbaa66a
4 changed files with 49 additions and 12 deletions
@@ -66,6 +66,23 @@ class TestAssignObject(test.bootstrap.IFC4):
with pytest.raises(RuntimeError):
self.file.by_id(rel_id)
def test_maintain_assignment_order_in_related_objects(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcTask")
subelements = [ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcTask") for _ in range(5)]
for i in range(5):
ifcopenshell.api.run(
"nest.assign_object", self.file, related_objects=subelements[: i + 1], relating_object=element
)
rel = self.file.by_type("IfcRelNests")[0]
assert rel.RelatedObjects == tuple(subelements[: i + 1])
# maintain the order in the affected relationships too
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcTask")
ifcopenshell.api.run(
"nest.assign_object", self.file, related_objects=subelements[2:3], relating_object=element2
)
assert rel.RelatedObjects == tuple(subelements[:2] + subelements[3:])
class TestAssignObjectIFC2X3(test.bootstrap.IFC2X3, TestAssignObject):
pass
@@ -50,5 +50,21 @@ class TestUnassignObject(test.bootstrap.IFC4):
ifcopenshell.api.run("nest.unassign_object", self.file, related_objects=[subelement])
assert len(self.file.by_type("IfcRelNests")) == 0
def test_maintain_assignment_order_in_related_objects(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcTask")
subelements = [ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcTask") for _ in range(5)]
rel = self.file.create_entity("IfcRelNests", RelatingObject=element, RelatedObjects=subelements)
original_order = subelements.copy()
# unassign elements in some random order
# skip 1 element to make sure rel won't get removed
removed_elements = set()
for i in (0, 3, 2, 4):
subelement = subelements[i]
ifcopenshell.api.run("nest.unassign_object", self.file, related_objects=[subelement])
removed_elements.add(subelement)
assert rel.RelatedObjects == tuple([o for o in original_order if o not in removed_elements])
class TestUnassignObjectIFC2X3(test.bootstrap.IFC2X3, TestUnassignObject):
pass