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,G1orF300. The letter says what the number means:X,YandZare positions,Fis the feed rate,Sthe spindle speed,Ta tool number.GandMwords 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 onlyX80is still aG1move; a feed rate set withFremains 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
(A first cut: a closed-ended slot)G21 G90 G17T1 M6S2500 M3G0 X10 Y30G0 Z2G1 Z-3 F100G1 X90 F300G0 Z20M5M30
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
G0is 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.G1is a feed move. The tool moves in a straight line at the feed rate set byF, here in millimetres per minute. It can cut or feed through air.- The spindle has to be turning first.
Ssets its speed in revolutions per minute,M3starts it turning clockwise seen from above, andM5stops it.T1 M6changes to tool 1. - A move goes in one straight line.
G1 X80 Y40 Z-2moves all three axes together and they all arrive at once. An axis left out of the line stays where it is:G1 X80moves 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 X30moves 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 X30moves it 30 mm, to X 40. - G21, millimetres
X30means 30 mm andF300means 300 mm per minute with the simulator'sG94feed mode.- G20, inches
- The same
X30means 30 inches, which is 762 mm, and, withG94, 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 whileG90makes every X and Y a position.G90.1makes 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.
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.
Arcs: a rounded outline
(Groove around a rounded rectangle)G21 G90 G17T2 M6S3000 M3G0 X28 Y15G0 Z2G1 Z-1.5 F80G1 X72 F250G3 X80 Y23 I0 J8G1 Y37G3 X72 Y45 I-8 J0G1 X28G3 X20 Y37 I0 J-8G1 Y23G3 X28 Y15 I8 J0G0 Z20M5M30
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
G99rises to R between holes, andG98rises 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 firstG81. - The cycle is modal. After a
G81, a line with only X or Y drills another hole with the same depth, R and feed.G80ends the cycle; after it, a line with only X or Y is an error until aG0orG1says 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
(Four holes with a drilling cycle)G21 G90 G17T3 M6S1500 M3G0 X15 Y15G0 Z10G98 G81 X15 Y15 Z-8 R2 F120X85Y45X15G80G0 Z20M5M30
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
G1toG0. 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
G3for aG2, and see which way that corner turns now. - In the holes program, try
G99instead ofG98, then add a fifth hole. - In the G-code text, type a line the simulator does not read, such as
G41orM7, 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
G0rapid,G1feed,G2andG3arcs in the XY plane,G81drilling cycle andG80to end it.- Modes
G17XY plane,G20inches,G21millimetres,G90absolute,G91incremental,G90.1andG91.1for I and J,G94feed per minute,G98andG99for the drilling cycle.G40,G49andG54are read as the settings the simulator always has: no cutter compensation, no tool length offset, and its one work offset.- Machine
M3spindle on,M5off,M6tool change,M8andM9coolant on and off (shown, with no effect on the cut),M2orM30program end: stops spindle and coolant, restores G90 and G1, and leaves the tool at its last position.- Words
X Y Zpositions,I Jarc centre,Rarc radius or drilling R plane,Ffeed rate,Sspindle speed,Ttool,Nline 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,G98orG99before 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.