From cb2895cc410c421ec9dc760b9bdb46b1ac027861 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Tue, 15 Oct 2024 15:05:24 +0500 Subject: [PATCH] util.shape for materials and material_ids --- src/bonsai/bonsai/bim/import_ifc.py | 11 -------- .../bonsai/bim/module/project/operator.py | 2 +- .../ifcopenshell/util/shape.py | 25 +++++++++++++++++++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/bonsai/bonsai/bim/import_ifc.py b/src/bonsai/bonsai/bim/import_ifc.py index a9d3451d07..bb57ec47a5 100644 --- a/src/bonsai/bonsai/bim/import_ifc.py +++ b/src/bonsai/bonsai/bim/import_ifc.py @@ -70,17 +70,6 @@ class MaterialCreator: self.mesh = mesh self.obj = obj - - # mesh["ios_materials"] can contain: - # - ifc style id if style assigned to the representation items directly - # or through material with a style; - # - ifc material id if both true: - # - element has a material without a style; - # - there are parts of the geometry that has no other style assigned to them; - # - -1 in case if there is no material; - # - 0 in case if there are default materials used. - # Though 0 value will not occur as we don't use default materials in IfcImporter. - self.parsed_meshes.add(self.mesh.name) self.load_texture_maps(shape_has_openings) self.assign_material_slots_to_faces() diff --git a/src/bonsai/bonsai/bim/module/project/operator.py b/src/bonsai/bonsai/bim/module/project/operator.py index de923110fa..0f14285f32 100644 --- a/src/bonsai/bonsai/bim/module/project/operator.py +++ b/src/bonsai/bonsai/bim/module/project/operator.py @@ -1567,7 +1567,7 @@ class LoadLinkedProject(bpy.types.Operator): has_processed_chunk = False ms = np.vstack([default_mat, ifcopenshell.util.shape.get_material_colors(shape.geometry)]) - mi = np.frombuffer(shape.geometry.material_ids_buffer, dtype=np.int32) + mi = ifcopenshell.util.shape.get_faces_material_style_ids(shape.geometry) for geom_material_idx, geom_material in enumerate(shape.geometry.materials): if not geom_material.instance_id(): ms[geom_material_idx + 1] = (0.8, 0.8, 0.8, 1) diff --git a/src/ifcopenshell-python/ifcopenshell/util/shape.py b/src/ifcopenshell-python/ifcopenshell/util/shape.py index 4a92d6c79f..ec6caec705 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/shape.py +++ b/src/ifcopenshell-python/ifcopenshell/util/shape.py @@ -20,6 +20,7 @@ import shapely import shapely.ops import numpy as np import numpy.typing as npt +import ifcopenshell.ifcopenshell_wrapper as W import ifcopenshell.util.element import ifcopenshell.util.placement import ifcopenshell.util.representation @@ -300,6 +301,30 @@ def get_normals(geometry: ShapeType) -> npt.NDArray[np.float64]: return np.frombuffer(geometry.normals_buffer, dtype="d").reshape(-1, 3) +def get_shape_material_styles(geometry: ShapeType) -> tuple[W.style, ...]: + """Get list of material styles. + + Possible values for `style.instance_id()`: + - IFC style id if style assigned to the representation items directly + or through material with a style; + - IFC material id if both true: + - element has a material without a style; + - there are parts of the geometry that has no other style assigned to them; + - -1 in case if there is no material; + - 0 in case if there are default materials used. + """ + return geometry.materials + + +def get_faces_material_style_ids(geometry: ShapeType) -> npt.NDArray[np.int32]: + """Get material styles ids for the geometry faces. + + Return a list of corresponding indices of styles from get_shape_material_styles for each face. + If face has no style assigned, index -1 is used. + """ + return np.frombuffer(geometry.material_ids_buffer, dtype="i") + + def get_faces_representation_item_ids(geometry: ShapeType) -> npt.NDArray[np.int32]: """Get representation item ids for the geometry faces.""" return np.frombuffer(geometry.item_ids_buffer, dtype="i")