What Problems Do CNC Macros Solve? 8 Shop-Floor Problems Fanuc Macro B Fixes
Most CNC programs are written as fixed lists of coordinates. That works well until the drawing changes, a customer orders a family of similar parts, or a tool starts to wear halfway through a batch. Then someone edits numbers by hand at the control, and that is where mistakes creep in.
CNC macro programming, called Custom Macro B on Fanuc controls, adds variables, maths, conditions and loops to ordinary G-code. Below are eight real shop-floor problems that macros solve, each with a short code example.
Note: all code below is a reference example only. Variable ranges, system variable numbers and options differ between controls and machine builders. Always check your control manual and prove the program out with single block and a dry run before cutting.
1. Families of parts mean endless near-identical programs
A shop makes the same flange in ten diameters, or the same bush in six lengths. Without macros, that means ten or six separate programs to write, check and store. If one feature changes, every program has to be edited.
A parametric macro solves this. You write the geometry once, using variables, and pass the sizes in with a G65 call:
O1000 (MAIN - CALL FLANGE MACRO)
G65 P9010 D80.0 Z-25.0 F0.2
M30
O9010 (FACE AND TURN - D=#7 Z=#26 F=#9)
G00 X[#7+2.0] Z2.0
G01 Z#26 F#9
G00 X[#7+5.0]
Z2.0
M99
In a G65 call, the letters become local variables: D goes to #7, Z to #26 and F to #9. To make a different size, change one line in the main program.
2. Manual edits at the control cause errors
Every time an operator edits coordinates in a long program, there is a chance of a typo: a missing minus sign, a decimal point in the wrong place, or one line changed and its partner forgotten. Macros reduce that risk by keeping the values that change in a few clearly named variables at the top of the program, or in common variables such as #500 that the operator sets on the macro variable page. The rest of the program does the maths, so related dimensions stay consistent.
3. Measuring parts by hand slows production
Stopping the machine to measure with a micrometer costs time, and the result depends on who is measuring. With a spindle or table probe, macros can read the probe trigger position from system variables and act on it: update a work offset, adjust a wear offset, or stop the machine if a part is out of tolerance. Probe manufacturers ship their own macro packages for this, and understanding Macro B makes it much easier to customise and troubleshoot them.
4. Tool wear changes sizes during a batch
Over a long run, inserts wear and diameters drift. Macros can read and write tool offsets directly. A simple example is counting parts and alerting the operator to change a tool after a set number of cycles:
#501=#501+1 (PART COUNTER)
IF [#501 LT 200] GOTO 10
#501=0
#3000=1 (CHANGE T0101 INSERT)
N10 M30
The common variable #501 keeps its value after power off on most controls, so the count survives between shifts. The same idea can be used to apply a small wear offset correction after a given number of parts, based on your own measured results.
5. Bolt circles and hole patterns are tedious to program
Calculating every X and Y position for a bolt circle by hand is slow and easy to get wrong, especially with odd hole counts or a start angle. A macro does the trigonometry in a loop:
(BOLT CIRCLE - 6 HOLES ON 100 PCD)
#1=6 (NUMBER OF HOLES)
#2=50.0 (RADIUS)
#3=0 (START ANGLE)
#4=0 (COUNTER)
G81 Z-10.0 R2.0 F100 K0 L0
WHILE [#4 LT #1] DO1
#5=#3+[360/#1*#4]
X[#2*COS[#5]] Y[#2*SIN[#5]]
#4=#4+1
END1
G80
Change #1, #2 or #3 and the whole pattern updates. The same structure works for slots, pockets and engraving patterns.
6. Programs run without safety checks
A normal program will run with whatever values it is given, even impossible ones. Macros can check inputs before any motion and stop the machine with a clear message using alarm variable #3000:
IF [#7 EQ #0] THEN #3000=1 (DIAMETER D NOT GIVEN)
IF [#7 GT 300.0] THEN #3000=2 (DIAMETER TOO LARGE)
Here #0 is the “null” variable, so the first line catches a missing argument. These few lines prevent a crash caused by a forgotten or mistyped value, and the operator sees exactly what is wrong.
7. Special threads and grooves don’t have canned cycles
Standard threads have G76 and G92. Buttress threads, progressive pitch threads, rope grooves and multi-groove V-pulleys do not. Programming them pass by pass means hundreds of lines. A parametric macro calculates each pass from depth, pitch, flank angle and insert width, so one program handles every size. Before running a special profile, it helps to see it first. Our earlier post, Simulate Before You Cut, shows how to check thread and groove geometry in a free simulator.
8. Too much depends on CAM for simple, repeated work
CAM is excellent for complex 3D surfaces. For simple, repeated features, though, reposting large CAM files for every small change is slow, and the output is hard to edit at the machine. A macro is short, readable and can be changed at the control by changing a variable. Many shops use both: CAM for complex shapes, and macros for repeated features, patterns, probing and checks.
If you already have repetitive CAM-generated G-code, MacroFusion™ is MacroMill desktop software for turning suitable repetitive code into cleaner, reusable, variable-driven macro logic.
How to start learning CNC macro programming
- Learn the basics first: local, common and system variables, arithmetic, IF and GOTO, and WHILE loops.
- Practise safely: test macros on a simulator before going near a machine. Try the free MacroMill CNC lathe simulators.
- Start small: a part counter, a bolt circle or an input check is a good first macro.
- Use proven examples: download our free CNC macro resources.
For a structured path, CNC Automation Pro™ 2026 covers Fanuc Macro B and other controls, from beginner to advanced level, with pre-written macros, custom canned cycles, technical books and certification.
Frequently asked questions
What is Fanuc Macro B?
Custom Macro B is Fanuc’s macro programming language. It adds variables, maths, conditions, loops and subprogram calls with arguments (G65) to standard G-code.
Does my machine support macros?
Most modern Fanuc controls include Custom Macro, but on some older machines it is an option. Check the control parameters or ask your machine builder.
Are macros hard to learn?
If you can already read G-code, the basics take a short time to learn. Start with variables and simple IF statements, then move on to loops and G65 calls.
Can macros damage a machine?
Writing to the wrong system variable or offset can cause problems, which is why you should test on a simulator, add #3000 checks, and prove out with single block.
MacroMill

Leave a Reply