You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为VTK Point自定义ID?Python FE数据转VTK网格问题

解决VTK自定义点ID的问题

问题根源

VTK的点ID是内部维护的连续整数索引(范围为0 <= id < GetNumberOfPoints()),vtkSetPoint仅支持使用这个内部索引,无法直接设置非连续的自定义FE模型ID。要保留自定义ID,需将其作为点属性数据附加到VTK网格中。

实现步骤

  1. 创建VTK点集合,按顺序添加所有点(使用VTK内部连续索引)
  2. 将自定义ID数组转换为VTK数据数组,添加到点数据(PointData)中
  3. 构建非结构化网格并导出VTK文件

完整代码示例

import numpy as np
import vtk
from vtk.util.numpy_support import numpy_to_vtk

# 输入的FE模型数据
points = np.array([
    [0.0, 0.0, 0.0],
    [1.0, 0.0, 0.0],
    [1.0, 1.0, 0.0],
    [0.0, 1.0, 0.0],
    [0.0, 0.0, 1.0],
    [1.0, 0.0, 1.0],
    [1.0, 1.0, 1.0],
    [0.0, 1.0, 1.0]
])

ids = np.array([10, 21, 22, 23, 34, 15, 36, 7])

# 1. 初始化VTK点集合,按顺序插入所有点
vtk_points = vtk.vtkPoints()
for idx, coord in enumerate(points):
    vtk_points.InsertPoint(idx, coord)

# 2. 将自定义ID转为VTK数组并命名,作为点属性添加
vtk_custom_ids = numpy_to_vtk(ids, deep=True, array_type=vtk.VTK_INT)
vtk_custom_ids.SetName("FE_Point_ID")  # 设置属性名,方便后续识别

# 3. 创建非结构化网格并关联点和属性
unstructured_grid = vtk.vtkUnstructuredGrid()
unstructured_grid.SetPoints(vtk_points)
unstructured_grid.GetPointData().AddArray(vtk_custom_ids)

# (可选)添加单元示例:创建一个六面体单元,使用VTK内部点索引
hex_cell = vtk.vtkHexahedron()
for i in range(8):
    hex_cell.GetPointIds().SetId(i, i)
unstructured_grid.InsertNextCell(hex_cell.GetCellType(), hex_cell.GetPointIds())

# 4. 导出VTK文件
writer = vtk.vtkUnstructuredGridWriter()
writer.SetFileName("fe_model_with_custom_ids.vtk")
writer.SetInputData(unstructured_grid)
writer.Write()

验证方式

用ParaView打开导出的VTK文件后,选择网格并打开Point Data面板,即可看到名为FE_Point_ID的属性字段,其值完全对应FE模型的自定义ID。

内容的提问来源于stack exchange,提问作者RaHeSch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 21:55:48