From e9ed850b780743db421858ccdfd314a8ee590efe Mon Sep 17 00:00:00 2001 From: "Sigma Dimensions (Yass)" <79010126+myoualid@users.noreply.github.com> Date: Sat, 23 Sep 2023 16:04:01 +0100 Subject: [PATCH] Proposal to use resource's parent cost, reducing manual insertions --- .../calculate_cost_item_resource_value.py | 24 ++++------------- .../ifcopenshell/util/resource.py | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/api/cost/calculate_cost_item_resource_value.py b/src/ifcopenshell-python/ifcopenshell/api/cost/calculate_cost_item_resource_value.py index 2787b0048c..e947654b3a 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/cost/calculate_cost_item_resource_value.py +++ b/src/ifcopenshell-python/ifcopenshell/api/cost/calculate_cost_item_resource_value.py @@ -18,6 +18,7 @@ import ifcopenshell.api import ifcopenshell.util.date +import ifcopenshell.util.resource class Usecase: @@ -105,8 +106,10 @@ class Usecase: total_cost = 0 for resource in resources: - cost = self.get_cost(resource) - quantity = self.get_quantity(resource) + cost = ifcopenshell.util.resource.get_cost(resource) + quantity = ifcopenshell.util.resource.get_quantity(resource) + if not cost: + cost = ifcopenshell.util.resource.get_parent_cost(resource) # Concept to standardise - Not defined in schema, but this makes manual scheduling of resources 10x faster and less duplicate data. if not cost or not quantity: continue total_cost += cost * quantity @@ -114,20 +117,3 @@ class Usecase: if total_cost: cost_value = ifcopenshell.api.run("cost.add_cost_value", self.file, parent=self.settings["cost_item"]) cost_value.AppliedValue = self.file.createIfcMonetaryMeasure(total_cost) - - def get_cost(self, resource): - total = 0 - for cost_value in resource.BaseCosts or []: - total += cost_value.AppliedValue.wrappedValue if cost_value.AppliedValue else 0 - return total - - def get_quantity(self, resource): - total = 0 - if resource.BaseQuantity: - return resource.BaseQuantity[3] - if resource.Usage and resource.Usage.ScheduleWork: - # For now we assume either hourly or daily depending on how duration is stored - duration = ifcopenshell.util.date.ifc2datetime(resource.Usage.ScheduleWork) - if duration.days: - return duration.days - return duration.seconds / 60 / 60 diff --git a/src/ifcopenshell-python/ifcopenshell/util/resource.py b/src/ifcopenshell-python/ifcopenshell/util/resource.py index f1e17dcc42..f3ce2082e0 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/resource.py +++ b/src/ifcopenshell-python/ifcopenshell/util/resource.py @@ -113,5 +113,32 @@ def get_resource_required_work(resource): iso_string = f"P{required_work}D" return iso_string + def get_nested_resources(resource): return [object for rel in resource.IsNestedBy or [] for object in rel.RelatedObjects] + + +def get_cost(resource): + total = 0 + for cost_value in resource.BaseCosts or []: + total += cost_value.AppliedValue.wrappedValue if cost_value.AppliedValue else 0 + return total + + +def get_quantity(resource): + total = 0 + if resource.BaseQuantity: + return resource.BaseQuantity[3] + if resource.Usage and resource.Usage.ScheduleWork: + # For now we assume either hourly or daily depending on how duration is stored + duration = ifcopenshell.util.date.ifc2datetime(resource.Usage.ScheduleWork) + if duration.days: + return duration.days + return duration.seconds / 60 / 60 + +def get_parent_cost(resource): + if not resource.Nests: + return + else: + cost = get_cost(resource.Nests[0].RelatingObject) + return cost