Murray State University School of Engineering CourseWiki

G-code and Toolpaths

How a CNC mill reads a program line by line: rapid and feed moves, absolute and incremental coordinates, units, arcs and a drilling cycle. Build a program from blocks and watch it cut a simulated block.

What is G-code?

What is G-code?

A CNC mill moves its tool by reading a program from the top down, one line at a time. The language most mills read is called G-code. Each line holds a few words, and together they say where the tool goes next, how fast, and what the spindle and tool are doing.

Start with CNC Control Fundamentals for the control and machine types, CNC Axes and Offsets for coordinate references, and CNC Program Structure for common Fanuc-style milling codes. G- and M-code meanings vary by control. The simulator below uses its own documented subset; check the machine's manual for real programs.

Word
A letter and a number, such as X20, G1 or F300. The letter says what the number means: X, Y and Z are positions, F is the feed rate, S the spindle speed, T a tool number. G and M words are codes that choose what kind of move or machine action the line is.
Block
One line of the program. Machinists often call a line a block; in the simulator below, each block you snap into place is one line of G-code, with the same number.
Work zero
The point the program’s numbers count from. Here, as in Milling Operations, X 0, Y 0 is the front left corner of the block and Z 0 is its finished top face. The uncut stock starts at Z +1. Positions refer to the tip of the tool, on its centre line: Z −3 is 3 mm below work zero and 4 mm below that raw top. A pointed drill reaches its programmed Z at its tip; the full-diameter hole is shallower.
Modal
Most codes stay in force until another code from their group replaces them. After a G1, a line with only X80 is still a G1 move; a feed rate set with F remains available until changed. Not every word is modal. For ordinary G0/G1 moves, an omitted axis stays in place; it does not repeat the preceding incremental displacement.

A first cut: one slot

  1. (A first cut: a closed-ended slot)
  2. G21 G90 G17
  3. T1 M6
  4. S2500 M3
  5. G0 X10 Y30
  6. G0 Z2
  7. G1 Z-3 F100
  8. G1 X90 F300
  9. G0 Z20
  10. M5
  11. M30
05010003060XYX0 Y0startTOP VIEWback vise jawfront vise jawZ +20Z +10Z 0Z −10Z −20jaw topFRONT VIEW, CUT THROUGH THE BLOCK AT Y 30.0Cut depthZ 0−5−10−15−20throughfeed (G1, G2, G3): cutsrapid (G0): through the air
A first cut: one slot. Mill a closed-ended slot 10 mm wide to Z−3 with a Ø10 end mill. The raw top is Z+1, so the cut is 4 mm deep. Lift clear and stop. The program on the left, numbered line by line, and the block after it has run. Solid lines are feed moves, dashed lines are rapids; the darker the cut, the deeper it goes.

Read the program above from the top. Line 2 sets millimetres, absolute positions and the XY plane. Line 3 puts tool 1, a Ø10 end mill, in the spindle, and line 4 starts the spindle at 2500 rpm. Lines 5 and 6 move the tool through the air to just above the start of the slot. Line 7 feeds it to Z −3, cutting 4 mm below the raw top at Z +1. Line 8 feeds it along the closed-ended slot, and line 9 lifts it clear. Line 10 stops the spindle and line 11 ends the program.

Rapid moves put the tool in place through the air; feed moves cut

Rapid moves put the tool in place through the air; feed moves cut

  • G0 is a rapid move. The control uses its rapid positioning rate, subject to machine limits and applicable overrides. It is for getting the tool to where a cut starts, through the air, never for cutting.
  • G1 is a feed move. The tool moves in a straight line at the feed rate set by F, here in millimetres per minute. It can cut or feed through air.
  • The spindle has to be turning first. S sets its speed in revolutions per minute, M3 starts it turning clockwise seen from above, and M5 stops it. T1 M6 changes to tool 1.
  • A move goes in one straight line. G1 X80 Y40 Z-2 moves all three axes together and they all arrive at once. An axis left out of the line stays where it is: G1 X80 moves along X only.

A rapid into metal is a crash: the tool arrives at full traverse speed with nothing to say it should be cutting. The simulator stops a rapid at the point where it would touch the block and says so. It does the same if a feed move reaches the metal with the spindle stopped.

