Don't assume the first representation when checking materials

This commit is contained in:
Dion Moult
2020-03-20 09:38:41 +11:00
parent 83bba733c7
commit 3b8b0cbbbf
+24 -7
View File
@@ -16,18 +16,33 @@ class MaterialCreator():
self.parsed_meshes = [] self.parsed_meshes = []
self.ifc_import_settings = ifc_import_settings self.ifc_import_settings = ifc_import_settings
def create(self, element, object, mesh): def create(self, element, obj, mesh):
self.object = object self.obj = obj
self.mesh = mesh self.mesh = mesh
if not element.Representation: if not element.Representation:
return return
if self.ifc_import_settings.should_treat_styled_item_as_material \ if self.ifc_import_settings.should_treat_styled_item_as_material \
and self.mesh.name in self.parsed_meshes: and self.mesh.name in self.parsed_meshes:
return return
for item in element.Representation.Representations[0].Items: if self.parse_representations(element):
return # styled items override material styles
self.parse_material(element)
def parse_representations(self, element):
for representation in element.Representation.Representations:
if self.parse_representation(representation):
return True
def parse_representation(self, representation):
for item in representation.Items:
if self.parse_representation_item(item):
return True
def parse_representation_item(self, item):
if item.is_a('IfcMappedItem'): if item.is_a('IfcMappedItem'):
item = item.MappingSource.MappedRepresentation.Items[0] item = item.MappingSource.MappedRepresentation.Items[0]
if item.StyledByItem: if not item.StyledByItem:
return
styled_item = item.StyledByItem[0] styled_item = item.StyledByItem[0]
if styled_item.Name: if styled_item.Name:
material_name = styled_item.Name material_name = styled_item.Name
@@ -55,7 +70,9 @@ class MaterialCreator():
else: else:
# Proper behaviour # Proper behaviour
self.assign_material_to_mesh(self.materials[material_name], is_styled_item=True) self.assign_material_to_mesh(self.materials[material_name], is_styled_item=True)
return # styled items override material styles return True
def parse_material(self, element):
for association in element.HasAssociations: for association in element.HasAssociations:
if association.is_a('IfcRelAssociatesMaterial'): if association.is_a('IfcRelAssociatesMaterial'):
material_select = association.RelatingMaterial material_select = association.RelatingMaterial
@@ -117,8 +134,8 @@ class MaterialCreator():
self.parsed_meshes.append(self.mesh.name) self.parsed_meshes.append(self.mesh.name)
self.mesh.materials.append(material) self.mesh.materials.append(material)
if is_styled_item: if is_styled_item:
self.object.material_slots[0].link = 'OBJECT' self.obj.material_slots[0].link = 'OBJECT'
self.object.material_slots[0].material = material self.obj.material_slots[0].material = material
class IfcImporter(): class IfcImporter():
def __init__(self, ifc_import_settings): def __init__(self, ifc_import_settings):