diff --git a/src/ifcopenshell-python/ifcopenshell/util/element.py b/src/ifcopenshell-python/ifcopenshell/util/element.py index df0b5d8786..6f7d28509f 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/element.py +++ b/src/ifcopenshell-python/ifcopenshell/util/element.py @@ -764,26 +764,30 @@ def get_elements_by_style( style = file.by_type("IfcSurfaceStyle")[0] elements = ifcopenshell.util.element.get_elements_by_style(file, style) """ - is_curve_style = style.is_a("IfcCurveStyle") results = set() inverses = list(ifc_file.get_inverse(style)) while inverses: inverse = inverses.pop() inverse_class = inverse.is_a() - if is_curve_style and inverse_class in ("IfcFillAreaStyleHatching", "IfcFillAreaStyle"): - inverses.extend(ifc_file.get_inverse(inverse)) - continue - if inverse.is_a("IfcPresentationStyleAssignment"): + # IfcPresentationStyleAssignment for < IFC4X3. + # IfcFillAreaStyleHatching->IfcFillAreaStyle only for IfcCurveStyle. + # IfcFillAreaStyleTiles->IfcFillAreaStyle is not restricted to IfcCurveStyle. + if inverse_class in ( + "IfcPresentationStyleAssignment", + "IfcFillAreaStyleHatching", + "IfcFillAreaStyle", + "IfcFillAreaStyleTiles", + ): inverses.extend(ifc_file.get_inverse(inverse)) continue if not inverse.is_a("IfcStyledItem"): continue - if inverse.Item: - [ - results.update(get_elements_by_representation(ifc_file, i)) - for i in ifc_file.get_inverse(inverse.Item) - if i.is_a("IfcShapeRepresentation") - ] + if geometry_item := inverse.Item: + for inverse_ in ifc_file.get_inverse(geometry_item): + if inverse_.is_a("IfcShapeRepresentation"): + results.update(get_elements_by_representation(ifc_file, inverse_)) + # IfcFillAreaStyleTiles requires .Item to be set. + inverses.extend(ifc_file.get_inverse(inverse)) else: styled_reps = [i for i in ifc_file.get_inverse(inverse) if i.is_a("IfcStyledRepresentation")] for styled_rep in styled_reps: diff --git a/src/ifcopenshell-python/test/util/test_element.py b/src/ifcopenshell-python/test/util/test_element.py index ddb84900a3..4bbb62077c 100644 --- a/src/ifcopenshell-python/test/util/test_element.py +++ b/src/ifcopenshell-python/test/util/test_element.py @@ -652,6 +652,29 @@ class TestGetElementsByStyle(test.bootstrap.IFC4): ) assert subject.get_elements_by_style(self.file, curve_style) == {element} + def test_getting_elements_of_a_styled_material_with_fill_area_style_tiles(self): + # NOTE: Not sure if this the exactly correct way to implement IfcFillAreaStyleTiles + # couldn't find any .ifc files implementing it, so taking my best guess. + # Ref: https://web.archive.org/web/20220711062839/https://ifcdoctor.com/2022/07/11/graphics-in-ifc/ + element = self.file.create_entity("IfcWall") + material = ifcopenshell.api.material.add_material(self.file) + ifcopenshell.api.material.assign_material(self.file, products=[element], material=material) + tile_style = self.file.create_entity("IfcCurveStyle") + tile_curve = self.file.create_entity("IfcGeometricSet") + styled_item = self.file.create_entity("IfcStyledItem", Styles=[tile_style], Item=tile_curve) + fill_style = self.file.create_entity("IfcFillAreaStyleTiles", Tiles=[styled_item]) + style = self.file.create_entity("IfcFillAreaStyle", FillStyles=[fill_style]) + self.file.create_entity( + "IfcMaterialDefinitionRepresentation", + RepresentedMaterial=material, + Representations=[ + self.file.create_entity( + "IfcStyledRepresentation", Items=[self.file.create_entity("IfcStyledItem", Styles=[style])] + ) + ], + ) + assert subject.get_elements_by_style(self.file, tile_style) == {element} + class TestGetElementsByRepresentation(test.bootstrap.IFC4): def test_getting_elements_of_a_styled_representation_item(self):