Manufacturing & engineering
G-code
Definition
G-code is the line-based programming language family used to command CNC machine motion and state. A program can set positions, feeds, units, coordinate systems, tool and cutter offsets, spindle direction, coolant, canned cycles, and program control, with controller-specific dialects and extensions.
Also known as: G code, RS-274
Updated
A program is more than XYZ motion#
The LinuxCNC language overview describes G-code as a line-based language derived from RS274/NGC. Each line, also called a block, contains words made from a letter and a value. G1 X3, for example, commands a straight move at the programmed feed rate to an X position of 3.
The active machine state matters as much as the coordinates. G-code can select inch or metric units, absolute or incremental distance, work coordinate systems, tool offsets, feed modes, spindle direction, and coolant. Many commands are modal, so they remain active until another command changes them.
Controllers do not all read the same dialect#
G-code is a family of related controller languages, not one perfectly portable file format. The same machining plan may need different codes, canned cycles, headers, tool-change logic, or safety blocks for different controls.
CAM systems therefore use a postprocessor to turn a generic toolpath into controller-specific output. Onshape's G-code export guide requires the user to choose a machine and post settings before code is written.
Safe output needs explicit state#
The LinuxCNC guide recommends a preamble that sets the modal state a program depends on. Units are especially important: a program that does not explicitly set inch or metric mode can run at the wrong scale on a controller carrying state from an earlier job.
Posted code should be reviewed and proved out using the shop's established machine procedure. A correct-looking toolpath in CAM does not establish that the controller will interpret every command as intended.
Sources
Related terms
CAM
CAM is software that turns CAD geometry into toolpaths and machine instructions, such as G-code for a CNC mill or lathe. It needs clean solid geometry, and it is where problems like unreachable corners and missing tool clearance show up.
Postprocessor
A CAM postprocessor converts software-neutral toolpath data into the NC code dialect and program structure expected by a specific CNC machine and controller. It maps motions, coordinate systems, tool changes, canned cycles, spindle and coolant commands, and other machine behavior into executable output.