Skip to content

Getting started: Python ​

openbim-ifcx on PyPI is a thin binding over the Rust crates: one abi3 wheel for CPython 3.9 and later.

sh
pip install openbim-ifcx

Every snippet on this page is a region of crates/openbim-ifcx-py/tests/python/test_guide.py, which the gate runs against the built wheel, so it works as shown.

Read, validate and write ​

python
from openbim_ifcx import IfcxFile

with open("model.ifcx", "rb") as f:
    file = IfcxFile.parse(f.read())  # bytes or str
print(file.header["id"], file.node_count)

report = file.validate()  # against the file's own schemas
for failure in report["failures"]:
    print(failure["node"], failure["attribute"], failure["kind"], failure["message"])

text = file.write(pretty=True)  # content, key order and numbers as read

Results are plain dicts and lists. A validation failure is {"node", "attribute", "pointer", "kind", "message"}, with a stable kind such as missing-schema or type-mismatch.

Compose layers and export GLB ​

python
from pathlib import Path

from openbim_ifcx import compose, export_glb, validate

# Layers weakest first: the last layer's opinions win.
layers = [Path("model.ifcx").read_bytes(), Path("overlay.ifcx").read_bytes()]

tree = compose(layers)  # {"path", "attributes", "children"}
print(list(tree["children"]))  # the root nodes

report = validate(layers)

Path("model.glb").write_bytes(export_glb(layers))  # binary glTF 2.0

export_glb takes y_up=False to keep IFCX's Z-up axes and origin_on_root=False to centre the model on the glTF origin.

Imports ​

python
# Imports are passed in, keyed by the exact import `uri`; the binding
# never reads files or the network itself.
imports = {"https://example.org/model/types.ifcx": Path("types.ifcx").read_bytes()}
tree = compose([main], imports=imports)

With imports (possibly empty) the layers become the imports of a synthetic main layer, as upstream's ifcx compose builds it, and every import must be supplied; integrity values are checked against the bytes.

Errors ​

Every failure raises IfcxError with a stable code: read, write, layer, compose, invalid-argument or glb. The reference lists the whole API, generated from the package source.

Released under the MIT licence. No buildingSMART files or texts are published here.