XbimEssentials icon indicating copy to clipboard operation
XbimEssentials copied to clipboard

Using IfcRelConnectsWithRealizingElements to get Realizing Elements

Open CamChatHel opened this issue 1 year ago • 1 comments

Hello Everyone, I have trouble reading an IFC file. I want to find all realizing elements in a connection with 2 elements using IfcRelConnectsWithRealizingElements.

I'm able to find connections (IfcRelConnectsElements) that one element has (selectedElement is an IIfcElement):

List<IIfcRelConnectsElements> connectionFrom = selectedElement.ConnectedFrom.ToList();

List<IIfcRelConnectsElements> connectionTo = selectedElement.ConnectedTo.ToList();

IfcRelConnectsWithRealizingElements is a subtype of IfcRelConnectsElements. Is there a quick and simple way to get the RealizingElements? At the moment i'm doing this:

List<IIfcRelConnectsElements> connectionFrom = element.ConnectedFrom.ToList().Where(s => s is IIfcRelConnectsWithRealizingElements).ToList();
List<IIfcRelConnectsElements> connectionTo = element.ConnectedTo.ToList().Where(s => s is IIfcRelConnectsWithRealizingElements).ToList();

//In all these elements find elements with realizing elements:
 foreach (IIfcRelConnectsWithRealizingElements item in connectionFrom)
        {
          foreach (IIfcElement realizingitem in item.RealizingElements)
            {
                     //do something with information realizingitem 
             }
          }
  foreach (IIfcRelConnectsWithRealizingElements item in connectionTo)
     {
        foreach (IIfcElement realizingitem in item.RealizingElements)
            {
             //do something with information realizingitem 
              }
      }

This works, but does not feel elegant. Any idea how to get this more smooth? I'm not a professional programmer and such little details are hard to get done better alone. I would be happy about support.

CamChatHel avatar Jul 06 '22 07:07 CamChatHel

Just use LINQ extensions properly:

var connectionFrom = element.ConnectedFrom
    .OfType<IIfcRelConnectsWithRealizingElements>()
    .SelectMany(rel => rel.RealizingElements);

martin1cerny avatar Jul 19 '22 14:07 martin1cerny