From 6e9309aefc06d640de8d69d6906b84244b3805f9 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Tue, 2 Jul 2024 17:41:41 +0500 Subject: [PATCH] common method for iterating over element's representations --- .../blenderbim/bim/module/geometry/data.py | 7 +------ src/blenderbim/blenderbim/tool/geometry.py | 21 ++++++++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/geometry/data.py b/src/blenderbim/blenderbim/bim/module/geometry/data.py index b98188ab20..0a4bb17c40 100644 --- a/src/blenderbim/blenderbim/bim/module/geometry/data.py +++ b/src/blenderbim/blenderbim/bim/module/geometry/data.py @@ -77,12 +77,7 @@ class RepresentationsData: if bpy.context.active_object.data and hasattr(bpy.context.active_object.data, "BIMMeshProperties"): active_representation_id = bpy.context.active_object.data.BIMMeshProperties.ifc_definition_id - representations = [] - if element.is_a("IfcProduct") and element.Representation: - representations = element.Representation.Representations - elif element.is_a("IfcTypeProduct"): - representations = [rm.MappedRepresentation for rm in element.RepresentationMaps or []] - for representation in representations: + for representation in tool.Geometry.get_representations_iter(element): representation_type = representation.RepresentationType if representation_type == "MappedRepresentation": representation_type = representation.Items[0].MappingSource.MappedRepresentation.RepresentationType diff --git a/src/blenderbim/blenderbim/tool/geometry.py b/src/blenderbim/blenderbim/tool/geometry.py index 9e6e14a184..cc9e6cdc52 100644 --- a/src/blenderbim/blenderbim/tool/geometry.py +++ b/src/blenderbim/blenderbim/tool/geometry.py @@ -41,6 +41,7 @@ from math import radians, pi from mathutils import Vector, Matrix from blenderbim.bim.ifc import IfcStore from typing import Union, Iterable, Optional, Literal +from typing import Iterator class Geometry(blenderbim.core.tool.Geometry): @@ -345,18 +346,22 @@ class Geometry(blenderbim.core.tool.Geometry): getattr(subcontext, "TargetView", None), ) + @classmethod + def get_representations_iter(cls, element: ifcopenshell.entity_instance) -> Iterator[ifcopenshell.entity_instance]: + if element.is_a("IfcProduct") and (rep := element.Representation): + for r in rep.Representations: + yield r + elif element.is_a("IfcTypeProduct") and (maps := element.RepresentationMaps): + for r in maps: + yield r.MappedRepresentation + @classmethod def get_representation_by_context( cls, element: ifcopenshell.entity_instance, context: ifcopenshell.entity_instance ) -> Union[ifcopenshell.entity_instance, None]: - if element.is_a("IfcProduct") and element.Representation: - for r in element.Representation.Representations: - if r.ContextOfItems == context: - return r - elif element.is_a("IfcTypeProduct") and element.RepresentationMaps: - for r in element.RepresentationMaps: - if r.MappedRepresentation.ContextOfItems == context: - return r.MappedRepresentation + for r in cls.get_representations_iter(element): + if r.ContextOfItems == context: + return r @classmethod def get_cartesian_point_coordinate_offset(cls, obj: bpy.types.Object) -> Union[Vector, None]: