Slicing 3D STL Meshes into Closed AutoCAD R12 DXF Polylines & Stacked Laser Contour Sheets in Pure TypeScript
DEV Community

Slicing 3D STL Meshes into Closed AutoCAD R12 DXF Polylines & Stacked Laser Contour Sheets in Pure TypeScript

Every CNC plasma operator, CO2 laser maker, and router shop hits the same wall: a customer or Thingiverse/Printables download gives you a 3D .STL bracket, stepped pipe flange, or topographic mesh, and you need a flat 2D AutoCAD R12 (AC1009) DXF or 1:1 LightBurn SVG to cut it out of sheet stock.

Naive online STL-to-DXF converters take a destructive shortcut: they project every triangle edge onto Z = 0. A 6,000-triangle mesh turns into 18,000 overlapping micro-lines that choke SheetCam, trigger double-cut burns in LightBurn, and produce zero closed contours.

To solve this properly in the browser with zero backend uploads, we built a deterministic geometric slicer in pure TypeScript - live and free at CutPath CAD 3D STL to 2D DXF & SVG Slicer.

1. Exact Triangle-Plane Intersection Without Coplanar Degeneracies

Given a binary or ASCII STL triangle soup and a cutting plane orthogonal to Z (Top-Down), Y (Front), or X (Side) at elevation h, each triangle (v0, v1, v2) produces either zero intersections or a clean 2D line segment (pA, pB).

The trap in real mechanical STLs is exact coplanar faces: if a flange sits at Z = 8.000 mm and the user slices at Z = 8.000 mm, every horizontal roof triangle lies on the plane, polluting the contour with internal triangulation diagonals.

We avoid coplanar pathologies with two rules:

  • Apply a deterministic sub-micron plane epsilon nudge (+1e-5 mm) whenever a slice lands directly on a mesh face band.
  • Treat signed distances with a strict half-open convention (d >= 0 vs d < 0) so shared vertices never emit duplicate segments across adjacent triangles.
function intersectTriangleWithPlane(ax: number, ay: number, az: number, bx: number, by: number, bz: number, cx: number, cy: number, cz: number, planeH: number): [number, number, number, number] | null {
  const da = az - planeH;
  const db = bz - planeH;
  const dc = cz - planeH;
  const aboveA = da >= 0;
  const aboveB = db >= 0;
  const aboveC = dc >= 0;
  if (aboveA === aboveB && aboveB === aboveC) return null;
  const pts: Array<[number, number]> = [];
  const edges: Array<[number, number, number, number, number, number, boolean, boolean]> = [
    [ax, ay, da, bx, by, db, aboveA, aboveB],
    [bx, by, db, cx, cy, dc, aboveB, aboveC],
    [cx, cy, dc, ax, ay, da, aboveC, aboveA]
  ];
  for (const [x1, y1, d1, x2, y2, d2, s1, s2] of edges) {
    if (s1 !== s2) {
      const t = d1 / (d1 - d2);
      pts.push([x1 + t * (x2 - x1), y1 + t * (y2 - y1)]);
    }
  }
  return pts.length === 2 ? [pts[0][0], pts[0][1], pts[1][0], pts[1][1]] : null;
}

2. Spatial-Hash Contour Stitching & Broken-STL Repair

Plane intersection yields an unordered soup of segments. To guarantee 0 open loops for CNC CAM software:

  • Quantized spatial-hash adjacency: Endpoints are bucketed onto a 0.005 mm grid so floating-point noise from binary Float32 STLs stitches cleanly into closed vertex rings.
  • Tolerance-bounded gap closure: Community STLs frequently have missing triangles or non-manifold seams. Any remaining open chains whose endpoints sit within repair tolerance are greedily stitched into closed rings, and every loop is checked with our live inspectDxfRaw verifier (CAM 100/100 ยท 0 OPEN).
  • Closed-loop Ramer-Douglas-Peucker simplification: Flat walls triangulated into dozens of collinear segments collapse into exact 2-point lines while circular bolt holes and organic topo curves stay within configurable tolerance (0.02 mm to 0.15 mm).
  • Even-odd containment & winding normalization: Every stitched loop is tested against every other loop by ray-casting to classify CUT_OUTER (CCW) vs CUT_INNER (CW) so plasma/laser CAM software always applies inside/outside kerf offsets correctly.

3. Multi-Level Blueprint Flattening vs. Stacked Laser Contour Layers

Real fabrication needs three distinct slicing modes:

  • Single Plane Slice: Scrub the live glowing cut plane along Z, Y, or X to isolate any flat bracket profile or elevation silhouette.
  • Multi-Level Blueprint Flattener: Probes 9 elevation bands across stepped mechanical parts (like a ร˜120 mm 4-bolt pipe flange with a ร˜62 mm raised neck boss). Primary through-cuts go onto CUT_OUTER / CUT_INNER; raised bosses and pockets automatically go onto STEP_DETAIL as reference/scribe geometry.
  • Stacked Laser / CNC Layers (L01-LN) + Automatic Dowel Pin Solver: Slices 3D terrains or sculptures into N sheets matching your exact stock thickness (3 mm Baltic Birch, 1/8" Acrylic, 6 mm Plywood, 1/4" MDF), packs them cleanly onto a cut sheet, and computes two shared-core alignment dowel pin coordinates (3.175 mm / 4 mm / 6 mm / 1/4") with guaranteed wall-edge clearance - automatically omitting pins on small peak islands where a hole would blow out the wall.

4. Strict AutoCAD R12 (AC1009) Output + Browser Privacy

Every exported file writes pure AutoCAD R12 POLYLINE + VERTEX + SEQEND entities (zero SPLINE, zero LWPOLYLINE incompatibilities), proper $MEASUREMENT headers for instant mm โ†” inch conversion, and a companion Cut-Only DXF stripped of engraving labels so plasma tables never pierce text strokes.

Try it free with zero upload and zero signup (5 built-in watertight STLs ready to test right away):

  • Interactive Slicer: CutPath 3D STL to 2D DXF & SVG Slicer
  • Full Technical Guide: Convert 3D STL to 2D DXF for Laser & CNC Plasma
  • Stacked Contour Guide: Stacked Laser-Cut Contour Models With Dowel Pins
  • 3D Mesh Inspector: Free Online 3D STL Viewer & Watertight Checker
  • Multi-Sheet Packing: CutPath 4ร—8 & 24ร—12 Sheet Nesting (WB-04)
Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.