I am a beginner. I encountered some issues during the use of VTK. The task is to use a plane to cut an object and extract the contour line of the cut. The cutting has been completed.
Hello and welcome to the group.
There doesn’t appear to be anything wrong with your code. I was able to plot a slice through a surface. Were you trying to slice through a volume or something?
Also rather than using screenshots (which are fine) you can enclose your code inside of triple “backticks” (`) to show your code like this using Markdown formatting:
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonDataModel import vtkPlane
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkFiltersCore import vtkCutter
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderer,
vtkRenderWindow,
vtkRenderWindowInteractor)
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera
#------------------------------------------------#
# Rather than use Reader, I just make an object. #
#------------------------------------------------#
sph = vtkSphereSource()
sph.SetCenter(0, 0, 0)
sph.SetRadius(5)
sph.SetPhiResolution(100)
sph.SetThetaResolution(100)
plane = vtkPlane()
plane.SetOrigin(0, 0, 0)
plane.SetNormal(1, 1, 0)
cutter = vtkCutter()
cutter.SetCutFunction(plane)
cutter.SetInputConnection(sph.GetOutputPort())
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(cutter.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
ren = vtkRenderer()
ren_win = vtkRenderWindow()
ren_win.SetWindowName('Extract Curve')
ren_win.SetSize(600, 600)
ren_win.AddRenderer(ren)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(ren_win)
style = vtkInteractorStyleTrackballCamera()
iren.SetInteractorStyle(style)
ren.AddActor(actor)
ren.ResetCamera()
ren_win.Render()
iren.Start()
You haven’t been terribly specific about what you want done with the extracted data, so there’s not much I can do other than repost the code I posted earlier with comments. I am guessing that since you are loading in a STL file that my vtkSphereSource would be a good prototype of your data.
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonDataModel import vtkPlane
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkFiltersCore import vtkCutter
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderer,
vtkRenderWindow,
vtkRenderWindowInteractor)
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera
import vtk
# I have no idea what your STL file is, so I'll just keep using the vtkSphereSource.
sph = vtkSphereSource()
sph.SetCenter(0, 0, 0)
sph.SetRadius(5)
sph.SetPhiResolution(100)
sph.SetThetaResolution(100)
plane = vtkPlane()
plane.SetOrigin(0, 0, 0)
plane.SetNormal(1, 1, 0)
cutter = vtkCutter()
cutter.SetCutFunction(plane)
cutter.SetInputConnection(sph.GetOutputPort())
# If you wanted to extract the vtkPolyData output from the cutter, you would
# just have to access it. I'm afraid that since you were not specific about
# what you meant by "extract" I'll just have to leave it at that. Extract to
# file? Extract the cell connectivity from? Extract the points thereof?
# No idea ...
cutter.Update()
extracted_polydata_from_cutter = cutter.GetOutput()
print(extracted_polydata_from_cutter)
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(cutter.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
ren = vtkRenderer()
ren_win = vtkRenderWindow()
ren_win.SetWindowName('Extract Curve')
ren_win.SetSize(600, 600)
ren_win.AddRenderer(ren)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(ren_win)
style = vtkInteractorStyleTrackballCamera()
iren.SetInteractorStyle(style)
ren.AddActor(actor)
ren.ResetCamera()
ren_win.Render()
iren.Start()