# How to extract the contour line?

**URL:** <https://discourse.vtk.org/t/how-to-extract-the-contour-line/14349>\
**Category:** Support\
**Tags:** python\
**Created:** [August 2, 2024, 1:37pm UTC](https://discourse.vtk.org/t/how-to-extract-the-contour-line/14349 "2024-08-02T13:37:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wyl](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/w/2acd7d/32.png) [@wyl](https://discourse.vtk.org/u/wyl)\
**Post date:** [August 2, 2024, 1:37pm UTC](https://discourse.vtk.org/t/how-to-extract-the-contour-line/14349/1 "2024-08-02T13:37:19Z")

</div>

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.  
 ![QQ20240802-213644](https://discourse.vtk.org/uploads/default/original/2X/d/d7e16c0e9cf579375d7e93f28ee29d116430b42d.png)

---

<div class="post-metadata">

**Author:** ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)\
**Post date:** [August 2, 2024, 4:04pm UTC](https://discourse.vtk.org/t/how-to-extract-the-contour-line/14349/2 "2024-08-02T16:04:22Z")

</div>

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](https://meta.discourse.org/t/formatting-posts-using-markdown-bbcode-and-html/239348):

```python
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()

```

---

<div class="post-metadata">

**Author:** ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)\
**Post date:** [August 5, 2024, 12:11pm UTC](https://discourse.vtk.org/t/how-to-extract-the-contour-line/14349/4 "2024-08-05T12:11:51Z")

</div>

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.

```python
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()

```
