Featured image of G-Code Generator: All You Need to Know
Machine See, Machine Do

G-Code Generator: All You Need to Know

Picture of David Pechter
by David Pechter
Apr 21, 2019

G-code generators create the detailed machine instructions necessary to create a part, either in 3D printing or CNC machining. Learn more about what a G-code generator is and how it works.

G-Code Generator What's G-Code?

A CNC laser cutter in action.
A CNC laser cutter in action. Source: weldflowmetal.com

These days, computer numerical control (CNC) machines range from 3D printers to lathes, mills, and engraving machines that use lasers, plasma, or water jets. Computer control manages tool movements, spindle speed and coolant flow for CNC lathes and mills, and extrusion and bed temperatures for 3D printers. Over time, a common computer language evolved for CNC: G-code.

G-code is the not-so-secret sauce that runs a 3D printer. Based on your STL model and your settings, your slicing program decides how to make your part. It takes into account layer thickness, infill percentage, support, adhesion, extrusion, and bed temperature to generate a G-code recipe for your part, which can be understood by your printer. Each motion of the printer is given, for example, to trace the perimeter, cross-hatch, or trace interior holes. Temperature control, extrusion, and retraction are also given in G-code.

For CNC machining and cutting, G-code starts the cutter, controls coolant, and moves the machine to execute a path or contour of engraving, milling, or cutting.

Alphabet Soup

A language of one-liners, many G-code commands do begin with the letter G, but in fact, all letters of the alphabet are used. Codes beginning with G, M, X, Y, and Z are the most common. In 3D printing specifically, E and F are used for extrusion and feed rate.

If you see something like GXX, that’s a preparatory code, and many of them move the machine. MXX codes are the miscellaneous commands, controlling things like coolant, spindle speed, fans, and heaters. X, Y, and Z are coordinates.


G-Code Generator What's a G-Code Generator?

Simplify3D, a 3D printing slicer.
Simplify3D, a 3D printing slicer. Source: All3DP

Generating G-code means creating a full description of how to make something with a machine. Originally, G-code was hand-coded – imagine that! For a simple machined part, this can work. With an understanding of machining, knowledge of the relevant G-codes, and some copy and paste, anyone can create the full G-code recipe for a machined part.

Slicers (3D Printing)

3D printing creates smooth, attractive parts because it uses a small nozzle to extrude thin layers of plastic. Even a small printed part can have hundreds of layers, each with hundreds of small motions, meaning tens of thousands of lines of G-code.

For that kind of recipe, hand coding is a problem. For 3D printing, G-code generators are called slicers because they “slice” an STL file into layers that can be extruded as an outline with cross-hatching or infill. A slicer also takes into account the extrusion temperature, fan, and speed requirements for running a particular material on your machine.

Other Generators (CNC Machining)

CNC machining may not have the thousands of steps of 3D printing, but it too can get involved. When cutting even a single pocket or through-hole, it takes several passes around the same path at increasing depths to make the complete feature. And even then, a finishing cut, removing a final layer of material, is typically used to get the best surface finish.

(Image: A CNC machine running G-code. Source: 1stmachineryauctions.com)

Other details that are hard to add by hand are tabs. These small bridges or breaks in the contours of the part keep it attached to the parent material. A CNC router code generator can add these easily. Automating the creation of multiple passes, finishing cuts, and tabs are just some of the ways a CNC code generator simplifies the G-coding process.


G-Code Generator Generation

Hint: This isn't G-Code; it's code to code G-code!
Hint: This isn't G-Code; it's code to code G-code! Source: techcrunch.com

Because CNC machines perform repetitive operations, the same basic cycle can be repeated. Let’s take a look at some of the steps that go into creating G-code, which are automated by a g-code generator.

3D Printing

After dividing an STL file into layers, the basic cycle for generating G-code for each layer is as follows:

The basic cycle for generating 3D printing G-code is as follows:

  1. Identify the outlines, both exterior and interior, in order to determine what’s “inside” and “outside” the layer.
  2. Determine how infill will be laid within the interior of the layer.
  3. Create an optimized traversal of the layer’s extrusion for outlines and infill.
  4. Set the speed, extrusion, temperatures, and fan speed.
  5. Create G and M codes for the start and end.
  6. Convert the full traversal path into GXX movements, adding extrusion E and feed rate F.

For more on how a slicer works, step by step, take a look at this DIY g-code generator on Instructables.

CNC Machining

Parts to be CNC milled are similarly divided into depths, where a cycle is repeated at each layer:

  1. Set exterior or interior for pockets or through-holes.
  2. Set the depth of cutting and passing required to produce the full contour height.
  3. Set the tool diameter or kerf (the cutting diameter of a laser, plasma, or a water jet).
  4. Set tool alignment to be inside or outside of the path.
  5. Set the spindle speed or cutting intensity.
  6. Create G and M codes for the start and end.
  7. Create positions for the path, including multiple passes at depth of cut to create the full contour.
  8. Convert the geometry into GXX motions.

For more on how to create CNC G-code, take a look at this online course.


G-Code Generator Examples

A G-code script.
A G-code script. Source: David Pechter / All3DP

To give you a little taste, here are some sample G-codes.

The following line homes X and Y:

G28 X0 Y0;

This one instructs a linearly interpolated movement using absolute X and Y coordinates:

G01 X61.888 Y127.862 E19.90544;

Here are two special codes for 3D printing:

M107; (start with the fan off)

M109 R245; (set the extruder temperature to 245 °C and wait)

And here are two for CNC lathes and mills:

M03; (spindle on, clockwise)

M08; (coolant on)

Thirsty for more? Check out our in-depth article on G-code examples. In our programming tutorial, we go even deeper with G-code commands.


G-Code Generator Generators

Visualized G-code for a Harley tank.
Visualized G-code for a Harley tank. Source: cnccookbook.com

In fact, there are a plethora of G-code generators out there.

For 3D printing, we have another article that lists the best 3D slicer software. One of the great things about 3D printing is that STLs are the standard input file for slicing.

For CNC machining, the story is a little more complicated. First, there are tools that help automate the G-code creation while leaving the understanding of the geometry to the user. You can see an example with this Excel-based G-code generator and a related tutorial video.

Next, we can start with a 2D source file, like a PDF, DXF, or an image. The program does the heavy lifting of interpreting the paths in the drawing or image. Some examples are Cut2D, FlowPath for water jet, DXF to G-code, and PixelCNC for images. There’s even an Inkscape G-code plotting extension for creating cutting programs from Inkscape designs.

Last, the most sophisticated G-code generators are called CAM, or computer-aided manufacturing. These tools interpret a broad range of files, including 3D solid models, and use them to automate the full G-code creation process. See our review of the best CAM software tools for SolidWorks and AutoCAD.

Feature image source: img.itch.zone

License: The text of "G-Code Generator: All You Need to Know" by All3DP is licensed under a Creative Commons Attribution 4.0 International License.

Subscribe to updates from All3DP

You are subscribed to updates from All3DP

You can’t subscribe to updates from All3DP. Learn more… Subscribe

You can’t subscribe to updates from All3DP. Learn more…

Recommended for you