get_element_by_style to support IfcFillAreaStyleTiles

This commit is contained in:
Andrej730
2024-10-01 14:50:08 +05:00
parent e040dc4562
commit 4b39631b98
2 changed files with 38 additions and 11 deletions
@@ -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:
@@ -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):