2013-02-06 45 views
0

我需要用python編寫一個xml vtk文件。 特別在StructuredGrid中。 我有座標x,y,z軸和空間中每個點的x,y,z分量的值。 我如何寫這個文件?python vtk作家

回答

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 class reference

+0

他指的是vtkStructuredGrid文件,而不是一個vtkPolyData文件。我不認爲他有一個vtkStructuredGrid內置,並需要從他目前的數據做到這一點。 – andybauer

-1

請檢查該#1鏈路調整註釋的代碼即可完成你的工作。 StackOverflow Link。具體的例子from 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