Walk and filter a real IFC assembly

Walk and filter a real IFC assembly#

The .cad accessor adds walk, find, and flatten for hierarchical CAD assemblies. walk yields (path, block) pairs; find filters by metadata predicates; flatten collapses every leaf to a single-level MultiBlock with path-named blocks.

This example walks a real BIM model, the buildingSMART “Single-family house” sample (CC-BY 4.0, buildingSMART International), and finds all walls by their IFC type, the canonical first step in any BIM analysis.

import pandas as pd
import pyvista as pv

import pyvista_cad
from pyvista_cad.examples import downloads

Read the real IFC building.

MultiBlock (0x7f6faa6d8fa0)
  N Blocks:   5
  X Bounds:   -2.964e+01, 8.900e+00
  Y Bounds:   -1.499e+01, 9.300e+00
  Z Bounds:   -1.300e+00, 5.700e+00


Drop stray geo-reference / survey markers placed far from the house. drop_spatial_outliers uses a robust MAD test on block position (markers have no reliable IFC class) and preserves the hierarchy, so the walk below still shows the real building tree.

root = root.cad.drop_spatial_outliers()

Walk the tree. Path, IFC type, and cell count.

rows = []
for path, block in root.cad.walk():
    n_cells = (
        block.n_cells
        if isinstance(block, pv.DataSet)
        else sum(b.n_cells for b in block if isinstance(b, pv.DataSet))
    )
    rows.append(
        {
            'path': str(path),
            'kind': type(block).__name__,
            'ifc_type': block.field_data.get('cad.ifc_type', [b''])[0],
            'cells': n_cells,
        }
    )
pd.DataFrame(rows)
path kind ifc_type cells
0 environment - site MultiBlock IfcSite 0
1 environment - site/house - site MultiBlock IfcSite 0
2 environment - site/house - site/Single-family ... MultiBlock IfcBuilding 70
3 environment - site/house - site/Single-family ... MultiBlock IfcBuildingStorey 133
4 environment - site/house - site/Single-family ... MultiBlock IfcSpace 36
5 environment - site/house - site/Single-family ... PolyData IfcFurniture 36
6 environment - site/house - site/Single-family ... PolyData IfcSlab 73
7 environment - site/house - site/Single-family ... PolyData IfcWall 12
8 environment - site/house - site/Single-family ... PolyData IfcWall 12
9 environment - site/house - site/Single-family ... PolyData IfcWall 12
10 environment - site/house - site/Single-family ... PolyData IfcWall 24
11 environment - site/house - site/Single-family ... PolyData IfcSpatialZone 24
12 environment - site/house - site/Single-family ... PolyData IfcBuildingElementProxy 46
13 living room PolyData IfcSpace 28
14 entry hall PolyData IfcSpace 12
15 house - roof - slab left PolyData IfcSlab 12
16 house - roof - slab right PolyData IfcSlab 40


Find every wall.

walls = root.cad.find(ifc_type='IfcWall')
pd.DataFrame({'path': [str(p) for p, _ in walls]})
path
0 environment - site/house - site/Single-family ...
1 environment - site/house - site/Single-family ...
2 environment - site/house - site/Single-family ...
3 environment - site/house - site/Single-family ...


Flatten the tree: every leaf becomes a path-named block.

list(root.cad.flatten().keys())
['environment - site/house - site/Single-family house/00 groundfloor/living room/kitchen', 'environment - site/house - site/Single-family house/00 groundfloor/floor', 'environment - site/house - site/Single-family house/00 groundfloor/house - outer wall - house right front', 'environment - site/house - site/Single-family house/00 groundfloor/house - outer wall - house right back', 'environment - site/house - site/Single-family house/00 groundfloor/house - outer wall - house left', 'environment - site/house - site/Single-family house/00 groundfloor/plumbing wall', 'environment - site/house - site/Single-family house/house - gross volume', 'environment - site/house - site/Single-family house/sand bedding', 'living room', 'entry hall', 'house - roof - slab left', 'house - roof - slab right']

Render the building with each element in its own color.

pl = pv.Plotter()
pl.add_mesh(root, multi_colors=True, show_edges=True, edge_color='black')
pl.show()
assembly tree walk

Total running time of the script: (0 minutes 0.605 seconds)