Skip to content

Retrieve materials and colors #642

Description

@seghier

Hello, i use this to retrieve materials and colors, sometimes i get no material and unknown type, do i miss something or this is normal?

private void ExtractMaterialData(IIfcProduct element, List<string> materialNames,
List<Color> materialColors, List<string> materialTypes, List<string> log)
{
    string materialName = "No Material";
    Color elementColor = Color.Gray;  // Changed from materialColor
    string materialType = "Unknown";

    try
    {
        // Method 1: Direct material associations
        var materialAssociations = element.HasAssociations?.OfType<IIfcRelAssociatesMaterial>();
        if (materialAssociations != null && materialAssociations.Any())
        {
            foreach (var association in materialAssociations)
            {
                var materialSelect = association.RelatingMaterial;

                if (materialSelect is IIfcMaterial material)
                {
                    materialName = !string.IsNullOrEmpty(material.Name) ? material.Name.ToString() : "Unnamed Material";
                    materialType = !string.IsNullOrEmpty(material.Category) ? material.Category.ToString() : "General";

                    // Try to get color from material
                    var matColor = GetMaterialColor(material);  // Changed variable name
                    if (matColor.HasValue)
                    {
                        elementColor = Color.FromArgb(
                            (int)(matColor.Value.R),  // Fixed property access
                            (int)(matColor.Value.G),  // Fixed property access
                            (int)(matColor.Value.B)); // Fixed property access
                    }
                }
                else if (materialSelect is IIfcMaterialLayerSetUsage layerSetUsage)
                {
                    var layerSet = layerSetUsage.ForLayerSet;
                    if (layerSet.MaterialLayers.Any())
                    {
                        var firstLayer = layerSet.MaterialLayers.First();
                        materialName = !string.IsNullOrEmpty(firstLayer.Material?.Name) ? firstLayer.Material.Name.ToString() : "Layered Material";
                        materialType = "Layered";
                    }
                }
            }
        }

        // Method 2: Check element type for materials
        if (materialName == "No Material" && element is IIfcElement ifcElement)
        {
            var elementType = ifcElement.IsTypedBy?.FirstOrDefault()?.RelatingType;
            if (elementType != null)
            {
                var typeAssociations = elementType.HasAssociations?.OfType<IIfcRelAssociatesMaterial>();
                if (typeAssociations != null && typeAssociations.Any())
                {
                    var association = typeAssociations.First();
                    if (association.RelatingMaterial is IIfcMaterial typeMaterial)
                    {
                        materialName = !string.IsNullOrEmpty(typeMaterial.Name) ? typeMaterial.Name.ToString() : "Type Material";
                        materialType = !string.IsNullOrEmpty(typeMaterial.Category) ? typeMaterial.Category.ToString() : "Type";
                    }
                }
            }
        }

        // Method 3: Try to get color from representation styles
        if (elementColor == Color.Gray)
        {
            elementColor = GetElementColor(element) ?? Color.Gray;
        }
    }
    catch (Exception ex)
    {
        log.Add($"Error extracting material for {element.GlobalId}: {ex.Message}");
    }

    materialNames.Add(materialName);
    materialColors.Add(elementColor);  // Changed from materialColor
    materialTypes.Add(materialType);
}
private Color? GetMaterialColor(IIfcMaterial material)
{
    try
    {
        // Look for material properties that might contain color information
        var properties = material.HasProperties;
        if (properties != null)
        {
            foreach (var property in properties)
            {
                if (property is IIfcMaterialProperties matProps)
                {
                    // Look for color-related properties
                    // This is simplified - actual implementation depends on your IFC file structure
                }
            }
        }
        return null;
    }
    catch
    {
        return null;
    }
}
private Color? GetElementColor(IIfcProduct element)
{
    try
    {
        if (element.Representation?.Representations != null)
        {
            foreach (var representation in element.Representation.Representations)
            {
                foreach (var item in representation.Items)
                {
                    if (item is IIfcStyledItem styledItem)
                    {
                        foreach (var style in styledItem.Styles.OfType<IIfcPresentationStyleAssignment>())
                        {
                            foreach (var styleSelect in style.Styles)
                            {
                                if (styleSelect is IIfcSurfaceStyle surfaceStyle)
                                {
                                    var renderingStyle = surfaceStyle.Styles
                                        .OfType<IIfcSurfaceStyleRendering>()
                                        .FirstOrDefault();

                                    if (renderingStyle?.SurfaceColour != null)
                                    {
                                        var ifcColor = renderingStyle.SurfaceColour;
                                        return Color.FromArgb(
                                            (int)(ifcColor.Red * 255),
                                            (int)(ifcColor.Green * 255),
                                            (int)(ifcColor.Blue * 255));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return null;
    }
    catch
    {
        return null;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions