diff --git a/src/blenderbim/blenderbim/bim/import_ifc.py b/src/blenderbim/blenderbim/bim/import_ifc.py index 4741c5d134..18583c357e 100644 --- a/src/blenderbim/blenderbim/bim/import_ifc.py +++ b/src/blenderbim/blenderbim/bim/import_ifc.py @@ -1069,10 +1069,14 @@ class IfcImporter: def create_materials(self): for material in self.file.by_type("IfcMaterial"): - blender_material = bpy.data.materials.new(material.Name) - self.link_element(material, blender_material) - self.material_creator.materials[material.id()] = blender_material - blender_material.use_fake_user = True + self.create_material(material) + + def create_material(self, material): + blender_material = bpy.data.materials.new(material.Name) + self.link_element(material, blender_material) + self.material_creator.materials[material.id()] = blender_material + blender_material.use_fake_user = True + return blender_material def create_styles(self): parsed_styles = set() @@ -1088,11 +1092,13 @@ class IfcImporter: for style in self.file.by_type("IfcSurfaceStyle"): if style.id() in parsed_styles: continue + self.create_style(style) + + def create_style(self, style, blender_material=None): + if not blender_material: name = style.Name or str(style.id()) blender_material = bpy.data.materials.new(name) - self.create_style(style, blender_material) - def create_style(self, style, blender_material): old_definition_id = blender_material.BIMObjectProperties.ifc_definition_id if not old_definition_id: self.link_element(style, blender_material) diff --git a/src/blenderbim/blenderbim/bim/module/project/operator.py b/src/blenderbim/blenderbim/bim/module/project/operator.py index 15401996ae..b30a8bdc74 100644 --- a/src/blenderbim/blenderbim/bim/module/project/operator.py +++ b/src/blenderbim/blenderbim/bim/module/project/operator.py @@ -346,9 +346,37 @@ class AppendLibraryElement(bpy.types.Operator): ifc_importer = import_ifc.IfcImporter(ifc_import_settings) ifc_importer.file = self.file ifc_importer.type_collection = type_collection + self.import_type_materials(element, ifc_importer) + self.import_type_styles(element, ifc_importer) ifc_importer.create_type_product(element) ifc_importer.place_objects_in_spatial_tree() + def import_type_materials(self, element, ifc_importer): + for rel in element.HasAssociations: + if not rel.is_a("IfcRelAssociatesMaterial"): + continue + for material in [e for e in self.file.traverse(rel) if e.is_a("IfcMaterial")]: + if IfcStore.get_element(material.id()): + continue + blender_material = ifc_importer.create_material(material) + self.import_material_styles(blender_material, material, ifc_importer) + + def import_type_styles(self, element, ifc_importer): + for representation_map in element.RepresentationMaps or []: + for element in self.file.traverse(representation_map): + if not element.is_a("IfcRepresentationItem") or not element.StyledByItem: + continue + for element2 in self.file.traverse(element.StyledByItem[0]): + if element2.is_a("IfcSurfaceStyle") and not IfcStore.get_element(element2.id()): + ifc_importer.create_style(element2) + + def import_material_styles(self, blender_material, material, ifc_importer): + if not material.HasRepresentation: + return + for element in self.file.traverse(material.HasRepresentation[0]): + if element.is_a("IfcSurfaceStyle") and not IfcStore.get_element(element.id()): + ifc_importer.create_style(element, blender_material) + class EnableEditingHeader(bpy.types.Operator): bl_idname = "bim.enable_editing_header" diff --git a/src/ifcopenshell-python/ifcopenshell/util/element.py b/src/ifcopenshell-python/ifcopenshell/util/element.py index f245f9b4e8..1dfeac623b 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/element.py +++ b/src/ifcopenshell-python/ifcopenshell/util/element.py @@ -74,9 +74,7 @@ def get_material(element, should_skip_usage=False): return relationship.RelatingMaterial relating_type = get_type(element) if hasattr(relating_type, "HasAssociations") and relating_type.HasAssociations: - for relationship in relating_type.HasAssociations: - if relationship.is_a("IfcRelAssociatesMaterial"): - return relationship.RelatingMaterial + return get_material(relating_type, should_skip_usage) def get_container(element):