This commit is contained in:
Andrej730
2025-03-14 10:43:47 +05:00
parent 10ecac33b8
commit 0e8810c1f5
118 changed files with 869 additions and 1203 deletions
@@ -51,20 +51,15 @@ def add_resource(
:param parent_resource: If this is a child resource (typically to a crew
resource), then nominate the parent IfcConstructionResource here.
:type parent_resource: ifcopenshell.entity_instance, optional
:param ifc_class: The class of resource chosen from
IfcConstructionEquipmentResource, IfcConstructionMaterialResource,
IfcConstructionProductResource, IfcCrewResource, IfcLaborResource,
or IfcSubContractResource.
:type ifc_class: str,optional
:param name: The name of the resource
:type name: str,optional
:param predefined_type: Consult the IFC documentation for the valid
predefined types for each type of resource class.
:type predefined_type: str,optional
:return: The newly created resource depending on the nominated IFC
class.
:rtype: ifcopenshell.entity_instance
Example:
@@ -76,25 +71,17 @@ def add_resource(
# Add some labour to our crew.
ifcopenshell.api.resource.add_resource(model, parent_resource=crew, ifc_class="IfcLaborResource")
"""
settings = {
"parent_resource": parent_resource,
"ifc_class": ifc_class,
"name": name,
"predefined_type": predefined_type,
}
resource = ifcopenshell.api.root.create_entity(
file,
ifc_class=settings["ifc_class"],
predefined_type=settings["predefined_type"],
name=settings["name"] or "Unnamed",
ifc_class=ifc_class,
predefined_type=predefined_type,
name=name or "Unnamed",
)
# TODO: this is an ambiguity by buildingSMART: Can we nest an IfcCrewResource under an IfcCrewResource ?
# https://forums.buildingsmart.org/t/what-are-allowed-to-be-root-level-construction-resources/3550
if settings["parent_resource"]:
ifcopenshell.api.nest.assign_object(
file, related_objects=[resource], relating_object=settings["parent_resource"]
)
if parent_resource:
ifcopenshell.api.nest.assign_object(file, related_objects=[resource], relating_object=parent_resource)
elif file.schema != "IFC2X3":
context = file.by_type("IfcContext")[0]
ifcopenshell.api.project.assign_declaration(file, definitions=[resource], relating_context=context)
@@ -42,12 +42,9 @@ def assign_resource(
(e.g. if the resource is a labour resource).
: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:
@@ -82,26 +79,18 @@ def assign_resource(
# This means that UCO is now our crane operator.
ifcopenshell.api.resource.assign_resource(model, relating_resource=crane, related_object=actor)
"""
settings = {
"relating_resource": relating_resource,
"related_object": related_object,
}
if settings["related_object"].HasAssignments:
for assignment in settings["related_object"].HasAssignments:
if (
assignment.is_a("IfclRelAssignsToResource")
and assignment.RelatingResource == settings["relating_resource"]
):
if related_object.HasAssignments:
for assignment in related_object.HasAssignments:
if assignment.is_a("IfclRelAssignsToResource") and assignment.RelatingResource == relating_resource:
return assignment
resource_of = None
if settings["relating_resource"].ResourceOf:
resource_of = settings["relating_resource"].ResourceOf[0]
if relating_resource.ResourceOf:
resource_of = relating_resource.ResourceOf[0]
if resource_of:
related_objects = list(resource_of.RelatedObjects)
related_objects.append(settings["related_object"])
related_objects.append(related_object)
resource_of.RelatedObjects = related_objects
ifcopenshell.api.owner.update_owner_history(file, **{"element": resource_of})
else:
@@ -110,8 +99,8 @@ def assign_resource(
**{
"GlobalId": ifcopenshell.guid.new(),
"OwnerHistory": ifcopenshell.api.owner.create_owner_history(file),
"RelatedObjects": [settings["related_object"]],
"RelatingResource": settings["relating_resource"],
"RelatedObjects": [related_object],
"RelatingResource": relating_resource,
}
)
return resource_of
@@ -25,15 +25,18 @@ import ifcopenshell.util.resource
def calculate_resource_usage(file: ifcopenshell.file, resource: ifcopenshell.entity_instance) -> None:
"""Calculates the number of resources required to perform scheduled work on a task."""
settings = {"resource": resource}
"""Calculates the number of resources required to perform scheduled work on a task.
if ifcopenshell.util.constraint.is_attribute_locked(settings["resource"], "Usage.ScheduleUsage"):
:param resource: The IfcConstructionResource to calculate the usage for.
:return: None
"""
if ifcopenshell.util.constraint.is_attribute_locked(resource, "Usage.ScheduleUsage"):
return
if not settings["resource"].Usage or not settings["resource"].Usage.ScheduleWork:
if not resource.Usage or not resource.Usage.ScheduleWork:
return
task = ifcopenshell.util.resource.get_task_assignments(settings["resource"])
task = ifcopenshell.util.resource.get_task_assignments(resource)
if not task or not task.TaskTime:
return
@@ -46,7 +49,7 @@ def calculate_resource_usage(file: ifcopenshell.file, resource: ifcopenshell.ent
seconds = task_duration.days * hours_per_day * 60 * 60
seconds += task_duration.seconds
person_hours = ifcopenshell.util.date.ifc2datetime(settings["resource"].Usage.ScheduleWork)
person_hours = ifcopenshell.util.date.ifc2datetime(resource.Usage.ScheduleWork)
required_resources = person_hours.total_seconds() / seconds
settings["resource"].Usage.ScheduleUsage = float(required_resources)
resource.Usage.ScheduleUsage = float(required_resources)
@@ -22,6 +22,9 @@ import ifcopenshell.util.element
def remove_resource_quantity(file: ifcopenshell.file, resource: ifcopenshell.entity_instance) -> None:
"""Removes the base quantity of a resource
:param resource: The IfcConstructionResource to remove the quantity from.
:return: None
Example:
.. code:: python
@@ -41,9 +44,7 @@ def remove_resource_quantity(file: ifcopenshell.file, resource: ifcopenshell.ent
# let's clean up our mess and remove the quantity.
ifcopenshell.api.resource.remove_resource_quantity(model, resource=labour)
"""
settings = {"resource": resource}
old_quantity = settings["resource"].BaseQuantity
settings["resource"].BaseQuantity = None
old_quantity = resource.BaseQuantity
resource.BaseQuantity = None
if old_quantity:
ifcopenshell.util.element.remove_deep(file, old_quantity)
@@ -29,12 +29,9 @@ def unassign_resource(
"""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: None
:rtype: None
Example:
@@ -60,13 +57,8 @@ def unassign_resource(
ifcopenshell.api.resource.unassign_resource(model,
relating_resource=crane, related_object=product)
"""
settings = {
"relating_resource": relating_resource,
"related_object": related_object,
}
for rel in settings["related_object"].HasAssignments or []:
if not rel.is_a("IfcRelAssignsToResource") or rel.RelatingResource != settings["relating_resource"]:
for rel in related_object.HasAssignments or []:
if not rel.is_a("IfcRelAssignsToResource") or rel.RelatingResource != relating_resource:
continue
if len(rel.RelatedObjects) == 1:
history = rel.OwnerHistory
@@ -75,6 +67,6 @@ def unassign_resource(
ifcopenshell.util.element.remove_deep2(file, history)
return
related_objects = list(rel.RelatedObjects)
related_objects.remove(settings["related_object"])
related_objects.remove(related_object)
rel.RelatedObjects = related_objects
ifcopenshell.api.owner.update_owner_history(file, **{"element": rel})