Skip to main content

Lab 2 — Slice, simulate and verify a print

📍 Where we are: Part IV · Virtual Labs: Build Without Hardware — Chapter 11 of 16. Lab 1 gave us printers that exist only in software. Here we feed them a real sliced part and ask how well the slicer's promises hold up.

The simple version

A slicer is like a route planner: it turns a 3D shape into a long list of turn-by-turn directions (the G-code) and tells you "about 6 minutes". Whether the trip really takes 6 minutes depends on the car, which here is the firmware that actually drives the motors. In this lab we plan a route, count its turns ourselves, and then "drive" it in two simulated cars to see how close the estimates are.

What this chapter covers​

We slice a small test part with the PrusaSlicer command line, write a standard-library Python analyzer that counts layers, extruded volume and travel versus print moves, and includes two print-time models. Then we replay the G-code through Klipper (batch mode) and the Marlin simulator from Lab 1 and compare four predictions with two simulated results. Everything is in examples/lab-slice-and-verify/. All numbers are from our run on 2026-11-15 (Apple-silicon Mac, arm64 Docker): PrusaSlicer 2.5.0, Klipper v0.13.0, Marlin bugfix-2.1.x built that day, Python 3.14 on the host.

Why check a print before you print it​

A slicer's output is a promise made without ever running the machine. Slicing itself is geometry: intersect the triangle mesh with a stack of planes, turn each cross-section into outlines and fill, order the moves. Chapters on slicers covered the idea; the literature has studied it since the early 1990s [1], and the central trade-off is still the one the 2003 review states: thin slices cost build time, thick slices cost surface quality [2]. What the slicer cannot know is how the firmware will smooth those thousands of tiny moves. So we ask three questions of the G-code before a printer touches it. What will it deposit? How long will it take? Will the firmware accept it?

Step 0: set up​

Lab 2 builds on Lab 1's Klipper image (cd ../lab-virtual-printer && make images first) and adds the slicer.

cd examples/lab-slice-and-verify
make image

The image took 2 min 16 s. We use PrusaSlicer 2.5.0 from Debian bookworm's arm64 package [3] because upstream's latest release, 2.9.7 (2 November 2026), listed a Windows installer, a macOS disk image and a zip, and no Linux package, among its assets when we looked [4]. That is a known cost: 2.5.0 is old, but its command line and the G-code it writes are what we measured. We did not try OrcaSlicer or CuraEngine.

Step 1: make a part​

make_model.py writes a binary STL with the standard library: a tube of 10 mm outer radius, 6 mm bore and 6 mm height, standing at (50, 50). It has curved walls (so many short segments), a hole (so the nozzle must travel) and a few dozen layers.

make model
# 512 triangles; analytic volume of the ideal tube 1206.4 mm^3

The slicer's own --info (run by make slice) reports a mesh volume of 1204.4 mm³, 0.2 % under the ideal tube because 64-sided polygons sit inside the circles.

Step 2: slice from the command line​

The PrusaSlicer CLI loads a config file and writes G-code [5]. Our config/printer-and-print.ini describes the simulated printer from Lab 1: 200 by 200 mm bed, gcode_flavor = reprap, 0.4 mm nozzle, 1.75 mm filament, 0.2 mm layers, 2 perimeters, 15 % gyroid infill, speeds of 30 mm/s (outer perimeter), 45, 80 (infill) and 150 (travel), acceleration 3000 mm/s², and the machine limits of Klipper's config (500 mm/s, 3000 mm/s²) so the slicer's own estimate is fair. We set all temperatures to 0 so the file contains no heating waits, and the start G-code uses Klipper's SET_KINEMATIC_POSITION because a virtual printer has no endstops to home against.

make slice
; filament used [mm] = 337.05
; filament used [cm3] = 0.81
; estimated printing time (normal mode) = 5m 45s
13295 out/tube.gcode

Slicing took 0.16 s. The slicer's report is the promise: 337.05 mm of filament, 0.81 cm³ and 5 min 45 s.

