Parametric G-Code Programming: # Variables, Arithmetic, and Real Automation Logic
Parametric G-code programming turns static toolpaths into intelligent, self-adapting programs.
By using variables, arithmetic, and logic conditions, CNC machines can automatically calculate positions, tool offsets, feedrates, and even make real-time decisions.
This guide covers real G-code macro examples, Fanuc-style variables, and Siemens/Heidenhain parameter logic for advanced automation.
📌 1. What Is Parametric Programming?
Parametric programming uses variables (#100, #101, etc.) to replace hard-coded numbers.
This allows dynamic calculations, making one program adaptable to many parts or sizes.
Traditional:
G01 X50. F200
Parametric:
#100=50.
G01 X#100 F200
Now, changing
#100changes every occurrence automatically.
📌 2. Variable Categories
| Range | Description | Retained After Power Off? |
|---|---|---|
| #100–#199 | Local variables | ❌ No |
| #500–#599 | Common variables | ✅ Yes |
| #1000+ | System variables | ⚙️ Machine dependent |
Local variables are cleared after reset; common ones persist until changed.
📌 3. Basic Arithmetic Operations
| Operation | Example | Result |
|---|---|---|
| Addition | #100 = 5 + 2 | 7 |
| Subtraction | #101 = 10 - 4 | 6 |
| Multiplication | #102 = 2 * 3 | 6 |
| Division | #103 = 20 / 5 | 4 |
| Power | #104 = 2 ** 3 | 8 |
| Modulus | #105 = 7 MOD 3 | 1 |
Complex formulas are supported:
#200 = [#101 + #102] * [#103 - 5]
📌 4. Example — Dynamic Hole Pattern Generator
#100 = 4 (NUMBER OF HOLES)
#101 = 25. (SPACING)
#102 = 0
WHILE [#102 LT #100] DO1
G81 X[#102 * #101] Y0 Z-15. R2. F150
#102 = [#102 + 1]
END1
G80
M30
Automatically drills 4 holes spaced 25 mm apart — one code fits any pattern size.
📌 5. IF Statements — Conditional Logic
#200 = 45
IF [#200 GT 50] THEN GOTO 100
G01 Z-10. F100
N100 G01 Z-20. F200
| Operator | Meaning |
|---|---|
| EQ | Equals |
| NE | Not equal |
| GT | Greater than |
| LT | Less than |
| GE | Greater or equal |
| LE | Less or equal |
Enables conditional path control and logic branching.
📌 6. Example — Conditional Feedrate Based on Diameter
#500 = 50. (DIAMETER)
IF [#500 GT 100] THEN #501 = 1500
IF [#500 LE 100] THEN #501 = 1000
G97 S#501 M03
Automatically adjusts spindle speed based on part diameter.
📌 7. Loops — Repeating Code Blocks
#100 = 0
WHILE [#100 LT 10] DO1
G01 X[#100 * 10] Y0. F500
#100 = [#100 + 1]
END1
Creates repeated motion automatically — no copy-paste needed.
📌 8. G65 Macro Subprogram Call
G65 P9001 A25. B10.
Inside O9001:
O9001
#100 = #1 + #2
G01 X#100 F200
M99
| Letter | Variable |
|---|---|
| A | #1 |
| B | #2 |
| C | #3 |
Passes arguments to subprograms — similar to a function call in programming.
📌 9. Macro Arguments Reference
| Letter | Variable |
|---|---|
| A | #1 |
| B | #2 |
| C | #3 |
| D | #7 |
| E | #8 |
| F | #9 |
| H | #11 |
| I | #4 |
| J | #5 |
| K | #6 |
Each letter automatically maps to a numbered variable when passed to G65 macros.
📌 10. Fanuc Example — Automatic Tool Change Reminder
#500 = [#500 + 1]
IF [#500 GE 50] THEN #3006=1 (TOOL CHANGE REQUIRED)
Alerts operator every 50 cycles to replace the tool.
📌 11. Haas Example — Adaptive Drilling Depth
#100 = 0
#101 = 20
WHILE [#100 LT 5] DO1
G81 Z[-#101] R2. F150
#101 = [#101 + 5]
#100 = [#100 + 1]
END1
Increases drilling depth by 5 mm after each hole — smart automation.
📌 12. Siemens Example — Parameter Variables
DEF REAL DEPTH=20
DEF INT HOLES=5
FOR I=1 TO HOLES
CYCLE83(DEPTH=-DEPTH*I, PECK=5)
END
Siemens syntax supports structured FOR loops and typed variables.
📌 13. Heidenhain Example — Q Parameterization
Q1 = +20
Q2 = +5
LBL 1
CYCL DEF 200 DRILLING Q201=-Q1
Q1 = Q1 + Q2
LBL CALL 1 REP3
Automatically increases depth per repetition using Q parameters.
📌 14. Arithmetic Tool Compensation
#200 = #5063 - 2.0 (OFFSET Z BY 2 MM)
G43 H01 Z#200
Dynamically adjusts tool offset depending on probe readings.
📌 15. Real Example — Bolt Circle Machining
#100 = 6 (HOLE COUNT)
#101 = 50. (CIRCLE RADIUS)
#102 = 0
WHILE [#102 LT #100] DO1
#103 = COS[#102*360/#100] * #101
#104 = SIN[#102*360/#100] * #101
G81 X#103 Y#104 Z-15. R2. F200
#102 = [#102 + 1]
END1
G80
Automatically positions holes around a circle — no CAD needed.
📌 16. String Logic and Status Checks (Fanuc)
| System Variable | Function |
|---|---|
| #4001 | Current motion mode |
| #4120 | Active tool number |
| #3000 | Alarm trigger |
| #5003 | Spindle load |
IF [#5003 GT 85.] THEN #3006=1 (LOAD HIGH)
Enables real-time process monitoring.
📌 17. Conditional Machining with Macro Logic
IF [#500 LT 10] THEN G65 P9000
IF [#500 GE 10] THEN G65 P9001
Runs different subroutines depending on measured data or sensor feedback.
📌 18. Complex Mathematical Example — Taper Calculation
#100 = 50. (LENGTH)
#101 = 20. (SMALL DIA)
#102 = 40. (LARGE DIA)
#103 = [#102 - #101] / [#100]
G01 X#102 Z0
G01 X#101 Z-#100 F[#103*100]
Calculates taper angle and automatically sets feed proportional to slope.
📌 19. AI + Macro Integration (2025–2030)
- Self-optimizing macros using sensor feedback
- Adaptive feedrate macros powered by AI-predicted chatter zones
- Auto-calculated offsets based on real probe data
- Smart learning loops to refine programs over time
- Cloud-synced variables for production traceability
Future CNC macros will function like real-time microcontrollers — analyzing and reacting in milliseconds.
📌 20. Best Practices
| Goal | Method |
|---|---|
| Reusable programs | Replace constants with variables |
| Smart logic | Use IF, WHILE, and arithmetic |
| Multi-part automation | Combine with subprograms |
| Safe operation | Include error checks before loops |
| Data tracking | Log #500+ variables for analysis |
✅ Conclusion
Parametric G-code transforms your CNC into an intelligent automation system.
Using variables, logic, and math, you can create adaptive, self-adjusting programs that react to tool wear, part size, or even real-time sensor data.
In Industry 4.0, macros are not “extra” — they’re the language of intelligent manufacturing.
Leave a comment