0

我需要用 python 编写一个 xml vtk 文件。特别是在结构化网格中。我有 x、y、z 轴的坐标以及空间中每个点的 x、y、z 分量的值。我怎样才能写这个文件?

4

2 回答 2

1

(编辑)你可以通过这个函数来做到这一点(在python中)

def writeVtkStructuredGrid(StructuredGrid, file_name):
    """
    StructuredGrid  : StructuredGrid input object
    file_name   : output xml path file (example: 'out.vtp')
    """

    sg = vtkXMLStructuredGridWriter()
    sg.SetFileName(file_name)
    sg.SetInput(StructuredGrid)
    sg.Write()

关于 vtkXMLPolyDataWriter 对象的文档位于: vtkXMLPolyDataWriter 类参考

于 2013-02-06T16:16:30.417 回答
-1

请检查此 Stackoverflow 链接,调整注释代码,您可以完成工作。StackOverflow 链接来自tvtk的具体例子

示例代码是

from numpy import mgrid, empty, sin, pi
from tvtk.api import tvtk, write_data

# Generate some points.
x, y, z = mgrid[1:6:11j, 0:4:13j, 0:3:6j]
base = x[..., 0] + y[..., 0]
# Some interesting z values.
for i in range(z.shape[2]):
        z[..., i] = base * 0.25 * i

# The actual points.
pts = empty(z.shape + (3,), dtype=float)
pts[..., 0] = x
pts[..., 1] = y
pts[..., 2] = z

# Simple scalars.
scalars = x * x + y * y + z * z
# Some vectors
vectors = empty(z.shape + (3,), dtype=float)
vectors[..., 0] = (4 - y * 2)
vectors[..., 1] = (x * 3 - 12)
vectors[..., 2] = sin(z * pi)

# We reorder the points, scalars and vectors so this is as per VTK's
# requirement of x first, y next and z last.
pts = pts.transpose(2, 1, 0, 3).copy()
pts.shape = pts.size / 3, 3
scalars = scalars.T.copy()
vectors = vectors.transpose(2, 1, 0, 3).copy()
vectors.shape = vectors.size / 3, 3

# Create the dataset.
sg = tvtk.StructuredGrid(dimensions=x.shape, points=pts)
sg.point_data.scalars = scalars.ravel()
sg.point_data.scalars.name = 'temperature'
sg.point_data.vectors = vectors
sg.point_data.vectors.name = 'velocity'
write_data(sg, 'test') # creates test.vtu xml file
#write_data(sg, 'test.vtk') # creates test.vtk
于 2015-06-05T05:15:53.123 回答