Sub-routine Generator
Toolpath Map
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
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
- 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
- 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
Generated code is a starting point. Check it against your control's programming manual and prove it out with a dry run before cutting.