# Python Planar Contours to Closed Surface

**URL:** <https://discourse.slicer.org/t/python-planar-contours-to-closed-surface/20023>\
**Category:** Support\
**Tags:** python\
**Created:** [October 5, 2021, 1:36pm UTC](https://discourse.slicer.org/t/python-planar-contours-to-closed-surface/20023 "2021-10-05T13:36:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![darcymason](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/darcymason/32/12674_2.png) [@darcymason](https://discourse.slicer.org/u/darcymason)\
**Post date:** [October 5, 2021, 1:36pm UTC](https://discourse.slicer.org/t/python-planar-contours-to-closed-surface/20023/1 "2021-10-05T13:36:28Z")

</div>

Operating system: Win10  
Slicer version: 4.11

Hi,

I’m looking to convert RTSTRUCT contours to STL files in a Python script.  
If there is a simple robust path someone can point to, then great, otherwise what I have seen  
that looks promising is from [a 2019 discussion](https://discourse.vtk.org/t/mesh-from-contours/896/4) pointing to a conversion rule class in SlicerRT.  
([Link to the code](https://github.com/SlicerRt/SlicerRT/blob/master/DicomRtImportExport/ConversionRules/vtkPlanarContourToClosedSurfaceConversionRule.cxx)).

I’m quite new to Slicer and Python connections to it (and conversion rules), and I’m not sure how to find Python binding names. Looking for something like `vtkPlanarContourToClosedSurfaceConversionRule`, I don’t see anything in the Python interactor autocomplete that mentions PlanarContour, but perhaps I have not found the right ‘path’ to get to it. I’ve tried mainly through `slicer.xxx` and `slicer.modules.dicomrtimportexport.xxx`. There are several similar conversion rules available from the slicer object, e.g. `slicer.vtkBinaryLabelmapToClosedSurfaceConversionRule`. Does the rule need to be registered somehow, or is it not available in current releases?

I’ve tried in Slicer 4.11 and in preview 4.13, both on Windows with SlicerRT extension installed.

Thanks in advance for any help

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [October 5, 2021, 1:45pm UTC](https://discourse.slicer.org/t/python-planar-contours-to-closed-surface/20023/2 "2021-10-05T13:45:03Z")

</div>

I suggest you take a look at this script. The only thing you need to change is that instead of binary labelmaps you want to convert into closed surface and instead of nrrd you want to save to STL.

[https://github.com/SlicerRt/SlicerRT/blob/master/BatchProcessing/BatchStructureSetConversion.py](https://github.com/SlicerRt/SlicerRT/blob/master/BatchProcessing/BatchStructureSetConversion.py)

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [October 5, 2021, 2:00pm UTC](https://discourse.slicer.org/t/python-planar-contours-to-closed-surface/20023/3 "2021-10-05T14:00:22Z")

</div>

BTW, welcome @darcymason 👍

As an aside you’ll see that a lot of the dicom infrastructure in Slicer assumes you are working in Slicer’s python which historically was different from regular python. But these days we’ve been able to re-sync things so that most pip packages ‘just work’ in Slicer. This makes us think we might refactor Slicer dicom code for more general purpose use, but we haven’t put the work in for that yet.

---

<div class="post-metadata">

**Author:** ![darcymason](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/darcymason/32/12674_2.png) [@darcymason](https://discourse.slicer.org/u/darcymason)\
**Post date:** [October 5, 2021, 3:00pm UTC](https://discourse.slicer.org/t/python-planar-contours-to-closed-surface/20023/4 "2021-10-05T15:00:17Z")

</div>

Thanks for the quick replies, and for the welcome, @pieper😁

> [@cpinter](#):
>
> I suggest you take a look at this script. The only thing you need to change is that instead of binary labelmaps you want to convert into closed surface and instead of nrrd you want to save to STL.
> 
> [SlicerRT/BatchProcessing/BatchStructureSetConversion.py at master · SlicerRt/SlicerRT · GitHub](https://github.com/SlicerRt/SlicerRT/blob/master/BatchProcessing/BatchStructureSetConversion.py)

I had seen that before but got stuck on the conversion rule seen in the discussion I linked, but per your suggestion, it seems I could use `segNode.CreateClosedSurfaceRepresentation()`. It appears there are no arguments to the function, so is there a way I could specify the particular rule (or does that happen to point to the one I’m interested in)? And … a quick pointer to how to adjust settings prior (e.g. smoothing) would be very helpful.

Thanks again
