util.representation.get_representations_iter

This commit is contained in:
Andrej730
2024-09-03 16:24:14 +05:00
parent b957505bfc
commit bec1d913c5
2 changed files with 19 additions and 18 deletions
+2 -9
View File
@@ -382,20 +382,13 @@ class Geometry(bonsai.core.tool.Geometry):
@classmethod @classmethod
def get_representations_iter(cls, element: ifcopenshell.entity_instance) -> Iterator[ifcopenshell.entity_instance]: def get_representations_iter(cls, element: ifcopenshell.entity_instance) -> Iterator[ifcopenshell.entity_instance]:
if element.is_a("IfcProduct") and (rep := element.Representation): return ifcopenshell.util.representation.get_representations_iter(element)
for r in rep.Representations:
yield r
elif element.is_a("IfcTypeProduct") and (maps := element.RepresentationMaps):
for r in maps:
yield r.MappedRepresentation
@classmethod @classmethod
def get_representation_by_context( def get_representation_by_context(
cls, element: ifcopenshell.entity_instance, context: ifcopenshell.entity_instance cls, element: ifcopenshell.entity_instance, context: ifcopenshell.entity_instance
) -> Union[ifcopenshell.entity_instance, None]: ) -> Union[ifcopenshell.entity_instance, None]:
for r in cls.get_representations_iter(element): return ifcopenshell.util.representation.get_representation(element, context)
if r.ContextOfItems == context:
return r
@classmethod @classmethod
def get_cartesian_point_coordinate_offset(cls, obj: bpy.types.Object) -> Union[Vector, None]: def get_cartesian_point_coordinate_offset(cls, obj: bpy.types.Object) -> Union[Vector, None]:
@@ -20,7 +20,7 @@ import numpy as np
import numpy.typing as npt import numpy.typing as npt
import ifcopenshell import ifcopenshell
import ifcopenshell.util.placement 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"] CONTEXT_TYPE = Literal["Model", "Plan", "NotDefined"]
@@ -112,6 +112,19 @@ def is_representation_of_context(
return representation.ContextOfItems.ContextType == 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( def get_representation(
element: ifcopenshell.entity_instance, element: ifcopenshell.entity_instance,
context: Union[ifcopenshell.entity_instance, CONTEXT_TYPE], 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. :param target_view: A TargetView string, or any if left blank.
:return: The first IfcShapeRepresentation matching the criteria. :return: The first IfcShapeRepresentation matching the criteria.
""" """
if element.is_a("IfcProduct") and element.Representation: for r in get_representations_iter(element): # type: ignore
for r in element.Representation.Representations: if is_representation_of_context(r, context, subcontext, target_view):
if is_representation_of_context(r, context, subcontext, target_view): return r
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
def resolve_representation(representation: ifcopenshell.entity_instance) -> ifcopenshell.entity_instance: def resolve_representation(representation: ifcopenshell.entity_instance) -> ifcopenshell.entity_instance: