A normal G-code program is a list of fixed numbers. Change the part width and you rewrite the program. Fanuc Custom Macro B adds variables, arithmetic and loops, so one program can adapt to a new size or depth by changing a single value.
Several generators in the Shader7 CNC Machinist hub write Macro B code. That makes them a practical way to learn: you can generate a program, see the macro in context and change it with confidence. This guide uses their output as examples.
Variables
A macro variable is written as # followed by a number. You assign it with =:
#1=0 (CURRENT DEPTH VAR)
#2=-5.000 (TOTAL DEPTH)
#3=-1.000 (DEPTH OF CUT)
Anywhere a number would go, you can use a variable instead: G01 Z#1 F200.
| Range | Type | Typical use |
|---|---|---|
| #1 – #33 | Local | Working values inside a program or macro call |
| #100 – #199 | Common (cleared at power-off on many controls) | Values shared between programs |
| #500 – #999 | Common (retained) | Values that must survive a restart, such as counters |
| #1000 and up | System | Machine data such as offsets, modal states and alarms |
The Shader7 generators use local variables (#1 to #33) for part and tool values and common variables (#101 to #114) in the XY chamfer program for stock limits.
Arithmetic and functions
Macro B evaluates expressions in square brackets:
#4=[#30/2] (radius from diameter)
#7=#4+#5 (radius plus cutter radius)
#5=[[#3/2]+[#9*TAN[#4]]] (radius at the current chamfer height)
Useful functions include SIN, COS, TAN, ATAN, SQRT, ABS, ROUND, FIX (round down) and FUP (round up). Angles are in degrees.
Example 1: the face-milling depth loop (IF / GOTO)
The Mill / Drill Contour generator's Face Milling tab writes this loop:
#1=0 (CURRENT DEPTH VAR)
#2=-5.000 (TOTAL DEPTH)
#3=-1.000 (DEPTH OF CUT)
N10 (DEPTH LOOP)
#1=#1+#3
IF[#1 LT #2] THEN #1=#2
G00 Z...
G00 X... Y...
G01 Z#1 F...
(--- MILLING PASSES ---)
...
G00 Z...
IF[#1 GT #2] GOTO 10 (REPEAT FOR NEXT DEPTH)
How it works:
-
1 starts at 0 and gets one depth of cut deeper on each loop.
IF[#1 LT #2] THEN #1=#2stops the last pass from going below the total depth. With a total of −5.5 and a cut of −1, the final pass is −5.5, not −6.- After each layer,
IF[#1 GT #2] GOTO 10jumps back to N10 until the final depth has been cut.
To take one extra skim at the machine, you only change #2.
Example 2: the circular chamfer (WHILE / DO / END)
The Circular Chamfer generator uses a WHILE loop:
WHILE[#1 GT #2] DO1
#9=ABS[#1-#2] (DISTANCE TO BASE)
#5=[[#3/2]+[#9*TAN[#4]]] (CURRENT RADIUS)
G90 G1 Z#1 F...
G91 G1 G41 D#7 X#5 F...
G03 X0 Y0 I-#5 J0
G1 G40 X-#5 F...
#1=#1-#6 (NEXT PASS)
END1
WHILE [condition] DO1 … END1 repeats the block while the condition is true. The number after DO and END (1, 2 or 3) labels the loop, so loops can be nested. Here the radius grows with height above the base, so each pass cuts a slightly bigger circle, forming a cone.
Example 3: the helical circle mill (IF / GOTO with incremental moves)
The ID / OD Circular Interpolation generator combines G52, incremental moves and a loop:
#4=[#30/2] ;
#6=#4-#5 ;
G52 X#16 Y#17 ;
...
G91 G01 Y#6 F... ;
N29 G03 X0 Y0 Z-#3 I0 J-#6 ;
#15=[#15-#3] ;
IF[#15 GT #2] GOTO 29 ;
Each pass of N29 is one full circle that also moves down by #3, giving a helix. #15 tracks the current depth in absolute terms, because the moves themselves are incremental.
Example 4: the XY chamfer offset per pass
The XY Chamfer generator stores the raw stock edges in #101 to #104, then for each pass calculates how far in to move along the bevel:
#8=[ABS[#2] - ABS[#1]] / TAN[#4] (OFFSET)
#111=#101 + #8
#112=#102 - #8
The same program can then chamfer a different size of part by changing the stock limits.
Comparison operators
| Operator | Meaning |
|---|---|
| EQ | equal to |
| NE | not equal to |
| GT | greater than |
| GE | greater than or equal to |
| LT | less than |
| LE | less than or equal to |
Good habits with macros
- Comment every variable. The generators add a comment on each assignment, such as
(TOTAL DEPTH). Keep doing that when you edit. - Guard the last pass. Use a clamp like
IF[#1 LT #2] THEN #1=#2so rounding never takes the tool too deep. - Avoid endless loops. Make sure the loop variable always moves towards the exit condition. A depth of cut of 0 never finishes.
- Watch G90/G91. Incremental moves inside loops are powerful but easy to misread. Track absolute position in a variable, as the circle program does with #15.
- Check your control's options. Macro B is an option on some Fanuc controls. Haas and many others support compatible syntax, but test on your control.
- Single block through the first loop and watch the macro variable page to confirm values change as expected.
Try it yourself: change one variable
- Open the Face Milling tab and generate a program for a 200 × 100 mm block with a total depth of 3 mm and a 1 mm depth of cut.
- Find
#2=-3.000 (TOTAL DEPTH)and#3=-1.000 (DEPTH OF CUT). - Without regenerating, imagine changing
#2to −3.5. The loop now runs four times: −1, −2, −3, then the clamp line limits the last pass to −3.5. - Change
#3to −0.5 instead, and the loop runs six times for the original 3 mm.
Doing this on paper, then confirming with the control's graphics, is the fastest way to become confident with macros.
Debugging a macro on the machine
- Watch the macro variable screen while running in single block. You can see #1, #2 and the rest change on each pass.
- Add a temporary M00 inside a loop to stop and check the value of a variable.
- Use alarms on purpose. Many controls let you write
#3000=1 (MESSAGE)to stop with your own alarm text when an input is invalid, for exampleIF[#3 GE 0] THEN #3000=1 (DOC MUST BE NEGATIVE). - Check for vacant variables. An unset variable is "vacant", not zero, and behaves differently in some expressions. Always assign every variable before using it.
Frequently asked questions
Is Custom Macro B standard on every Fanuc control?
Not always. It is an option on some models. Check your control's parameters or ask your machine builder.
What is the difference between IF/GOTO and WHILE?
IF/GOTO jumps to a block number when a condition is true and can jump forwards or backwards. WHILE/DO/END repeats a block while a condition is true and is easier to read for loops.
Can I edit the generated variables at the machine?
Yes. That is the point of the macro format. Change the values in the assignment lines at the top of the program, such as the final depth or angle, without touching the toolpath lines.
Why does the face-milling loop use LT and GT with negative numbers?
Depths below the top face are negative, so a deeper cut is a smaller number. "#1 LT #2" means #1 has gone deeper than the final depth.
Which Shader7 tools write macro code?
The Mill / Drill Contour face milling program, the Circular Chamfer and XY Chamfer generators, the ID / OD Circular Interpolation generator, and the Parametric Macro dialect in 3D CAD Studio.
Generate a macro program
Open the Mill / Drill Contour generator, create a face milling program and read its depth loop. Then try the chamfer and ID / OD generators to see WHILE loops and helical IF/GOTO loops.