diff --git a/src/blenderbim/blenderbim/bim/module/style/__init__.py b/src/blenderbim/blenderbim/bim/module/style/__init__.py index fb1ece3cb0..e2ec666495 100644 --- a/src/blenderbim/blenderbim/bim/module/style/__init__.py +++ b/src/blenderbim/blenderbim/bim/module/style/__init__.py @@ -24,6 +24,7 @@ classes = ( operator.AddPresentationStyle, operator.AddStyle, operator.AddSurfaceTexture, + operator.AssignStyleToSelected, operator.BrowseExternalStyle, operator.RemoveTextureMap, operator.ChooseTextureMapPath, diff --git a/src/blenderbim/blenderbim/bim/module/style/operator.py b/src/blenderbim/blenderbim/bim/module/style/operator.py index 10af6dfd95..02ad4b2af0 100644 --- a/src/blenderbim/blenderbim/bim/module/style/operator.py +++ b/src/blenderbim/blenderbim/bim/module/style/operator.py @@ -23,6 +23,7 @@ import blenderbim.bim.handler import blenderbim.tool as tool import blenderbim.core.style as core import ifcopenshell.api +import ifcopenshell.api.style import ifcopenshell.util.representation from blenderbim.bim.module.style.prop import switch_shading from pathlib import Path @@ -951,3 +952,49 @@ class SaveUVToStyle(bpy.types.Operator, tool.Ifc.Operator): self.report({"INFO"}, f"UV saved to the style {style.Name}") return {"FINISHED"} + + +class AssignStyleToSelected(bpy.types.Operator, tool.Ifc.Operator): + bl_idname = "bim.assign_style_to_selected" + bl_label = "Assign Style To Selected" + bl_description = "Assign style to the selected objects' active representations" + bl_options = {"REGISTER", "UNDO"} + + style_id: bpy.props.IntProperty(name="Style ID") + + @classmethod + def poll(cls, context): + if not context.selected_objects: + cls.poll_message_set("No objects selected") + return False + return True + + def _execute(self, context): + if self.style_id == 0: + self.report({"ERROR"}, "No style provided") + return {"CANCELLED"} + ifc_file = tool.Ifc.get() + style = ifc_file.by_id(self.style_id) + + representations: dict[ifcopenshell.entity_instance, bpy.types.Object] = {} + for obj in context.selected_objects: + representation = tool.Geometry.get_active_representation(obj) + if not representation: + continue + representation = tool.Geometry.resolve_mapped_representation(representation) + representations.setdefault(representation, obj) + + if not representations: + self.report({"INFO"}, "No IFC objects with representations selected.") + return {"FINISHED"} + + for representation in representations: + ifcopenshell.api.style.assign_representation_styles( + ifc_file, + shape_representation=representation, + styles=[style], + should_use_presentation_style_assignment=tool.Geometry.should_use_presentation_style_assignment(), + ) + + tool.Geometry.reload_representation(representations.values()) + return {"FINISHED"} diff --git a/src/blenderbim/blenderbim/bim/module/style/ui.py b/src/blenderbim/blenderbim/bim/module/style/ui.py index 571696b187..4fbfee85fb 100644 --- a/src/blenderbim/blenderbim/bim/module/style/ui.py +++ b/src/blenderbim/blenderbim/bim/module/style/ui.py @@ -65,6 +65,8 @@ class BIM_PT_styles(Panel): row.operator("bim.duplicate_style", text="", icon="DUPLICATE").style = style.ifc_definition_id row.operator("bim.select_by_style", text="", icon="RESTRICT_SELECT_OFF").style = style.ifc_definition_id + op = row.operator("bim.assign_style_to_selected", text="", icon="BRUSH_DATA") + op.style_id = style.ifc_definition_id op = row.operator("bim.unlink_style", text="", icon="UNLINKED") op.style = style.ifc_definition_id op = row.operator("bim.enable_editing_style", text="", icon="GREASEPENCIL")