This simulator moves a rapid in one straight line, the way the LinuxCNC G-code reference describes G0. Controls differ in how they move during a rapid, so check how the one you use moves before you count on the path a rapid takes.

G90 and G91, G20 and G21: two modes that change what every number means

G90 and G91, G20 and G21: two modes that change what every number means

G90, absolute
A number is a position, measured from work zero. With the tool at X 10, G1 X30 moves it to X 30.
G91, incremental
A number is a distance from where the tool is now. With the tool at X 10, the same G1 X30 moves it 30 mm, to X 40.
G21, millimetres
X30 means 30 mm and F300 means 300 mm per minute with the simulator's G94 feed mode.
G20, inches
The same X30 means 30 inches, which is 762 mm, and, with G94, the feed rate is in inches per minute. A millimetre program read in inches asks for moves 25.4 times as long.

Because these modes change the meaning of every line after them, a program sets both near its start, before the first move. The simulator insists on it: a move before G20 or G21 and G90 or G91 have been set stops the program, and so does a change of units after the machine has started moving.

Work through a point list in Absolute and Incremental Positioning. Units and feed mode are separate settings; G95, where supported, uses feed per spindle revolution.

G2 turns clockwise and G3 counterclockwise, seen from above; Practice

G2 turns clockwise and G3 counterclockwise, seen from above

An arc move cuts along part of a circle in the XY plane (G17) at the feed rate. Its direction is the way it turns as seen looking down on the work from above: G2 clockwise, G3 counterclockwise. The line gives the arc’s end point with X and Y, and its centre in one of two ways.

  • I and J say where the centre is, measured in X and Y from the point where the arc starts. That is the usual mode, G91.1, and it holds even while G90 makes every X and Y a position. G90.1 makes I and J a position too in this simulator. Those decimal codes are not universal; check the control's centre-mode rules.
  • R gives the radius instead, and the control works out the centre. A positive R selects an arc of up to half a turn, a negative R the longer way round in the convention used here. An R arc cannot be a full circle, because a circle through one point has no single centre; a full circle uses I and J with its end point the same as its start.

Worked example. The tool is at X 72, Y 15, moving right along the bottom of an outline. G3 X80 Y23 I0 J8 puts the centre 0 in X and 8 in Y from the start, at X 72, Y 23. Turning counterclockwise about it, the tool rounds a corner of radius 8 and ends heading up the right-hand side at X 80, Y 23. The end point has to lie on the circle: change J8 to J6 and the start radius is 6 mm while the end radius is √(8² + 2²) = √68, about 8.246 mm, so the simulator stops at that line instead of guessing.

A radius-10 quarter arc from A 12,8 to B 22,18 around C 12,18 uses G03 I0 J10. Reversing from B to A uses G02 I-10 J0.

In the diagram, absolute endpoints and start-relative centres give G03 X22 Y18 I0 J10 from A to B, then G02 X12 Y8 I-10 J0 back to A. Each start point needs its own centre offsets. Radius format can instead use G03 X22 Y18 R10 for the first arc under the stated convention.

G17 uses X/Y and I/J; G18 uses X/Z and I/K; G19 uses Y/Z and J/K. I, J and K correspond to centre components along X, Y and Z. Judge arc direction looking from the positive normal axis toward the plane: +Z for XY, +Y for XZ, +X for YZ. The simulator supports XY arcs only.

Practice quarter arcs

Arcs: a rounded outline

  1. (Groove around a rounded rectangle)
  2. G21 G90 G17
  3. T2 M6
  4. S3000 M3
  5. G0 X28 Y15
  6. G0 Z2
  7. G1 Z-1.5 F80
  8. G1 X72 F250
  9. G3 X80 Y23 I0 J8
  10. G1 Y37
  11. G3 X72 Y45 I-8 J0
  12. G1 X28
  13. G3 X20 Y37 I0 J-8
  14. G1 Y23
  15. G3 X28 Y15 I8 J0
  16. G0 Z20
  17. M5
  18. M30
