Skip to content

Commit 41486ba

Browse files
committed
New utility to get layers of an element. See IfcOpenShell#1869.
1 parent 2120653 commit 41486ba

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/ifcopenshell-python/ifcopenshell/util/element.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ def get_material(element, should_skip_usage=False):
101101
return get_material(relating_type, should_skip_usage)
102102

103103

104+
def get_layers(ifc_file, element):
105+
layers = []
106+
representations = []
107+
if getattr(element, "Representation", None):
108+
representations = [element.Representation]
109+
elif getattr(element, "RepresentationMaps", None):
110+
representations = element.RepresentationMaps
111+
for representation in representations:
112+
for subelement in ifc_file.traverse(representation):
113+
if subelement.is_a("IfcShapeRepresentation"):
114+
layers.extend(subelement.LayerAssignments or [])
115+
elif subelement.is_a("IfcGeometricRepresentationItem"):
116+
layers.extend(subelement.LayerAssignment or [])
117+
return layers
118+
119+
104120
def get_container(element, should_get_direct=False):
105121
if should_get_direct:
106122
if hasattr(element, "ContainedInStructure") and element.ContainedInStructure:

src/ifcopenshell-python/test/util/test_element.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,42 @@ def test_getting_an_inherited_material_from_the_elements_type(self):
214214
assert subject.get_material(element) == material
215215

216216

217+
class TestGetlayers(test.bootstrap.IFC4):
218+
def test_getting_the_layer_of_a_product_representation(self):
219+
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
220+
layer = ifcopenshell.api.run("layer.add_layer", self.file)
221+
representation = self.file.createIfcShapeRepresentation()
222+
element.Representation = self.file.createIfcProductDefinitionShape(Representations=[representation])
223+
ifcopenshell.api.run("layer.assign_layer", self.file, item=representation, layer=layer)
224+
assert subject.get_layers(self.file, element) == [layer]
225+
226+
def test_getting_the_layer_of_a_product_item(self):
227+
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
228+
layer = ifcopenshell.api.run("layer.add_layer", self.file)
229+
item = self.file.createIfcExtrudedAreaSolid()
230+
representation = self.file.createIfcShapeRepresentation(Items=[item])
231+
element.Representation = self.file.createIfcProductDefinitionShape(Representations=[representation])
232+
ifcopenshell.api.run("layer.assign_layer", self.file, item=item, layer=layer)
233+
assert subject.get_layers(self.file, element) == [layer]
234+
235+
def test_getting_the_layer_of_a_type_product_representation(self):
236+
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
237+
layer = ifcopenshell.api.run("layer.add_layer", self.file)
238+
representation = self.file.createIfcShapeRepresentation()
239+
element.RepresentationMaps = [self.file.createIfcRepresentationMap(MappedRepresentation=representation)]
240+
ifcopenshell.api.run("layer.assign_layer", self.file, item=representation, layer=layer)
241+
assert subject.get_layers(self.file, element) == [layer]
242+
243+
def test_getting_the_layer_of_a_type_product_item(self):
244+
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
245+
layer = ifcopenshell.api.run("layer.add_layer", self.file)
246+
item = self.file.createIfcExtrudedAreaSolid()
247+
representation = self.file.createIfcShapeRepresentation(Items=[item])
248+
element.RepresentationMaps = [self.file.createIfcRepresentationMap(MappedRepresentation=representation)]
249+
ifcopenshell.api.run("layer.assign_layer", self.file, item=item, layer=layer)
250+
assert subject.get_layers(self.file, element) == [layer]
251+
252+
217253
class TestGetContainerIFC4(test.bootstrap.IFC4):
218254
def test_getting_the_spatial_container_of_an_element(self):
219255
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")

0 commit comments

Comments
 (0)