3D Printer slicer

Use the following code to slice an STL model in your 3D printing application.

ReadSTL rs = new ReadSTL(@"c:\devdept\bunny.stl");
rs.DoWork();
rs.AddToScene(viewportLayout1);

Mesh m = (Mesh) rs.Entities[0];
m.ColorMethod = colorMethodType.byEntity;
m.Color = Color.Aquamarine;

Plane p = Plane.XY;

LinkedList<SharedEdge>[] edgesPerVertex;
Utility.GetEdgesWithoutDuplicates(m.Triangles, m.Vertices.Length, out edgesPerVertex);

int num = 100;

List<Entity> sections = new List<Entity>(num);

for (int iz = 0; iz < num; iz++)
{
    p.Translate(new Vector3D(0, 0, m.BoxSize.Z / num));

    ICurve[] sCurves = m.Section(p, 0, edgesPerVertex);

    foreach (Entity c in sCurves)
    {
        sections.Add(c);
    }
}

viewportLayout1.Entities.AddRange(sections, Color.Blue);

An this is the result:

slicer.png

Was this article helpful?
1 out of 1 found this helpful
Have more questions? Submit a request

Comments

2 comments
  • Great Article,

    How to save these slices as separate 2D images (e.g. PNG files)

  • After setting ViewportLayout.BoundingBox.OverrideSceneExtents = true and setting BoundingBox.Min and Max, you may want to try building a Region entity from each closed contour on the same plane, hide all the other contours (Entity.Visible), set top view (ViewportLayout.SetView()), set orthographic projection (ViewportLayout.Camera.OrientationMode), fit the bounding box to screen (ViewportLayout.ZoomFit()) and call ViewportLayout.RenderToBitmap() to generate the image.

Please sign in to leave a comment.