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
485 stars 172 forks source link

Attaching IfcMaterialLayerSet to IfcProduct #514

Closed GVladislavG closed 1 year ago

GVladislavG commented 1 year ago

Hello, team! I try to attach IfcMaterialLayerSet to IfcProduct. But can't understand how to do it because IfcProduct.Material is null and can't be set directly. I thought code should look like this

typeId = model.Metadata.ExpressTypeId("IFCRELASSOCIATESMATERIAL");
IIfcRelAssociatesMaterial assMat = model.Instances.New(model.Metadata.GetType(typeId)) as IIfcRelAssociatesMaterial;
assMat.RelatingMaterial = ifcProduct.Material;
MatLayerSet.AssociatedTo.Append(assMat);

But it doesn't work.

I also tried this code according to https://buildingsmart.github.io/ProductData/schema/templates/material-layer-set.html

assMat.RelatingMaterial = MatLayerSet;
assMat.RelatedObjects.Append(ifcProduct);
ifcProduct.HasAssociations.Append(assMat);

No result, unfortunately.

Thank you!

GVladislavG commented 1 year ago

Right code sample

typeId = model.Metadata.ExpressTypeId("IFCRELASSOCIATESMATERIAL");
IIfcRelAssociatesMaterial assMat = model.Instances.New(model.Metadata.GetType(typeId)) as IIfcRelAssociatesMaterial;
assMat.RelatingMaterial = MatLayerSet;
assMat.RelatedObjects.Add(ifcProduct);
andyward commented 1 year ago

Glad you sorted it. That material-layer-set diagram actually shows the attributes to use. I think could have also done:

// assMat.RelatedObjects.Add(ifcProduct);
ifcProduct.HasAssociations.Add(assMat);

BTW, rather than use the Express type to get schema independence you should be able to use this approach which avoids the casting and string literals.

var factory=new Create(model); // Factory to create the correct entity type for the schema
IIfcRelAssociatesMaterial assMat = factory.RelAssociatesMaterial(mat => 
{
  mat.RelatingMaterial = MatLayerSet;
  mat.RelatedObjects.Add(ifcProduct);
});

https://github.com/xBimTeam/XbimEssentials/blob/a99168f09248b0edc8aa2ba617451d972ff3c9eb/Xbim.Ifc2x3/Interfaces/IFC4/Create.cs#L2159