Introduction to scripting with Ifcopenshell in system environment

Introduction to scripting with Ifcopenshell in system environment#

In this activity you will set up the necessary environment for development using Python and IfcOpenShell. You will create a minimal script counting the amount of walls found in a given IFC-STEP model.

Knowledge of Python is assumed.

Preparing development environment#

Using Anaconda for managing your Python environment is recommended for advanced users. When installing Anaconda, make sure to check the Add Anaconda to my PATH environment variable-box.

Detailed instructions for installation is available at Install Ifcopenshell.

  • Make sure you have an appropriate text-editor installed. Vscode is recommended.

  • Make sure you have a recent version of Python installed. Python 3.11 is recommended.

  • Install ifcopenshell either by:

    • PIP: In your CLI run pip install ifcopenshell.

    • Conda: conda install -c conda-forge ifcopenshell pythonocc-core. This includes OpenCascade which may be useful for geometric operations further down the road.

Hello ifcopenshell#

Here is a minimal example counting the amount of wall entities found in a model:

import ifcopenshell
model = ifcopenshell.open('/path/to/your/model.ifc')

walls = model.by_type('IfcWall')
print(f"Walls in model: {len(walls)}")

Save the above code in a .py file and then execute it. Watch the command line output to read the wall count.

Attention

Beware of absolute and relative file paths! If pointing to your model using a relative path, the path is relative to the working directory of which you are executing the code, and not the path of the .py file itself.