From 2aa5e9d808f085298bcbd682d11790554e623a49 Mon Sep 17 00:00:00 2001 From: Sigma Dimensions <79010126+myoualid@users.noreply.github.com> Date: Sun, 30 Apr 2023 15:35:17 +0100 Subject: [PATCH] - refactor IOS.API resource calculation to re-use shared utilities - run black --- .../ifcopenshell/api/resource/add_resource.py | 7 ++- .../api/resource/calculate_resource_work.py | 58 ++----------------- .../api/resource/edit_resource_quantity.py | 5 +- 3 files changed, 14 insertions(+), 56 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/api/resource/add_resource.py b/src/ifcopenshell-python/ifcopenshell/api/resource/add_resource.py index 6fa25a0bc9..6a9ac81167 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/resource/add_resource.py +++ b/src/ifcopenshell-python/ifcopenshell/api/resource/add_resource.py @@ -21,7 +21,12 @@ import ifcopenshell.api class Usecase: def __init__( - self, file, parent_resource=None, ifc_class="IfcCrewResource", name=None, predefined_type="NOTDEFINED" + self, + file, + parent_resource=None, + ifc_class="IfcCrewResource", + name=None, + predefined_type="NOTDEFINED", ): """Add a new construction resource diff --git a/src/ifcopenshell-python/ifcopenshell/api/resource/calculate_resource_work.py b/src/ifcopenshell-python/ifcopenshell/api/resource/calculate_resource_work.py index a9f2c5e06c..c1d09b3ea0 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/resource/calculate_resource_work.py +++ b/src/ifcopenshell-python/ifcopenshell/api/resource/calculate_resource_work.py @@ -20,6 +20,7 @@ import math import ifcopenshell.api import ifcopenshell.util.date import ifcopenshell.util.element +import ifcopenshell.util.resource class Usecase: @@ -59,28 +60,10 @@ class Usecase: self.settings = {"resource": resource} def execute(self): - self.productivity = ifcopenshell.util.element.get_psets( + amount_worked = ifcopenshell.util.resource.get_resource_required_work( self.settings["resource"] - ).get("EPset_Productivity", None) - if not self.productivity: - # Proposal for Schema - If instance doesn't have any productivity, use the parent's productivity - if any. - if not self.settings["resource"].Nests: - return - else: - parent_resource = self.settings["resource"].Nests[0].RelatingObject - self.productivity = ifcopenshell.util.element.get_psets( - parent_resource - ).get("EPset_Productivity", None) - if not self.productivity: - return - - unit_consumed = self.get_unit_consumed() - self.unit_produced_name = self.productivity.get( - "BaseQuantityProducedName", None ) - unit_produced = self.productivity.get("BaseQuantityProducedValue", None) - total_produced = self.get_total_produced() - if not unit_consumed or not unit_produced or not total_produced: + if not amount_worked: return if not self.settings["resource"].Usage: ifcopenshell.api.run( @@ -88,37 +71,4 @@ class Usecase: self.file, resource=self.settings["resource"], ) - if "T" in self.productivity.get("BaseQuantityConsumed", None): - seconds = (unit_consumed.days * 24 * 60 * 60) + unit_consumed.seconds - amount_worked = total_produced / unit_produced * seconds - self.settings[ - "resource" - ].Usage.ScheduleWork = f"PT{amount_worked / 60 / 60}H" - else: - days = unit_consumed.days + (unit_consumed.seconds / (24 * 60 * 60)) - amount_worked = total_produced / unit_produced * days - self.settings["resource"].Usage.ScheduleWork = f"P{amount_worked}D" - - def get_unit_consumed(self): - duration = self.productivity.get("BaseQuantityConsumed", None) - if not duration: - return - return ifcopenshell.util.date.ifc2datetime(duration) - - def get_total_produced(self): - total = 0 - for rel in self.settings["resource"].HasAssignments or []: - if not rel.is_a("IfcRelAssignsToProcess"): - continue - for rel2 in rel.RelatingProcess.HasAssignments or []: - if not rel2.is_a("IfcRelAssignsToProduct"): - continue - if self.unit_produced_name == "Count": - total += 1 - else: - psets = ifcopenshell.util.element.get_psets(rel2.RelatingProduct) - for pset in psets.values(): - for name, value in pset.items(): - if name == self.unit_produced_name: - total += float(value) - return total + self.settings["resource"].Usage.ScheduleWork = amount_worked diff --git a/src/ifcopenshell-python/ifcopenshell/api/resource/edit_resource_quantity.py b/src/ifcopenshell-python/ifcopenshell/api/resource/edit_resource_quantity.py index 275cd5177b..f88ed466fd 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/resource/edit_resource_quantity.py +++ b/src/ifcopenshell-python/ifcopenshell/api/resource/edit_resource_quantity.py @@ -51,7 +51,10 @@ class Usecase: physical_quantity=time, attributes={"TimeValue": 8.0}) """ self.file = file - self.settings = {"physical_quantity": physical_quantity, "attributes": attributes or {}} + self.settings = { + "physical_quantity": physical_quantity, + "attributes": attributes or {}, + } def execute(self): for name, value in self.settings["attributes"].items():