NamePlateGenerator:
Fast, Automated Generation of GCode Files to Engrave Text
Overview
One of my projects required many 4 x 2.5 inch bi-color plastic panels (nameplates) be to be engraved with text using a CNC router. These nameplates are the white text on black background labels similar to the type seen at botanical gardens identifying plants. There were over 250 nameplates which needed to be suitably processed - each with five items of distinct and variable length text. Manually creating such nameplates with standard CAD software would be exceptionally repetitive and would take an improbably long time. Clearly there was a need for an automated solution - thus the NamePlateGenerator software was created.
The nameplates needed to be able to set arbitrary text in all four corners, a line of text in the middle and also drill two screw mounting holes. In addition, the typeface and font size needed to be configurable and all of the text should be appropriately justified. In other words, whatever text was to be placed in the lower right hand corner needed to have it's start point offset so that the end and lower edge of the text was positioned at a configurable distance (the margin) from the right and bottom edges of the nameplate. Similar calculations are required for every other text item.
Since CNC machines will only accept GCode, the software also needed to be able to convert TrueType fonts into suitable GCode. This conversion involves flattening out the splines and Bezier curves which form the definition of a TTF font character into a sequence of small connected straight line segments. The number of segments used for the curve (the Smoothing Factor) needed to be a settable feature. A low enough Smoothing Factor (smaller means smoother) produces output indistinguishable to the human eye from a normal curve. Too large a Smoothing Factor and you end up with far fewer GCode lines but also have "blocky" segmented text reminiscent of a 1980's video game.
Other required features were a WISIWYG interface to display on the screen a reasonably accurate representation of what will be engraved when the GCode is run and the ability to process a delimited text file with the five text items per line in order to bulk generate a large number of GCode files.
The State of the Project
The NamePlateGenerator software contains on-screen controls which enable the user to manually enter four items of corner text along with a centered string of main text, choose the fonts, sizes, margins and set numerous other CNC things such as depth of cut and size of nameplate. A representation of the output is displayed on the screen. At the press of a button, the NamePlateGenerator software can generate a GCode file which will engrave text identical to the screen display when run on a CNC machine. In addition, a mode which can consume a delimited text file and automatically generate an arbitrary number of such files is available. The generated GCode is displayed in a panel on the right hand side of the screen and can be saved as a file or copy and pasted elsewhere.
This project was created to meet a specific purpose for a given set of input and output requirements. The software is not polished and is not particularly productionised. This means that the operation is not as smooth and generic as might be desired and there are not as many features as there probably should be. There are, however, no known bugs. In addition, please realize that now that the software has done the job for which it was intended, development is essentially over as far as the author is concerned. In particular, please note that the NamePlateGenerator only uses Imperial units (inches) since those are the units of the authors CNC machine and there are no plans to add metric support.
The NamePlateGenerator software is pretty much only offered as a starting point for others who may wish to adapt it. If you do make enhancements that would be useful to others (and which you would like to share) please generate a pull request on the GitHub repo.
The NamePlateGenerator Common Controls
- Plate Width
- The width of the nameplate in inches. Changing this value adjusts the text positioning.
- Plate Height
- The height of the nameplate in inches. Changing this value adjusts the text positioning
- Plate Margin
- The amount of space between the corner text and the edge of the plate. The margin distance is equal on all edges. It would not be hard to add a feature to specify an individual margin for each side.
- Touchdown Offset
- The distance between the margin and the point where the bit touches down to mark the drill hole. This is equal for all margins and touchdowns.
- Touchdown ZDepth
- The depth the bit sinks into the material to mark the drill hole. If an appropriately sized engraving bit is used it is possible to use this feature to sink the bit through the material hence drilling the hole. Otherwise the touchdown just marks the position for later manual drilling with a suitably sized bit.
- CenterZDepth
- The depth to sink the bit when engraving the center text. Since engraving bits are usually conical this setting also determines the width of the lines in center text. This value should be negative since it is below the surface of the material.
- CornerZDepth
- The depth to sink the bit when engraving the corner text. Since engraving bits are usually conical this setting also determines the width of the lines in corner text. This depth is used for all corner text values - it would not be hard to add support for individual depths for each corner text value. This value should be negative since it is below the surface of the material.
- ClearZHeight
- The height at which to retract the bit in order to move it off the work at the start and completion of the run. This height should be high enough to clear any hold down clamps etc. This value should be positive since it is above the surface of the material.
- FastMoveZHeight
- The height at which to retract the bit in order to move it to the next letter in the text. This should be fairly small so that the Z axis does not spend an excessive amount of time moving up and down every time it needs to engrave a new letter. This value should be positive since it is above the surface of the material.
- TextFeedRate
- The XY feed rate to use when engraving text.
- FastFeedRate
- The XY feed rate to use when performing fast moves between engraving positions.
- ZFeedRate
- The feed rate at which the Z axis will move.
- UL Text
- The text to engrave in the upper left corner.
- LL Text
- The text to engrave in the lower left corner.
- UR Text
- The text to engrave in the upper right corner.
- LR Text
- The text to engrave in the lower right corner.
- Center Text
- The text to engrave centered in the nameplate.
- Add Boundary Rect
- If enabled, this option engraves a boundary rectangle around the outside of the plate.
- Add Vert. Touchdowns
- If enabled, this option places two touchdowns on the vertical centerline a Touchdown Offset distance from the margin. These appear as little white dots on the GUI display.
- Add Horiz. Touchdowns
- If enabled, this option places two touchdowns on the horizontal centerline a Touchdown Offset distance from the margin. These appear as little white dots on the GUI display.
- Smoothing Factor
- The text to engrave must be converted from TrueType font curves into a simulation which uses small connected straight line segments to approximate the curve. This value indicates how close an approximation is generated. A smaller value here means a better approximation but also more lines in the GCode. A value of 0.25 is quite a fine approximation and is probably a realistic lower bound for smaller font sizes. This value can also go above 1 but when doing so the approximation is not good and the letters tend to look "blocky".
- Center Text Font and Height
- The type face and font size to use for the center text.
- Corner Text Font and Height
- The type face and font size to use for the corner text. All corner text uses the same font and size. It would not be difficult to make this individually adjustable.
- Output Dir
- The output directory. In Manual mode this just provides the default output directory. In CSV mode this is the directory in which the auto-generated files are placed.
The NamePlateGenerator Manual Controls
- Generate from Screen
- Pressing this button generates the GCode from the on-screen values and also updates the GUI display panel.
- Save
- Pressing this button saves the generated GCode to a file.
The NamePlateGenerator Automatic Controls
- CSV File
- The full path and name of a CSV file which contains the values to auto-process into GCode files. The delimiter in this file is a pipe symbol "|" and the order of the values is CenterText|UpperLeft|UpperRight|LowerRight|LowerLeft. Any field which is to be blank can just be left empty. The download comes with a sample input CSV file named nebula.csv to use for practice.
- Generate from 5Col CSV
- Pressing this button generates the GCode from the CSV file. The currently set on-screen values (other than the text definitions) are used to provide all of the generation parameters. The output is written to the location specified by the Output Dir setting. The file name is the content CenterText value with all spaces removed and a filename extension of .ngc.
How to Use the NamePlateGenerator Software
- Set the output directory. In CSV mode all files will be written to this directory and in Manual mode it is just the default for the Save button.
- Set all of the parameters appropriately.
- Press the Generate From Screen button. This will update the GUI display panel and the GCode. Adjust things till you are satisfied and save the GCode file.
- Run the GCode on your CNC machine as a test. Note that there is no provision in the software for the bit type or diameter. This means the width of the lines in the engraved text is NOT accurately shown on the GUI display. For conical bits, the engraved text width is entirely dependent on how deep you sink the bit. You will probably have to make a couple of test runs. The default values seem to do a satisfactory job engraving a 4x2.5 inch nameplate with a 0.125 inch, 30 degree conical engraving bit.
- If the manual run was satisfactory, and you wish to bulk generate nameplates, compose a delimited CSV and press the Generate from 5Col CSV button.
- The bulk generated GCode files will be placed in the output directory and will have a filename based on the center text value with all spaces removed.
What the NamePlateGenerator Software Does Not Do
- The NamePlateGenerator software does not have any ability to produce metric GCode output files. Even though the author prefers metric, the machine on which the GCode was to be run is Imperial (inch based). Since this was a one-off project for a specific use there was no attempt to provide metric support. It probably would not be too hard to add metric support.
- The NamePlateGenerator software has no ability to customize the header and footer GCode lines. It is trivial to do this by editing the code though.
- Be aware that the default code moves the bit 10 inches vertically off the work when the engraving operation is complete. This is so the nameplate blank can be changed for the next run. This may not be suitable for some machines.
- The name of the bulk generated output file names is fixed. The file names are always
<CenterTextWithNoSpaces>.ngc
. It would be simple to change this in the code.
- If there is an error reading the CSV file the software just stops and displays a message. There is no attempt to cope with the error and finish processing what it can. The generation process is very quick and it is trivial just to completely re-run the processing job.
- Re-running a the bulk processing job will overwrite any existing GCode files with the same name in the output directory. There is no attempt to warn you before this happens.
- The NamePlateGenerator software does not automatically update the GUI representation or output GCode when the parameters (or text values) are changed. The Generate from Screen button must be pressed to update the GUI and GCode - this was done to improve performance on slower machines.
- The NamePlateGenerator software does not remember its parameters and settings over restarts. This feature could be added relatively easily. If you wish to retain a permanent record of your settings you may wish to take a screenshot prior to closing the software.
- The GUI representation of the engraved text is not quite accurate. There is no mechanism in the NamePlateGenerator software to input the characteristics of the engraving bit. This means that all text is drawn as thin lines on the display. Since most engraving bits are conical, the width of each line in the character is entirely derived from how deep you sink the bit into the material. This depth of cut effect is not represented in the GUI since it was easier and quicker to make some test cuts and set the depths appropriately from those. The positioning of the text in the GUI will be accurately shown but the width of that text will not.
- Similarly, the touchdowns for the drill holes are shown on the screen as small white dots. In reality the diameter of these touch downs will entirely depend on how deep you sink the bit for them. This is a settable feature and it is possible to drive the bit right through the material to make a hole the size of the bit itself.
- The NamePlateGenerator software does not do "bounds checking". In other words, if you enter text of a size or length which causes it to run off the edge of the nameplate or to overlay other text, no error will be generated. It is up to you to check the bounds before running the GCode file.
Other Things You Might Wish to Know
- If you are engraving you will probably want to use something called a single stroke font (sometimes called a stick font) for the text. TrueType font characters are usually composed of closed curves so that the character can be filled in with the foreground color. Thus, if you magnify it enough, even something like an 'I' char is actually the outline around long thin rectangular boxes. When engraving small text, this "multi-strokes close together issue" causes visual appearance problems and you may wish to use a special single stroke font. You can find a discussion of this problem and a helpful list of free single stroke fonts at: http://www.imajeenyus.com/computer/20150110_single_line_fonts/index.shtml.
- The NamePlateGenerator software, by default, wants to use a font called 1CAMBam9. This font will not be installed on your PC by default. You can find this font and an interesting discussion of the backstory at: http://www.OfItselfSo.com/MiscNotes/CAMBamStickFonts.php.
- As mentioned in previous sections, a TrueType font needs to be converted to a sequence of GCode straight line segments so that it can be engraved. Actually this process is really quite simple when using .NET - if you know how. However, it is hard to find a discussion of the technique on the Internet. In order to rectify this problem, a reasonably detailed discussion has been placed here: http://www.OfItselfSo.com/MiscNotes/TrueTypeFontToGCode.php Of course you can always look at the NamePlateGenerator source code on GitHub as well.
Things Programmers Might Wish to Know
- The source code contains an interesting demonstration of TrueType Font to GCode conversion in .NET. Just drop a breakpoint in the panelLabelDisplay_Paint routine and the debugger will walk you right through it. Everything is extensively commented and a fuller discussion can be found here.
- The source code contains a useful subroutine named InvertForOutput which uses a transformation matrix to convert a graphics path from the standard .NET coordinate system (0,0 is at top left and positive Y moves down the screen) to one in which 0,0 is at the screen center and positive Y moves upwards.
- The same InvertForOutput routine also converts the path coordinates from Windows screen DPI values to inch values suitable for GCode output. This is also done automatically by the same transformation matrix.
The Source Code
The NamePlateGenerator Application is open source and is released under the MIT License. You can download, clone, fork or create pull requests at the following address:
https://github.com/OfItselfSo/NamePlateGenerator
Download the Binary
The best option is to acquire the code from the Github repo (see above) and compile it yourself (Visual Studio 2019 C# was used for development). However,if you just want to use the code as-is, a pre-compiled binary NamePlateGenerator.exe
file is available below. There is no installer - just unzip it where you want it and run.
Acknowledgments
- The icon used for the NamePlateGenerator app is "Palm Tree Icon", by DaPino.
- The fundamental concepts of how to use .NET to convert TTF Fonts to GCode were derived from Jim Steinbrecher's "Font to G-Code" software. The Visual Basic source for this work can be found at https://www.vbforums.com/showthread.php?666224-Font-to-G-Code-CNC-VB2005E. Thanks Jim!
- Joseph Coffland's free CAMotics CNC Simulator https://camotics.org/ was used extensively to test the GCode output during development. Great software - thanks Joseph!
- The list of Nebulae used in the sample CSV file was derived from Wikipedia.
License
The contents of this web page are provided "as is" without any warranty of any kind and without any claim to accuracy. Please be aware that the information provided may be out-of-date, incomplete, erroneous or simply unsuitable for your purposes. Any use you make of the information is entirely at your discretion and any consequences of that use are entirely your responsibility. All source code is provided under the terms of the MIT License.