From 5c3f0f0df9c8192921c7146c15608bbc623cc936 Mon Sep 17 00:00:00 2001 From: Gorgious56 Date: Wed, 8 Jan 2025 10:28:27 +0100 Subject: [PATCH] Fix floating precision artifacts when displaying float values in the Attributes panel Minor fixes on previous commit --- src/bonsai/bonsai/bim/helper.py | 14 ++++++++++ src/bonsai/bonsai/bim/module/attribute/ui.py | 11 +++----- src/bonsai/bonsai/bim/module/model/door.py | 27 +++++++++++--------- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/bonsai/bonsai/bim/helper.py b/src/bonsai/bonsai/bim/helper.py index 643bac7e37..2eb7860671 100644 --- a/src/bonsai/bonsai/bim/helper.py +++ b/src/bonsai/bonsai/bim/helper.py @@ -254,6 +254,20 @@ def export_attributes( ENUM_ITEMS_DATA = Union[bpy.types.PropertyGroup, bpy.types.ID, bpy.types.Operator, bpy.types.OperatorProperties] +def get_display_value(value: str, float_decimal_precision: int = 6) -> str: + """ + This will get rid of the floating point precision artifacts in float values stored as a string + """ + try: + digits = len(value.split(".")[1]) + value = float(value) + if digits > 6: # Maximal decimal float precision + value = round(value, float_decimal_precision) + except (ValueError, IndexError): # Not castable to a float or no decimal places (eg integer) + pass + return str(value) + + def prop_with_search( layout: bpy.types.UILayout, data: ENUM_ITEMS_DATA, diff --git a/src/bonsai/bonsai/bim/module/attribute/ui.py b/src/bonsai/bonsai/bim/module/attribute/ui.py index 8565181af3..85062852dc 100644 --- a/src/bonsai/bonsai/bim/module/attribute/ui.py +++ b/src/bonsai/bonsai/bim/module/attribute/ui.py @@ -20,6 +20,7 @@ import bonsai.bim.helper from bpy.types import Panel from bonsai.bim.ifc import IfcStore from bonsai.bim.module.attribute.data import AttributesData +import bonsai.tool as tool def draw_ui(context, layout, attributes): @@ -43,8 +44,8 @@ def draw_ui(context, layout, attributes): for attribute in attributes: row = layout.row(align=True) row.label(text=attribute["name"]) - # row.label(text=attribute["value"]) - op = row.operator("bim.select_similar", text=attribute["value"], icon="NONE", emboss=False) + value = bonsai.bim.helper.get_display_value(attribute["value"]) + op = row.operator("bim.select_similar", text=value, icon="NONE", emboss=False) op.key = attribute["name"] # TODO: reimplement, see #1222 @@ -62,11 +63,7 @@ class BIM_PT_object_attributes(Panel): @classmethod def poll(cls, context): - if not context.active_object: - return False - if not IfcStore.get_element(context.active_object.BIMObjectProperties.ifc_definition_id): - return False - return bool(context.active_object.BIMObjectProperties.ifc_definition_id) + return tool.Ifc.get_entity(context.active_object) def draw(self, context): if not AttributesData.is_loaded: diff --git a/src/bonsai/bonsai/bim/module/model/door.py b/src/bonsai/bonsai/bim/module/model/door.py index d557279bad..a8aa7b66c2 100644 --- a/src/bonsai/bonsai/bim/module/model/door.py +++ b/src/bonsai/bonsai/bim/module/model/door.py @@ -584,7 +584,7 @@ class CancelEditingDoor(bpy.types.Operator, tool.Ifc.Operator): bl_label = "Cancel Editing Door on Selected Objects" bl_options = {"REGISTER", "UNDO"} - def cancel_editing_door_on_object(self, obj, element): + def cancel_editing_door_on_object(self, obj): element = tool.Ifc.get_entity(obj) if not tool.Blender.Modifier.is_door(element): return @@ -652,19 +652,22 @@ class EnableEditingDoor(bpy.types.Operator, tool.Ifc.Operator): bl_label = "Enable Editing Door on Selected Objects" bl_options = {"REGISTER", "UNDO"} + def edit_door_on_obj(self, obj): + element = tool.Ifc.get_entity(obj) + if not tool.Blender.Modifier.is_door(element): + return + props = obj.BIMDoorProperties + data = json.loads(ifcopenshell.util.element.get_pset(element, "BBIM_Door", "Data")) + data.update(data.pop("lining_properties")) + data.update(data.pop("panel_properties")) + + # required since we could load pset from .ifc and BIMDoorProperties won't be set + props.set_props_kwargs_from_ifc_data(data) + props.is_editing = True + def _execute(self, context): for obj in context.selected_objects: - props = obj.BIMDoorProperties - element = tool.Ifc.get_entity(obj) - if not tool.Blender.Modifier.is_door(element): - continue - data = json.loads(ifcopenshell.util.element.get_pset(element, "BBIM_Door", "Data")) - data.update(data.pop("lining_properties")) - data.update(data.pop("panel_properties")) - - # required since we could load pset from .ifc and BIMDoorProperties won't be set - props.set_props_kwargs_from_ifc_data(data) - props.is_editing = True + self.edit_door_on_obj(obj) return {"FINISHED"}