Search code examples
c#linqifcbimxbim

How to get the material data of specific IfcElement


In the basic operations in the xbim examples https://docs.xbim.net/examples/basic-model-operations.html, it shows how to retrieve the single value properties of a specific IfcElement. Based on this I've tried to get the material data.

I've written the following:

var id = "3NBPkknun6EuV9fpeE6rFh";

var theWall = model.Instances.FirstOrDefault<IIfcElement>(d => d.GlobalId == id);

var materials = theWall.HasAssociations
                        .Where(r => r.RelatingMaterial is IIfcMaterialProfileSet)
                        .SelectMany(r=> ((IIfcMaterialProfileSet)r.RelatingMaterial).MaterialProfiles)
                        .OfType<IIfcMaterialProfile>();

It gives me this error:

'IIfcRelAssociates' does not contain a definition for 'RelatingMaterial' and no accessible extension method 'RelatingMaterial' accepting a first argument of type 'IIfcRelAssociates' could be found (are you missing a using directive or an assembly reference?)

I understand that I have to use the IfcRelAssociatesMaterial, but I can't figure how. How can I retrieve the material information?


Solution

  • IIfcObjectDefinition's HasAssociations returns a set of IIfcRelAssociates but you want just the derived type IIfcRelAssociatesMaterial which has the RelatingMaterial property. See https://standards.buildingsmart.org/IFC/RELEASE/IFC4/ADD2/HTML/schema/ifcproductextension/lexical/ifcrelassociatesmaterial.htm

    So, it's just a matter of adding .OfType<IIfcRelAssociatesMaterial> to constrain the query to Material associations. i.e.

    var materials = theWall.HasAssociations.OfType<IIfcRelAssociatesMaterial>() 
    // rest of the query