From 24df52e2f7f7af248300744de2e233372b8b3dd7 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Wed, 26 Feb 2025 16:36:46 +0500 Subject: [PATCH] Do not purge orphaned IfcDocumentReferences #6117 --- .../bonsai/bim/module/debug/operator.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/bonsai/bonsai/bim/module/debug/operator.py b/src/bonsai/bonsai/bim/module/debug/operator.py index 3561a005a1..8fa39b1470 100644 --- a/src/bonsai/bonsai/bim/module/debug/operator.py +++ b/src/bonsai/bonsai/bim/module/debug/operator.py @@ -513,6 +513,7 @@ class PrintUnusedElementStats(bpy.types.Operator): if self.ignore_styled_items: ignore_classes += ["IfcStyledItem"] ignore_classes += [ + "IfcDocumentReference", # Document references for sheet elements (drawings, schedules, etc). "IfcIndexedColourMap", # Only referenced by inverse attributes. "IfcIndexedTextureMap", # Only referenced by inverse attributes. ] @@ -613,18 +614,30 @@ class PurgeUnusedElementsByClass(bpy.types.Operator, tool.Ifc.Operator): "IfcUnitAssignment", "IfcVirtualGridIntersection", ] + whitelist_exceptions = { + # Document references for sheet elements (drawings, schedules, etc). + "IfcExternalReference": ("IfcDocumentReference",), + } total_purged = 0 + schema = tool.Ifc.schema() while True: total_batches = 0 print("*" * 100) total_batch_purged = 0 for ifc_class in whitelisted_classes: total_class_purged = 0 + + # Ensure class is present in the schema. try: - elements = tool.Ifc.get().by_type(ifc_class) - except: - continue # Probably not in this schema? + schema.declaration_by_name(ifc_class) + except RuntimeError: + continue + + elements = tool.Ifc.get().by_type(ifc_class) + if ifc_class in whitelist_exceptions: + elements = [e for e in elements if not any(e.is_a(c) for c in whitelist_exceptions[ifc_class])] + to_purge = set() for element in elements: try: