-
Notifications
You must be signed in to change notification settings - Fork 20
/
convert-vtp-to-32.py
executable file
·64 lines (49 loc) · 1.51 KB
/
convert-vtp-to-32.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
# This script is used to convert VTK VTP file 'ModelFaceID', 'CapID' and 'GlobalElementID'
# Int64 Cell DataArrays to Int32 Cell DataArrays.
#
# Usage:
#
# convert-vtp-to-32.py FILE_NAME.vtp
#
# Output:
#
# FILE_NAME-convertted.vtp
import os
import sys
import vtk
def replace_cell_data(polydata, data_name):
''' Replace the PolyData 'data_name' cell DataArray.
'''
num_cells = polydata.GetNumberOfCells()
old_data = polydata.GetCellData().GetArray(data_name)
polydata.GetCellData().RemoveArray(data_name)
new_data = vtk.vtkIntArray()
new_data.SetNumberOfValues(num_cells)
new_data.SetName(data_name)
for i in range(num_cells):
value = old_data.GetValue(i)
new_data.SetValue(i, value)
polydata.GetCellData().AddArray(new_data)
if __name__ == '__main__':
file_name = sys.argv[1];
file_base_name, ext = os.path.splitext(file_name)
reader = vtk.vtkXMLPolyDataReader()
reader.SetFileName(file_name)
reader.Update()
polydata = reader.GetOutput()
# Replace cell arrays.
#
data_name = 'ModelFaceID'
replace_cell_data(polydata, data_name)
data_name = 'CapID'
replace_cell_data(polydata, data_name)
data_name = 'GlobalElementID'
replace_cell_data(polydata, data_name)
# Write new PolyData file.
file_name = file_base_name + "-converted.vtp"
writer = vtk.vtkXMLPolyDataWriter()
writer.SetInputData(polydata)
writer.SetFileName(file_name)
writer.Update()
writer.Write()