MacroMill CNC Automation Software

From Hard-Coded G-Code to a Parametric Program: Turn a Milling Contour Into Variables

MacroMill VMC simulator for FANUC 0i-MF Plus style controls

From Hard-Coded G-Code to a Parametric Program: Turn a Milling Contour Into Variables You Can Change in Seconds

Most profile programs are written once for one part. Every coordinate, depth and feed is typed in as a fixed number. When the customer asks for the same part 20 mm longer, someone has to recalculate the corner points, edit a dozen lines, and hope they didn’t miss one.

In this tutorial we take a real hard-coded Fanuc milling contour and convert it step by step into a parametric Macro B program. At the end, a new part size is a three-number change.

Note: all code below is a reference example only. Check it against your control manual, cutter compensation settings and machine builder’s documentation, and prove it out with single block and a dry run before cutting.

The part

An outside profile of a 100 x 60 mm rectangle with 10 mm corner radii, cut 6 mm deep in three 2 mm passes with a 10 mm end mill. The work offset zero is the lower-left corner of the part, on the top face. We use G41 (cutter compensation left) with an M03 spindle, which gives climb milling and means the tool travels clockwise around the outside of the part. So every corner is a clockwise arc (G02).

Step 1: the hard-coded program

O1000 (RECT PROFILE 100X60 R10 - HARD CODED)
G90 G17 G21 G40 G49 G80
G54 G00 X-15.0 Y10.0
S3000 M03
G43 H01 Z50.0
Z2.0
G01 Z-2.0 F200        (PASS 1)
G41 D01 X0 F400
Y50.0
G02 X10.0 Y60.0 R10.0
G01 X90.0
G02 X100.0 Y50.0 R10.0
G01 Y10.0
G02 X90.0 Y0 R10.0
G01 X10.0
G02 X0 Y10.0 R10.0
G01 Y30.0
G40 G01 X-15.0
G00 Z2.0
(...THE SAME 13 LINES AGAIN FOR Z-4.0 AND Z-6.0...)
G00 Z50.0
M05
M30

Each arc ends 10 mm from the corner along both axes, which matches R10. For example, the arc from X0 Y50 to X10 Y60 has its centre at X10 Y50. The contour is repeated for every depth pass, so the full program is about 50 lines. To make the part 120 x 80, you would have to recalculate and edit most of the coordinates by hand, in every pass.

Step 2: find the numbers that really matter

Look at the program and ask which numbers are inputs and which are results. X90 is not an input. It is the length (100) minus the corner radius (10). Y50 is the width minus the radius. Only a handful of values actually define the part:

VariableMeaningExample value
#100Length in X100.0
#101Width in Y60.0
#102Corner radius10.0
#103Total depth6.0
#104Step-down per pass2.0
#105Cutting feed (mm/min)400
#106Spindle speed (rpm)3000
#107Tool diameter (for clearance)10.0
#108X origin of the part corner0
#109Y origin of the part corner0

Step 3: replace coordinates with expressions

Every coordinate on the contour can now be written from these inputs:

  • Left edge X = #108, and right edge X = #108+#100
  • Bottom edge Y = #109, and top edge Y = #109+#101
  • The start and end of each corner arc are offset by #102 from the corner
  • The lead-in clearance is 1.5 x the tool diameter, so the tool always starts clear of the part

