IfcOpenShell

From FreeCAD Documentation
Revision as of 19:56, 23 July 2020 by Renatorivo (talk | contribs) (Marked this version for translation)
Other languages:

Description

IfcOpenShell is an open source (LGPL 3) software library that helps developers work with the industry foundation classes (IFC) file format. The IFC file format can be used to describe building and construction data. The format is commonly used for building information modelling (BIM), for example, mechanical loading analysis and thermal studies. IfcOpenShell is primarily a collection of C++ libraries, however, as it has Python bindings, it can be easily integrated with programs like FreeCAD and Blender.

IfcOpenShell uses OpenCASCADE internally to convert the implicit geometry in IFC files into explicit geometry that other CAD packages can understand.

As of v0.19, FreeCAD is able to import IFC files as long as the ifcopenshell Python module is available in the system. Likewise, the Arch and BIM Workbenches can export a building model to the IFC format so that it can be used in other applications.

To verify that IfcOpenShell is installed in your system, try to import it from the Python console; the library is correctly installed if no error message is returned.

import ifcopenshell

Installation

IfcOpenShell can be installed in various ways depending on your operating system and Python environment.

Conda

For Windows and MacOS systems, FreeCAD distributions put together with the Conda package manager usually include IfcOpenShell already so no further installation is necessary. Get the appropriate distribution from the Download page.

The AppImage for Linux is also based on Conda, and it also includes IfcOpenShell.

Linux

You may install IfcOpenShell from your distribution's package manager, if it is available.

sudo apt install ifcopenshell

However, please notice that packages provided by a Linux repository tend to be old, and not contain the latest developments in the software. If you want to be sure you are using the newest software, use a Conda-based distribution of FreeCAD, a pre-compiled IfcOpenShell distribution, or compile IfcOpenShell yourself.

Using a pre-compiled IfcOpenShell package

There is a special repository of the IfcOpenShell project that compiles regularly the IfcOpenShell libraries for various systems (Linux, Windows, MacOS), architectures (32-bit and 64-bit), and Python versions (2.7, 3.x). To use these pre-compiled libraries, you must pick the right version that matches your operating system, architecture, and the major and minor numbers for the Python that is used with FreeCAD. This means that the first two numbers must match (Python 3.6 and 3.7 are considered distinct versions) while the third one (micro) does not matter (Python 3.6.5 and 3.6.12 are considered to be the same for compatibility purposes).

  1. Head to the build repository IfcOpenBot/IfcOpenShell. This repository is not for development, it only contains a copy of the main repository as well as pre-compiled packages.
  2. As of this writing (2020), the master branch of the IfcOpenShell project does not contain the latest code, so we need to select the desired branch, for example, v0.6.0.
  3. Click on the commit number, which will take you to the list of commits for the branch, for example, IfcOpenBot/IfcOpenShell/commits/v0.6.0.
  4. Go back in the history until you find a commit that has a comment. This will indicate the moment when pre-compiled libraries were released.
  5. Click on the commit. You will see a comment by IfcOpenBot showing a table of combinations of operating system, architecture, and Python version. Choose the right link for "Python" to match your version of FreeCAD. The "Blender", "IfcConvert", and "IfcGeomServer" packages are not needed for FreeCAD usage.
  6. The downloaded package needs to be extracted, and the extracted directory needs to be moved to a directory that is searched by Python to find modules.
unzip ifcopenshell-python-36-v0.6.0-4baec57-linux64.zip
  • The search path can be found by inspecting the sys.path variable in the Python console.
import sys
print(sys.path)
  • If you'd like to install the IfcOpenShell library only for your normal user, and not affect system libraries, you should place the extracted ifcopenshell/ folder in the user's home directory.
mv -t $HOME/.local/lib/python3.6/site-packages/ ifcopenshell
  • If you'd like to install the IfcOpenShell library system-wide, you typically need superuser privileges to write to system directories; this is usually a site-packages directory, or a dist-packages directory for Debian/Ubuntu distributions.
sudo mv -t /usr/local/lib/python3.6/dist-packages/ ifcopenshell

If the directory is correctly moved, test that the ifcopenshell module is accessible from the Python console.

>>> import ifcopenshell
>>> print(ifcopenshell.version)
0.6.0b0

To remove the installed library, just remove the corresponding directory.

rm -rf $HOME/.local/lib/python3.6/site-packages/ifcopenshell/
sudo rm -rf /usr/local/lib/python3.6/dist-packages/ifcopenshell/

Compiling

Compiling IfcOpenShell is recommended only for advanced users. The process is similar to compiling FreeCAD on Linux, so if you have done this already, then you may already have the necessary requisites like OpenCASCADE's development files. The process uses the CMake configuration tool to produce a custom Makefile for use with the Make tool. The general instructions are outlined in the IfcOpenShell code repository.

The general steps are as follows:

  1. Get the source code of IfcOpenShell from its main repository.
  2. Gather all dependencies for compiling, including installing a C++ compiler, CMake, and Make, and the development files for Boost, libxml2, OpenCASCADE, SWIG, Python, and OpenCOLLADA (optional). Most of these components are strictly optional, however, for use with FreeCAD they should all be installed except for OpenCOLLADA, as it only provides support for DAE files.
  3. Run cmake to generate a Makefile, then compile the libraries by running make.
  4. Install the ifcopenshell Python module in the appropriate site-packages directory so that it is found by FreeCAD.

