Skip to content

Commit 1432a68

Browse files
committed
python script to combine vtk files
1 parent 1e17f5b commit 1432a68

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

scripts/combine_vtk.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""Script to be used to combine separate assembly VTK visualization
2+
files into one single VTK file.
3+
"""
4+
5+
import argparse
6+
import warnings
7+
import glob
8+
from vtk import vtkAppendFilter, vtkUnstructuredGridWriter, \
9+
vtkUnstructuredGrid, vtkUnstructuredGridReader
10+
11+
12+
def parse_args():
13+
parser = argparse.ArgumentParser(description="Combine separate assembly VTK " +\
14+
"visualization files into one single VTK file.")
15+
parser.add_argument('-f', '--files', type=str, nargs='+',
16+
help="vtk files to combine")
17+
parser.add_argument('-o', '--output', type=str, help="name for output file")
18+
return parser.parse_args()
19+
20+
21+
def check_input(files):
22+
"""Check files are all type *.vtk"""
23+
ferror_list = []
24+
for f in files:
25+
if f[-4:] != ".vtk":
26+
ferror_list.append(f)
27+
if len(ferror_list) > 0:
28+
raise Exception("Files are not '*.vtk' type: {}".format(ferror_list))
29+
30+
31+
def check_output(output):
32+
"""Check that output name is valid type. Use default name if not."""
33+
if output[-4:] != ".vtk":
34+
warnings.warn("Output filename is not valid. " +\
35+
"Using default name: combined_output.vtk")
36+
output = 'combined_output.vtk'
37+
return output
38+
39+
40+
def combine_files(files, outname):
41+
"""combine all files into single vtk file named outname."""
42+
reader = vtkUnstructuredGridReader()
43+
append = vtkAppendFilter()
44+
for file in files:
45+
reader.SetFileName(file)
46+
reader.Update()
47+
unstructured = vtkUnstructuredGrid()
48+
unstructured.ShallowCopy(reader.GetOutput())
49+
append.AddInputData(unstructured)
50+
append.Update()
51+
writer = vtkUnstructuredGridWriter()
52+
writer.SetFileName(outname)
53+
writer.SetInputData(append.GetOutput())
54+
writer.Write()
55+
56+
57+
if __name__ == "__main__":
58+
args = parse_args()
59+
60+
# get file names
61+
if args.files:
62+
filenames = args.files
63+
else:
64+
filenames = glob.glob("*.vtk")
65+
66+
# get output name
67+
if args.output:
68+
output = check_output(args.output)
69+
else:
70+
output = "combined_output.vtk"
71+
72+
check_input(filenames)
73+
74+
print("Combining files: {}".format(filenames))
75+
combine_files(filenames, output)

0 commit comments

Comments
 (0)