diff --git a/src/bonsai/bonsai/tool/geometry.py b/src/bonsai/bonsai/tool/geometry.py index 7eb851a5c2..ea2c1fc44d 100644 --- a/src/bonsai/bonsai/tool/geometry.py +++ b/src/bonsai/bonsai/tool/geometry.py @@ -382,20 +382,13 @@ class Geometry(bonsai.core.tool.Geometry): @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 + return ifcopenshell.util.representation.get_representations_iter(element) @classmethod def get_representation_by_context( cls, element: ifcopenshell.entity_instance, context: ifcopenshell.entity_instance ) -> Union[ifcopenshell.entity_instance, None]: - for r in cls.get_representations_iter(element): - if r.ContextOfItems == context: - return r + return ifcopenshell.util.representation.get_representation(element, context) @classmethod def get_cartesian_point_coordinate_offset(cls, obj: bpy.types.Object) -> Union[Vector, None]: diff --git a/src/ifcopenshell-python/ifcopenshell/util/representation.py b/src/ifcopenshell-python/ifcopenshell/util/representation.py index 4809f89b95..573c3692e0 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/representation.py +++ b/src/ifcopenshell-python/ifcopenshell/util/representation.py @@ -20,7 +20,7 @@ import numpy as np import numpy.typing as npt import ifcopenshell import ifcopenshell.util.placement -from typing import Optional, Union, TypedDict, Literal +from typing import Optional, Union, TypedDict, Literal, Iterator CONTEXT_TYPE = Literal["Model", "Plan", "NotDefined"] @@ -112,6 +112,19 @@ def is_representation_of_context( return representation.ContextOfItems.ContextType == context +def get_representations_iter(element: ifcopenshell.entity_instance) -> Iterator[ifcopenshell.entity_instance]: + """Get an iterator with element's IfcShapeRepresentations. + + :param element: An IfcProduct or IfcTypeProduct + """ + 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 + + def get_representation( element: ifcopenshell.entity_instance, context: Union[ifcopenshell.entity_instance, CONTEXT_TYPE], @@ -126,14 +139,9 @@ def get_representation( :param target_view: A TargetView string, or any if left blank. :return: The first IfcShapeRepresentation matching the criteria. """ - if element.is_a("IfcProduct") and element.Representation: - for r in element.Representation.Representations: - if is_representation_of_context(r, context, subcontext, target_view): - return r - elif element.is_a("IfcTypeProduct") and element.RepresentationMaps: - for r in element.RepresentationMaps: - if is_representation_of_context(r.MappedRepresentation, context, subcontext, target_view): - return r.MappedRepresentation + for r in get_representations_iter(element): # type: ignore + if is_representation_of_context(r, context, subcontext, target_view): + return r def resolve_representation(representation: ifcopenshell.entity_instance) -> ifcopenshell.entity_instance: