Fix issue reloading AuthoringData from workspace at every draw call

If data was already loaded for BIMTool ("all") this ` elif ifc_element_type == "all" and AuthoringData.data["ifc_element_type"] is not None:` would always fail and this `AuthoringData.data["ifc_element_type"] != ifc_element_type` would always result to True, constantly recalculating data on every draw call.

This may uncover some issues that were hidden by the constant update (e.g. the bug fixed in the next commit).

Simplified it so now ifc_element_type is almost always referring to either ifc class or None. It's still using "all" in the BIMTool itself but then it's converted to None when passed to draw methods.
This commit is contained in:
Andrej730
2025-02-24 18:22:23 +05:00
parent ce6339869d
commit 1d815cdb67
2 changed files with 9 additions and 17 deletions
+3 -6
View File
@@ -26,7 +26,7 @@ from ifcopenshell.util.doc import get_entity_doc, get_predefined_type_doc
import bonsai.tool as tool
from math import degrees
from natsort import natsorted
from typing import Union
from typing import Union, Optional
def refresh():
@@ -45,17 +45,14 @@ class AuthoringData:
data = {}
type_thumbnails = {}
types_per_page = 9
ifc_element_type = None
is_loaded = False
@classmethod
def load(cls, ifc_element_type=None):
def load(cls, ifc_element_type: Optional[str] = None):
cls.is_loaded = True
cls.props = tool.Model.get_model_props()
if ifc_element_type:
cls.ifc_element_type = None if ifc_element_type == "all" else ifc_element_type
cls.data["default_container"] = cls.default_container()
cls.data["ifc_element_type"] = cls.ifc_element_type
cls.data["ifc_element_type"] = ifc_element_type
cls.data["ifc_classes"] = cls.ifc_classes()
cls.data["ifc_class_current"] = cls.ifc_class_current()
# Make sure .ifc_classes() was run before next lines
@@ -97,14 +97,15 @@ class BimTool(WorkSpaceTool):
cls, context: bpy.types.Context, layout: bpy.types.UILayout, ws_tool: bpy.types.WorkSpaceTool
) -> None:
props = tool.Geometry.get_geometry_props()
ifc_element_type = None if cls.ifc_element_type == "all" else cls.ifc_element_type
if props.mode == "ITEM":
EditItemUI.draw(context, layout)
elif (
active_ifc_object := (context.active_object and tool.Ifc.get_entity(context.active_object))
) and context.selected_objects:
EditObjectUI.draw(context, layout, ifc_element_type=cls.ifc_element_type)
EditObjectUI.draw(context, layout, ifc_element_type=ifc_element_type)
else:
CreateObjectUI.draw(context, layout, ifc_element_type=cls.ifc_element_type)
CreateObjectUI.draw(context, layout, ifc_element_type=ifc_element_type)
class WallTool(BimTool):
@@ -500,9 +501,7 @@ class CreateObjectUI:
layout: bpy.types.UILayout
@classmethod
def draw(
cls, context: bpy.types.Context, layout: bpy.types.UILayout, ifc_element_type: Optional[str] = None
) -> None:
def draw(cls, context: bpy.types.Context, layout: bpy.types.UILayout, ifc_element_type: Union[str, None]) -> None:
cls.layout = layout
cls.props = tool.Model.get_model_props()
@@ -516,15 +515,13 @@ class CreateObjectUI:
if not AuthoringData.is_loaded:
AuthoringData.load(ifc_element_type)
elif ifc_element_type == "all" and AuthoringData.data["ifc_element_type"] is not None:
AuthoringData.load("all")
elif AuthoringData.data["ifc_element_type"] != ifc_element_type:
AuthoringData.load(ifc_element_type)
if ifc_element_type and context.region.type == "TOOL_HEADER":
if context.region.type == "TOOL_HEADER":
tool_name = (
"Multi Object Tool"
if ifc_element_type == "all"
if ifc_element_type is None
else format_ifc_camel_case(ifc_element_type.removesuffix("Type")) + " Tool"
)
cls.layout.label(text=tool_name, icon="TOOL_SETTINGS")
@@ -747,8 +744,6 @@ class EditObjectUI:
if not AuthoringData.is_loaded:
AuthoringData.load(ifc_element_type)
elif ifc_element_type == "all" and AuthoringData.data["ifc_element_type"] is not None:
AuthoringData.load("all")
elif AuthoringData.data["ifc_element_type"] != ifc_element_type:
AuthoringData.load(ifc_element_type)