From d3bcb8c6b943ee65363a3b649c0f29f24a432526 Mon Sep 17 00:00:00 2001 From: "Sigma Dimensions (Yass)" <79010126+myoualid@users.noreply.github.com> Date: Thu, 24 Aug 2023 15:51:33 +0100 Subject: [PATCH] updating a duration will not auto update schedule usage --- .../bim/module/resource/operator.py | 4 +-- .../blenderbim/bim/module/resource/prop.py | 13 ++++++---- .../blenderbim/bim/module/resource/ui.py | 2 +- .../blenderbim/bim/module/sequence/prop.py | 2 ++ src/blenderbim/blenderbim/core/resource.py | 3 +-- src/blenderbim/blenderbim/core/sequence.py | 1 + src/blenderbim/blenderbim/core/tool.py | 3 +-- src/blenderbim/blenderbim/tool/resource.py | 14 +++++------ src/blenderbim/blenderbim/tool/sequence.py | 1 + .../api/resource/calculate_resource_usage.py | 9 +------ .../api/resource/calculate_resource_work.py | 14 +---------- .../api/resource/edit_resource_time.py | 9 +++---- .../api/sequence/edit_task_time.py | 11 ++++++++ .../ifcopenshell/util/constraint.py | 25 ++++++++++++++----- .../ifcopenshell/util/resource.py | 2 +- 15 files changed, 60 insertions(+), 53 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/resource/operator.py b/src/blenderbim/blenderbim/bim/module/resource/operator.py index b3eaea14b2..fbdd4374f7 100644 --- a/src/blenderbim/blenderbim/bim/module/resource/operator.py +++ b/src/blenderbim/blenderbim/bim/module/resource/operator.py @@ -424,7 +424,6 @@ class CalculateResourceUsage(bpy.types.Operator, tool.Ifc.Operator): bl_idname = "bim.calculate_resource_usage" bl_label = "Calculate Resource Usage" bl_options = {"REGISTER", "UNDO"} - resource: bpy.props.IntProperty() @classmethod def poll(cls, context): @@ -434,7 +433,8 @@ class CalculateResourceUsage(bpy.types.Operator, tool.Ifc.Operator): task = tool.Resource.get_task_assignments(active_resource) if task and tool.Sequence.has_duration(task): return True + return False def _execute(self, context): - core.calculate_resource_usage(tool.Ifc, tool.Resource, resource=tool.Ifc.get().by_id(self.resource)) + core.calculate_resource_usage(tool.Ifc, tool.Resource, resource=tool.Ifc.get().by_id(tool.Resource.get_highlighted_resource())) diff --git a/src/blenderbim/blenderbim/bim/module/resource/prop.py b/src/blenderbim/blenderbim/bim/module/resource/prop.py index 615e1e8b60..9ea20d446c 100644 --- a/src/blenderbim/blenderbim/bim/module/resource/prop.py +++ b/src/blenderbim/blenderbim/bim/module/resource/prop.py @@ -84,16 +84,19 @@ def updateResourceUsage(self, context): props = context.scene.BIMResourceProperties if not props.is_resource_update_enabled: return - - if self.schedule_usage == "": + if not self.schedule_usage: return resource = tool.Ifc.get().by_id(self.ifc_definition_id) - tool.Resource.run_edit_resource_time(resource, attributes={"ScheduleUsage": self.schedule_usage}) - tool.Resource.load_resource_properties() + if resource.Usage and resource.Usage.ScheduleUsage == self.schedule_usage: + return + tool.Resource.run_edit_resource_time(resource, attributes={ + "ScheduleUsage": self.schedule_usage + }) tool.Sequence.load_task_properties() + tool.Resource.load_resource_properties() + tool.Sequence.refresh_task_resources() blenderbim.bim.module.resource.data.refresh() blenderbim.bim.module.sequence.data.refresh() - tool.Sequence.refresh_task_resources() blenderbim.bim.module.pset.data.refresh() diff --git a/src/blenderbim/blenderbim/bim/module/resource/ui.py b/src/blenderbim/blenderbim/bim/module/resource/ui.py index 7304bd25e6..29cb56aa07 100644 --- a/src/blenderbim/blenderbim/bim/module/resource/ui.py +++ b/src/blenderbim/blenderbim/bim/module/resource/ui.py @@ -142,7 +142,7 @@ class BIM_PT_resources(Panel): row2col2 = col2.row() row2col2.prop(self.tprops.resources[self.props.active_resource_index], "schedule_usage", text="") row2col3 = col3.row() - row2col3.operator("bim.calculate_resource_usage", text="", icon="TEMP").resource = ifc_definition_id + row2col3.operator("bim.calculate_resource_usage", text="", icon="TEMP") op = row2col3.operator( "bim.add_usage_constraint" if not is_usage_locked else "bim.remove_usage_constraint", text="", diff --git a/src/blenderbim/blenderbim/bim/module/sequence/prop.py b/src/blenderbim/blenderbim/bim/module/sequence/prop.py index b806fa445c..4ea2f667d8 100644 --- a/src/blenderbim/blenderbim/bim/module/sequence/prop.py +++ b/src/blenderbim/blenderbim/bim/module/sequence/prop.py @@ -225,7 +225,9 @@ def updateTaskDuration(self, context): task_time = tool.Ifc.run("sequence.add_task_time", task=task) tool.Ifc.run("sequence.edit_task_time", task_time=task_time, attributes={"ScheduleDuration": duration}) SequenceData.load() + blenderbim.core.sequence.load_task_properties(tool.Sequence) bpy.ops.bim.load_task_properties() + tool.Sequence.load_resources() def get_schedule_predefined_types(self, context): diff --git a/src/blenderbim/blenderbim/core/resource.py b/src/blenderbim/blenderbim/core/resource.py index 6b1ee21719..c026139446 100644 --- a/src/blenderbim/blenderbim/core/resource.py +++ b/src/blenderbim/blenderbim/core/resource.py @@ -23,7 +23,6 @@ def load_resources(resource): resource.load_resources() resource.load_resource_properties() - def add_resource(tool_ifc, resource_tool, ifc_class, parent_resource=None): tool_ifc.run("resource.add_resource", ifc_class=ifc_class, parent_resource=parent_resource) load_resources(resource_tool) @@ -184,7 +183,7 @@ def edit_productivity_pset(ifc, resource_tool): def add_usage_constraint(ifc, resource_tool, resource=None, reference_path=None): - metric = resource_tool.has_usage_metric(resource) + metric = resource_tool.has_metric_constraint(resource, "Usage") if metric: return print("Must remove existing metric first") diff --git a/src/blenderbim/blenderbim/core/sequence.py b/src/blenderbim/blenderbim/core/sequence.py index e0258d39a1..2321796783 100644 --- a/src/blenderbim/blenderbim/core/sequence.py +++ b/src/blenderbim/blenderbim/core/sequence.py @@ -193,6 +193,7 @@ def edit_task_time(ifc, sequence, task_time=None): task = sequence.get_active_task() sequence.load_task_properties(task=task) sequence.disable_editing_task_time() + sequence.load_resources() def assign_predecessor(ifc, sequence, task=None): diff --git a/src/blenderbim/blenderbim/core/tool.py b/src/blenderbim/blenderbim/core/tool.py index 731543b61f..30905d0928 100644 --- a/src/blenderbim/blenderbim/core/tool.py +++ b/src/blenderbim/blenderbim/core/tool.py @@ -615,8 +615,7 @@ class Resource: def get_resource_time_attributes(cls): pass def get_resource_time(cls, resource): pass def go_to_resource(cls, resource): pass - def has_metric_constraint(cls, resource, attribute): pass - def has_usage_metric(cls, resource): pass + def has_metric_constraint(cls, resource): pass def import_resources(cls, file_path): pass def load_cost_value_attributes(cls, cost_value): pass def load_productivity_data(cls): pass diff --git a/src/blenderbim/blenderbim/tool/resource.py b/src/blenderbim/blenderbim/tool/resource.py index 13d51f8d05..3c5698505d 100644 --- a/src/blenderbim/blenderbim/tool/resource.py +++ b/src/blenderbim/blenderbim/tool/resource.py @@ -397,12 +397,8 @@ class Resource(blenderbim.core.tool.Resource): @classmethod def has_metric_constraint(cls, resource, attribute): - metrics = ifcopenshell.util.constraint.has_metric_constraints(resource, attribute) - return metrics[0] if metrics else None - - @classmethod - def has_usage_metric(cls, resource): - return cls.has_metric_constraint(resource, "Usage") + metrics = ifcopenshell.util.constraint.get_metric_constraints(resource, attribute) + return True if metrics else False @classmethod def run_edit_resource_time(cls, resource, attributes): @@ -446,4 +442,8 @@ class Resource(blenderbim.core.tool.Resource): @classmethod def get_nested_resources(cls, resource): - return ifcopenshell.util.resource.get_nested_resources(resource) \ No newline at end of file + return ifcopenshell.util.resource.get_nested_resources(resource) + + @classmethod + def is_attribute_locked(cls, resource, attribute): + return ifcopenshell.util.constraint.is_attribute_locked(resource, attribute) \ No newline at end of file diff --git a/src/blenderbim/blenderbim/tool/sequence.py b/src/blenderbim/blenderbim/tool/sequence.py index 97340431a0..6fea1e3639 100644 --- a/src/blenderbim/blenderbim/tool/sequence.py +++ b/src/blenderbim/blenderbim/tool/sequence.py @@ -421,6 +421,7 @@ class Sequence(blenderbim.core.tool.Sequence): @classmethod def load_resources(cls): blenderbim.core.resource.load_resources(tool.Resource) + cls.refresh_task_resources @classmethod def get_task_inputs(cls, task): diff --git a/src/ifcopenshell-python/ifcopenshell/api/resource/calculate_resource_usage.py b/src/ifcopenshell-python/ifcopenshell/api/resource/calculate_resource_usage.py index 1137b23cf3..08602c9f0f 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/resource/calculate_resource_usage.py +++ b/src/ifcopenshell-python/ifcopenshell/api/resource/calculate_resource_usage.py @@ -31,14 +31,7 @@ class Usecase: self.settings = {"resource": resource} def execute(self): - metrics = ifcopenshell.util.constraint.has_metric_constraints( - self.settings["resource"], "Usage.ScheduleUsage" - ) - if ( - metrics - and metrics[0].ConstraintGrade == "HARD" - and metrics[0].Benchmark == "EQUALTO" - ): + if ifcopenshell.util.constraint.is_attribute_locked(self.settings["resource"], "Usage.ScheduleUsage"): return if ( not self.settings["resource"].Usage 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 ac978573e0..a3df2a710b 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/resource/calculate_resource_work.py +++ b/src/ifcopenshell-python/ifcopenshell/api/resource/calculate_resource_work.py @@ -60,7 +60,7 @@ class Usecase: self.settings = {"resource": resource} def execute(self): - if self.has_hard_constraint(): + if ifcopenshell.util.constraint.is_attribute_locked(self.settings["resource"], "Usage.ScheduleWork"): return amount_worked = ifcopenshell.util.resource.get_resource_required_work( self.settings["resource"] @@ -74,15 +74,3 @@ class Usecase: resource=self.settings["resource"], ) self.settings["resource"].Usage.ScheduleWork = amount_worked - - def has_hard_constraint(self): - metrics = ifcopenshell.util.constraint.has_metric_constraints( - self.settings["resource"], "Usage.ScheduleWork" - ) - if ( - metrics - and metrics[0].ConstraintGrade == "HARD" - and metrics[0].Benchmark == "EQUALTO" - ): - return True - return False diff --git a/src/ifcopenshell-python/ifcopenshell/api/resource/edit_resource_time.py b/src/ifcopenshell-python/ifcopenshell/api/resource/edit_resource_time.py index 22d800a5c6..6a4b2029d4 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/resource/edit_resource_time.py +++ b/src/ifcopenshell-python/ifcopenshell/api/resource/edit_resource_time.py @@ -77,10 +77,10 @@ class Usecase: del self.settings["attributes"]["ActualFinish"] for name, value in self.settings["attributes"].items(): - metrics = ifcopenshell.util.constraint.has_metric_constraints( + metrics = ifcopenshell.util.constraint.get_metric_constraints( self.resource, "Usage." + name ) - if metrics and self.is_hard_constraint(metrics[0]): + if metrics and ifcopenshell.util.constraint.is_hard_constraint(metrics[0]): continue if value: if "Start" in name or "Finish" in name or name == "StatusTime": @@ -94,7 +94,7 @@ class Usecase: setattr(self.settings["resource_time"], name, value) if ( name == "ScheduleUsage" - and ifcopenshell.util.constraint.has_metric_constraints( + and ifcopenshell.util.constraint.get_metric_constraints( self.resource, "Usage.ScheduleWork" ) ): @@ -104,9 +104,6 @@ class Usecase: "sequence.calculate_task_duration", self.file, task=task ) - def is_hard_constraint(self, metric): - return bool(metric.ConstraintGrade == "HARD" and metric.Benchmark == "EQUALTO") - def get_resource(self): return [ e diff --git a/src/ifcopenshell-python/ifcopenshell/api/sequence/edit_task_time.py b/src/ifcopenshell-python/ifcopenshell/api/sequence/edit_task_time.py index 7ccb1056ce..4aed2fd050 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/sequence/edit_task_time.py +++ b/src/ifcopenshell-python/ifcopenshell/api/sequence/edit_task_time.py @@ -125,6 +125,8 @@ class Usecase: or "ScheduleDuration" in self.settings["attributes"].keys() ): ifcopenshell.api.run("sequence.cascade_schedule", self.file, task=self.task) + if self.settings["task_time"].ScheduleDuration: + self.handle_resource_calculation() def calculate_finish(self): finish = ifcopenshell.util.sequence.get_start_or_finish_date( @@ -173,3 +175,12 @@ class Usecase: for e in self.file.get_inverse(self.settings["task_time"]) if e.is_a("IfcTask") ][0] + + def handle_resource_calculation(self): + resources = ifcopenshell.util.sequence.get_task_resources(self.task, is_deep=False) + for resource in resources: + if ifcopenshell.util.constraint.is_attribute_locked(resource, "Usage.ScheduleWork"): + ifcopenshell.api.run("resource.calculate_resource_usage", self.file, resource=resource) + #TODO: If the duration changes, this implies the productivity rate must change to accomModate the new Schedule Work to be calculated. + # elif ifcopenshell.util.constraint.is_attribute_locked(resource, "Usage.ScheduleUsage"): + # ifcopenshell.api.run("resource.calculate_resource_work", self.file, resource=resource) diff --git a/src/ifcopenshell-python/ifcopenshell/util/constraint.py b/src/ifcopenshell-python/ifcopenshell/util/constraint.py index 66397bb58c..b2a38d6060 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/constraint.py +++ b/src/ifcopenshell-python/ifcopenshell/util/constraint.py @@ -20,10 +20,9 @@ def get_constraints(product): constraints = [] - if product.HasAssociations: - for rel in product.HasAssociations: - if rel.is_a("IfcRelAssociatesConstraint"): - constraints.append(rel.RelatingConstraint) + for rel in product.HasAssociations or []: + if rel.is_a("IfcRelAssociatesConstraint"): + constraints.append(rel.RelatingConstraint) return constraints def get_metrics(constraint): @@ -48,7 +47,7 @@ def get_metric_reference(metric, is_deep=True): reference = metric.ReferencePath return get_reference_Attribute(reference, "") -def has_metric_constraints(resource, attribute): +def get_metric_constraints(resource, attribute): metrics = [] for constraint in get_constraints(resource) or []: for metric in get_metrics(constraint) or []: @@ -59,4 +58,18 @@ def has_metric_constraints(resource, attribute): metrics.append(metric) if metrics: return metrics - return None \ No newline at end of file + return None + +def is_hard_constraint(metric): + if metric.ConstraintGrade == "HARD" and metric.Benchmark == "EQUALTO": + return True + +def is_attribute_locked(product, attribute): + is_locked = False + metrics = get_metric_constraints( + product, attribute + ) + for metric in metrics or []: + if is_hard_constraint(metric): + is_locked = True + return is_locked diff --git a/src/ifcopenshell-python/ifcopenshell/util/resource.py b/src/ifcopenshell-python/ifcopenshell/util/resource.py index ad75887361..f1e17dcc42 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/resource.py +++ b/src/ifcopenshell-python/ifcopenshell/util/resource.py @@ -114,4 +114,4 @@ def get_resource_required_work(resource): return iso_string def get_nested_resources(resource): - return [object for rel in resource.IsNestedBy or [] for object in rel.RelatedObjects] \ No newline at end of file + return [object for rel in resource.IsNestedBy or [] for object in rel.RelatedObjects]