Thanks a lot!
I have tried using GMSH to open a stl file, the code is here:
def add_physical_for_gmsh(ifile, ofile = None):
gmsh.initialize()
gmsh.open(ifile)
gmsh.model.geo.synchronize()
volumes = gmsh.model.getEntities(dim=3)
if volumes:
physical_group_tag = gmsh.model.addPhysicalGroup(3, [volume[1] for volume in volumes], name="AllVolumes")
else:
raise RuntimeError()
if not ofile:
ofile = ifile
gmsh.write(ofile)
gmsh.finalize()
def createGeometryAndMesh(stlfile, outfile, angle=40, mesh_size_factor=1.0, includeBoundary = True, forceParametrizablePatches=True):
gmsh.clear()
gmsh.initialize()
gmsh.merge(stlfile)
gmsh.model.occ.removeAllDuplicates()
gmsh.model.mesh.classifySurfaces(angle * math.pi / 180., includeBoundary,
forceParametrizablePatches,
180 * math.pi / 180.)
gmsh.model.mesh.createGeometry()
gmsh.model.geo.synchronize()
s = gmsh.model.getEntities(2)
l = gmsh.model.geo.addSurfaceLoop([e[1] for e in s])
gmsh.model.geo.addVolume([l])
gmsh.model.geo.synchronize()
volumes = gmsh.model.getEntities(dim=3)
physical_group_tag = gmsh.model.addPhysicalGroup(3, [volume[1] for volume in volumes], name="AllVolumes")
gmsh.model.geo.synchronize()
gmsh.option.setNumber("Mesh.MeshSizeFactor", mesh_size_factor)
gmsh.model.mesh.generate(3)
gmsh.option.setNumber("Mesh.Optimize", 1)
gmsh.model.mesh.optimize("Netgen")
gmsh.model.geo.synchronize()
gmsh.write(outfile)
gmsh.finalize()
stl_filename = '1.stl'
msh_filename = '1.msh'
createGeometryAndMesh(stl_filename, msh_filename)
add_physical_for_gmsh(msh_filename, ofile = None)
print('add_finished')
domain, cell_markers, facet_markers = dfx.io.gmshio.read_from_msh(msh_filename, MPI.COMM_WORLD, gdim=3)
print('ok')
add_finished
has been printed, while the ok
is not been printed.The error info is shown as follows:
Info : [ 90%] Processing parametrizations
Info : Done reading '1.msh'
Segmentation fault
I wonder if it is the problem with my computer or with the structure 1.stl
,which I have shown before.
And I have also tried other stl file using this code. And the error info went like this:
Traceback (most recent call last):
File "/zjlab/create_mesh_createGeometryAndMesh.py", line 79, in <module>
createGeometryAndMesh(stl_filename, msh_filename)
File "/zjlab/create_mesh_createGeometryAndMesh.py", line 65, in createGeometryAndMesh
gmsh.model.mesh.generate(3)
File "/root/miniconda3/lib/python3.12/site-packages/gmsh.py", line 2060, in generate
raise Exception(logger.getLastError())
Exception: Invalid boundary mesh (overlapping facets) on surface 74 surface 141
maybe this is related to the problems of the structure.
And I have also tried tetwild, which can implement format conversion successfully. However, I can’t find the python-api of the tetwild and I have thousands of stl files to deal with.
So could you plz help me with this question, or do you have any good ideas to batch convert stl files?