From db075c7160fe18c53cdc658a1f8dec5cbe64be65 Mon Sep 17 00:00:00 2001 From: Ryan Schultz Date: Sat, 26 Oct 2024 13:26:11 -0500 Subject: [PATCH] display the sum of all selected objects --- src/bonsai/bonsai/bim/module/pset/ui.py | 10 ++++- .../bonsai/bim/module/search/operator.py | 40 ++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/bonsai/bonsai/bim/module/pset/ui.py b/src/bonsai/bonsai/bim/module/pset/ui.py index baf694255e..80f105dbd7 100644 --- a/src/bonsai/bonsai/bim/module/pset/ui.py +++ b/src/bonsai/bonsai/bim/module/pset/ui.py @@ -102,6 +102,11 @@ def draw_psetqto_ui( allow_removing: bool = True, filter_keyword: str = "", ) -> None: + + + select_similar_op = context.active_operator + calculated_sum = getattr(select_similar_op, "calculated_sum", 0.0) + filter_keyword = filter_keyword.lower() box = layout.box() row = box.row(align=True) @@ -195,7 +200,10 @@ def draw_psetqto_ui( row.label(text=prop["Name"]) op = row.operator("bim.select_similar", text=nominal_value, icon="NONE", emboss=False) op.key = '"' + pset["Name"].replace('"', '\\"') + '"."' + prop["Name"].replace('"', '\\"') + '"' - + # calculate sum of all selected objects + if select_similar_op: + if op.key == select_similar_op.key and calculated_sum != 0 and isinstance(float(nominal_value), (int, float)): + row.label(text=f"(Sum: {calculated_sum})") if not has_props_displayed: row = box.row() row.scale_y = 0.8 diff --git a/src/bonsai/bonsai/bim/module/search/operator.py b/src/bonsai/bonsai/bim/module/search/operator.py index 7b7b90db38..ae73aa961a 100644 --- a/src/bonsai/bonsai/bim/module/search/operator.py +++ b/src/bonsai/bonsai/bim/module/search/operator.py @@ -696,8 +696,19 @@ class SelectSimilar(Operator, tool.Ifc.Operator): bl_idname = "bim.select_similar" bl_label = "Select Similar" bl_options = {"REGISTER", "UNDO"} + bl_description = ( + "Select objects with a similar value\n\n" + + "SHIFT+CLICK display the sum of all selected objects" + ) key: bpy.props.StringProperty() + calculate_sum: bpy.props.BoolProperty(name="Calculate Sum of Selected Objects", default=False, options={"SKIP_SAVE"}) + calculated_sum: bpy.props.FloatProperty(name="Calculated Sum", default=0.0) + + def invoke(self, context, event): + if event.type == "LEFTMOUSE" and event.shift: + self.calculate_sum = True + return self.execute(context) def _execute(self, context): props = context.scene.BIMSearchProperties @@ -707,9 +718,26 @@ class SelectSimilar(Operator, tool.Ifc.Operator): if key == "PredefinedType": key = "predefined_type" value = ifcopenshell.util.selector.get_element_value(element, key) - for obj in context.visible_objects: - element = tool.Ifc.get_entity(obj) - if not element: - continue - if ifcopenshell.util.selector.get_element_value(element, key) == value: - obj.select_set(True) + + + if self.calculate_sum and isinstance(value, (int, float)): + total = 0 + for obj in context.selected_objects: + element = tool.Ifc.get_entity(obj) + if not element: + continue + value = ifcopenshell.util.selector.get_element_value(element, key) + if value: + total += value + self.calculated_sum = total + bpy.context.window_manager.clipboard = str(self.calculated_sum) + self.report({"INFO"}, f"({self.calculated_sum}) was copied to the clipboard.") + else: + self.calculated_sum = 0 + for obj in context.visible_objects: + element = tool.Ifc.get_entity(obj) + if not element: + continue + if ifcopenshell.util.selector.get_element_value(element, key) == value: + obj.select_set(True) +