Conflicts between Netgen and FreeCAD's Fem module

Hi all,

A quick introduction first. FreeCAD has an FEM module that implement two different meshers : GMSH and Netgen. Unfortunately, refinement and parameters tuning for Netgen cannot be done directly from FreeCAD’s UI (nor python commands as it uses a pre-compiled version). As I want to perform my meshing using Netgen, I implemented a python script to perform meshing using Netgen’s python functions, linked to a FreeCAD file.
This is a pretty straightforward process and the only functions I use from netgen are:

geo = OCCGeometry(stp_file)
param = MeshingParameters(...params...)

And this works great ! I can control my Netgen parameters and apply refinement wherever I think relevant.
Unfortunately, once this work done, I try to merge it with my Fem scripts. There seems to be a conflict between Netgen and FreeCAD’s Fem module. During import, depending on which module I import first, the following errors are triggered :

Traceback (most recent call last):
  File "c:/Users/username/AppData/Roaming/FreeCAD/Macro/", line 120, in <module>
    import netgen
  File "C:\Users\username\AppData\Local\Programs\FreeCAD 0.21\bin\lib\site-packages\netgen\", line 44, in <module>
    from . import libngpy
ImportError: DLL load failed while importing libngpy: The specified procedure could not be found.


Traceback (most recent call last):
  File "c:/Users/username/AppData/Roaming/FreeCAD/Macro/", line 11, in <module>
    import Fem
ImportError: DLL load failed while importing Fem: The specified procedure could not be found.

To execute python code, FreeCAD uses python 3.8.17. I created a venv in VSC that takes FreeCAD’s python executable to run scripts. Both individual scripts run perfectly in said venv, but when I add the missing import, both then fail with the errors here-above.

Therefore here are my questions :

  • What is the python version that netgen uses ? Could it be that this is an issue ? Maybe I should install the same netgen version as FreeCAD is using (I don’t see how this could cause conflicts as FreeCAD uses a pre-compiled version as said).
  • Is there anywhere I could retrieve only the functions/class I need ? Maybe by getting a narrowed-down version of netgen I could get this to work.
  • Any other idea on how to solve this compatibility issue ?

As both modules work perfectly individually, i’ve excluded the possibility of missing dependencies, but maybe I am wrong ?

Even if you don’t have a hands-in complete solution, any potential hint on how to solve this would already be of great help! I cannot seem to find any similar cases on FreeCAD’s forum…

EDIT: Maybe running a precompiled version of netgen through the subprocess module could be a solution ? I am very new at building from source. Is there a way to create an executable that’d take as arguments a step file and a meshing_parameters.txt file (which would include the path to the mesh refinement file, as currently done in python) ?

Thanks a lot !

I don’t know much about the freecad integration, but you can install netgen/ngsolve with any “modern” python version just as a pip package, so this should not be much of a problem.
I guess if you run your script in a subprocess with python there should be less chance of an error?

But else I’d more try to ask this on the freecad forum as there might be more people having experience with this…

Hi Christopher,

Thanks for your help !

Indeed, i’m trying to solve this on the FreeCAD forum in parallel as well, I figured that maybe someone here would have encountered a similar error.

Regarding the subprocess option, i didn’t think about running another python script with this tool ! This should work as modules would be loaded independently. I’ll keep looking for a more comprehensive solution (because i’d like to understand why this error is triggered !) and if nothing works, I’ll keep this in mind as backup.

If i find the solution, i’ll come back here to give you guys an update, in case this happens to someone else :slight_smile:

Thanks again and all the best.

I meant putting this into a python file and calling that python file in a subprocess. then just a mesh save (or export to another format) and read the mesh back into freecad. This should be quite straightforward and maybe solve the problem.

Hi !

Indeed, your solution would’ve worked as well.

For completion sake, we found an issue in the python’s version FreeCAD was using depending on installs on Windows.

When using the 64-bit installer, the wrong python version would be installed alongside FC ( Whereas using the .7z installation installs python

This is what was seemingly causing the issue. We couldn’t find any other explanation.

Investigation here : Conflict between Fem and netgen package - FreeCAD Forum.

Thanks again Christopher !

All the best.