Step 3: analyze the G-code yourself​

gcode_stats.py (Python standard library, 300 lines, 10 unit tests) parses G0/G1/G90/G91/M82/M83/G92/G4/M204 and Klipper's position command, classifies every move and sums.

make analyze
layers : 30 (layer height 0.2 mm, 30 ;LAYER_CHANGE comments)
print moves : 11835 (9906.0 mm of path)
travel moves : 279 (1146.4 mm of path, 10.4 % of all XY path)
retract / unretract : 129 / 128 extrude-only moves
filament extruded : 337.05 mm = 0.811 cm3
time, naive planner : 7m 59.2s (479.2 s)
time, junction planner: 5m 47.3s (347.3 s)

Check it by hand. 6 mm at 0.2 mm is 30 layers. The extruded filament matches the slicer's report to the hundredth of a millimetre, and 337.05 mm of 1.75 mm filament is π × 0.875² × 337.05 ≈ 811 mm³, about two thirds of the 1206 mm³ ideal solid tube because the inside is 15 % infill. A tenth of the horizontal path is travel with no material, and the nozzle retracts 129 times.

The two time models differ by 38 %, and that is the point of the exercise. The naive model brings every move to a stop at its end, so each of the 12,000 short moves pays for its own acceleration and braking, as in the trapezoid computation from the firmware chapter. The junction model is a port of the look-ahead planner in Klipper's toolhead.py and extruder.py [6]: at every corner it lets the nozzle carry speed through, limited by the square-corner-velocity rule (5 mm/s by default, minimum_cruise_ratio 0.5) [7], by the extruder's own corner limit and by the slow Z axis, then runs a backward and forward pass over the queue. Our unit tests pin it to closed-form cases: a single 100 mm move at 100 mm/s and 3000 mm/s² takes 1.0333 s, two collinear moves save exactly one acceleration and braking, and a velocity request of 10,000 mm/s is clamped to max_velocity.

Step 4: ask Klipper's planner​

Batch mode from Lab 1 runs Klipper's real planner on the file in 1.7 s of wall time:

make klipper
Exiting (print time 347.510s)

Subtract the 0.25 s start-up buffer we met in Lab 1 and Klipper's own answer is 347.26 s. Our port gave 347.3 s: they agree to 0.02 %, for the right reason, since the port copies Klipper's algorithm. The slicer's 345 s is 0.7 % lower.

Step 5: replay on the Marlin simulator​

Klipper's planner is one answer. We also streamed the same G-code to the Marlin simulator at real-time speed (realtime_scale 1) using its built-in host-print feature, which sends each line and waits for ok inside the simulation; make marlin takes about 5.3 minutes of wall time per run. Marlin does not know SET_KINEMATIC_POSITION, so the script substitutes G92 X0 Y0 Z0. We ran it twice: with the stock Simulator-example limits (feedrate 300 mm/s on X and Y, 5 mm/s on Z), and after M203/M201 set the same 500 mm/s and 3000 mm/s² that Klipper and the slicer were given. Marlin refuses to set junction deviation below 0.01 mm [8], so that one parameter stays at its default.

make compare
source time seconds vs Klipper
PrusaSlicer 2.5.0 estimate (in the G-code) 5m45.0s 345.0 -0.7%
gcode_stats.py, naive (rest-to-rest) 7m59.2s 479.2 +38.0%
gcode_stats.py, Klipper-style junction planner 5m47.3s 347.3 -0.0%
Klipper v0.13.0 batch mode (print_time - 0.25 s) 5m47.3s 347.3 +0.0%
Marlin simulator, limits aligned with Klipper 5m16.5s 316.5 -8.9%
Marlin simulator, stock Simulator-example limits 5m16.4s 316.4 -8.9%

