[PYTHON] vtk (CAD system) Writer Summary

CAD Writer

I will summarize how to output data in .stl`` .ply .vtk format with vtk.

Please obtain the data to be read from the following

terminal


$ git clone https://github.com/matsubaraDaisuke/vtk-data-set.git
$ cd vtk-data-set
$ cd geometry && ls

Basic code

import vtk

# reader
filename = "flange.stl"
reader = vtk.vtkSTLReader()
reader.SetFileName(filename)
reader.Update()

# filter
filter = vtk.vtkGeometryFilter()
filter.SetInputConnection(reader.GetOutputPort())
filter.Update()

# mapper
mapper = vtk.vtkCompositePolyDataMapper2()
mapper.SetInputConnection(filter.GetOutputPort()) #Set filter in mapper
mapper.SetScalarModeToUseCellFieldData() #Set for scalar data

# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)             #Set mapper for actor
actor.GetProperty().EdgeVisibilityOn()

# renderer
renderer = vtk.vtkRenderer()
renderer.AddActor(actor)            #Set actor in renderer

##Background color setting
renderer.GradientBackgroundOn()      #Set gradient background
renderer.SetBackground2(0.2,0.4,0.6) #Top color
renderer.SetBackground(1,1,1)        #Bottom color

#Window
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(renderer)         #Set renderer in Window
iren = vtk.vtkRenderWindowInteractor();
iren.SetRenderWindow(renWin);
renWin.SetSize(850, 850)
renWin.Render()
iren.Start();

image.png

When outputting CAD data, comment out because mapper to ʻiren` are unnecessary.

vtkSTLWriter

When outputting .stl

class reference https://vtk.org/doc/nightly/html/classvtkSTLWriter.html

writer = vtk.vtkSTLWriter()
writer.SetFileName("output.stl")
writer.SetInputConnection(filter.GetOutputPort())
writer.Write()

vtkPolyDataWriter

When outputting .ply

class reference https://vtk.org/doc/nightly/html/classvtkPolyDataWriter.html

writer = vtk.vtkPolyDataWriter()
writer.SetFileName("output.vtk")
writer.SetInputConnection(filter.GetOutputPort())
writer.Write()

vtkPLYWriter

When outputting .vtk

class reference https://vtk.org/doc/nightly/html/classvtkPLYWriter.html

writer = vtk.vtkPLYWriter()
writer.SetFileName("output.ply")
writer.SetInputConnection(filter.GetOutputPort())
writer.Write()

Recommended Posts

vtk (CAD system) Writer Summary
Summary of restrictions by file system