05010003060XYX0 Y0startTOP VIEWback vise jawfront vise jawZ +20Z +10Z 0Z −10Z −20jaw topFRONT VIEW, CUT THROUGH THE BLOCK AT Y 15.0Cut depthZ 0−5−10−15−20throughfeed (G1, G2, G3): cutsrapid (G0): through the air
Arcs: a rounded outline. With a Ø6 end mill, follow a 60 × 30 rounded rectangle with R8 corners at Z−1.5. The groove is 2.5 mm below the raw top, Z+1. Four G3 arcs turn the corners. The program on the left, numbered line by line, and the block after it has run. Solid lines are feed moves, dashed lines are rapids; the darker the cut, the deeper it goes.
A drilling cycle packs a whole hole into one line

A drilling cycle packs a whole hole into one line

G81 X15 Y15 Z-8 R2 F120 drills a hole at X 15, Y 15. The tool moves there at rapid, comes down at rapid to R, the height just above the work where feeding begins, feeds down to Z, the drill-tip depth, and rises back up at rapid.

  • How high it rises. In this simulator G99 rises to R between holes, and G98 rises to the height the tool was at when the cycles began, or to R if that is higher. The simulator needs one of the two before the first G81.
  • The cycle is modal. After a G81, a line with only X or Y drills another hole with the same depth, R and feed. G80 ends the cycle; after it, a line with only X or Y is an error until a G0 or G1 says how to move.
Without cutter compensation, program the tool centre path

Without cutter compensation, program the tool centre path

With cutter compensation off, as in this simulator, X and Y describe where the centre of the tool goes. The metal is cut by its edge, one radius further out. A Ø10 end mill fed along Y 30 cuts a slot from Y 25 to Y 35. To leave a wall at X 80 with the metal beyond it cut away, the centre runs along X 85. Milling Operations works through the same rule for a shoulder and a pocket.

Controls can do that offset for you with cutter radius compensation, G41 and G42. The simulator does not model it, so every program here is written as the path of the centre, and a line with G41 or G42 stops with a message. CNC Axes and Offsets explains left/right compensation and tool-length offsets.

Simulator: build a program from blocks and watch it run; Interactive

Simulator: build a program from blocks and watch it run

Pick a program, change it, and run it on a simulated block of metal in a vise. Every block is one line of G-code; the same interpreter reads it whether you build it from blocks or type it, and the machine runs exactly what it reads.

How to drive it. Press a block in the program to open it and change its numbers; leave a number empty to leave that word out of the line. Add a block from the row above the program, either by pressing it, which puts it after the open block, or by dragging it into place. Drag a block by its handle to move it; with the keyboard, focus the handle and press Alt with ↑ or ↓. Play runs the program, Step runs one block and stops, Back goes back one block and Restart starts again with fresh stock. The block that is running is outlined in gold, and its move is drawn in gold on the views; press a move in the top view to open the block that makes it. Any change to the program puts the simulation back to its start, so it always shows the program as it now is.

Holes: a drilling cycle

  1. (Four holes with a drilling cycle)
  2. G21 G90 G17
  3. T3 M6
  4. S1500 M3
  5. G0 X15 Y15
  6. G0 Z10
  7. G98 G81 X15 Y15 Z-8 R2 F120
  8. X85
  9. Y45
  10. X15
  11. G80
  12. G0 Z20
  13. M5
  14. M30
05010003060XYX0 Y0startTOP VIEWback vise jawfront vise jawZ +20Z +10Z 0Z −10Z −20jaw topFRONT VIEW, CUT THROUGH THE BLOCK AT Y 45.0Cut depthZ 0−5−10−15−20throughfeed (G1, G2, G3): cutsrapid (G0): through the air
Holes: a drilling cycle. Drill four Ø6 holes with the tip reaching Z−8: 9 mm below the raw top, Z+1. Full-diameter depth is shorter because of the drill point. G81 drills the first hole; X or Y lines repeat it until G80. The program on the left, numbered line by line, and the block after it has run. Solid lines are feed moves, dashed lines are rapids; the darker the cut, the deeper it goes.
Things to try in the G-code simulator; Checklist

