From 1facba797d7d8ada24ac86fe3433e3e8bbb6e31d Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Tue, 15 Oct 2024 14:30:36 +0500 Subject: [PATCH] aggregate data - small optimization check for first BBIM_Linked_Aggregate instead of iterating over all rels --- .../bonsai/bim/module/aggregate/data.py | 28 +++++++++++-------- src/bonsai/bonsai/bim/module/aggregate/ui.py | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/bonsai/bonsai/bim/module/aggregate/data.py b/src/bonsai/bonsai/bim/module/aggregate/data.py index cdef903ce2..5b609654ea 100644 --- a/src/bonsai/bonsai/bim/module/aggregate/data.py +++ b/src/bonsai/bonsai/bim/module/aggregate/data.py @@ -19,6 +19,7 @@ import bpy import bonsai.tool as tool import ifcopenshell.util.element +from typing import Union def refresh(): @@ -68,23 +69,26 @@ class AggregateData: return bool(cls.get_related_objects()) @classmethod - def total_linked_aggregate(cls): + def total_linked_aggregate(cls) -> Union[int, None]: element = tool.Ifc.get_entity(bpy.context.active_object) aggregate = ifcopenshell.util.element.get_aggregate(element) if not aggregate: - return [] + return if not element: - return [] + return - product_linked_agg_group = [ - r - for r in getattr(aggregate, "HasAssignments", []) or [] - if r.is_a("IfcRelAssignsToGroup") - if "BBIM_Linked_Aggregate" in r.RelatingGroup.Name - ] + product_linked_agg_group = next( + ( + r + for r in getattr(aggregate, "HasAssignments", []) or [] + if r.is_a("IfcRelAssignsToGroup") + if "BBIM_Linked_Aggregate" in r.RelatingGroup.Name + ), + None, + ) - if not product_linked_agg_group: - return [] + if product_linked_agg_group is None: + return - total = len(product_linked_agg_group[0].RelatedObjects) + total = len(product_linked_agg_group.RelatedObjects) return total diff --git a/src/bonsai/bonsai/bim/module/aggregate/ui.py b/src/bonsai/bonsai/bim/module/aggregate/ui.py index 183ec0064c..f30211bce2 100644 --- a/src/bonsai/bonsai/bim/module/aggregate/ui.py +++ b/src/bonsai/bonsai/bim/module/aggregate/ui.py @@ -140,7 +140,7 @@ class BIM_PT_linked_aggregate(Panel): else: row.label(text=f"{Number_Linked_Aggregates} Linked Aggregates") op = row.operator("bim.duplicate_linked_aggregate_to_3d_cursor", text="", icon="DUPLICATE") - if type(Number_Linked_Aggregates) is int: + if Number_Linked_Aggregates is not None: if Number_Linked_Aggregates > 0: op = row.operator("bim.select_linked_aggregates", text="", icon="OUTLINER_DATA_POINTCLOUD") op.select_parts = False