From 1c7e7c5269866c7ce1514e058f30e52b6251e007 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Tue, 3 Sep 2024 12:22:11 +0500 Subject: [PATCH] prohibit user from removing a profile that still has inverses #5101 To avoid creating invalid material profile sets, representation items. Currently the only inverse that is allowed before removal is profile properties. Example - https://i.imgur.com/UobI4pL.png --- .../bonsai/bim/module/profile/operator.py | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/bonsai/bonsai/bim/module/profile/operator.py b/src/bonsai/bonsai/bim/module/profile/operator.py index 432f137b1c..2c0a7faee8 100644 --- a/src/bonsai/bonsai/bim/module/profile/operator.py +++ b/src/bonsai/bonsai/bim/module/profile/operator.py @@ -68,7 +68,28 @@ class RemoveProfileDef(bpy.types.Operator, tool.Ifc.Operator): def _execute(self, context): props = context.scene.BIMProfileProperties current_index = props.active_profile_index - ifcopenshell.api.run("profile.remove_profile", tool.Ifc.get(), profile=tool.Ifc.get().by_id(self.profile)) + + ifc_file = tool.Ifc.get() + profile = ifc_file.by_id(self.profile) + + # Save user from creating invalid IFC. + inverse_classes = {i.is_a() for i in ifc_file.get_inverse(profile)} + schema = ifcopenshell.schema_by_name(ifc_file.schema) + + # Allow removing profile with IfcProfileProperties. + for inverse_class in inverse_classes.copy(): + declaration = schema.declaration_by_name(inverse_class) + if declaration._is("IfcProfileProperties"): + inverse_classes.remove(inverse_class) + + if inverse_classes: + error_msg = "Cannot remove profile as it's still part of other IFC entities:" + for inverse_class in inverse_classes: + error_msg += "\n- " + inverse_class + self.report({"ERROR"}, error_msg) + return {"CANCELLED"} + + ifcopenshell.api.run("profile.remove_profile", ifc_file, profile=profile) bpy.ops.bim.load_profiles() # preserve selected index if possible