nest.unassign_object - support batching #4474

This commit is contained in:
Andrej730
2024-04-09 17:12:25 +05:00
parent 97db95085a
commit 06b84cfa24
8 changed files with 118 additions and 75 deletions
@@ -22,48 +22,48 @@ import ifcopenshell.util.element
class Usecase:
def __init__(self, file, related_object=None):
"""Unassigns a related_object from its nest.
def __init__(self, file: ifcopenshell.file, related_objects: list[ifcopenshell.entity_instance]):
"""Unassigns related_objects from their nests.
An object (the whole within a decomposition) is Nested by zero or one more smaller objects.
This function will remove this nesting relationship.
If the object is not part of a nesting relationship, nothing will happen.
:param related_object: The child of the nesting relationship, typically
an IfcElement.
:type related_object: ifcopenshell.entity_instance.entity_instance
:return: None if the nest has only one child, otherwise the IfcRelNests relationship instance
:param related_objects: The list of children of the nesting relationship,
typically IfcElements.
:type related_objects: list[ifcopenshell.entity_instance.entity_instance]
:return: None
:rtype: None
Example:
.. code:: python
task = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcSite")
subtask1 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuilding")
subtask2 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuilding")
task = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcTasks")
subtask1 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcTask")
subtask2 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcTask")
ifcopenshell.api.run("nest.assign_object", model, related_objects=[subtask1], relating_object=task)
ifcopenshell.api.run("nest.assign_object", model, related_objects=[subtask2], relating_object=task)
# The relationship is returned as task still has subtask2
rel = ifcopenshell.api.run("nest.unassign_object", model, related_object=subtask1)
# Nothing is returned, as the relationship has no related objects
ifcopenshell.api.run("nest.unassign_object", model, related_object=subtask2)
# nothing is returned
rel = ifcopenshell.api.run("nest.unassign_object", model, related_objects=[subtask1])
# nothing is returned, relationship is removed
ifcopenshell.api.run("nest.unassign_object", model, related_objects=[subtask2])
"""
self.file = file
self.settings = {"related_object": related_object}
self.settings = {"related_objects": related_objects}
def execute(self):
for rel in self.settings["related_object"].Nests or []:
if not rel.is_a("IfcRelNests"):
continue
if len(rel.RelatedObjects) == 1:
def execute(self) -> None:
related_objects = set(self.settings["related_objects"])
rels = set(rel for object in related_objects if (rel := next((rel for rel in object.Nests), None)))
for rel in rels:
related_objects = set(rel.RelatedObjects) - related_objects
if related_objects:
rel.RelatedObjects = list(related_objects)
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": rel})
else:
history = rel.OwnerHistory
self.file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
return
related_objects = list(rel.RelatedObjects)
related_objects.remove(self.settings["related_object"])
rel.RelatedObjects = related_objects
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": rel})
return rel