CNCCookbook: Be a Better CNC'er

Enter a drawing to win a SideStop work stop and receive our free weekly newsletter.

Enter Now!


Macro Example: Digits

CNCCookbook's G-Code Tutorial


A Set of Macros for Engraving Digits

In this example, we'll show how to create a set of macros for engraving digits.

To start out, I wanted to create a "stick font" for the digits. I used my CAD program (Rhino3D) to create a DXF file of the 10 digits (right click and select download on this link to get the DXF file). I was careful to only use line segments and arcs, since those are the only shapes we can create in g-code. In addition, I scaled this alphabet so each digit was 1" high and 1/2" wide. They look pretty good printed in 1" x 1" squares.

I loaded that DXF file into my OneCNC CAM program and was very quickly able to post g-code. Here's what the stick digits looked like with their OneCNC toolpath for engraving:

OneCNC Stick Digit Engraving

Stick Digits in OneCNC...

The digits are not bad looking and are drawn on 1" x 1" squares. They are simple enough it would have been easy to hand code the g-code by referring to the CAD drawing, but since I have good CAM handy, why not take avantage of it?

From that CAM post (right click the link to download), I was able to create the following g-code subprograms. Each fragment is adjusted to engrave the digit as though the top left corner of its 1" x 1" box is located at X0Y1 and the baseline is X0Y0. In addition, we will assume they're called as subprograms with #500 containing the X baseline, #501 containing the Y baseline, and #502 containing the desired height of the engraved digit. While I created everything in inches, these subprograms don't know or care from inches.

The One as it came out of the CAM post with sequence renumbering, "O" number, and M99 added.

N10 (Digit "1")
N30 G00 X0.5 Y1. Z0.2
N40 Z0.05
N50 G01 Z-0.1 F1.5 S0
N60 Y0.
N70 G00 Z0.2
N80 M99

Now that will engrave a one at baseline position X0Y0. Let's parameterize it to use #500, #501, and #502 to locate and size the digit, #503 is Z=0, and #504 is cutting depth:

N10 (Digit "1")
N30 G00 X[#502*0.5+#500] Y[#502*1.0+#501] Z[#503+0.2]
N40 Z[#503+0.05]
N50 G01 Z[#503-#504]
N60 Y#501
N70 G00 Z[#503+0.2]
N80 M99

Note that I could also have relied on relative coordinates instead of adding the X and Y offsets to the digits, but since we're creating an example for macro programming, I went with the macro approach.

Subsequent digits are each offset to the right in 1" squares. So I had to process each one by cutting it out of the original CAM program, adding an "O" number and comments at the top, renumbering the "N" numbers, and then shifting all the coordinates left by however many inches over the digit was from the leftmost position.

Shifting the coordinates left was just a matter of updating the "X" words with a little address arithmetic using G-Wizard Editor. Here's what the basic program with backplot looks like before we shift:

Note the "2" digit is offset 1" to the right of the origin...

Here is the Adjust Addresses screen set up to subtract 1 from every X word's address:

G-Code Address Adjustment

Adjust every X by subtracting 1 from its address.

And here is the result after adjustment:

G-Code for the "2" Digit

"2" digit after shifting left 1"...

As you can see, Adjust Addresses is a pretty handy command. I still need to parameterize the program for the "2", but we're close. To finish a set of macros suitable for engraving digits, I need to go through and perform these operations on all the remaining digits too.



Try the Free Trial Version of G-Wizard G-Code Editor...


No credit card required--just your name and email.


Next Article: Conditions and Looping




CNCCookbook's G-Code Course


Featured Articles

Step-By-Step Guide to Making CNC Parts

CNC Router Cutter Types

Why Use a Single Flute Endmill?

Step and Servo Motor Sizing

The Truth About Tool Deflection

10 TIps for Router Aluminum Cutting

2 Tools for Calculating Cut Depth and Stepover

CNC Machine Hourly Rate Calculator

Special Purpose CNC Calculators

Feeds and Speeds Guide

CNC Cutter Guide

Feeds and Speeds By Material

G-Code Tutorial

  Feed Rate Calculator

Sales, and Special Deals


GCode is complicated.
G-Wizard Editor
makes it easy.

Try It!


Feeds and Speeds:
Made Easy.

Try G-Wizard



Do you want to be a better CNC'er?

Get Better Tool Life, Surface Finish, and Material Removal Rates.


Start Now, It's Free!