Open almost any professionally written CNC program and one of the first lines looks something like G17 G21 G40 G49 G80 G90 G94. It moves nothing and cuts nothing. It is called a safety line, a safe start block or a safe start line, and it is there because a CNC control has a memory. Most G-codes are modal: once commanded, they stay active until something changes them. A safety line sets the important ones to known values, so the program does not depend on what the last program, the last MDI command or the last interrupted run left behind.

Why the control’s memory is the problem

Modal codes save typing. You write G01 once and every following line feeds until you write G00. You write G91 once and every move is incremental until you write G90. That convenience has a cost: the control carries those states across the end of a program into the next one, depending on how reset is configured, and certainly across an interrupted run or an operator’s MDI command.

So a program that assumes absolute positioning, but starts on a control left in G91 by an operator jogging in MDI, will make its first move relative to wherever the tool is. A program that assumes no cutter comp, but starts with G41 still active from an aborted job, will offset every move by the tool radius. The modal vs non-modal G-codes post covers the mechanism in more depth. The safety line is the fix: assume nothing, state everything that matters.

Where the leftover states come from

It helps to know how a control ends up in an unexpected state, because each route is a reason for one of the codes. The most common are:

  • The previous program. Depending on how reset is configured, some modal states survive M30 or a reset and carry into the next program.
  • MDI commands. An operator types G91 to jog a known distance, or G20 to check an inch dimension, and leaves it active.
  • An interrupted run. A feed hold and reset in the middle of a canned cycle, a compensated contour or an incremental section leaves those modes in place.
  • A different programmer or CAM post. Programs from different sources assume different defaults, and one that sets G18 for a lathe-style operation on a mill-turn can leave it active for the next.
  • Power on defaults. The modes a control starts in after power up are set by parameters, and they may not match what a program assumes.

None of these is unusual. Every shop sees all five in a normal week. The safety line exists because they are routine, not because they are rare.

A typical mill safety line, code by code

CodeWhat it setsModal groupWhat goes wrong without it
G17XY plane for arcs and compensationPlane selectionArcs and cutter comp in the wrong plane after a G18 or G19 job
G21 or G20Metric or inch unitsUnitsEvery dimension 25.4 times too large or too small
G40Cutter radius compensation offCutter compEvery move offset by the tool radius
G49Tool length compensation offTool lengthUnexpected Z offset until the next G43
G80Canned cycle offMotion / cycleFirst positioning move drills a hole with a leftover G73 or G83 peck cycle
G90Absolute positioningDistance modeFirst move relative to wherever the tool is
G94Feed per minuteFeed modeFeed read per revolution, far too fast or too slow

Each code earns its place. Units are a good example: a metric program run in inch mode makes every move 25.4 times larger, which on a mill usually means an overtravel alarm or a crash. The G20 vs G21 inch vs metric post shows how easy it is to inherit the wrong one.

The LinuxCNC references for G40, G49 and G80 describe each cancel code individually. The safety line simply puts them all in one place.

Variations you will see

There is no single correct safety line. Shops and CAM posts vary it:

  • Work offset included. Many add G54 or the job’s offset: G17 G21 G40 G49 G80 G90 G54. That makes the offset explicit rather than inherited.
  • G49 left out. Some programmers prefer not to cancel tool length comp at the start, because the next tool change and G43 will set it anyway, and on some controls G49 in the wrong place can cause a sudden Z move.
  • G28 or G53 retract. Some add a safe Z retract on the next line, such as G91 G28 Z0 followed by G90, or a G53 move. The choice is discussed in Fusion 360 G28 vs G53 safe retract.
  • Lathe versions. On a lathe the line differs: G18 instead of G17, G40 still, often G97 or G96 for spindle mode and G99 or G98 for feed per revolution or per minute, which on a lathe have different meanings from the mill’s canned cycle return codes.
  • Split across lines. Some controls limit how many G-codes from different groups can share a line, so the safety line may be two or three lines.

What matters is the principle: every modal state your program relies on is set explicitly before the first move.

One per tool, not just one per program

A safety line at the very top protects the start of the program. It does nothing for an operator who restarts at tool 5 after a broken tool. If tool 4 left G41 active, or a canned cycle running, the restart inherits it.

The fix is a safety line at the start of every tool section. Most CAM posts do this automatically, writing a short block before each tool change. Hand programmers should copy the habit. It turns every tool into a safe restart point, which is the foundation of how to safely restart a CNC program from the middle.

