Structure of an NC program
The structure of an NC program explained: program start, tool call, approach, machining and program end – with a commented G-code example from CAM output.
In most cases, NC code follows a clear structure – especially when it was generated by CAM software. Depending on the section, the program contains different information and instructions: from the program header through tool calls to the actual machining blocks.
The following graphic shows what such a structure looks like:
The structural elements at a glance
The individual structural elements contain different information:
- Program start
- Program name
- Program header
- Additional information
- Tool call
- Tool information
- Spindle speed
- Operation start
- Structuring element (comment, section marker)
- Speed change
- Machining configuration
- Main spindle / counter spindle
- Driven tools
- Approach
- Tool length compensation
- Work offset
- Tilting
- Coolant
- Machining configuration
- RTCP
- Transmit
- Unclamping / clamping the rotary axes
- Machining
- Feed rate
- Radius compensation
- Geometry information
- Machining cycles
- Retract
- Retract to safe height
- Coolant off
- Program end
- Spindle and coolant stop
- Program end command (M30)
Example: a CAM-generated program skeleton
This is what the skeleton of a typical CAM-generated milling program looks like. The comments in parentheses map each block to the structural elements in the graphic:
%
O1000 (PROGRAM NAME)
G90 G94 G17 G21 (SAFETY BLOCK: ABSOLUTE, MM/MIN, XY PLANE, METRIC)
T1 M6 (TOOL CALL: LOAD TOOL 1)
S8000 M3 (SPINDLE CLOCKWISE AT 8000 RPM)
G54 G0 X10 Y10 (APPROACH: WORK OFFSET G54, RAPID TO START POSITION)
G43 H1 Z50 (ACTIVATE TOOL LENGTH COMPENSATION, SAFE HEIGHT)
M8 (COOLANT ON)
G1 Z-2 F300 (FEED DOWN TO CUTTING DEPTH)
G1 X60 F800 (MACHINING: LINEAR MOVE AT CUTTING FEED)
G1 Y40
G1 X10
G0 Z50 (RETRACT TO SAFE HEIGHT)
M9 (COOLANT OFF)
M5 (SPINDLE STOP)
M30 (PROGRAM END)
%Each subsequent operation repeats this pattern: tool call, approach, machining, retract. Only after the last operation does M30 end the program.
The exact syntax of individual commands differs from control to control. The example uses widely supported standard commands – you can find details on the individual dialects in the Controls chapter.