xBimTeam / XbimEssentials

A .NET library to work with data in the IFC format. This is the core component of the Xbim Toolkit
https://xbimteam.github.io/
Other
477 stars 171 forks source link

Material name? #523

Closed seghier closed 9 months ago

seghier commented 9 months ago

Hi How can we retrieve the material name from an IFC file?

In this code i try o.Material, obj.Material but the result always empty

private void PrintHierarchy(IIfcObjectDefinition o, int level, DataTable resultTable)
        {
            var newRow = resultTable.NewRow();
            newRow["Name"] = o.Name;
            newRow["Type"] = o.GetType().Name;
            newRow["Level"] = level;

            // Check if the object is an IIfcObject and cast it to access its properties
            if (o is IIfcObject obj)
            {
                newRow["Description"] = GetPropertyValue(obj, "Description");
                newRow["EntityLabel"] = obj.EntityLabel.ToString();
                newRow["Guid"] = obj.GlobalId.ToString();
                newRow["OwnerHistory"] = GetPropertyValue(obj, "OwnerHistory");
            }
            else
            {
                newRow["Description"] = "N/A";
                newRow["EntityLabel"] = "N/A";
                newRow["Guid"] = "N/A";
                newRow["OwnerHistory"] = "N/A";
            }

            resultTable.Rows.Add(newRow);

            var spatialElement = o as IIfcSpatialStructureElement;
            if (spatialElement != null)
            {
                foreach (var containedElement in spatialElement.ContainsElements)
                {
                    foreach (var element in containedElement.RelatedElements)
                    {
                        PrintHierarchy(element, level + 1, resultTable);
                    }
                }
            }

            foreach (var item in o.IsDecomposedBy.SelectMany(r => r.RelatedObjects))
            {
                PrintHierarchy(item, level + 1, resultTable);
            }
        }