Prerequisites

Get the source code of the project and place it in a custom directory to which you have full write access.

As of this writing (2020), the master branch of the IfcOpenShell project does not contain the latest code, so we need to clone a specific branch.

git clone https://github.com/IfcOpenShell/IfcOpenShell -b v0.6.0 ifcopenshell-source

Install the basic compilation tools.

sudo apt install git cmake gcc g++ libboost-all-dev

OpenCASCADE

Install the development files of OpenCASCADE.

sudo apt install libocct*-dev

Which expands to

sudo apt install libocct-data-exchange-dev libocct-draw-dev libocct-foundation-dev libocct-modeling-algorithms-dev libocct-modeling-data-dev libocct-ocaf-dev libocct-visualization-dev

You may use the community edition (OCE) of OpenCASCADE as well, however, please notice that this version is old and no longer recommended by FreeCAD as of 2020.

Python wrapper

For usage with FreeCAD you need the Python wrapper which uses SWIG to generate the proper interfaces from the C++ classes.

sudo apt-get install python-all-dev swig

CMake configuration

It is recommended to perform the compilation in a specific build directory.

mkdir -p ifcopenshell-build
cd ifcopenshell-build

cmake ../ifcopenshell-source/cmake/

Notice that the CMakeLists.txt file that drives CMake is inside the cmake/ directory.

Depending on your Linux distribution, and the way you installed the dependencies, you may have to define some CMake variables so that the proper libraries are found.

Specifying the OpenCASCADE libraries

If you manually compiled OpenCASCADE, or if the libraries are not in a standard directory, you may have to set the proper variables.

cmake \
    -DOCC_INCLUDE_DIR=/usr/include/opencascade \
    -DOCC_LIBRARY_DIR=/usr/lib/x86_64-linux-gnu \
    ../ifcopenshell-source/cmake/

Without OpenCOLLADA

If you don't need OpenCOLLADA support you need to turn it off explicitly.

cmake \
    -DOCC_INCLUDE_DIR=/usr/include/opencascade \
    -DOCC_LIBRARY_DIR=/usr/lib/x86_64-linux-gnu \
    -DCOLLADA_SUPPORT=FALSE \
    ../ifcopenshell-source/cmake/

With OpenCOLLADA

If you manually compiled OpenCOLLADA, or if the libraries are not in a standard directory, you may have to set the proper variables.

cmake \
    -DOCC_INCLUDE_DIR=/usr/include/opencascade \
    -DOCC_LIBRARY_DIR=/usr/lib/x86_64-linux-gnu \
    -DOPENCOLLADA_INCLUDE_DIR=/usr/local/include/opencollada \
    -DOPENCOLLADA_LIBRARY_DIR=/usr/local/lib/opencollada  \
    -DPCRE_LIBRARY_DIR=/usr/lib/x86_64-linux-gnu/ \
    ../ifcopenshell-source/cmake/

Specifying the libxml2 libraries

If the libxml2 libraries are not found, or if the libraries are not in a standard directory, you may have to set the proper variables.

cmake \
    -DOCC_INCLUDE_DIR=/usr/include/opencascade \
    -DOCC_LIBRARY_DIR=/usr/lib/x86_64-linux-gnu \
    -DCOLLADA_SUPPORT=FALSE \
    -DLIBXML2_INCLUDE_DIR=/usr/include/libxml2 \
    -DLIBXML2_LIBRARIES=/usr/lib/x86_64-linux-gnu/libxml2.so \
    ../ifcopenshell-source/cmake/

Specifying user installation directory

cmake \
    -DOCC_INCLUDE_DIR=/usr/include/opencascade \
    -DOCC_LIBRARY_DIR=/usr/lib/x86_64-linux-gnu \
    -DCOLLADA_SUPPORT=FALSE \
    -DLIBXML2_INCLUDE_DIR=/usr/include/libxml2 \
    -DLIBXML2_LIBRARIES=/usr/lib/x86_64-linux-gnu/libxml2.so \
    -DUSERSPACE_PYTHON_PREFIX=ON \
    ../ifcopenshell-source/cmake/

Troubleshooting and other options

All configuration options are available in the CMakeLists.txt file located inside the ifcopenshell-source/cmake/ directory. You may look for other options that can be set here if there are problems when running CMake.

Compiling

If there were no error messages you can proceed to compile the libraries by running make, where N is the number of processors that you want to assign to the compilation. Choose at least one less than the total number of CPU cores that you have.

make -j N

Installing

If the compilation doesn't report any error, you may install the compiled shared and static libraries, as well as the Python wrapper to the corresponding directories.

sudo make install

By default, the CMAKE_INSTALL_PREFIX is /usr/local, so all compiled files will be installed to this directory, which normally requires elevated privileges.

/usr/local/bin
/usr/local/include
/usr/local/lib

In similar way, the Python wrapper will be placed in a site-packages directory, or a dist-packages directory for Debian/Ubuntu distributions.

/usr/lib/python3/dist-packages/ifcopenshell/

More information