Standard G-code is static. When you type X2.0, the machine goes exactly to 2.0. But what if you wanted to tell the machine to "go to the length of the part plus the radius of the tool"? To do that, you need to add algebra and logic to your code. Welcome to the world of Macro B (Parametric Programming).

1. The Foundation: Variables

In macro programming, instead of hard-coding a number, you use a Variable. Variables in CNC are represented by the hash/pound sign (#) followed by a number. Think of a variable as a blank box that you can store a number inside.

Variable Types (Fanuc Standard)
  • #1 to #33 (Local Variables): Used for passing data into subprograms. They clear out when the program resets.
  • #100 to #199 (Common Variables): Great for everyday macro math. They clear when the machine loses power.
  • #500 to #999 (Permanent Variables): These numbers stay in the machine's memory even if you turn the power off.

2. Assigning Values and Basic Math

You assign a value to a variable using an equals sign. Once assigned, you can use that variable anywhere a standard number would go (like an X, Y, Z, or F parameter).

(Assigning a value to variable #100)
#100 = 5.0125.0

(The machine reads this exactly like G01 X5.0)
G01 X#100 F20.0

You can also do math directly inside the brackets [ ]. The machine will solve the math problem before it makes the move.

#101 = 2.050.0 (Length)
#102 = 0.512.0 (Tool Radius)

(Move to Length plus Tool Radius)
G01 X[#101 + #102]

3. The Real Power: Logic and Loops

Math is great, but Logic is where macros shine. You can tell the machine to make decisions using IF, THEN, and GOTO statements.

GOTO (Jump)

GOTO 10 tells the controller to skip everything and immediately jump to the line block named N10.

Operators

EQ = Equal To
NE = Not Equal To
GT = Greater Than
LT = Less Than

Example: A Simple Pecking Loop

Let's say you want to face a part down in increments without writing out 50 lines of code. You can use a loop.

#100 = 0.00.0 (Starting Z Depth)

N10 (Start of Loop)
#100 = [#100 - 0.051.25] (Subtract depth increment)
G01 Z#100 F10.0 (Move to new depth)
G01 X5.0 (Make the cut)
G00 X0.0 (Retract back)

(The Logic Check:)
IF [#100 GT -0.5-12.5] GOTO 10 (If we haven't reached final depth, loop back to N10)

4. Practical Use: Family of Parts

Why do shops use macros? Family of Parts programming. Imagine you make rectangular base plates that come in 10 different sizes. Instead of keeping 10 different CAM programs updated, you write one Macro program. At the top of the program, the operator simply types in the length and width of today's plate, and the machine automatically adjusts all the toolpaths.

Macro Safety Warning

The machine does exactly what your math tells it to do. If you divide by zero, or if you create an "infinite loop" (where the IF condition can never be met), the machine will either alarm out or crash. Always test new macros cutting air, high above the part, with single-block turned on!

Take Your Setup to the Next Level

Parametric programming is the foundation for advanced machine probing. Once you master variables, you can write programs where the machine probes a casting, finds the center, records that offset to a variable, and machines the part perfectly automatically.

Conclusion

Macro programming transforms you from a machine operator into a software developer. While CAM software generates complex 3D paths, knowing how to write a quick parametric loop or setup script directly at the control is what separates good machinists from the elite.