Your G-code has a memory

Read motion and extrusion commands as a stateful program, including the resets that make naive previews lie.

Guides· · 3 min read

A G-code file is not a spreadsheet of independent coordinates. Commands change the meaning of later commands. Read a line without its history and a perfectly ordinary movement can look like a sudden attempt to redecorate the room.

This guide uses documented Marlin behavior. Other firmware, slicer extensions, binary formats, and printer-specific commands need their own interpretation. A readable file is not permission to send it to a machine.

Three pieces of state

Track the current position, the positioning mode, and the extrusion mode. Feed rate is also modal: a later move can use the previously specified value. Marlin documents G0/G1 as linear movement, G90/G91 as positioning-mode changes, and M82/M83 as extrusion-mode changes. Its G90/G91 behavior also affects extrusion mode, so a parser must follow the documented dialect rather than maintaining two unrelated toggles. Marlin Marlin Marlin Marlin Marlin

That sounds fussy until a visualizer treats a reset as a giant extrusion. Then it becomes everyone’s problem.

Walk a tiny example

The following is an educational parsing fixture. Do not run it on a printer: it omits machine setup, homing, temperature checks, and other required context.

G21
G90
M82
G92 E0
G1 X10 Y0 E1 F600
M83
G1 X20 E1
G92 E0
G1 X30 E-0.5

Assume an initial logical position of X0 Y0 solely for this exercise. The first move ends at X10 Y0 and advances absolute E from 0 to 1. M83 switches subsequent extrusion values to relative increments. The second move ends at X20 and adds another unit of extrusion. G92 changes the logical coordinate; it is not itself an instruction to traverse that distance. The final relative E value is negative, so it must not be counted as positive deposition. Marlin

The XYZ path totals 30 mm under those assumptions. Positive E increments total 2 mm, while the negative increment is 0.5 mm. That is bookkeeping for this fixture, not a claim about actual extruded volume or printed-part mass.

What a trustworthy preview should admit

A basic viewer can display supported linear segments, separate positive extrusion from travel and retraction, and show the command responsible for a segment. It should also report unsupported commands and an unknown initial position.

If the file contains arcs, tool changes, coordinate-system operations, firmware retraction, or volumetric extrusion, a limited parser needs to either implement their semantics or identify the gap. Silently skipping a command while presenting an exact total is worse than declining to calculate it.

Layer detection has another wrinkle: slicer comments are conventions, while changes in Z can include travel lifts. Label inferred layer groups as inferred. Do not automatically treat every upward movement as a new deposited layer.

Time is a separate problem

Path length divided by requested feed rate describes an idealized travel duration for a segment at constant speed. Real printer motion also depends on acceleration, limits, and controller behavior. Heating and waiting commands introduce other delays.

A viewer can teach you where the file requests movement without claiming the machine will finish in exactly that time. For a real job, use the slicer’s estimate as an estimate and compare it with measured completion time on your configuration.

A useful inspection habit

Pick one suspicious area in the slicer preview, locate its commands, and read backward until the coordinate and extrusion modes are established. Check units and resets before deciding a number is absurd. Sometimes the file is wrong. Sometimes the spreadsheet in your head forgot its state.

Next: The hole you drew is not the hole you printed applies the same skepticism to geometry.

Sources

Put this to work