So G01 X90.0 becomes G01 X[#108+#100-#102], and G02 X100.0 Y50.0 R10.0 becomes G02 X[#108+#100] Y[#109+#101-#102] R#102.

Step 4: a WHILE loop for the depth passes

Instead of copying the contour for every pass, a loop increases the depth by the step-down until it reaches the total depth. If the total depth isn’t an exact multiple of the step-down, the last pass is clamped to the final depth:

#120=0
WHILE [#120 LT #103] DO1
#120=#120+#104
IF [#120 GT #103] THEN #120=#103
(...CUT ONE PASS AT Z-#120...)
END1

With a 6 mm depth and 2 mm step-down, this runs at Z-2, Z-4 and Z-6, then stops because 6 is no longer less than 6. With a 5 mm depth, it runs at Z-2, Z-4 and Z-5.

Step 5: input checks with #3000

A parametric program will try to run whatever values you give it, so check them before any motion. #3000 stops the machine with an alarm and your message:

IF [#102 LE 0] THEN #3000=1 (CORNER RADIUS MUST BE POSITIVE)
IF [[#102*2] GT #101] THEN #3000=2 (RADIUS TOO LARGE FOR WIDTH)
IF [[#102*2] GT #100] THEN #3000=3 (RADIUS TOO LARGE FOR LENGTH)
IF [#103 LE 0] THEN #3000=4 (DEPTH MUST BE POSITIVE)
IF [#104 LE 0] THEN #3000=5 (STEP-DOWN MUST BE POSITIVE)

The full parametric program

O1001 (RECT PROFILE - PARAMETRIC)
#100=100.0   (LENGTH X)
#101=60.0    (WIDTH Y)
#102=10.0    (CORNER RADIUS)
#103=6.0     (TOTAL DEPTH)
#104=2.0     (STEP-DOWN)
#105=400     (FEED)
#106=3000    (RPM)
#107=10.0    (TOOL DIAMETER)
#108=0       (X ORIGIN)
#109=0       (Y ORIGIN)
(--- CHECKS ---)
IF [#102 LE 0] THEN #3000=1 (CORNER RADIUS MUST BE POSITIVE)
IF [[#102*2] GT #101] THEN #3000=2 (RADIUS TOO LARGE FOR WIDTH)
IF [[#102*2] GT #100] THEN #3000=3 (RADIUS TOO LARGE FOR LENGTH)
IF [#103 LE 0] THEN #3000=4 (DEPTH MUST BE POSITIVE)
IF [#104 LE 0] THEN #3000=5 (STEP-DOWN MUST BE POSITIVE)
(--- DERIVED VALUES ---)
#110=#107*1.5          (LEAD-IN CLEARANCE)
#111=#108+#100         (RIGHT EDGE X)
#112=#109+#101         (TOP EDGE Y)
#113=#109+#101/2       (LEAD-OUT Y)
G90 G17 G21 G40 G49 G80
G54 G00 X[#108-#110] Y[#109+#102]
S#106 M03
G43 H01 Z50.0
#120=0
WHILE [#120 LT #103] DO1
#120=#120+#104
IF [#120 GT #103] THEN #120=#103
G00 Z2.0
G01 Z-#120 F[#105/2]
G41 D01 X#108 F#105
Y[#112-#102]
G02 X[#108+#102] Y#112 R#102
G01 X[#111-#102]
G02 X#111 Y[#112-#102] R#102
G01 Y[#109+#102]
G02 X[#111-#102] Y#109 R#102
G01 X[#108+#102]
G02 X#108 Y[#109+#102] R#102
G01 Y#113
G40 G01 X[#108-#110]
G00 Y[#109+#102]
END1
G00 Z50.0
M05
M30

With the example values, this produces exactly the same moves as the hard-coded program: X-15 Y10, X0, Y50, the arc to X10 Y60, X90, and so on. The difference is that none of the coordinates are typed in.

Why use R rather than I and J? Each arc here is a quarter circle of less than 180°, so R is unambiguous and easy to read. For arcs of 180° or more, use I and J.

Alter it in seconds

A new order comes in for a 120 x 80 mm part with 15 mm corners. Change three lines:

#100=120.0   (LENGTH X)
#101=80.0    (WIDTH Y)
#102=15.0    (CORNER RADIUS)

Every corner point, arc and lead-out is recalculated by the control, and the depth loop doesn’t change. A deeper part with a smaller step-down is just #103 and #104.

Here is the same idea in action: a 120 x 120 radius corner programmed in five steps without CAM.

Going further: a G65 custom cycle

To reuse the profile in any program, move the body into a macro program and pass the sizes as arguments. In a G65 call, the address letters map to local variables: A to #1, B to #2, C to #3, D to #7, E to #8, F to #9, X to #24 and Y to #25.

(MAIN PROGRAM)
G90 G54 G00 X-20.0 Y-20.0
S3000 M03
G43 H01 Z50.0
G65 P9200 A100.0 B60.0 C10.0 D6.0 E2.0 F400 X0 Y0
G00 Z50.0
M30

O9200 (RECT PROFILE CYCLE)
(A=#1 LENGTH  B=#2 WIDTH  C=#3 RADIUS)
(D=#7 DEPTH   E=#8 STEP   F=#9 FEED)
(X=#24 Y=#25 ORIGIN - TOOL DIA 10 ASSUMED)
IF [#1 EQ #0] THEN #3000=1 (LENGTH A MISSING)
IF [#2 EQ #0] THEN #3000=2 (WIDTH B MISSING)
IF [#3 LE 0] THEN #3000=3 (RADIUS C MUST BE POSITIVE)
IF [[#3*2] GT #2] THEN #3000=4 (RADIUS TOO LARGE FOR WIDTH)
IF [[#3*2] GT #1] THEN #3000=5 (RADIUS TOO LARGE FOR LENGTH)
IF [#8 LE 0] THEN #3000=6 (STEP E MUST BE POSITIVE)
IF [#24 EQ #0] THEN #24=0
IF [#25 EQ #0] THEN #25=0
#10=15.0
#11=#24+#1
#12=#25+#2
G00 X[#24-#10] Y[#25+#3]
#20=0
WHILE [#20 LT #7] DO1
#20=#20+#8
IF [#20 GT #7] THEN #20=#7
G00 Z2.0
G01 Z-#20 F[#9/2]
G41 D01 X#24 F#9
Y[#12-#3]
G02 X[#24+#3] Y#12 R#3
G01 X[#11-#3]
G02 X#11 Y[#12-#3] R#3
G01 Y[#25+#3]
G02 X[#11-#3] Y#25 R#3
G01 X[#24+#3]
G02 X#24 Y[#25+#3] R#3
G01 Y[#25+#2/2]
G40 G01 X[#24-#10]
G00 Y[#25+#3]
END1
G00 Z2.0
M99

Now the operator sees one readable line, G65 P9200 A120.0 B80.0 C15.0 D6.0 E2.0 F400, instead of 50 lines of coordinates. Note that D here is the depth argument (#7). The cutter compensation number D01 inside the macro is a separate, fixed address.

A note for lathes

The same method works on a lathe. Diameters, shoulder lengths, chamfers and radii become variables, and the profile between the P and Q blocks of G71/G70 is written with expressions. Our post Combining Fanuc Macro B With Canned Cycles includes a lathe example.

Try it before you cut

If you have lots of repetitive CAM-generated code, MacroFusion™ helps turn suitable repetitive G-code into reusable, variable-driven macro logic. To learn Macro B from the ground up, CNC Automation Pro™ 2026 covers it from beginner to advanced level, including custom canned cycles.

Frequently asked questions

Why use #100-series variables in the main program?

Common variables #100–#199 are easy to see on the macro variable page and are normally cleared at power off, which suits values that belong to one job. Inside a G65 macro, use local variables (#1–#33) instead.

Does cutter compensation work with variables?

Yes. G41 and G40 work the same way. Make sure the lead-in and lead-out moves are long enough for compensation to start and cancel, which is why the clearance is based on the tool diameter.

Can I make it a pocket instead of a profile?

Yes, but a pocket also needs a stepover loop and an entry strategy (ramp or helix), and the tool radius must be smaller than the corner radius.

MacroMill

Get free CNC macro tips & simulator updates

New tutorials and MacroMill simulator deals, straight to your inbox.

We don’t spam! Read our privacy policy for more info.

Responses

  1. […] Start with a bolt hole circle. It uses variables, SIN/COS and a WHILE loop in about ten lines. Then try the parametric contour in our post From Hard-Coded G-Code to a Parametric Program. […]

  2. […] changing numbers into variables. Keep the proven toolpath and replace only the dimensions. Our post From Hard-Coded G-Code to a Parametric Program shows this step by […]

Leave a Reply