Generate functions for all API usecases for better static code features. See #2693.

This commit is contained in:
Dion Moult
2024-05-06 14:35:39 +10:00
parent 10f894e2ea
commit d11ec67129
330 changed files with 13283 additions and 13751 deletions
@@ -21,57 +21,54 @@ import ifcopenshell.api
import ifcopenshell.util.element
class Usecase:
def __init__(self, file: ifcopenshell.file, related_objects: list[ifcopenshell.entity_instance]):
"""Unassigns related_objects from their nests.
def unassign_object(file: ifcopenshell.file, related_objects: list[ifcopenshell.entity_instance]) -> None:
"""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.
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.
If the object is not part of a nesting relationship, nothing will happen.
:param related_objects: The list of children of the nesting relationship,
typically IfcElements.
:type related_objects: list[ifcopenshell.entity_instance]
:return: None
:rtype: None
:param related_objects: The list of children of the nesting relationship,
typically IfcElements.
:type related_objects: list[ifcopenshell.entity_instance]
:return: None
:rtype: None
Example:
Example:
.. code:: python
.. code:: python
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)
# 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_objects": related_objects}
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)
# 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])
"""
settings = {"related_objects": related_objects}
def execute(self) -> None:
related_objects = set(self.settings["related_objects"])
ifc2x3 = self.file.schema == "IFC2X3"
if ifc2x3:
rels = set(
rel
for object in related_objects
if (rel := next((rel for rel in object.Decomposes if rel.is_a("IfcRelNests")), None))
)
related_objects = set(settings["related_objects"])
ifc2x3 = file.schema == "IFC2X3"
if ifc2x3:
rels = set(
rel
for object in related_objects
if (rel := next((rel for rel in object.Decomposes if rel.is_a("IfcRelNests")), None))
)
else:
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", file, **{"element": rel})
else:
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)
history = rel.OwnerHistory
file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(file, history)