Macro Example: G-Code Engraving Digits

3 months by cncdivi

Macro Example: G-Code Engraving 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



Like what you read on CNCCookbook?

Join 100,000+ CNC'ers!  Get our latest blog posts delivered straight to your email inbox once a week for free. Plus, we’ll give you access to some great CNC reference materials including:

  • Our Big List of over 200 CNC Tips and Techniques
  • Our Free GCode Programming Basics Course
  • And more!

Just enter your name and email address below:

Full Name
Email *
100% Privacy: We will never Spam you!

Rate this post

Recently updated on April 4th, 2024 at 10:14 am