From c76a5a7ee704304c8c5e88c632dce9ad7abf9ec1 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Mon, 19 Feb 2024 17:33:49 +0500 Subject: [PATCH] promote get_shape_aspects to util.element method --- .../blenderbim/bim/module/geometry/data.py | 2 +- src/blenderbim/blenderbim/tool/geometry.py | 12 -------- .../ifcopenshell/util/element.py | 29 +++++++++++++++++++ .../test/util/test_element.py | 18 ++++++++++++ 4 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/geometry/data.py b/src/blenderbim/blenderbim/bim/module/geometry/data.py index 4bd32ab0fa..4165fd9f8e 100644 --- a/src/blenderbim/blenderbim/bim/module/geometry/data.py +++ b/src/blenderbim/blenderbim/bim/module/geometry/data.py @@ -102,7 +102,7 @@ class RepresentationsData: # shape aspects matching context of the active representation matching_shape_aspects = [] - for shape_aspect in tool.Geometry.get_shape_aspects(element): + for shape_aspect in ifcopenshell.util.element.get_shape_aspects(element): matching_representation = tool.Geometry.get_shape_aspect_representation(shape_aspect, base_representation) if matching_representation: matching_shape_aspects.append(shape_aspect) diff --git a/src/blenderbim/blenderbim/tool/geometry.py b/src/blenderbim/blenderbim/tool/geometry.py index 0f9a1a7118..193bd1c921 100644 --- a/src/blenderbim/blenderbim/tool/geometry.py +++ b/src/blenderbim/blenderbim/tool/geometry.py @@ -779,18 +779,6 @@ class Geometry(blenderbim.core.tool.Geometry): also_consider = list(consider_inverses) ifcopenshell.util.element.remove_deep2(ifc_file, representation_item, also_consider=also_consider) - @classmethod - def get_shape_aspects(cls, element): - # IfcProduct - if hasattr(element, "Representation"): - return element.Representation.HasShapeAspects - - # IfcTypeProduct - shape_aspects = [] - for repersentation_map in element.RepresentationMaps: - shape_aspects += repersentation_map.HasShapeAspects - return shape_aspects - @classmethod def create_shape_aspect(cls, product_shape, base_representation, items, previous_shape_aspect=None): """ diff --git a/src/ifcopenshell-python/ifcopenshell/util/element.py b/src/ifcopenshell-python/ifcopenshell/util/element.py index ae2be10471..6ad072d48d 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/element.py +++ b/src/ifcopenshell-python/ifcopenshell/util/element.py @@ -16,7 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with IfcOpenShell. If not, see . +from __future__ import annotations import ifcopenshell +from typing import List def get_pset(element, name, prop=None, psets_only=False, qtos_only=False, should_inherit=True, verbose=False): @@ -379,6 +381,33 @@ def get_types(type): return [] +def get_shape_aspects(element: ifcopenshell.entity_instance) -> List[ifcopenshell.entity_instance]: + """Gets element shape aspects + + :param element: The element to get the shape aspects of. + :type element: ifcopenshell.entity_instance.entity_instance + :return: The associated shape aspects of the element. + :rtype: list[ifcopenshell.entity_instance.entity_instance] + + Example: + + .. code:: python + + element = ifcopenshell.by_type("IfcWall")[0] + shape_aspect = ifcopenshell.util.element.get_shape_aspects(element) + """ + + # IfcProduct + if hasattr(element, "Representation"): + return element.Representation.HasShapeAspects + + # IfcTypeProduct + shape_aspects = [] + for repersentation_map in element.RepresentationMaps: + shape_aspects += repersentation_map.HasShapeAspects + return shape_aspects + + def get_material(element, should_skip_usage=False, should_inherit=True): """Gets the material of the element diff --git a/src/ifcopenshell-python/test/util/test_element.py b/src/ifcopenshell-python/test/util/test_element.py index 9e6f5ffaad..a534ca2b4e 100644 --- a/src/ifcopenshell-python/test/util/test_element.py +++ b/src/ifcopenshell-python/test/util/test_element.py @@ -319,6 +319,24 @@ class TestGetTypesIFC2X3(test.bootstrap.IFC2X3): assert subject.get_types(element_type) == (element,) +class TestGetShapeAspects(test.bootstrap.IFC4): + def test_getting_the_shape_aspects_of_a_product_type(self): + element_type = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType") + shape_aspect = self.file.createIfcShapeAspect() + representation_map = self.file.createIfcRepresentationMap() + element_type.RepresentationMaps = (representation_map,) + shape_aspect.PartOfProductDefinitionShape = representation_map + assert tuple(subject.get_shape_aspects(element_type)) == (shape_aspect,) + + def test_getting_the_shape_aspects_of_a_product(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + shape_aspect = self.file.createIfcShapeAspect() + product_shape = self.file.createIfcProductDefinitionShape() + element.Representation = product_shape + shape_aspect.PartOfProductDefinitionShape = product_shape + assert tuple(subject.get_shape_aspects(element)) == (shape_aspect,) + + class TestGetMaterial(test.bootstrap.IFC4): def test_getting_the_material_of_a_product(self): element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")