Optimizing CNC G-code safely (arc fitting, simplification, self-checks)
cncgcodearc-fittingdouglas-peuckeroptimizationverification
cncfileoptimizer.com rewrites CNC G-code to be smaller and faster without changing what gets cut, entirely in the browser. The techniques are standard; the lessons are about guaranteeing the output is still the same part.
Techniques
- Douglas-Peucker simplification of G1 runs within a tolerance.
- Arc fitting: replace runs of short G1 segments with G2/G3 arcs (circle fit, e.g. Kasa least squares).
- Pass thinning for over-fine finishing stepovers (Reading Fusion 360 G-code (toolpath signatures, passes, the Personal license quirk)).
- Canned-cycle expansion (G81/G83 into plain moves) for controllers without them.
- Restoring rapids that Fusion Personal turned into slow G1 moves.
- Rotary wrap (mapping one linear axis to a rotary axis): disables arc fitting, because wrapped output cannot hold XY arcs, and rewrites the axis word in place.
- Output resolution as one user-facing knob: a maximum deviation in mm (Full, 0.02, 0.05, 0.1, 0.25) that raises the arc and simplify tolerances and forces simplification. On a fixture, "Low" (0.1 mm) cut 7,978 moves to 822. Existing G2/G3 arcs in the input are not coarsened by it.
The self-check (every result must pass)
- Envelope (output stays inside the input's bounds), Depth (never deeper), Arcs (valid geometry),
Syntax, and Path: sample the output cutting path along its whole length (up to about 1 M samples)
and confirm every sample is within
max(arcTol, simplifyTol, resolution) * 1.05plus rounding of the original cutting path, and vice versa unless passes were deliberately thinned. A 3D grid-hashed segment index walked cell by cell makes this fast. Canned cycles are modeled as vertical segments. - Never ship a change that turns the check red on the fixtures.
Arc-fitting lesson
Fitted arcs strayed up to 2x the tolerance on coarse polylines and failed the tool's own Path check. Fix: re-center each fitted arc on the perpendicular bisector of its end points (so it passes exactly through both) and fit at 0.85 x the tolerance to leave margin.
Performance notes
- The Path check is slow on programs that repeat the same XYZ path many times (449k lines with 56 identical copies took about 20 s, down from 123 s); distinct Z levels are fine because the index is 3D.
- Long text diffs: patience diff with a Myers fallback and an operation budget; very large files fall back to greedy alignment.
Architecture (reusable for browser tools)
Pure DOM-free ES modules for the engine (also run under Node for tests and benchmarks), heavy work in a module Web Worker, a hand-written WebGL toolpath viewer, and a settings UI generated from the engine's DEFAULTS object. See Offline-first browser tools (module workers, precache lists, no build step).
See also
- G-code reference for CNC milling
- 3-axis CAM from STL (height maps, drop-cutter finishing, gouge checks)
- 2.5-D CNC design-to-G-code (distance-field offsetting, independent verification)
Linked from
- 2.5-D CNC design-to-G-code (distance-field offsetting, independent verification)
- 3-axis CAM from STL (height maps, drop-cutter finishing, gouge checks)
- G-code reference for CNC milling
- Offline-first browser tools (module workers, precache lists, no build step)
- Reading Fusion 360 G-code (toolpath signatures, passes, the Personal license quirk)