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,65 +21,57 @@ import ifcopenshell.api
import ifcopenshell.util.element
class Usecase:
def __init__(self, file, relating_resource=None, related_object=None):
"""Removes the relationship between a resource and object
def unassign_resource(file, relating_resource=None, related_object=None) -> None:
"""Removes the relationship between a resource and object
:param relating_resource: The IfcResource to assign the object to.
:type relating_resource: ifcopenshell.entity_instance
:param related_object: The IfcProduct or IfcActor to assign to the
object.
:type related_object: ifcopenshell.entity_instance
:return: The newly created IfcRelAssignsToResource
:rtype: ifcopenshell.entity_instance
:param relating_resource: The IfcResource to assign the object to.
:type relating_resource: ifcopenshell.entity_instance
:param related_object: The IfcProduct or IfcActor to assign to the
object.
:type related_object: ifcopenshell.entity_instance
:return: The newly created IfcRelAssignsToResource
:rtype: ifcopenshell.entity_instance
Example:
Example:
.. code:: python
.. code:: python
# Add our own crew
crew = ifcopenshell.api.run("resource.add_resource", model, ifc_class="IfcCrewResource")
# Add our own crew
crew = ifcopenshell.api.run("resource.add_resource", model, ifc_class="IfcCrewResource")
# Add some a tower crane to our crew.
crane = ifcopenshell.api.run("resource.add_resource", model,
parent_resource=crew, ifc_class="IfcConstructionEquipmentResource", name="Tower Crane 01")
# Add some a tower crane to our crew.
crane = ifcopenshell.api.run("resource.add_resource", model,
parent_resource=crew, ifc_class="IfcConstructionEquipmentResource", name="Tower Crane 01")
# Our tower crane will be placed via this physical product.
product = ifcopenshell.api.run("root.create_entity", model,
ifc_class="IfcBuildingElementProxy", predefined_type="CRANE")
# Our tower crane will be placed via this physical product.
product = ifcopenshell.api.run("root.create_entity", model,
ifc_class="IfcBuildingElementProxy", predefined_type="CRANE")
# Let's assign our crane to the resource. The crane now represents
# the resource.
ifcopenshell.api.run("resource.assign_resource", model,
relating_resource=crane, related_object=product)
# Let's assign our crane to the resource. The crane now represents
# the resource.
ifcopenshell.api.run("resource.assign_resource", model,
relating_resource=crane, related_object=product)
# Undo it.
ifcopenshell.api.run("resource.unassign_resource", model,
relating_resource=crane, related_object=product)
"""
self.file = file
self.settings = {
"relating_resource": relating_resource,
"related_object": related_object,
}
# Undo it.
ifcopenshell.api.run("resource.unassign_resource", model,
relating_resource=crane, related_object=product)
"""
settings = {
"relating_resource": relating_resource,
"related_object": related_object,
}
def execute(self):
for rel in self.settings["related_object"].HasAssignments or []:
if (
not rel.is_a("IfcRelAssignsToResource")
or rel.RelatingResource != self.settings["relating_resource"]
):
continue
if len(rel.RelatedObjects) == 1:
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
for rel in settings["related_object"].HasAssignments or []:
if not rel.is_a("IfcRelAssignsToResource") or rel.RelatingResource != settings["relating_resource"]:
continue
if len(rel.RelatedObjects) == 1:
history = rel.OwnerHistory
file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(file, history)
return
related_objects = list(rel.RelatedObjects)
related_objects.remove(settings["related_object"])
rel.RelatedObjects = related_objects
ifcopenshell.api.run("owner.update_owner_history", file, **{"element": rel})
return rel