Parametric Programming with FANUC-style Macros: Complete Reference & Practical Examples
Meta Description: Learn FANUC-style macro programming with full syntax, variable types, conditional statements, and real-world CNC examples. Unlock advanced logic for high-efficiency and smart automation.
🔧 What is Parametric Programming?
Parametric programming in CNC involves using variables, conditional logic, and loops to create reusable, flexible, and dynamic G-code programs. It significantly reduces repetition, supports logic-based decisions, and adapts to variable part configurations—perfect for high-mix manufacturing.
FANUC-style macro programming is the most widely supported system in industrial controllers like FANUC, Haas, Mitsubishi, and Siemens (to a degree).
📊 Variable Types in Macro B
| Variable Range | Usage | Persistence |
|---|---|---|
| #1 to #33 | Local temporary variables | Cleared after execution |
| #100 to #199 | Local persistent | Saved across power cycles |
| #500 to #999 | Global persistent | Used across all programs |
| #1000+ | System-specific (tool offsets, machine data) | Read-only/write depending on control |
🧠 Conditional Statements
Conditional logic lets the CNC machine make decisions. FANUC supports IF/THEN and WHILE/END loops:
#1 = 25 IF [#1 GT 20] THEN #3000=1 (VALUE TOO HIGH)
This line stops the machine with a custom alarm if #1 exceeds 20.
🔁 Looping Example: Hole Array Generator
Create a dynamic hole grid using variables:
#100 = 0 (Start X)
#101 = 0 (Start Y)
#102 = 20 (X step)
#103 = 20 (Y step)
#104 = 4 (Columns)
#105 = 3 (Rows)
#110 = 0
WHILE [#110 LT #105] DO1
#111 = 0
WHILE [#111 LT #104] DO2
G00 X[#100 + #111 * #102] Y[#101 + #110 * #103]
G81 Z-12. R2. F100
#111 = #111 + 1
END2
#110 = #110 + 1
END1
G80
This will drill 12 holes in a 4×3 matrix. Change a single variable to resize the pattern instantly!
🔄 Reusing Code with Macros and Subprograms
Use macros to define repeatable routines with different input values:
O1000 #1 = 5 (X pos) #2 = 10 (Y pos) #3 = 15 (Depth) M98 P2000 M30 O2000 G00 X#1 Y#2 G01 Z-#3 F150 G00 Z5 M99
This lets you drill at variable locations/depths using passed-in values.
📘 Macro Functions Cheat Sheet
| Function | Syntax | Description |
|---|---|---|
| Assignment | #1 = 10 | Set value |
| Arithmetic | +, -, *, /, MOD | Standard math |
| Comparison | EQ, NE, GT, GE, LT, LE | Conditional logic |
| IF-THEN | IF [#1 EQ 5] THEN … | Decision |
| WHILE-DO-END | WHILE [#1 LT 10] DO1 … END1 | Loops |
| M99 | Return | Returns to main |
| M98 | Call subprogram | Reuses code |
| #3000 | IF condition THEN #3000=1 (message) | Stops machine |
🛑 Tool Safety with Logic
Ensure tool offsets are safe before cutting:
IF [#2002 LT 0.1] THEN #3000=2 (TOOL LENGTH TOO SHORT)
This checks tool offset variable #2002 and halts if it’s too short.
🔌 Macros + Industry 4.0: The Future
As factories become smarter, macros will support:
- Sensor-integrated condition checks
- Tool life tracking with logic
- Adaptive feedrate and spindle control
- Automated tool compensation based on feedback
Some controllers allow macros to react to digital input/output, PLC flags, or part probes, making autonomous machining a reality.
✅ Summary
- Use variables to create scalable, flexible G-code
- Leverage IF/WHILE for intelligent decisions
- Write macros to reduce programming time and errors
- Validate logic with dry runs and simulation
- Plan for future-proof automation with logic-based coding
Whether you’re drilling symmetrical patterns, setting up family-of-parts jobs, or adding sensor-based logic, macro programming gives you the tools to work smarter—not harder.
Leave a comment