diff --git a/src/bonsai/bonsai/bim/module/project/operator.py b/src/bonsai/bonsai/bim/module/project/operator.py index c399042a92..24deefbe2a 100644 --- a/src/bonsai/bonsai/bim/module/project/operator.py +++ b/src/bonsai/bonsai/bim/module/project/operator.py @@ -50,6 +50,7 @@ from bonsai.bim import export_ifc from collections import defaultdict from math import radians from pathlib import Path +from collections import defaultdict from mathutils import Vector, Matrix from bpy.app.handlers import persistent from ifcopenshell.geom import ShapeElementType @@ -219,9 +220,8 @@ class RefreshLibrary(bpy.types.Operator): condition = tool.Project.get_filter_for_active_library() for importable_type in sorted(tool.Project.get_appendable_asset_types()): - if (elements := library_file.by_type(importable_type)) and next(condition(elements), None): - new = self.props.library_elements.add() - new.name = importable_type + if (elements := library_file.by_type(importable_type)) and (elements := list(condition(elements))): + elements = self.props.add_library_asset_group(importable_type, len(elements)) return {"FINISHED"} @@ -241,7 +241,6 @@ class ChangeLibraryElement(bpy.types.Operator): library_file = IfcStore.library_file assert library_file self.library_file = library_file - ifc_classes: set[str] = set() self.props.active_library_element = self.element_name crumb = self.props.library_breadcrumb.add() @@ -250,22 +249,25 @@ class ChangeLibraryElement(bpy.types.Operator): filter_elements = tool.Project.get_filter_for_active_library() elements = self.library_file.by_type(self.element_name) elements = list(filter_elements(elements)) - [ifc_classes.add(e.is_a()) for e in elements] + ifc_classes_elements: dict[str, list[ifcopenshell.entity_instance]] = defaultdict(list) + for element in elements: + ifc_classes_elements[element.is_a()].append(element) self.props.library_elements.clear() - if len(ifc_classes) == 1 and list(ifc_classes)[0] == self.element_name: - for name, ifc_definition_id in sorted([(self.get_name(e), e.id()) for e in elements]): + if len(ifc_classes_elements) == 1 and list(ifc_classes_elements)[0] == self.element_name: + for name, ifc_definition_id in sorted( + [(self.get_name(e), e.id()) for e in ifc_classes_elements[self.element_name]] + ): self.add_library_asset(name, ifc_definition_id) else: - for ifc_class in sorted(ifc_classes): + for ifc_class in sorted(ifc_classes_elements): if ifc_class == self.element_name: continue - new = self.props.library_elements.add() - new.name = ifc_class - for name, ifc_definition_id, ifc_class in sorted([(self.get_name(e), e.id(), e.is_a()) for e in elements]): - if ifc_class == self.element_name: - self.add_library_asset(name, ifc_definition_id) + self.props.add_library_asset_group(ifc_class, len(ifc_classes_elements[ifc_class])) + elements_ = ifc_classes_elements[self.element_name] + for name, ifc_definition_id, ifc_class in sorted([(self.get_name(e), e.id(), e.is_a()) for e in elements_]): + self.add_library_asset(name, ifc_definition_id) return {"FINISHED"} def get_name(self, element: ifcopenshell.entity_instance) -> str: diff --git a/src/bonsai/bonsai/bim/module/project/prop.py b/src/bonsai/bonsai/bim/module/project/prop.py index bc65a42f5d..c5a97078d0 100644 --- a/src/bonsai/bonsai/bim/module/project/prop.py +++ b/src/bonsai/bonsai/bim/module/project/prop.py @@ -108,8 +108,10 @@ def update_filter_mode(self: "BIMProjectProperties", context: bpy.types.Context) class LibraryElement(PropertyGroup): - # For IFC class groups only name is filled, for assets - all fields. name: StringProperty(name="Name") + # Asset group. + asset_count: IntProperty(name="Asset Count") + # Asset. ifc_definition_id: IntProperty(name="IFC Definition ID") is_declared: BoolProperty(name="Is Declared", default=False) is_appended: BoolProperty(name="Is Appended", default=False) @@ -121,6 +123,7 @@ class LibraryElement(PropertyGroup): if TYPE_CHECKING: name: str + asset_count: int ifc_definition_id: int is_declared: bool is_appended: bool @@ -291,6 +294,12 @@ class BIMProjectProperties(PropertyGroup): def clipping_planes_objs(self) -> list[bpy.types.Object]: return list({cp.obj for cp in self.clipping_planes if cp.obj}) + def add_library_asset_group(self, name: str, asset_count: int) -> LibraryElement: + new = self.library_elements.add() + new.name = name + new.asset_count = asset_count + return new + def get_library_element_index(self, lib_element: LibraryElement) -> int: return next((i for i in range(len(self.library_elements)) if self.library_elements[i] == lib_element)) diff --git a/src/bonsai/bonsai/bim/module/project/ui.py b/src/bonsai/bonsai/bim/module/project/ui.py index ba9e1b057c..175c23c391 100644 --- a/src/bonsai/bonsai/bim/module/project/ui.py +++ b/src/bonsai/bonsai/bim/module/project/ui.py @@ -478,6 +478,10 @@ class BIM_UL_library(UIList): op = row.operator("bim.append_library_element", text="", icon="APPEND_BLEND") op.definition = item.ifc_definition_id op.prop_index = data.get_library_element_index(item) + else: + row_ = row.row() + row_.alignment = "RIGHT" + row_.label(text=str(item.asset_count)) class BIM_UL_filter_categories(UIList):