Trying to get IfcOpenShell running in RhinoCode

Hello @eirannejad (and everybody else :slight_smile: )

following these examples, I managed to get basic IfcOpenShell queries running in RhinoCode (in Rhino 8.8 SRC from yesterday). I manually downloaded IfcOpenShell for Python 3.9 from here and installed it into .rhinocode in my user folder.
Now I’m trying to get into Geometry Processing via IfcOpenShell… Follwoing this guide, I tried to get the following code snippet running:

import ifcopenshell
import ifcopenshell.geom

ifc_file = ifcopenshell.open('model.ifc')
element = ifc_file.by_type('IfcWall')[0]

settings = ifcopenshell.geom.settings()
settings.set(settings.USE_PYTHON_OPENCASCADE, True)

try:
    shape = geom.create_shape(settings, element)
    geometry = shape.geometry # see #1124
    # These are methods of the TopoDS_Shape class from pythonOCC
    shape_gpXYZ = geometry.Location().Transformation().TranslationPart()
    # These are methods of the gpXYZ class from pythonOCC
    print(shape_gpXYZ.X(), shape_gpXYZ.Y(), shape_gpXYZ.Z())
except:
    print("Shape creation failed")

Unfortunately, to be able to access settings.USE_PYTHON_OPENCASCADE depends on pythonOCC, a package for which there only seems to be a Conda install avaliable (see PythonOCC getting started guide).

Searching for solutions how to use Conda in RhinoCode I found your post on how to point RhinoCode to an existing Conda Environment. So using MiniConda I installed both IfcOpenShell and pythonOCC-core into a new Conda Environment:

conda create -n openshell python=3.9
conda activate openshell
conda install -c conda-forge ifcopenshell pythonocc-core

Then I pointed RhinoCode to that Environment via a new path entry in the Editor Options and using

# env: C:\Users\[username]\miniconda3\envs\openshell\Lib\site-packages

IfcOpenShell seems to be found flawlessly that way, unfortunately the error with the missing dependency remains. I boiled the script down to the following 5 lines to be able to cath the error more easily:

Then I tried it the other way round and executed the exact same script (without the line with # env) directly in the Conda Environment Python. There it worked flawlessly and I could see USE_PYTHON_OPENCASCADE being an element of the settings object.

Please excuse this lengthy description, I’m a simple python scripter and not too much into things like PIP and Conda so I tried to bite my way through… Unfortunately I’m now stuck and don’t know how to continue. Do you maybe have a hint on how I could get this to work? When I managed to get it running in the Conda Environemnt I felt like very close to the solution… but I don’t know to take it over to Rhino…

It would be great if you could give me a hint on how to move on… Thanks!

IFCOpenShell has some dependencies. We do not find the Pypi build to be that good. Here is a thread on it:

Hello Scott,
thank you for this link, I’ve seen the thread you’re linking to when searching for ways to get the first Examples on their page running. I managed to get IfcOpenShell running in a similar way @eirannejad and you describe it in that thread.

The issue I have now is when using geometry processing functionality of IfcOpenShell even more dependencies are used (eg pythonOCC) and I have difficulties installiong them as that seems only to be possible via Conda.

I think I got quite far but I’ missing the last steps - as described above.

Maybe someone has a hint? Thank you!

@romio82

I did a little search and pythonocc seems to be only available on conda (There is a very very old version on pypi). You should be able to wire up the conda environment as shown in this thread to the Rhino Python 3 runtime and import the pythonocc package:

#! python 3
import os
import os.path as op
import sys
import ctypes


def connect_conda(conda_env_path: str):
    """Wire up search paths to conda environment
    
    Args:
        conda_env_path (str): absolute root path of conda environment
    """
    # add the paths of site-packages in conda environment so packages can be found e.g. compas
    sys.path.append(op.join(conda_env_path, r"Lib\site-packages"))

    # tell python where it can find dlls. this is required to find all other .dll files that
    # are installed as part of the other packages in the conda environment e.g. fblas
    os.add_dll_directory(op.join(conda_env_path, r'Library\bin'))


connect_conda(r'C:\Users\ein\.conda\envs\wood-dev')

import OCC

print(OCC)

See more information here:

1 Like

Thank you @eirannejad,
that’s a huge step forward! Following the steps you describe I’m now able to import OCC and also a few dependencies needed by IfcOpenShell such as OCC.Core.BRepTools.

Unfortunately if I now import ifcopenshell.geom which is needed to do Geometry Processing in IfcOpenShell, within its .__init__ it tries to do
from OCC.Core import V3d, TopoDS, gp, AIS, Quantity, BRepTools, Graphic3d
and fails with that:

All of these packages are in place within site-packages.OCC.Core in the Conda environment, and importing ifcopenshell.geom from the environemt’s python works flawlessly - even importing as example OCC.Core.TopoDS directly from the script works as you can see in the screenshot. But called from the dependency inside the Rhino script these packages seem to be outside of scope.

My first guess would be that the subfolders of site-packages should also be appended to sys.path, is there a way to do this? Or do you see another reason why this happens?

Thank you very much for your support, I appreciate it a lot!!

@romio82

The error is related to this:

from OCC.Core import _V3d

as it throws a DLL load exception. note the The specified procedure could not be found part

Traceback (most recent call last):
  File "file:///C:/Users/ein/Downloads/test_conda_pythonocc_ifcopenshell.py", line 26, in <module>
ImportError: DLL load failed while importing _V3d: The specified procedure could not be found.

Seems like there is conflict with one of the core native dlls that makes IlmThread.dll fail to load. I will take a deeper look

Thank you for investigating, @eirannejad!
Looking forward to what you’ll find out!

Probably conflicting with IlmThread.dll loaded by RhinoCycles.

1 Like