Things to try in the G-code simulator

  • Step through the slot program one block at a time. Watch the machine state change at blocks 2, 3 and 4 before anything moves.
  • Make the slot deeper and shorter, then run it again. Which numbers did you change?
  • Drag the Spindle on block below the first feed move, and read why the machine stops.
  • Open block 7 of the slot program and switch it from G1 to G0. What does a rapid do when it meets the metal?
  • Change the slot program to G91. Before you press Play, work out where its first move will go.
  • In the arcs program, swap one G3 for a G2, and see which way that corner turns now.
  • In the holes program, try G99 instead of G98, then add a fifth hole.
  • In the G-code text, type a line the simulator does not read, such as G41 or M7, and read what it says.
What the simulator reads, and what it leaves out

What the simulator reads, and what it leaves out

The simulator reads a small part of G-code, modelled on the way the LinuxCNC control reads it. Any other code, or a line that is not G-code at all, stops the program at that line with a message saying what is wrong. Nothing is skipped.

Motion
G0 rapid, G1 feed, G2 and G3 arcs in the XY plane, G81 drilling cycle and G80 to end it.
Modes
G17 XY plane, G20 inches, G21 millimetres, G90 absolute, G91 incremental, G90.1 and G91.1 for I and J, G94 feed per minute, G98 and G99 for the drilling cycle. G40, G49 and G54 are read as the settings the simulator always has: no cutter compensation, no tool length offset, and its one work offset.
Machine
M3 spindle on, M5 off, M6 tool change, M8 and M9 coolant on and off (shown, with no effect on the cut), M2 or M30 program end: stops spindle and coolant, restores G90 and G1, and leaves the tool at its last position.
Words
X Y Z positions, I J arc centre, R arc radius or drilling R plane, F feed rate, S spindle speed, T tool, N line number, and comments in brackets or after a semicolon.
Tools
T1 Ø10 end mill, T2 Ø6 end mill, T3 Ø6 drill, T4 Ø12 spot and chamfer tool, T5 Ø50 face mill: the tools of Milling Operations, with the same rules about what each can cut.
  • Some rules are the simulator’s own. It needs units and distance mode set before the first move, a speed before M3, G98 or G99 before a drilling cycle, and, for a tool change, the spindle stopped and the tool at Z +20 or higher. A real control may be less strict, or strict about different things.
  • Only straight down and straight across, through metal. The block is modelled as a height above each point, so the tool can cut moving along Z alone, or in X and Y at one height. A move in X or Y and Z at once is followed exactly through the air, but stops if it would reach the metal: ramping and helical moves are not modelled.
  • Arcs are cut as short straight chords, each within 0.01 mm of the true arc. They are drawn as true arcs.
  • Simple timing. Feed moves run at their F and rapids at 5000 mm per minute, with no acceleration, no blending of one move into the next, and no time for tool changes.
  • Not a check of a real program. The speeds and feeds in the examples are round teaching numbers, not cutting recommendations; Feeds and Speeds is about choosing them. A program that runs here has met only this simulator’s rules. Controls differ in the codes they accept, in their starting modes and in how they change tools, so a program for a real machine is checked against the manual for its control. See CNC Programming and Prove-Out for graphics, dry run, single block and overrides; follow Equipment Operating Procedure for machine use.
How the simulator reads and runs a program

Each line is read in the order a control carries out the parts of a line: feed rate, spindle speed and tool first, then the tool change, the spindle, coolant, the modes, the move, and last the program end. So S2500 M3 G1 X20 F100 starts the spindle before it moves. Two codes from one group on a line, such as G0 G1, are an error.

The interpreter first reads the whole program, and a second machine runs it at once to find its path, its time and the first place it would stop; that is the path drawn before you press Play. Pressing Play runs the program again on the block, move by move. The metal, the vise and the tools are those of the simulator in Milling Operations: every cut is kept as the space the tool swept, and a move that would break a tool rule or hit the vise stops where it would happen.

An arc’s end point has to be on its circle to within 0.005 mm or 0.1 % of the radius, whichever is larger. A rapid, or a feed with the spindle stopped, stops a hair short of the first metal it would touch. The volume removed is an estimate from a 0.25 mm grid.