Macro B (Fanuc Custom Macro B, also called parametric programming) adds variables, maths and logic to G-code. Instead of X2.0 you can write X[#101 + #102], test a condition with IF, repeat a block of code with WHILE, and call your own cycle with G65, passing it arguments like a built-in canned cycle. That lets one program cut a whole family of parts, drill any bolt circle, or react to probing results. This guide covers the syntax with working examples. The examples follow the Fanuc standard; Haas and many other controls use very similar syntax, but always check your control's manual.
1. The Foundation: Variables
In macro programming, instead of hard-coding a number, you use a variable. A variable is the pound/hash sign (#) followed by a number. Think of it as a box that holds one number. Which box you use matters, because the ranges behave differently.
Variable Types (Fanuc Standard)
- #0 (Null or "vacant"): always empty. Use it to test whether a variable has a value, for example
IF [#24 EQ #0]. A vacant variable is not the same as zero in anEQorNEtest, but it counts as zero in arithmetic. - #1 to #33 (Local variables): belong to one macro call. Each
G65call gets its own fresh set, filled with the arguments you pass. They are cleared when the macro ends with M99 and on reset. - #100 to #199 (Common variables): shared by all programs and macros. They are cleared when the power goes off (a parameter can change this on some controls). Some controls with the larger option also have #100 to #499.
- #500 to #999 (Permanent common variables): shared by all programs and kept when the power is off. Use them for settings that must survive, such as a part counter or a calibrated value. How many you have depends on the option fitted.
- #1000 and up (System variables): read or change the machine's own data: offsets, modal states, positions, alarms and timers. Some are read-only.
Useful system variables
System variable numbers vary between control models and options, so confirm them in your manual before writing to any of them. These are common on Fanuc mills:
| Variable | What it holds |
|---|---|
#3000 | Write a number (1 to 200) and a message to stop the program with an alarm: #3000=1 (DIA MISSING) |
#3006 | Stop with a message the operator can continue from with cycle start |
#3001 | Millisecond timer, useful for timing a cycle |
#4001 to #4022 | Current modal G-codes by group, such as #4003 = 90 or 91, #4014 = active work offset (54 to 59) |
#5021 to #5023 | Current machine position in X, Y and Z |
#5221 to #5223 | G54 work offset X, Y and Z (G55 starts at #5241, and so on in steps of 20) |
2. Assigning Values and Basic Math
You assign a value to a variable with an equals sign. Once assigned, you can use the variable anywhere a number would go (after X, Y, Z, F and so on).
#100 = 5.0125.0
(The machine reads this exactly like G01 X5.0)
G01 X#100 F20.0
You can also do maths inside square brackets [ ]. The control solves it before it makes the move. Round brackets ( ) are comments in G-code, so they can never be used for maths.
#102 = 0.512.0 (Tool Radius)
(Move to Length plus Tool Radius)
G01 X[#101 + #102]
Operators and functions
- Arithmetic:
+ - * /. Functions and brackets are worked out first, then*and/, then+and-. Brackets can be nested up to five deep. - Trig:
SIN[#1],COS[#1],TAN[#1]andATAN[#1]/[#2], all in degrees. - Other functions:
SQRT,ABS,ROUND,FIX(round down),FUP(round up), plus logicalAND,ORandXOR.
Example: the length of the diagonal of a 3" × 4" rectangle is #110 = SQRT[3*3 + 4*4], which gives 5.0. The number of 0.75" passes needed to cover 4.2" is #111 = FUP[4.2 / 0.75], which gives 6 (4.2 ÷ 0.75 = 5.6, rounded up).
3. The Real Power: Logic and Loops
Maths is useful, but logic is where macros shine. You can make the program decide what to do with IF, GOTO and WHILE.
GOTO (Jump)
GOTO10 jumps straight to the block numbered N10 in the same program, forwards or backwards.
IF [#1 GT 10] GOTO20 jumps only if the condition is true. IF [#1 GT 10] THEN #2 = 0 runs one assignment only if it is true.
Comparison Operators
EQ = equal to NE = not equal to
GT = greater than GE = greater than or equal
LT = less than LE = less than or equal
Example: A Facing Loop with IF and GOTO
Face a part down in steps without writing out every pass. The tool starts above the part, to the left of X0.
N10 (Start of loop)
#100 = #100 - 0.051.25 (Subtract depth increment)
G01 Z#100 F10.0 (Feed to new depth)
G01 X5.0 (Make the cut)
G00 Z0.1 (Retract clear of the part first)
G00 X-0.6 (Rapid back to the start side)
(The logic check:)
IF [#100 GT -0.5-12.5] GOTO10 (Not at final depth yet: loop back to N10)
With 0.05" steps to −0.5" this takes ten passes. Retract Z before the rapid return, or the tool will drag across the freshly cut face.
WHILE loops: the cleaner way
WHILE [condition] DO1 repeats everything up to END1 for as long as the condition is true. The number after DO and END (1, 2 or 3) pairs them up, so you can nest up to three loops. WHILE loops are easier to read than GOTO jumps, and on many controls they run faster because the control does not have to search for an N number.
WHILE [#100 GT -0.5] DO1
#100 = #100 - 0.05
G01 Z#100 F10.0
G01 X5.0
G00 Z0.1
G00 X-0.6
END1
One caution with both loops: decimal maths is not always exact, so a value you expect to be −0.5 might be stored as −0.4999999. If a loop must stop on an exact value, compare with a little margin, such as GT [-0.5 + 0.0001], or count passes with a whole-number counter instead.
4. Calling a Macro with G65 and Arguments
G65 P9010 calls program O9010 as a macro, and the letters after it are passed in as local variables. This is how you build your own "canned cycles". Unlike M98, which simply runs a subprogram (see the M98/M99 subroutine generator), G65 gives the macro its own fresh set of local variables. G65 P9010 L3 runs it three times. M99 at the end returns to the calling program.
| Letter | A | B | C | D | E | F | H | I | J | K | M |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Variable | #1 | #2 | #3 | #7 | #8 | #9 | #11 | #4 | #5 | #6 | #13 |
| Letter | Q | R | S | T | U | V | W | X | Y | Z |
|---|---|---|---|---|---|---|---|---|---|---|
| Variable | #17 | #18 | #19 | #20 | #21 | #22 | #23 | #24 | #25 | #26 |
G, L, N, O and P cannot be used as arguments. This is Fanuc's argument specification I, the one most people use.
Example: A Bolt Circle Drilling Macro
The macro drills H holes equally spaced on a circle of diameter D around X, Y, starting at angle A, using G81 with depth Z, R-plane R and feed F.
G65 P9010 X0. Y0. D3.0 H6. A0. Z-0.5 R0.1 F8.0
O9010 (BOLT CIRCLE DRILL)
IF [#7 EQ #0] GOTO900 (D missing)
IF [#11 LT 1] GOTO900 (H missing or zero)
#30 = 0 (Hole counter)
#31 = 360 / #11 (Angle between holes)
#32 = #7 / 2 (Circle radius)
WHILE [#30 LT #11] DO1
#33 = #1 + #30 * #31 (Angle of this hole)
G81 X[#24 + #32 * COS[#33]] Y[#25 + #32 * SIN[#33]] Z#26 R#18 F#9
#30 = #30 + 1
END1
G80
M99
N900 #3000 = 1 (D OR H MISSING)
Check the second hole by hand: angle = 0 + 1 × 60 = 60°, X = 1.5 × cos 60° = 0.750, Y = 1.5 × sin 60° = 1.299. The bolt circle calculator gives the same coordinates, so you can prove a macro's maths before it runs.
Always Use Decimal Points in G65 Arguments
On many controls, an argument written without a decimal point is read in the smallest input unit, so D3 can arrive as 0.003 instead of 3.0. Write D3.0 or D3. every time. This is one of the most common reasons a new macro does something strange.
5. Practical Use: Family of Parts
Why do shops use macros? Family of parts programming. Imagine you make rectangular base plates in ten sizes. Instead of keeping ten CAM programs up to date, you write one program. At the top, the operator types in the length and width of today's plate, and all the toolpaths follow.
#102 = 4.0 (PLATE WIDTH Y)
#103 = 0.5 (HOLE INSET FROM EACH EDGE)
G81 X#103 Y#103 Z-0.6 R0.1 F8.0
X[#101 - #103]
Y[#102 - #103]
X#103
G80
For a 6.0" × 4.0" plate this drills corner holes at (0.5, 0.5), (5.5, 0.5), (5.5, 3.5) and (0.5, 3.5). Change #101 and #102 and the same program drills any size plate. Add a check such as IF [#101 GT 12.0] GOTO900 with a #3000 alarm so a typing error cannot send the tool off the table.
6. Macros and Probing
Parametric programming is the foundation of machine probing. A probing cycle is a macro: it moves the probe, reads the trigger position from a system variable, does the maths, and writes the result into a work offset such as #5221 (G54 X). Once you understand variables, you can read those cycles, add your own tolerance checks (alarm with #3000 if a bore is out of size), or count parts in a permanent variable.
Writing to system variables changes the machine's real data. A wrong value in #5223 moves G54 Z, and the next tool can crash. Test any macro that writes offsets on a part with plenty of clearance, and read back the offset page before you cut. The work coordinates guide and tool length offsets guide explain what those offsets do.
Macro Safety Warning
The machine does exactly what your maths tells it to. A divide by zero stops with an alarm; an infinite loop (an IF or WHILE condition that can never change) runs until you stop it; a sign error moves the tool somewhere you did not expect. Always prove new macros cutting air, well above the part, with single block on, rapid override turned down, and the variable page open so you can watch the values change.
7. Common Mistakes
- Round brackets for maths.
X(#1+#2)is a comment, not a calculation. UseX[#1+#2]. - Missing decimal points in G65 arguments, as above.
- Clashing variables. Probing packages and other shop macros often reserve ranges of common and permanent variables. Check what is already used before you pick numbers, and keep a list on the machine.
- Forgetting look-ahead. The control reads and calculates several blocks ahead of the move it is making. If a calculation depends on a position or a value that changes during the move, check your manual for how to stop the read-ahead first.
- Unmatched DO and END numbers, or a GOTO to an N number that does not exist. Both stop with an alarm.
- No argument checks. Test for missing (
#0) or silly values at the top of every macro and stop with#3000.
For the G-codes used inside macros, keep the G and M code reference handy, and see the canned cycles guide for G81 to G89.
Frequently Asked Questions
Conclusion
Macro programming turns the control into a small computer. Variables hold your numbers, brackets do the maths, IF and WHILE make the decisions, and G65 lets you package it all as your own cycle. Start with a simple loop or a bolt circle, check the maths by hand, prove it out in the air, and you will soon be writing programs that handle whole families of parts.