I'm working on a project that creates and manipulates meshes on geometric objects using the finite element method. So far, the user imports a file (.step) and selects areas of the object to create meshes. I'm using the gmsh API and the VTK interface for the software.
componentSelected.meshGeometry.append(filter.GetOutput()) # Add the polygon junction object to the component's mesh geometry list
writer = vtk.vtkSTLWriter() # Create the STL file writing object
writer.SetInputData(filter.GetOutput()) # Set the polygon junction object as the STL file writing object
writer.SetFileName("grid.stl") # Set the output file name as "grid.stl"
writer.Write() # Write the output file
gmsh.initialize() # Initialize Gmsh
gmsh.merge("grid.stl") # Import the mesh file
gmsh.model.mesh.create_edges() # Create mesh edges
gmsh.model.mesh.create_faces() # Create mesh faces
gmsh.model.mesh.create_topology() # Create mesh topology
gmsh.model.mesh.create_geometry() # Create mesh geometry
# gmsh.model.addPhysicalGroup(1, [1], 1) # Define physical group 1 as polygon 1
# gmsh.model.geo.add_curve_loop([1], 1) # Define curve loop 1 as curve 2
gmsh.model.occ.synchronize() # Synchronize geometry
gmsh.model.geo.synchronize() # Synchronize geometry
if meshType == 6:
# If the mesh type is triangular
gmsh.option.set_number('Mesh.Algorithm', meshType) # Set the mesh type to triangular
gmsh.option.setNumber('Mesh.MeshSizeMin', 0) # Set the minimum mesh size
gmsh.option.setNumber('Mesh.MeshSizeMax', 1) # Set the maximum mesh size
elif meshType == 8:
# If the mesh type is quadrangular
gmsh.option.setNumber('Mesh.Algorithm', meshType) # Set the mesh type to quadrangular
gmsh.option.setNumber('Mesh.MeshSizeMin', 0) # Set the minimum mesh size
gmsh.option.setNumber('Mesh.MeshSizeMax', 1) # Set the maximum mesh size
gmsh.option.setNumber('Mesh.RecombineAll', 1) # Set recombination of all meshes to true
gmsh.option.setNumber('Mesh.RecombinationAlgorithm', 3) # Set the mesh recombination algorithm to "blossom full-quad"
gmsh.option.setNumber("Mesh.SubdivisionAlgorithm", 1) # Set the mesh subdivision algorithm to "all quadrangles"
gmsh.model.geo.synchronize() # Synchronize geometry
gmsh.model.mesh.generate(2) # Generate the mesh
gmsh.write("mesh.vtk") # Write the mesh in VTK format
gmsh.finalize() # Finalize Gmsh
reader = vtk.vtkDataSetReader() # Create the VTK file reading object
reader.SetFileName("mesh.vtk") # Set the input file name as "mesh.vtk"
reader.Update() # Update the VTK file reading object
grid = reader.GetOutput() # Set the VTK file reading object as the mesh object
filter = vtk.vtkUnstructuredGridGeometryFilter() # Create the unstructured mesh geometry filtering object
filter.SetInputData(grid) # Set the mesh object as the unstructured mesh geometry filtering object
filter.Update() # Update the unstructured mesh geometry filtering object
edges = vtk.vtkExtractEdges() # Create the edges extraction object
edges.SetInputConnection(filter.GetOutputPort()) # Set the unstructured mesh geometry filtering object as the edges extraction object
edges.Update() # Update the edges extraction object
try:
componentSelected.meshGeometry[main.selectedId] = grid # Set the mesh object as the component's mesh geometry
mapper = vtk.vtkDataSetMapper() # Create the mesh data mapping object
mapper.SetInputData(edges.GetOutput()) # Set the edges extraction object as the mesh data mapping object
mapper2 = vtk.vtkDataSetMapper() # Create the mesh data mapping object
mapper2.SetInputData(grid) # Set the mesh object as the mesh data mapping object
actor = vtk.vtkActor() # Create the actor object
actor.SetMapper(mapper) # Set the mesh data mapping object as the actor object
actor.GetProperty().SetColor(0, 0, 0) # Set the color of the actor object to black
actor.GetProperty().SetInterpolationToFlat()
actor.GetProperty().EdgeVisibilityOn() # Set the visibility of the actor's edges to true
actor2 = vtk.vtkActor() # Create the actor object
actor2.SetMapper(mapper2) # Set the mesh data mapping object as the actor object
actor2.GetProperty().SetInterpolationToFlat()
actor2.GetProperty().SetColor(componentSelected.redColor, componentSelected.greenColor, componentSelected.blueColor) # Set the color of the actor object to the component's color
componentSelected.meshActor.append(actor) # Add the actor object to the list of component mesh actors
componentSelected.geometryMeshActor.append(actor2) # Add the actor object to the list of component geometry mesh actors
main.ren.AddActor(actor) # Add the actor object to the scene
main.componentId = None # Set the component identifier to null
main.selectedId += 1 # Increment the selection identifier
# Add the mesh to the scene
for actor in componentSelected.meshActor:
main.ren.AddActor(actor)
main.vtkWidget.GetRenderWindow().Render()
main.selectedComponents.clear() # Clear the list of selected components
main.selectedCells.clear() # Clear the list of selected cells
return
except Exception as error:
# If the mesh is not generated
message = QtGui.QMessageBox()
message.setText(f"error: {error}")
message.exec()
return
To connect a mesh using Gmsh and visualize it using VTK in Python, you can use the
pygmsh
library to generate the mesh with Gmsh and then usepyvista
orvtk
to visualize it.First, make sure you have
pygmsh
,pyvista
, andvtk
installed in your Python environment. You can install them using pip:Here's an example of how you can generate a simple mesh with Gmsh and visualize it using VTK:
This example generates a simple circular mesh using Gmsh through
pygmsh
, extracts the points and cells from the generated mesh, and then creates apyvista
mesh (pv.PolyData
) using those points and cells. Finally, it usespyvista
to visualize the mesh.You can modify the geometry and mesh generation process in the
create_mesh
function according to your specific requirements. Thepygmsh
library offers various geometric primitives and methods to create complex geometries.Remember to adjust the mesh generation and visualization process as needed based on your specific mesh requirements and geometry.