Export to STL / Read Netgens Neutral Format


I’m looking for a way to transform STEP Files to lowpoly 3DFiles (f.e. STL). With netgen’s GUI I received excellent results. Now I’m working on a way to do that via command line. I found an solution to transform STEPs to the Netgen neutral format with python. As far as good, I want to test around with the available meshing paramters, but I have problems to examine the neutral format. Where is it loadable? Or my generated file is corrupt, I dont know.
Is there are solution for exporting STL Files?

Here my solution and many thanks for help!!

→ convert.py

FREECADPATH = ‘/usr/share/netgen’
import sys

import netgen.meshing as meshing
from netgen.meshing import MeshingParameters
from netgen.csg import *
from netgen.NgOCC import *

geo = LoadOCCGeometry(‘Part_003.step’)

m1 = geo.GenerateMesh(maxh=1000, perfstepsend=meshing.MeshingStep.MESHSURFACE)

import exportNeutral
exportNeutral.Export (m1, “shaft.mesh”)

→ exportNeutral.py

import sys

def Export (mesh, filename):
“”" export Netgen mesh to neutral format “”"

print ("export mesh in neutral format to file = ", filename)

f = open (filename, 'w')

points = mesh.Points()
print (len(points), file=f)
for p in points:
    print (p.p[0], p.p[1], p.p[2], file=f)

volels = mesh.Elements3D();
print (len(volels), file=f)
for el in volels:
    print (el.index, end="   ", file=f)
    for p in el.points:
        print (p.nr, end=" ", file=f)

Hi Matt,

I tested your Export function like this:

mesh = Mesh(unit_cube.GenerateMesh(maxh=0.5))
Export(mesh.ngmesh, ‘neutral.mesh’)

When I import into Netgen, it doesn’t give any errors

[color=green]Reading Neutral Format
21 Points, 28 Elements.
import mesh from /home/dow/python/ngsolve_notebooks/neutral.mesh
Read User File
But I don’t see the mesh when it is selected. However, if I select ‘edges’, I can see small squares indicating the vertices.

This document on page 20 specifies that the surface elements should also be listed (after the volume elements). I modified the Export function to include that information (see attached), but the imported file still doesn’t display for me.

Also, when I tried to import from the OCC module, I got package not found.

[spoiler]----> 1 from netgen.NgOCC import *

~/ngsuite/ngsolve-install/lib/python3/dist-packages/netgen/NgOCC.py in ()
----> 1 from netgen.libngpy._NgOCC import *
2 from netgen.libngpy._meshing import MeshingParameters
4 def NgOCC_meshing_func (geom, **args):
5 if “mp” in args:

ModuleNotFoundError: No module named ‘netgen.libngpy._NgOCC’; ‘netgen.libngpy’ is not a package[/spoiler]

That seems odd , since I’m running a build based on the latest source and I can see NgOCC.py in the source tree.

EDIT: The ModuleNotFoundError was because I didn’t have Open Cascade support installed and had not specified -DUSE_OCC=ON when configuring NGSolve before building from source.

I found all the information here: CMake options

Attachment: exportNeutral.py

I found a little more information in the forum on this and by some testing.
You can do this in Python:

from netgen.csg import unit_cube mesh = unit_cube.GenerateMesh(maxh=.5) mesh.Export('cube.mesh','Neutral Format')
There are several other formats available. If you give an invalid format it lists all the valid format names. If I try to load ‘cube.mesh’ in Netgen, it behaves the same as the neutral format files generated by hand before. Nothing shows when I select to view the mesh, but vertices show if I select to view edges. I think the file must be valid, because if I do this:

import ngsolve mesh = ngsolve.Mesh('cube.mesh') mesh.ngmesh.Save('cube.vol')
I get a valid mesh that will load correctly into Netgen. I think there may be a display issue in Netgen with the neutral format.


the neutral format which is exported is not exactly the same which is expected by the import function.
The import/export is thought for coupling to other software packages.

If you just want to save the mesh, you can use the save function ddrake proposed.

There is also an option to export in STL format

mesh.Export('filename','STL Format')


Hello Christoph,

many thanks for this hint, it’s a very simple solution for my problem. It works this way! Here is my final script:

FREECADPATH = ‘/usr/share/netgen’
import sys

import netgen.meshing as meshing
from netgen.meshing import MeshingParameters
from netgen.csg import *
from netgen.NgOCC import *

geo = LoadOCCGeometry(‘models/Part_008.step’)

m1 = geo.GenerateMesh(maxh=1000, perfstepsend=meshing.MeshingStep.MESHSURFACE, grading=0.3, curvaturesafety=0.7)

m1.Export(‘shaft.stl’,‘STL Format’)


ps: I tested around with the parameters above (maxh, perfstepsend, grading and curvaturesafety) to get the mesh coarser, but I get everytime the same result. I will continue testing, maybe you have experience with this?!

Hi Matthias,

some of the parameters you tested are not properly forwarded to meshing functions. Thus you got the same mesh all the time.

I made a quick fix and submitted a merge request. I’ll come back to you when it is merged or properly fixed.