如何在Python中使用gmsh和vtk连接网格

问题描述 投票:0回答:1

我正在开发一个使用有限元方法创建和操作几何对象上的网格的项目。到目前为止,用户导入文件 (.step) 并选择对象区域来创建网格。我正在使用该软件的 gmsh API 和 VTK 接口。

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

unconnected meshes in a .step extractor fan

python mesh vtk finite-element-analysis gmsh
1个回答
0
投票

要使用 Gmsh 连接网格并使用 Python 中的 VTK 对其进行可视化,您可以使用

pygmsh
库通过 Gmsh 生成网格,然后使用
pyvista
vtk
对其进行可视化。

首先,确保您的 Python 环境中安装了

pygmsh
pyvista
vtk
。您可以使用 pip 安装它们:

pip install pygmsh pyvista vtk

下面是如何使用 Gmsh 生成简单网格并使用 VTK 将其可视化的示例:

import pygmsh
import pyvista as pv

# Create a function to define a simple mesh using pygmsh
def create_mesh():
    geom = pygmsh.built_in.Geometry()

    # Define a circle
    geom.add_circle([0.0, 0.0, 0.0], 1.0)

    # Generate the mesh
    mesh = pygmsh.generate_mesh(geom)
    return mesh

# Create the mesh using pygmsh
mesh = create_mesh()

# Extract points and cells from the mesh
points = mesh.points
cells = mesh.cells_dict["triangle"]

# Create a PyVista mesh from the points and cells
pv_mesh = pv.PolyData(points, cells)

# Create a plotter and visualize the mesh
plotter = pv.Plotter()
plotter.add_mesh(pv_mesh, show_edges=True)
plotter.show()

此示例使用 Gmsh 通过

pygmsh
生成一个简单的圆形网格,从生成的网格中提取点和单元,然后使用这些点和单元创建
pyvista
网格 (
pv.PolyData
)。最后,它使用
pyvista
来可视化网格。

您可以根据您的具体要求在

create_mesh
功能中修改几何形状和网格生成过程。
pygmsh
库提供了各种几何基元和方法来创建复杂的几何形状。

请记住根据您的特定网格要求和几何形状根据需要调整网格生成和可视化过程。

© www.soinside.com 2019 - 2024. All rights reserved.