diff --git a/src/bonsai/bonsai/bim/module/debug/operator.py b/src/bonsai/bonsai/bim/module/debug/operator.py index 3b801935ed..24a2cf5968 100644 --- a/src/bonsai/bonsai/bim/module/debug/operator.py +++ b/src/bonsai/bonsai/bim/module/debug/operator.py @@ -42,7 +42,7 @@ from bpy_extras.io_utils import ImportHelper, ExportHelper from pathlib import Path from bonsai import get_debug_info, format_debug_info from bonsai.bim.ifc import IfcStore -from typing import get_args, Union, Any, TYPE_CHECKING, Literal, get_args +from typing import get_args, Union, Any, TYPE_CHECKING, Literal, get_args, assert_never if TYPE_CHECKING: from bonsai.bim.prop import Attribute @@ -747,16 +747,14 @@ class PurgeUnusedObjects(bpy.types.Operator, tool.Ifc.Operator): bl_label = "Purge Unused Objects" bl_options = {"REGISTER", "UNDO"} - object_type: bpy.props.EnumProperty( + object_type: bpy.props.EnumProperty( # pyright: ignore[reportRedeclaration] name="Object Type", - items=( - ("TYPE", "Type", ""), - ("PROFILE", "Profile", ""), - ("STYLE", "Style", ""), - ("MATERIAL", "Material", ""), - ), + items=((s, s.capitalize(), "") for s in get_args(PurgeObjectType)), ) + if TYPE_CHECKING: + object_type: PurgeObjectType + def _execute(self, context): object_type = self.object_type if object_type == "TYPE": @@ -767,9 +765,10 @@ class PurgeUnusedObjects(bpy.types.Operator, tool.Ifc.Operator): purged = tool.Style.purge_unused_styles() elif object_type == "MATERIAL": purged = tool.Material.purge_unused_materials() + elif object_type in ("APPLICATION", "ORGANIZATION"): + purged = core.purge_unused_elements(tool.Ifc, tool.Debug, "IfcApplication") else: - self.report({"ERROR"}, f"Invalid object type {object_type}.") - return {"CANCELLED"} + assert_never(object_type) self.report({"INFO"}, f"{purged} unused {object_type.lower()}s were purged.") @@ -788,6 +787,10 @@ class PurgeUnusedObjects(bpy.types.Operator, tool.Ifc.Operator): props = tool.Material.get_material_props() if props.is_editing: bpy.ops.bim.load_materials() + elif object_type == "ORGANIZATION": + props = tool.Owner.get_owner_props() + if tool.Ifc.get_entity_by_id(props.active_organisation_id) is None: + props.active_organisation_id = 0 class MergeIdenticalObjects(bpy.types.Operator, tool.Ifc.Operator): diff --git a/src/bonsai/bonsai/bim/module/project/ui.py b/src/bonsai/bonsai/bim/module/project/ui.py index 836b9cc978..df40dde4af 100644 --- a/src/bonsai/bonsai/bim/module/project/ui.py +++ b/src/bonsai/bonsai/bim/module/project/ui.py @@ -648,8 +648,10 @@ class BIM_PT_purge(Panel): row = layout.row(align=True) row.label(text="Organizations: ") + row.operator("bim.purge_unused_objects", text="Purge Unused").object_type = "ORGANIZATION" row.operator("bim.merge_identical_objects", text="Merge Identical").object_type = "ORGANIZATION" row = layout.row(align=True) row.label(text="Applications: ") row.operator("bim.merge_identical_objects", text="Merge Identical").object_type = "APPLICATION" + row.operator("bim.merge_identical_objects", text="Merge Identical").object_type = "APPLICATION" diff --git a/src/bonsai/bonsai/core/debug.py b/src/bonsai/bonsai/core/debug.py index 7975bdfae3..4a5abda91f 100644 --- a/src/bonsai/bonsai/core/debug.py +++ b/src/bonsai/bonsai/core/debug.py @@ -38,5 +38,9 @@ def purge_unused_elements(ifc: type[tool.Ifc], debug: type[tool.Debug], ifc_clas ifc_file = ifc.get() unused_elements = [i for i in ifc_file.by_type(ifc_class) if ifc_file.get_total_inverses(i) == 0] unused_elements_amount = len(unused_elements) - debug.remove_unused_elements(unused_elements) + if ifc_class == "IfcApplication": + for element in unused_elements: + ifc.run("owner.remove_application", application=element) + else: + debug.remove_unused_elements(unused_elements) return unused_elements_amount