Combining Fanuc Macro B With Canned Cycles: 6 Worked Examples
Canned cycles such as G81, G83, G84, G71 and G76 do the hard work of drilling, pecking, tapping, roughing and threading in one line. Fanuc Custom Macro B adds variables, maths, loops and checks. When you put the two together, you get short programs that the control executes reliably and that adapt to new sizes by changing a few numbers.
This post shows six practical ways to combine macros with canned cycles, and the modal pitfalls to watch out for. If you are new to macros, start with our overview What Problems Do CNC Macros Solve?
Note: all code below is a reference example only. Cycle formats, parameters, available options and variable ranges differ between Fanuc control models and machine builders. Always check your control manual and prove the program out with single block and a dry run before cutting.
First, how canned cycles behave inside a macro
A few rules explain almost every problem people have when mixing the two:
- Canned cycles are modal. Once G81, G83 or G84 is active, every block that contains an X or Y move drills a hole at that position until you cancel with G80. That is exactly what makes them useful in a loop, but a forgotten G80 means the next positioning move also drills a hole.
- Macro statements don’t trigger the cycle. Lines such as
#5=#5+1orWHILE [...] DO1are calculations, not motion, so they don’t drill a hole. Only blocks with axis words do. - G98 and G99 decide the return height. G98 returns to the initial Z level after each hole, and G99 returns to the R plane. Use G98 when there are clamps or steps between holes, and G99 only when the path between holes is clear.
- R is the rapid plane. The tool rapids down to R and feeds from there. A wrong R value, especially on a part with steps, is a common cause of crashes.
- K0 or L0 sets up the cycle without drilling. On most Fanuc mill controls, adding K0 (or L0 on some) to the cycle block stores the cycle data without drilling at the current position, so the loop can supply every hole position itself.
Example 1: Bolt circle with G81 in a WHILE loop
The control calculates each X and Y position with COS and SIN, and the modal G81 drills at each one.
(BOLT CIRCLE - G81 SPOT/DRILL)
#1=8 (NUMBER OF HOLES)
#2=40.0 (BOLT CIRCLE RADIUS)
#3=22.5 (START ANGLE)
#4=0.0 (CENTRE X)
#5=0.0 (CENTRE Y)
#10=0 (HOLE COUNTER)
G90 G54 G00 X#4 Y#5
G43 H01 Z50.0
G98 G81 Z-12.0 R2.0 F120 K0
WHILE [#10 LT #1] DO1
#11=#3+[360/#1*#10]
X[#4+#2*COS[#11]] Y[#5+#2*SIN[#11]]
#10=#10+1
END1
G80
G00 Z50.0
To drill a different pattern, change #1 to #5. The G80 straight after END1 makes sure the next move doesn’t drill.
Example 2: Parametric peck drilling with G83
Peck depth is usually chosen from the drill diameter. A macro can calculate Q from the diameter so the same program works for different drills:
#20=8.5 (DRILL DIAMETER)
#21=-30.0 (HOLE DEPTH Z)
#22=#20*0.5 (PECK = 0.5 x DIAMETER)
IF [#22 LT 1.0] THEN #22=1.0
G98 G83 X20.0 Y15.0 Z#21 R2.0 Q#22 F90
X60.0
X100.0
G80
The 0.5 x diameter rule here is only an example starting point, so use your tooling supplier’s recommendation. The IF line stops the peck from becoming too small to be useful.
Example 3: Rectangular grid tapping with G84
Two nested loops tap a grid of holes. With G84 rigid tapping, the feed must match the pitch. In feed per minute (G94), that means F = spindle speed x pitch, which the macro calculates:
#1=4 (COLUMNS)
#2=3 (ROWS)
#3=25.0 (X SPACING)
#4=20.0 (Y SPACING)
#5=1.25 (PITCH M8)
#6=600 (RPM)
#7=#6*#5 (FEED MM/MIN)
M03 S#6
M29 S#6 (RIGID TAP - CHECK YOUR MACHINE)
G98 G84 Z-15.0 R3.0 F#7 K0
#10=0
WHILE [#10 LT #2] DO1
#11=0
WHILE [#11 LT #1] DO2
X[#11*#3] Y[#10*#4]
#11=#11+1
END2
#10=#10+1
END1
G80
The rigid tapping command (M29 or another M-code, or a parameter setting) depends on the machine builder, so check your manual.
Example 4: A G65 drilling macro with arguments and #3000 checks
Wrapping the cycle in a macro program gives you a custom cycle that the operator calls in one line. Safety checks stop the machine with a clear message if an argument is missing or wrong.
(MAIN PROGRAM)
G65 P9100 H6 R50.0 Z-10.0 F150 A0
M30
O9100 (BOLT HOLE DRILL - H=#11 R=#18 Z=#26 F=#9 A=#1)
IF [#11 EQ #0] THEN #3000=1 (HOLE COUNT H MISSING)
IF [#18 LE 0] THEN #3000=2 (RADIUS R MUST BE POSITIVE)
IF [#26 GE 0] THEN #3000=3 (DEPTH Z MUST BE NEGATIVE)
IF [#1 EQ #0] THEN #1=0
#30=0
G98 G81 Z#26 R2.0 F#9 K0
WHILE [#30 LT #11] DO1
#31=#1+[360/#11*#30]
X[#18*COS[#31]] Y[#18*SIN[#31]]
#30=#30+1
END1
G80
M99
In a G65 call, the address letters map to local variables: A goes to #1, F to #9, H to #11, R to #18 and Z to #26. Because R is used as an argument here, the R plane inside the macro is written as a fixed value. Note that the pattern centre is the current work offset zero.
Example 5: Lathe G71/G70 roughing with variable dimensions
On a lathe, variables can drive the profile between the P and Q blocks of G71, so one program handles a family of shafts. This uses the two-block G71 format found on most current Fanuc lathe controls:
#1=30.0 (FINISHED DIAMETER)
#2=-40.0 (SHOULDER Z)
#3=1.0 (CHAMFER)
#4=50.0 (STOCK DIAMETER)
G00 X[#4+2.0] Z2.0
G71 U2.0 R0.5
G71 P10 Q20 U0.4 W0.1 F0.25
N10 G00 X[#1-2*#3]
G01 Z0 F0.15
X#1 Z-#3
Z#2
N20 X[#4+2.0]
G70 P10 Q20
G00 X100.0 Z100.0
Check that your control allows macro expressions inside the G71 profile blocks, since some older models restrict what can appear between P and Q. It is best to test the result in a simulator first. Our free CNC lathe simulators are a quick way to see a turned profile before running it.
Example 6: G76 threading driven by variables
For a standard metric thread, a macro can calculate the minor diameter from the major diameter and pitch, then feed it into G76:
#1=20.0 (MAJOR DIAMETER)
#2=1.5 (PITCH)
#3=0.6134*#2 (EXTERNAL THREAD DEPTH, PER SIDE)
#4=#1-2*#3 (MINOR DIAMETER)
#5=-25.0 (THREAD END Z)
G00 X[#1+4.0] Z5.0
G76 P020060 Q100 R0.05
G76 X#4 Z#5 P920 Q350 F#2
G00 X100.0 Z100.0
Be careful with units. On the second G76 block, P (thread height) and Q (first cut depth) are normally entered in the control’s least input increment, for example microns, without a decimal point. Here P920 is about 0.92 mm, matching 0.6134 x 1.5. If you calculate P with a variable, round it to the correct units. The thread depth factor varies with the thread standard and your insert, so confirm it against your thread tables.
For thread forms that G76 can’t cut, such as buttress or progressive pitch threads, a full parametric macro is usually needed. Our post Simulate Before You Cut shows how to check those profiles first.
Common pitfalls checklist
- Always cancel with G80 after the last hole, and before any tool change or positioning move.
- Don’t reuse cycle letters as G65 arguments by mistake. R, Z, Q and F all mean something in a drilling cycle and something else as macro arguments.
- Use G98 when unsure. Returning to the initial level is slower but safer.
- Watch rounding. COS and SIN results may have long decimals, and the control rounds them to its input increment. This is normal, but check critical positions.
- Use the null value check.
IF [#n EQ #0]catches arguments that were never passed. - Test before cutting. Use a simulator, then single block and a dry run on the machine.
Going further
Once you are comfortable wrapping cycles in macros, the next step is building your own custom canned cycles. CNC Automation Pro™ 2026 covers Fanuc Macro B and other controls, from beginner to advanced level, including custom canned cycles, pre-written macros, books and certification. If you have repetitive CAM-generated code that you want to turn into reusable macro logic, take a look at MacroFusion™. For free examples, see our CNC macro resources.
Frequently asked questions
Can I call a canned cycle inside a WHILE loop?
Yes. Set up the cycle once, ideally with K0 so it doesn’t drill at the start point, then supply X and Y positions inside the loop. Cancel with G80 after END.
Why did my machine drill an extra hole?
Usually the cycle was still active because G80 was missing, or the cycle block itself contained an X/Y position without K0.
Can variables be used in G71 and G76?
On most controls, yes, but check the units of P and Q in G76 and any restrictions your control places on the blocks between P and Q in G71.
MacroMill

Leave a Reply