From 17eb45fb4eb2417eabb2ea5b4f1aa7dcc147aa1a Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Tue, 25 Jun 2024 13:45:25 +0500 Subject: [PATCH] Object metadata to double check element belongs to the current project Just to be safe if it got from other Blender session and there is an IFC element with the same id in the current project. I guess we can't check it anywhere but information in "Object metadata" should very accurate as here user might decide to unlink it. --- .../blenderbim/bim/module/root/data.py | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/root/data.py b/src/blenderbim/blenderbim/bim/module/root/data.py index bab7589932..2f8c503d68 100644 --- a/src/blenderbim/blenderbim/bim/module/root/data.py +++ b/src/blenderbim/blenderbim/bim/module/root/data.py @@ -23,6 +23,7 @@ import ifcopenshell.util.schema from ifcopenshell.util.doc import get_entity_doc, get_predefined_type_doc, get_class_suggestions import blenderbim.tool as tool from blenderbim.bim.ifc import IfcStore +from typing import Union def refresh(): @@ -30,6 +31,7 @@ def refresh(): class IfcClassData: + element: Union[ifcopenshell.entity_instance, None] = None data = {} is_loaded = False @@ -41,6 +43,7 @@ class IfcClassData: cls.data["ifc_classes"] = cls.ifc_classes() cls.data["ifc_classes_suggestions"] = cls.ifc_classes_suggestions() # Call AFTER cls.ifc_classes() cls.data["contexts"] = cls.contexts() + cls.data["has_entity"] = cls.has_entity() cls.data["name"] = cls.name() cls.data["has_inherited_predefined_type"] = cls.has_inherited_predefined_type() @@ -147,15 +150,27 @@ class IfcClassData: return results @classmethod - def has_entity(cls): - try: - return tool.Ifc.get_entity(bpy.context.active_object) - except: - pass + def has_entity(cls) -> Union[ifcopenshell.entity_instance, None]: + obj = bpy.context.active_object + cls.element = None + if not obj: + return + + element = tool.Ifc.get_entity(obj) + if not element: + return + + # Double check + linked_obj = tool.Ifc.get_object(element) + if linked_obj != obj: + return + + cls.element = element + return cls.element @classmethod - def name(cls): - element = tool.Ifc.get_entity(bpy.context.view_layer.objects.active) + def name(cls) -> Union[str, None]: + element = cls.element if not element: return name = element.is_a() @@ -165,24 +180,24 @@ class IfcClassData: return name @classmethod - def has_inherited_predefined_type(cls): - element = tool.Ifc.get_entity(bpy.context.view_layer.objects.active) + def has_inherited_predefined_type(cls) -> bool: + element = cls.element if not element: - return + return False if element_type := ifcopenshell.util.element.get_type(element): # Allow for None due to https://github.com/buildingSMART/IFC4.3.x-development/issues/818 return ifcopenshell.util.element.get_predefined_type(element_type) not in ("NOTDEFINED", None) return False @classmethod - def ifc_class(cls): - element = tool.Ifc.get_entity(bpy.context.view_layer.objects.active) + def ifc_class(cls) -> Union[str, None]: + element = cls.element if element: return element.is_a() @classmethod - def can_reassign_class(cls): - element = tool.Ifc.get_entity(bpy.context.view_layer.objects.active) + def can_reassign_class(cls) -> bool: + element = cls.element if element: if element.is_a("IfcOpeningElement") or element.is_a("IfcOpeningStandardCase"): return False @@ -191,3 +206,4 @@ class IfcClassData: for product in cls.ifc_products(): if element.is_a(product[0]): return True + return False