Marlin finishes the same file about 9 % sooner than Klipper, and the limits made no visible difference (316.48 s against 316.42 s). We tried to explain the gap with our model and could not close it: giving Klipper's model the square-corner velocity equivalent to Marlin's 0.01 mm junction deviation (8.51 mm/s), switching off minimum_cruise_ratio and removing the extruder corner limit brought it only down to 336.7 s, and a far more permissive 20 mm/s corner speed gave 328.0 s. So the difference is not those three rules; we did not find the rest. What we can say is the observation: for this part, two open-source firmwares planned the same file about 30 s apart.

A pipeline: an STL is sliced by the PrusaSlicer command line into G-code, which goes to three checks, a Python analyzer, Klipper batch mode and the Marlin simulator, and a final comparison table. One sliced file, three checks: the analyzer's junction model and Klipper's planner agree at 347.3 s, the slicer says 345 s, and the Marlin simulator says 316.4 s. Original diagram by the authors, created with AI assistance.

When it breaks​

  • gcode_flavor = klipper is not accepted. PrusaSlicer 2.5.0 printed key = "gcode_flavor" loaded = "klipper substituted = "reprap" and carried on. We now set reprap explicitly; the slicer and Klipper figures above were unchanged.
  • prusa-slicer --version prints Unknown option --version. In 2.5.0 use --help; the version appears in its banner.
  • Our first model was 12 mm tall. It sliced to 25,741 lines and an estimated 10 min 41 s, which makes a real-time Marlin replay too long for a lab. We halved the height.
  • Marlin silently ignored a setting. Our first "aligned" run sent M205 J0.00345, Klipper's equivalent junction deviation. Marlin answered Error:?J out of range (0.01 to 0.3), which the script did not print, and the run looked fine. The script now prints rejected setup commands and the aligned run uses only values Marlin accepts.
  • Do not fast-forward the replay. Lab 1 showed realtime_scale 20 distorting move times; the replay forces 1.
  • Must home axis first in Klipper. The start G-code must be SET_KINEMATIC_POSITION, and the config must enable [force_move]; both are in the lab files.
  • Heating commands are untested here. Because we set temperatures to 0 the file has no M104 or M109, and we did not test how batch mode handles them.

What to try next​

  1. Slice at 0.1 mm and 0.3 mm layers and see whether the model keeps matching Klipper's own answer.
  2. Raise perimeter_speed and infill_speed and find the speed where acceleration, not the speed setting, decides the time.
  3. Halve max_accel in the slicer config, the Klipper config and Marlin's M201/M204, and compare how the three estimates move.
  4. Replace gyroid with rectilinear infill and compare travel fraction, retractions and time.
  5. Slice the Lab 3 bracket with this pipeline.

Why it matters​

Print time and material are the numbers people plan around, and they are predictions. Replaying G-code in a simulated machine turns "the slicer says six minutes" into a measurement with an error bar, and it exposes which assumption (acceleration, corner speed, firmware) moves it. The same habit, estimate then simulate then compare, is how you would test any new slicer setting before spending filament on it.

In the real world​

Slicers print an estimate, and each firmware plans motion with its own look-ahead rules. The 9 % spread we measured between two open firmwares on one file is a reminder to calibrate a time estimate against the machine that will actually run the job.

Key terms​

  • Slicer — software that cuts a 3D model into layers and writes the G-code for each.
  • CLI (command-line interface) — running a program with typed options instead of a window, so it can be scripted.
  • Naive planner — a time model that stops the toolhead at the end of every move.
  • Junction speed — how fast the nozzle may pass the join between two moves without a stop.
  • Look-ahead — the planner looking several moves ahead so it can slow down in time for a corner.
  • Retraction — pulling filament back a little before a travel move to prevent oozing.
  • Travel move — a move with no extrusion.
  • Print time (Klipper) — the planner's own clock for when the last move ends, including a 0.25 s start-up buffer.
  • Host print — a program sending G-code line by line to a firmware, waiting for each acknowledgement.

Where this leads​

Next, Lab 3 designs a bracket in code, checks its strength with finite elements and optimises its shape, then sends the result back through the slicer used here.