Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?

More than 1 year has passed since last update.

@matsubara_d

vtk(CAD系)Writerまとめ

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

.plyを出力する場合

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

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

vtkPLYWriter

.vtkを出力する場合

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

writer = vtk.vtkPLYWriter()
writer.SetFileName("output.ply")
writer.SetInputConnection(filter.GetOutputPort())
writer.Write()
1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What is going on with this article?