Sub-routine Generator

Header & Call Config
Step & Repeat Pattern
A G52 local coordinate shift is applied before each repeat call if values are non-zero, then cancelled.
Toolpath Map
First Call
Repetitions
Complete G-Code Output

How the Sub-routine Generator Works

The generator puts your toolpath into a sub-program that ends in M99, then writes a main program that sets up the machine and calls it with M98. If you ask for more than one repetition with an X or Y shift, it moves the coordinate system with G52 before each extra call, so the same toolpath is cut at each new position.

A sub-program (subroutine) is a block of code stored under its own program number. The main program can call it as often as it needs to. M98 P1000 means run program O1000, and the M99 at the end of O1000 sends the control back to the line after the call. A repeated feature, such as a row of identical pockets or slots, only has to be written once.

Paste the moves for one feature only, in absolute coordinates (G90), positioned where the first feature should be cut. Include your own Z approach and retract moves, because the generator does not add any moves between calls.

How It Works

Simple call (1 repetition or no shift)
M98 P[sub] L[repetitions]
The L word tells the control how many times to run the sub-program in place. Use this for a sub-program written in incremental (G91) moves, such as a set of step-down passes, where each run carries on from the last.
Step and repeat (shift entered)
call 1: M98 P[sub]
before call k: G52 X[(k − 1) × X shift] Y[(k − 1) × Y shift]
after the last call: G52 X0 Y0
G52 sets a local coordinate system offset from the active work offset (G54 to G59). It does not move any axis. The absolute toolpath in the sub-program is therefore cut (k − 1) shifts away from the first one. Values are written to 4 decimal places in inch mode and 3 in metric.

Around the calls, the main program has a safety line (G00 G17 G40 G80 G90 plus G20 for inch or G21 for metric), the tool change (T M06), spindle start (S M03), the chosen work offset and coolant on (M08). At the end it turns the coolant off, rapids to Z2.0 with the spindle stopping, homes Z with G91 G28 Z0 and ends with M30. Z2.0 means 2 in or 2 mm depending on the units, so check that it clears your work and clamps.

Worked Examples

Inch: the built-in Example
  • Toolpath: a 1.0 × 1.0 in square at F20, repetitions 3, X shift 1.5
  • Call 1 cuts the square from X0 to X1.0
  • G52 X1.5000 Y0.0000, then call 2 cuts X1.5 to X2.5
  • G52 X3.0000 Y0.0000, then call 3 cuts X3.0 to X4.0
  • Then G52 X0 Y0 cancels the shift before the end of the program
Metric: diagonal pattern
  • Units: metric, repetitions 3, X shift 25, Y shift −10
  • Call 1 runs at the programmed position
  • G52 X25.000 Y-10.000 before call 2
  • G52 X50.000 Y-20.000 before call 3
  • With repetitions 4 and both shifts at 0, the output is a single line: M98 P1000 L4

Click Example on the page to reproduce the first case. The preview draws the first call in blue and the repeats as dashed green copies at each shift.

Tips for Using the Results

  • Check the feature first. Paste the sub-program toolpath into the G-Code 3D Viewer to check the single feature. The viewer does not follow M98 calls or G52 shifts, so dry-run the complete program on the machine, above the work, before cutting.
  • Confirm your control supports G52. Fanuc-style controls use G52 as a local coordinate shift, but some controls treat it differently or depend on a setting. Check the programming manual.
  • Choose free program numbers. Make sure the main and sub numbers are not already used on the machine. On many controls the 8000 and 9000 ranges are kept for protected or maker's macros.
  • Shifting or mirroring a whole program? Use the G-Code XYZ Shifter or G-Code Mirror instead of a sub-program.

Frequently Asked Questions

M98 calls a sub-program: M98 P1000 runs program O1000. M99 at the end of the sub-program returns control to the block after the M98 call. In a main program, M99 instead makes the program loop back to the start.

A G91 move shifts the tool but not the coordinate system, so a sub-program written in absolute coordinates would cut in the same place every time. The move would also run at the modal feed and the current depth. G52 shifts the coordinate system without moving any axis, so each call cuts at the new position.

L is the number of times the sub-program runs, so M98 P1000 L4 runs O1000 four times in a row. Some older Fanuc controls use a different format and put the count in front of the program number in the P word, so check your manual.

Yes, for jobs like repeated step-down passes. Set the shifts to 0 so the generator writes a single M98 P L call, and end the sub-program with G90 so the main program goes on in absolute mode. For step and repeat with a shift, write the sub-program in absolute G90 moves.

Generated code is a starting point. Check it against your control's programming manual and prove it out with a dry run before cutting.