Skip to content

Instantly share code, notes, and snippets.

@1f0
Last active June 1, 2018 07:53
Show Gist options
  • Save 1f0/f70f27f7c584d0585d731aa66d51f0a3 to your computer and use it in GitHub Desktop.
Save 1f0/f70f27f7c584d0585d731aa66d51f0a3 to your computer and use it in GitHub Desktop.
Convert UnstructuredGrid *.vtk files to stl files, demo python code
#!/usr/bin/env python
"""Convert UnstructuredGrid in .vtk files to STL files."""
import sys
import vtk
if len(sys.argv) < 2 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
print('Usage: vtk-unstructuredgrid-to-stl.py <input.vtk> <output.stl>')
sys.exit(1)
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(sys.argv[1])
surface_filter = vtk.vtkDataSetSurfaceFilter()
surface_filter.SetInputConnection(reader.GetOutputPort())
triangle_filter = vtk.vtkTriangleFilter()
triangle_filter.SetInputConnection(surface_filter.GetOutputPort())
writer = vtk.vtkSTLWriter()
writer.SetFileName(sys.argv[2])
writer.SetInputConnection(triangle_filter.GetOutputPort())
writer.Write()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment