32leaves.net

STL slicing for 3D printing

Some 3D printing methods like the additive layer manufacturing require the model to be sliced into discrete layers, which are then being printed one after another. These days I’m playing around with 3D printing thus I needed to perform some slicing myself. Unfortunately I didn’t like the methods to perform such slicing that much, so I decided to give it a shot and write my own.
Some time ago I wrote a utility that visualizes the flight of a quadrocopter. To make things easy I used the Visualization Toolkit. Remembering that I hit google and found an example the pretty much did what I wanted. The model’s loaded using vtkSTLReader and vtkStripper is employed to merge the polyline strips to connected components.
Unfortunately vtkStripper still has a bug (since 2004!) which rendered it unusable for my endeavor. It causes some images to look quite wrong (thus they’d be printed wrong) as it combined some polylines in an unsuitable manner. The slice pictured below has that white/inverted triangle which is not supposed to be there.

After patching vtkStripper.cxx with the patch attached to be bug, everything was fine. (Well pretty much, I’ve still experienced the problem one time, but hey, what’s perfect in this world ;-))
So the whole slicing process is:
  1. Slice STL model using vtkCutter and store the polylines in vtp files. By decuppling the cutting process from rendering the images, we gain flexibility, since we do not have to do the cutting each time we want to use a different rendering algorithm. The first step also computes the bounds of the model (using a bounding box) and stores them in a file.
  2. Convert polyline to SVG. We use SVG since it provides multiple benefits over directly rasterizing the polyline. First of all we retain control over the units (during the whole process you want to make sure you don’t mess up the units, or otherwise your printed object may be twice as large as anticipated or similar problems may occur).
  3. Use ImageMagick to rasterize the SVG graphics. Actually that’s something pretty cool, because in this step we can easily ensure that we’re using the correct resolution for our printer. So if we used an Ink printer to apply the binder during the 3D printing process, we could simply use the resolution of the printer.

So now that we have all slices we can (of course) print them, or we could make a little movie out of them, which is exactly what I did:

The model’s coming from Thingiverse and the music is from SoundCloud. You might notice the full model in upper right corner, that’s just visual sugar for the video and not part of the sliced images.

To make the whole process a little easier, I wrapped a Makefile around it and wrote a little ruby script that builds the environment for the makefile to work. That ruby script, as well as the source can be found in the ZIP file after the break. You’ll need VTK to build to build the tools and ImageMagick to run the whole thing.
Download me here.

5 Responses to “STL slicing for 3D printing”

  1. elsenhans says:

    Nice work there! Recently I tried to slice a STL file using the freesteel slicer utility and it did not work very well. I also tried some other freeware tools (as well as using povray) with the same result. It seems that your solution has no problem with detailed and complex STL files. I’d like to give it a try but I’m not that familiar with building from code. Could you please provide a ready-to-use plugin for ImageMagick?

    kind regards,
    elsenhans

  2. Moin

    I’ve just found your website while searching for a way to slice stl files, whit out doing scaling voodoo in order to get the unit right.
    Your approach looks promising since it could be easily integrated in the software for my diy 3d printer.

    While testing it out, I’ve encountered a small bug.
    There is a problem with objects, that would result in slices with a inner an a outer contour.
    To be more specific : If you slice a torus you’d expect the slices to be a ring like shape.
    Instead you’ll get a circle.

    For a really precise bug report I’ve made a test stl file.
    All files can be obtained here : http://www.sebastians-site.de/homepage/uploads/slicingbug/

    The file was generated using the example01 shipped with openscad.
    rendered.png is the file rendered in openscad.
    The two svg files show the problem.
    z-Step was 0.1mm.
    My vtk version is 5.8.0-7.
    As long as there is no ‘hole’/inner contour inside a shape in the slice, everything looks fine, but as soon as there should be a inner contour inside a shape, the shape is fully filled.

    I don’t know whether this a bug, creating the polylines or if saveing the svg causes trouble.

    I’d appreciate any hints on how to fix this.
    Unfortunately I did not work with vtk myself yet, so I’ll have to read the docs about it first.

    Greetings
    Sebastian

  3. Moin

    Just a small update …
    It’s a bug saving the data as svgs …

    I didn’t notice 10 minutes ago that all contours are present in the svg files … but they are all black.

    So all I need way to find out which of them are inner contours to make the white.

    Greetings
    Sebastian

  4. Dov Grobgeld says:

    It’s been a while since you wrote the above post. Did you ever find a way to determine the color to paint the vtk contours?

  5. Christian says:

    It’s been a while, indeed … since that post I haven’t really looked into VTK anymore, so I haven’t tried to color the contours.

Leave a Reply

Fork me on GitHub