3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

vtk(CAD系)Writerまとめ

Last updated at Posted at 2020-04-28

CAD系のWriter

.stl .ply .vtk形式のデータをvtkで出力する方法をまとめていきます

読みこむデータは以下から取得してください

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

基本となるコード

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()) #mapperにfilterを設定
mapper.SetScalarModeToUseCellFieldData() #scalarデータ用に設定

# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)             #actorにmapperを設定
actor.GetProperty().EdgeVisibilityOn()

# renderer
renderer = vtk.vtkRenderer()
renderer.AddActor(actor)            #rendererにactorを設定

##背景色の設定
renderer.GradientBackgroundOn()      #グラデーション背景を設定
renderer.SetBackground2(0.2,0.4,0.6) #上面の色
renderer.SetBackground(1,1,1)        #下面の色

#Window
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(renderer)         #Windowにrendererを設定
iren = vtk.vtkRenderWindowInteractor();
iren.SetRenderWindow(renWin);
renWin.SetSize(850, 850)
renWin.Render()
iren.Start();

image.png

CAD系のデータを出力する場合は、mapperからirenまでは不要なので、コメントアウトしてください

vtkSTLWriter

.stlを出力する場合

classリファレンス
https://vtk.org/doc/nightly/html/classvtkSTLWriter.html

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

vtkPolyDataWriter

.vtkを出力する場合

classリファレンス
https://vtk.org/doc/nightly/html/classvtkPolyDataWriter.html

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

vtkPLYWriter

.plyを出力する場合

classリファレンス
https://vtk.org/doc/nightly/html/classvtkPLYWriter.html

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?