From 625175013300dff34d4ee4dad27c7056867dbbc2 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Wed, 14 May 2025 12:00:38 +0500 Subject: [PATCH] common method for getting active drawing style --- .../bonsai/bim/module/drawing/operator.py | 21 +++++++++---------- src/bonsai/bonsai/bim/module/drawing/prop.py | 4 ++++ src/bonsai/bonsai/bim/module/drawing/ui.py | 8 +++---- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/bonsai/bonsai/bim/module/drawing/operator.py b/src/bonsai/bonsai/bim/module/drawing/operator.py index 0e2b114dc3..2c016c52d0 100644 --- a/src/bonsai/bonsai/bim/module/drawing/operator.py +++ b/src/bonsai/bonsai/bim/module/drawing/operator.py @@ -406,7 +406,7 @@ class CreateDrawing(bpy.types.Operator): assert context.scene and context.view_layer and context.screen context.scene.render.filepath = str(Path(svg_path).with_suffix(".png")) - drawing_style = self.props.drawing_styles[self.cprops.active_drawing_style_index] + assert (drawing_style := self.cprops.get_active_drawing_style()) if drawing_style.render_type == "DEFAULT": bpy.ops.render.render(write_still=True) @@ -2398,7 +2398,7 @@ class SaveDrawingStyle(bpy.types.Operator, tool.Ifc.Operator): bl_idname = "bim.save_drawing_style" bl_label = "Save Drawing Style" bl_options = {"REGISTER", "UNDO"} - bl_description = "Save current render setting to currently selected drawing style. Also resaves styles data to IFC" + bl_description = "Save current render settings to currently selected drawing style. Also resaves styles data to IFC" index: bpy.props.StringProperty() # TODO: check undo redo @@ -2519,18 +2519,17 @@ class ActivateDrawingStyle(bpy.types.Operator, tool.Ifc.Operator): assert scene and (camera := scene.camera) camera_props = tool.Drawing.get_camera_props(camera) ifc_file = tool.Ifc.get() - active_drawing_style_index = camera_props.active_drawing_style_index - props = tool.Drawing.get_document_props() - if active_drawing_style_index >= len(props.drawing_styles): + drawing_style = camera_props.get_active_drawing_style() + if drawing_style is None: self.report({"ERROR"}, "Could not find active drawing style") return {"CANCELLED"} - - self.drawing_style = props.drawing_styles[active_drawing_style_index] + self.drawing_style = drawing_style self.set_raster_style(context) self.set_query(context) + props = tool.Drawing.get_document_props() assert (drawing := props.get_active_drawing()) pset = tool.Pset.get_element_pset(drawing, "EPset_Drawing") assert pset @@ -2903,8 +2902,8 @@ class AddDrawingStyleAttribute(bpy.types.Operator): def execute(self, context): assert context.scene and (camera := context.scene.camera) props = tool.Drawing.get_camera_props(camera) - dprops = tool.Drawing.get_document_props() - dprops.drawing_styles[props.active_drawing_style_index].attributes.add() + assert (drawing_style := props.get_active_drawing_style()) + drawing_style.attributes.add() return {"FINISHED"} @@ -2918,8 +2917,8 @@ class RemoveDrawingStyleAttribute(bpy.types.Operator): def execute(self, context): assert context.scene and (camera := context.scene.camera) props = tool.Drawing.get_camera_props(camera) - dprops = tool.Drawing.get_document_props() - dprops.drawing_styles[props.active_drawing_style_index].attributes.remove(self.index) + assert (drawing_style := props.get_active_drawing_style()) + drawing_style.attributes.remove(self.index) return {"FINISHED"} diff --git a/src/bonsai/bonsai/bim/module/drawing/prop.py b/src/bonsai/bonsai/bim/module/drawing/prop.py index 38f3624bb5..5818adc59c 100644 --- a/src/bonsai/bonsai/bim/module/drawing/prop.py +++ b/src/bonsai/bonsai/bim/module/drawing/prop.py @@ -625,6 +625,10 @@ class BIMCameraProperties(PropertyGroup): exclude_filter_groups: bpy.types.bpy_prop_collection_idprop[BIMFilterGroup] update_props: bool + def get_active_drawing_style(self) -> Union[DrawingStyle, None]: + dprops = tool.Drawing.get_document_props() + return tool.Blender.get_active_uilist_element(dprops.drawing_styles, self.active_drawing_style_index) + # For now, this JSON dump are all the parameters that determine a camera's "Block representation" # By checking this, you will know whether or not the camera IFC representation needs to be refreshed def update_representation(self, matrix_world: Matrix) -> bool: diff --git a/src/bonsai/bonsai/bim/module/drawing/ui.py b/src/bonsai/bonsai/bim/module/drawing/ui.py index 15531e7121..b82f44fdf8 100644 --- a/src/bonsai/bonsai/bim/module/drawing/ui.py +++ b/src/bonsai/bonsai/bim/module/drawing/ui.py @@ -173,12 +173,13 @@ class BIM_PT_drawing_underlay(Panel): return bool((camera := context.scene.camera) and tool.Ifc.get_entity(camera)) def draw(self, context): + assert self.layout layout = self.layout layout.use_property_split = True assert context.scene and (camera := context.scene.camera) dprops = tool.Drawing.get_document_props() props = tool.Drawing.get_camera_props(camera) - drawing_index_is_valid = props.active_drawing_style_index < len(dprops.drawing_styles) + drawing_style = props.get_active_drawing_style() if not DrawingsData.is_loaded: DrawingsData.load() @@ -192,7 +193,7 @@ class BIM_PT_drawing_underlay(Panel): row.label(text="Current Shading Style:") row.label(text=current_shading_style) row.operator("bim.add_drawing_style", icon="ADD", text="") - if drawing_index_is_valid: + if drawing_style: row.operator("bim.remove_drawing_style", icon="X", text="").index = props.active_drawing_style_index row.operator("bim.reload_drawing_styles", icon="FILE_REFRESH", text="") @@ -207,9 +208,8 @@ class BIM_PT_drawing_underlay(Panel): "active_drawing_style_index", ) - if not drawing_index_is_valid: + if drawing_style is None: return - drawing_style = dprops.drawing_styles[props.active_drawing_style_index] row = layout.row(align=True) row.prop(drawing_style, "name")