What a safety line cannot do

A safety line resets modes. It cannot fix the physical setup or the data tables. It does not check that the work offset values are right, that the tool length offset matches the tool in the spindle, that the right tool is in the right pocket, or that a clamp is clear of the path. Those are setup and prove-out jobs.

It also cannot make up for a program that changes modes mid-tool and never sets them back. If a tool section switches to G91 for a few moves, it should switch back to G90 itself, not rely on the next tool’s safety line to tidy up.

Safety lines and CAM posts

Most CAM systems write a safety line automatically, and the post processor decides what goes in it. It is worth reading the top of a posted program once for each machine and checking the line matches what the machine and shop expect. Posts copied from a different machine sometimes carry a units code, a plane or a feed mode that suits the original machine and not yours. Fixing it once in the post fixes every future program.

Writing your own safety line

To build a safety line for a specific machine:

  1. List every modal group your programs depend on: plane, units, comp, length, cycle, distance mode, feed mode, work offset.
  2. Pick the default your programs assume for each.
  3. Check the controller manual for any limits on mixing groups on one line.
  4. Test it in MDI with the tool well clear, then check the modal status screen shows every code you expected.
  5. Use the same line in every program and at every tool, so operators learn to recognise it.

Consistency is half the value. An operator who sees the same safety line at every tool notices immediately when one is missing.

A worked example: a two tool program

%
O1001 (TWO TOOL EXAMPLE)
G17 G21 G40 G49 G80 G90 G94 G54
T1 M06
G43 H01 Z50. S3000 M03
(... tool 1 cutting ...)
G80 G40
G91 G28 Z0
G90
T2 M06
G17 G21 G40 G80 G90 G94 G54
G43 H02 Z50. S2500 M03
(... tool 2 cutting ...)
G80 G40
G91 G28 Z0
G90
M30
%

The top line sets everything. The second tool repeats it, leaving out G49 because G43 follows on the next line. Each tool ends by cancelling its own cycle and comp before retracting. If the operator restarts at T2, the program sets its own state there. Line by line, nothing is assumed. The M30 vs M02 program end post explains the end of the program.

Common safety line mistakes

  • Leaving out the units code, so a metric program runs in inch mode after an inch job.
  • Having a safety line only at the top, so mid-program restarts are unsafe.
  • Copying a mill safety line onto a lathe, where G98 and G99 mean feed modes rather than return planes.
  • Putting G49 somewhere it causes an unexpected Z move on that control.
  • Assuming the safety line has checked the setup.

Why beginners should learn it first

A safety line is one of the first things a new programmer should be able to write from memory, because it forces the right question every time: which modes does my program depend on? Knowing what each code means, and what goes wrong without it, is a large part of knowing G-code. The G-code practice drills include every code in a typical safety line.

Bottom line: assume nothing

A safety line sets plane, units, comp, length, cycle, distance mode, feed mode and often the work offset before any motion. It exists because modal codes outlive the program that set them. Put one at the top of every program and at the start of every tool, adapt it to your control and machine type, and remember that it resets modes, not the setup.

Frequently asked questions

What is a safety line in G-code?

A safety line, or safe start block, is a line near the top of a program or before each tool that sets every important modal state to a known value, for example G17 G21 G40 G49 G80 G90 G94. It protects the program from states left active by a previous program, MDI commands or an interrupted run.

What does G17 G40 G49 G80 G90 mean?

G17 selects the XY plane, G40 cancels cutter radius compensation, G49 cancels tool length compensation, G80 cancels any canned cycle and G90 sets absolute positioning. Together they put the control into a clean, predictable state before the program starts moving the tool.

Should every tool have its own safety line?

Most shops say yes. A safety line at the start of each tool lets the operator restart the program at that tool safely, because the modal states are set again right there. Without it, a restart in the middle inherits whatever the previous tool left active, which is how mid-program restarts crash.

What is the best way to memorise a safety line?

Learn it as one phrase with a reason for each code: plane, units, no comp, no length, no cycle, absolute, feed per minute. Write it from memory every day for a week. A free app like G-Code Sprint quizzes the individual codes in timed rounds, so the meaning of each word becomes automatic.

When is a safety line not enough?

When the problem is not a modal state. A safety line cannot fix a wrong work offset value, a wrong tool length in the offset table, a missing tool, or a clamp in the path. It sets the control’s modes, not the physical setup, so it complements careful setup and proving out rather than replacing them.

Sources