Professional CNC programmers rarely write every program from scratch. Instead they build libraries of reusable program structures using subprograms and macro logic.
These reusable structures allow programmers to automate repetitive machining operations, reduce program size, and create flexible programs that adapt to different part sizes.
This guide explains commonly used CNC programming patterns based on subprograms and macro logic used in professional manufacturing environments.
════════════════════════════════════════════════════════════
SECTION 1 — BASIC SUBPROGRAM STRUCTURE
════════════════════════════════════════════════════════════
Subprograms allow repeating machining operations without duplicating code.
Main program example
%
O1000
G90 G17 G40 G49 G80
G54
T1 M06
S3000 M03
M98 P2000 L4
M30
Subprogram
O2000
G01 X50 Y50 F200
G01 X0 Y50
M99
M98 calls the subprogram while M99 returns to the main program.
════════════════════════════════════════════════════════════
SECTION 2 — HOLE PATTERN SUBPROGRAM
════════════════════════════════════════════════════════════
Subprograms are commonly used to generate hole patterns.
Main program
%
O1100
G90 G17
G54
T2 M06
S2500 M03
M98 P3000 L3
M30
Subprogram
O3000
G81 X20 Y20 Z-15 R2 F120
X40 Y20
X60 Y20
G80
M99
The subprogram drills three holes and returns to the main program.
════════════════════════════════════════════════════════════
SECTION 3 — MACRO VARIABLES
════════════════════════════════════════════════════════════
Macro variables allow dynamic programming.
Example variable assignment
100 = 50
Variable used in motion command
G01 X#100 Y20 F200
This allows the program to change dimensions automatically.
════════════════════════════════════════════════════════════
SECTION 4 — LOOP MACHINING PATTERN
════════════════════════════════════════════════════════════
Loops automate repetitive operations.
Example loop program
101 = 10
WHILE[#101 LE 100] DO1
G01 X#101 Y20 F200
101 = #101 + 10
END1
This pattern automatically generates multiple machining passes.
════════════════════════════════════════════════════════════
SECTION 5 — CONDITIONAL MACHINING LOGIC
════════════════════════════════════════════════════════════
Macro programming allows conditional logic.
Example
IF[#100 GT 40] THEN G01 X50
The machine executes the command only if the condition is true.
════════════════════════════════════════════════════════════
SECTION 6 — AUTOMATED DRILLING PATTERN
════════════════════════════════════════════════════════════
Macro programs can automatically generate drilling locations.
Example
110 = 20
WHILE[#110 LE 120] DO1
G81 X#110 Y20 Z-15 R2 F120
110 = #110 + 20
END1
G80
This program drills multiple holes along a line.
════════════════════════════════════════════════════════════
SECTION 7 — TOOL LIFE MACRO
════════════════════════════════════════════════════════════
Macro variables can monitor tool usage.
Example
500 = #500 + 1
IF[#500 GT 100] THEN #3000 = 1
The machine stops after the tool has been used 100 times.
════════════════════════════════════════════════════════════
SECTION 8 — PARAMETRIC POCKET PROGRAM
════════════════════════════════════════════════════════════
Parametric programming allows machining different pocket sizes with the same program.
Example
120 = 40
G01 X#120
G01 Y#120
G01 X0
G01 Y0
Changing the variable automatically changes the pocket size.
════════════════════════════════════════════════════════════
SECTION 9 — MODULAR CNC PROGRAM DESIGN
════════════════════════════════════════════════════════════
Professional programmers structure programs using modular logic.
Typical structure
Main program
Subprogram library
Macro routines
Reusable program blocks reduce programming time and simplify program maintenance.
════════════════════════════════════════════════════════════
FINAL PRINCIPLE
Subprograms and macro programming transform CNC programs into flexible automation systems. By combining reusable program structures, variables, loops, and conditional logic, CNC programmers can create efficient programs that support complex manufacturing operations while reducing programming errors.
Leave a comment