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

Getting sub components of site #564

Open SunilApte opened 2 months ago

SunilApte commented 2 months ago

We are currently replacing HOOPS with Xbim. In HOOPS we have following code:

HPS.Component[] children = m_compIFCSites.GetSubcomponents();
                        foreach (Component compsurface in children.ToList())
                        {
                            var type = ComponentPropertyBuilder.FindStringPropertyTypecheck(compsurface);
                            if (type != "IFCBUILDING"/* && compsurface.GetName().ToLower().Contains("surface")*/)//Identity Data/Mark   Plot

                            {

I can get IfcSite using xbim. But how can I get sub components? I can get buildings and spaces from ifcsite. But I need all sub components under site whose type is not building.

How to achieve this?

martin1cerny commented 2 months ago

When you say subcomponents, do you mean the geometrical parts (often terrain surface in case of IfcSite), or do you mean semantic breakdown (building(s), level(s), space(s))?

SunilApte commented 2 months ago

Subcomponents in case of HOOPS is breakdown like buildings and other subcomponents of different Ifc elements. In case of xbim how to achieve above?

martin1cerny commented 2 months ago

You just need to iterate through the aggregation relation recursively.

private static void ExploreDecomposition(IIfcObjectDefinition entity, string indent)
{
    Console.WriteLine(indent + entity.Name);
    var children = entity.IsDecomposedBy.SelectMany(r => r.RelatedObjects);

    indent += "    ";
    foreach (var child in children)
    {
        ExploreDecomposition(child, indent);
    }
}

var site = model.Instances.FirstOrDefault<IIfcSite>();
ExploreDecomposition(site);

Note: Code is subject to optimisation and refactoring before used in production.