mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 01:41:57 +00:00
#2111 New feature to select by style and see style usage
This commit is contained in:
@@ -134,25 +134,65 @@ def get_material(element, should_skip_usage=False):
|
||||
|
||||
|
||||
def get_elements_by_material(ifc_file, material):
|
||||
results = []
|
||||
results = set()
|
||||
for inverse in ifc_file.get_inverse(material):
|
||||
if inverse.is_a("IfcRelAssociatesMaterial"):
|
||||
results.extend(inverse.RelatedObjects)
|
||||
results.update(inverse.RelatedObjects)
|
||||
elif inverse.is_a("IfcMaterialLayer"):
|
||||
for material_set in inverse.ToMaterialLayerSet:
|
||||
results.extend(get_elements_by_material(ifc_file, material_set))
|
||||
results.update(get_elements_by_material(ifc_file, material_set))
|
||||
elif inverse.is_a("IfcMaterialProfile"):
|
||||
for material_set in inverse.ToMaterialProfileSet:
|
||||
results.extend(get_elements_by_material(ifc_file, material_set))
|
||||
results.update(get_elements_by_material(ifc_file, material_set))
|
||||
elif inverse.is_a("IfcMaterialConstituent"):
|
||||
for material_set in inverse.ToMaterialConstituentSet:
|
||||
results.extend(get_elements_by_material(ifc_file, material_set))
|
||||
results.update(get_elements_by_material(ifc_file, material_set))
|
||||
elif inverse.is_a("IfcMaterialLayerSetUsage"):
|
||||
results.extend(get_elements_by_material(ifc_file, inverse))
|
||||
results.update(get_elements_by_material(ifc_file, inverse))
|
||||
elif inverse.is_a("IfcMaterialProfileSetUsage"):
|
||||
results.extend(get_elements_by_material(ifc_file, inverse))
|
||||
results.update(get_elements_by_material(ifc_file, inverse))
|
||||
elif inverse.is_a("IfcMaterialList"):
|
||||
results.extend(get_elements_by_material(ifc_file, inverse))
|
||||
results.update(get_elements_by_material(ifc_file, inverse))
|
||||
return results
|
||||
|
||||
|
||||
def get_elements_by_style(ifc_file, style):
|
||||
results = set()
|
||||
inverses = list(ifc_file.get_inverse(style))
|
||||
while inverses:
|
||||
inverse = inverses.pop()
|
||||
if inverse.is_a("IfcPresentationStyleAssignment"):
|
||||
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")
|
||||
]
|
||||
else:
|
||||
styled_reps = [i for i in ifc_file.get_inverse(inverse) if i.is_a("IfcStyledRepresentation")]
|
||||
for styled_rep in styled_reps:
|
||||
for material_def_rep in styled_rep.OfProductRepresentation:
|
||||
results.update(get_elements_by_material(ifc_file, material_def_rep.RepresentedMaterial))
|
||||
return results
|
||||
|
||||
|
||||
def get_elements_by_representation(ifc_file, representation):
|
||||
results = set()
|
||||
[results.update(pr.ShapeOfProduct) for pr in representation.OfProductRepresentation]
|
||||
for rep_map in representation.RepresentationMap:
|
||||
for inverse in ifc_file.get_inverse(rep_map):
|
||||
if inverse.is_a("IfcTypeProduct"):
|
||||
results.add(inverse)
|
||||
elif inverse.is_a("IfcMappedItem"):
|
||||
[
|
||||
results.update(get_elements_by_representation(ifc_file, rep))
|
||||
for rep in ifc_file.get_inverse(inverse)
|
||||
if rep.is_a("IfcShapeRepresentation")
|
||||
]
|
||||
return results
|
||||
|
||||
|
||||
|
||||
@@ -278,7 +278,7 @@ class TestGetElementsByMaterial(test.bootstrap.IFC4):
|
||||
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
||||
material = ifcopenshell.api.run("material.add_material", self.file)
|
||||
ifcopenshell.api.run("material.assign_material", self.file, product=element, material=material)
|
||||
assert subject.get_elements_by_material(self.file, material) == [element]
|
||||
assert subject.get_elements_by_material(self.file, material) == {element}
|
||||
|
||||
def test_getting_elements_of_a_material_layer_set(self):
|
||||
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
||||
@@ -290,9 +290,9 @@ class TestGetElementsByMaterial(test.bootstrap.IFC4):
|
||||
ifcopenshell.api.run("material.assign_material", self.file, product=element_type, material=material_set)
|
||||
ifcopenshell.api.run("material.assign_material", self.file, product=element, type="IfcMaterialLayerSetUsage")
|
||||
usage = self.file.by_type("IfcMaterialLayerSetUsage")[0]
|
||||
assert set(subject.get_elements_by_material(self.file, material)) == {element, element_type}
|
||||
assert set(subject.get_elements_by_material(self.file, material_set)) == {element, element_type}
|
||||
assert set(subject.get_elements_by_material(self.file, usage)) == {element}
|
||||
assert subject.get_elements_by_material(self.file, material) == {element, element_type}
|
||||
assert subject.get_elements_by_material(self.file, material_set) == {element, element_type}
|
||||
assert subject.get_elements_by_material(self.file, usage) == {element}
|
||||
|
||||
def test_getting_elements_of_a_material_profile_set(self):
|
||||
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
||||
@@ -304,18 +304,20 @@ class TestGetElementsByMaterial(test.bootstrap.IFC4):
|
||||
ifcopenshell.api.run("material.assign_material", self.file, product=element_type, material=material_set)
|
||||
ifcopenshell.api.run("material.assign_material", self.file, product=element, type="IfcMaterialProfileSetUsage")
|
||||
usage = self.file.by_type("IfcMaterialProfileSetUsage")[0]
|
||||
assert set(subject.get_elements_by_material(self.file, material)) == {element, element_type}
|
||||
assert set(subject.get_elements_by_material(self.file, material_set)) == {element, element_type}
|
||||
assert set(subject.get_elements_by_material(self.file, usage)) == {element}
|
||||
assert subject.get_elements_by_material(self.file, material) == {element, element_type}
|
||||
assert subject.get_elements_by_material(self.file, material_set) == {element, element_type}
|
||||
assert subject.get_elements_by_material(self.file, usage) == {element}
|
||||
|
||||
def test_getting_elements_of_a_material_constituent_set(self):
|
||||
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
||||
material = ifcopenshell.api.run("material.add_material", self.file)
|
||||
material_set = ifcopenshell.api.run("material.add_material_set", self.file, set_type="IfcMaterialConstituentSet")
|
||||
material_set = ifcopenshell.api.run(
|
||||
"material.add_material_set", self.file, set_type="IfcMaterialConstituentSet"
|
||||
)
|
||||
ifcopenshell.api.run("material.add_constituent", self.file, constituent_set=material_set, material=material)
|
||||
ifcopenshell.api.run("material.assign_material", self.file, product=element, material=material_set)
|
||||
assert set(subject.get_elements_by_material(self.file, material)) == {element}
|
||||
assert set(subject.get_elements_by_material(self.file, material_set)) == {element}
|
||||
assert subject.get_elements_by_material(self.file, material) == {element}
|
||||
assert subject.get_elements_by_material(self.file, material_set) == {element}
|
||||
|
||||
def test_getting_elements_of_a_material_list(self):
|
||||
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
||||
@@ -323,8 +325,97 @@ class TestGetElementsByMaterial(test.bootstrap.IFC4):
|
||||
material_set = ifcopenshell.api.run("material.add_material_set", self.file, set_type="IfcMaterialList")
|
||||
ifcopenshell.api.run("material.add_list_item", self.file, material_list=material_set, material=material)
|
||||
ifcopenshell.api.run("material.assign_material", self.file, product=element, material=material_set)
|
||||
assert set(subject.get_elements_by_material(self.file, material)) == {element}
|
||||
assert set(subject.get_elements_by_material(self.file, material_set)) == {element}
|
||||
assert subject.get_elements_by_material(self.file, material) == {element}
|
||||
assert subject.get_elements_by_material(self.file, material_set) == {element}
|
||||
|
||||
|
||||
class TestGetElementsByStyle(test.bootstrap.IFC4):
|
||||
def test_getting_elements_of_a_styled_representation_item(self):
|
||||
element = self.file.createIfcWall()
|
||||
style = self.file.createIfcSurfaceStyle()
|
||||
item = self.file.createIfcExtrudedAreaSolid()
|
||||
self.file.createIfcStyledItem(Item=item, Styles=[style])
|
||||
element.Representation = self.file.createIfcProductDefinitionShape(
|
||||
Representations=[self.file.createIfcShapeRepresentation(Items=[item])]
|
||||
)
|
||||
assert subject.get_elements_by_style(self.file, style) == {element}
|
||||
|
||||
def test_getting_elements_of_a_styled_mapped_representation_item(self):
|
||||
element = self.file.createIfcWall()
|
||||
style = self.file.createIfcSurfaceStyle()
|
||||
item = self.file.createIfcExtrudedAreaSolid()
|
||||
self.file.createIfcStyledItem(Item=item, Styles=[style])
|
||||
element.Representation = self.file.createIfcProductDefinitionShape(
|
||||
Representations=[
|
||||
self.file.createIfcShapeRepresentation(
|
||||
Items=[
|
||||
self.file.createIfcMappedItem(
|
||||
MappingSource=self.file.createIfcRepresentationMap(
|
||||
MappedRepresentation=self.file.createIfcShapeRepresentation(Items=[item])
|
||||
)
|
||||
)
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
assert subject.get_elements_by_style(self.file, style) == {element}
|
||||
|
||||
def test_getting_type_elements_of_a_styled_mapped_representation_item(self):
|
||||
element = self.file.createIfcWallType()
|
||||
style = self.file.createIfcSurfaceStyle()
|
||||
item = self.file.createIfcExtrudedAreaSolid()
|
||||
self.file.createIfcStyledItem(Item=item, Styles=[style])
|
||||
element.RepresentationMaps = [
|
||||
self.file.createIfcRepresentationMap(
|
||||
MappedRepresentation=self.file.createIfcShapeRepresentation(Items=[item])
|
||||
)
|
||||
]
|
||||
assert subject.get_elements_by_style(self.file, style) == {element}
|
||||
|
||||
def test_getting_elements_of_a_styled_material(self):
|
||||
element = self.file.createIfcWall()
|
||||
material = ifcopenshell.api.run("material.add_material", self.file)
|
||||
ifcopenshell.api.run("material.assign_material", self.file, product=element, material=material)
|
||||
style = self.file.createIfcSurfaceStyle()
|
||||
self.file.createIfcMaterialDefinitionRepresentation(
|
||||
RepresentedMaterial=material,
|
||||
Representations=[
|
||||
self.file.createIfcStyledRepresentation(Items=[self.file.createIfcStyledItem(Styles=[style])])
|
||||
],
|
||||
)
|
||||
assert subject.get_elements_by_style(self.file, style) == {element}
|
||||
|
||||
|
||||
class TestGetElementsByRepresentation(test.bootstrap.IFC4):
|
||||
def test_getting_elements_of_a_styled_representation_item(self):
|
||||
element = self.file.createIfcWall()
|
||||
representation = self.file.createIfcShapeRepresentation()
|
||||
element.Representation = self.file.createIfcProductDefinitionShape(Representations=[representation])
|
||||
assert subject.get_elements_by_representation(self.file, representation) == {element}
|
||||
|
||||
def test_getting_elements_of_a_styled_mapped_representation_item(self):
|
||||
element = self.file.createIfcWall()
|
||||
representation = self.file.createIfcShapeRepresentation()
|
||||
element.Representation = self.file.createIfcProductDefinitionShape(
|
||||
Representations=[
|
||||
self.file.createIfcShapeRepresentation(
|
||||
Items=[
|
||||
self.file.createIfcMappedItem(
|
||||
MappingSource=self.file.createIfcRepresentationMap(
|
||||
MappedRepresentation=representation
|
||||
)
|
||||
)
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
assert subject.get_elements_by_representation(self.file, representation) == {element}
|
||||
|
||||
def test_getting_type_elements_of_a_styled_mapped_representation_item(self):
|
||||
element = self.file.createIfcWallType()
|
||||
representation = self.file.createIfcShapeRepresentation()
|
||||
element.RepresentationMaps = [self.file.createIfcRepresentationMap(MappedRepresentation=representation)]
|
||||
assert subject.get_elements_by_representation(self.file, representation) == {element}
|
||||
|
||||
|
||||
class TestGetlayers(test.bootstrap.IFC4):
|
||||
|
||||
Reference in New Issue
Block a user