Finding children of IfcBuildingStorey using ifcopenshell

818 Views Asked by At

By using ifchopenshell I can easily traverse all storeys using this code

for ifc_storey in ifc_file.by_type("IfcBuildingStorey"):
    print(str(ifc_storey.Name))

However, for each storey I would like to find all the Ifc elements of type IfcSpace, that belongs to it.

How can I query this? Thank you.

1

There are 1 best solutions below

0
On

I finally solved it using this code:

def getChildrenOfType(ifcParentElement,ifcType):
    items=[]
    if type(ifcType) != list:
        ifcType=[ifcType]
    _getChildrenOfType(items,ifcParentElement,ifcType,0)
    return items

def _getChildrenOfType(targetList,element,ifcTypes,level):
    # follow Spatial relation
    if (element.is_a('IfcSpatialStructureElement')):
        for rel in element.ContainsElements:
            relatedElements = rel.RelatedElements
            for child in relatedElements:
                _getChildrenOfType(targetList,child, ifcTypes, level + 1)
    # follow Aggregation Relation
    if (element.is_a('IfcObjectDefinition')):
        for rel in element.IsDecomposedBy:
            relatedObjects = rel.RelatedObjects
            for child in relatedObjects:
                _getChildrenOfType(targetList,child, ifcTypes, level + 1)
    for typ in ifcTypes:
        if (element.is_a(typ)):
            targetList.append(element)