How to Make G Code for a CNC Machine
This guide walks through the full path from a 3D model to a proven G code file: defining machining requirements, setting up CAM, generating toolpaths, post-processing, and validating the program before the spindle turns. It is written for engineers and machinists who need parts that hold tolerance on the first run, not just code that looks correct on screen.

In this article
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Key takeaways
What G code actually controls on a CNC machine
G code is the instruction set that tells a CNC machine where to move, how fast, and with which tool. Each block of text is one command line. A typical line carries a motion mode such as G01 for linear feed, coordinates in X, Y, and Z, a feed rate in F, and sometimes a spindle speed in S. M codes handle auxiliary actions like coolant on, spindle start, and tool changes.
For a 3-axis mill, the axes are X, Y, and Z. A 4-axis machine adds one rotary axis, usually A. A 5-axis machine adds two rotary axes that can move at the same time as the linear axes. That simultaneity is where programming gets difficult, because the tool tip position depends on the combined motion of all five axes.
If you want to know how to make G code for a CNC machine that survives contact with metal, start by understanding what the control needs. It needs safe Z heights, known work offsets, correct tool length offsets, and feeds that match the material and cutter. Code that ignores any of these will either scrap the part or damage the setup.
- 1G00 rapid positioningMove at machine maximum speed. Never use it in the cut.
- 2G01 linear interpolationControlled feed move. Use for all cutting moves on straight paths.
- 3G02 / G03 circular interpolationClockwise and counterclockwise arcs. Needed for radii and bores.
- 4G54 to G59 work offsetsDefine where the part sits relative to machine zero.
Define part requirements before you make G code for CNC machine work
Before any programming, settle the manufacturing plan. Decide the operation type: 3-axis milling, 4-axis turning, or 5-axis simultaneous machining. Decide the stock form and how much material comes off each face. On a part with tight features on five sides, the workholding sequence often drives the whole program structure.
Tolerance drives toolpath strategy. A slot held to ±0.005 mm needs a different finishing approach than a bracket held to ±0.1 mm. A fine finish of Ra 0.2–0.8 μm usually means a separate semi-finish pass and a light finishing pass, not one heavy cut. If the print calls for Ra 1.6–3.2 μm as-machined, you can leave more material on the finish pass.
Material matters too. Aluminum 6061 and 7075 cut cleanly at high spindle speeds. Stainless 316 and 17-4PH work-harden if the cutter rubs instead of cutting, so feeds should stay aggressive enough to bite. Titanium Ti-6Al-4V and Inconel need lower surface speeds and more coolant. Write these values into the CAM tool library before you generate a single toolpath.
- 1Operation type3-axis, 4-axis, or 5-axis simultaneous. Match the machine to the feature access.
- 2Stock and workholdingDefine stock size and vise or fixture position. This sets your work offset.
- 3Critical tolerancesList the dimensions that matter. Program extra care only where needed.
CAM setup: how to make G code for CNC machine from a CAD model
Import the CAD model into CAM software and check it first. A model with open surfaces, duplicate faces, or zero-thickness walls will produce broken toolpaths. Repair the solid before you continue. Then set the stock model, usually as a bounding box plus a small allowance, and define the work coordinate system at a real, touchable datum on the part.
Choose tools from your actual tool crib, not from a default library. Enter the true cutter diameter, corner radius, flute count, and stick-out. Stick-out matters more than most people expect: a 6 mm end mill hanging 60 mm out of the holder will deflect and chatter even at conservative feeds. Keep stick-out as short as the geometry allows.
Set cutting parameters per material. For aluminum 6061 with a 6 mm carbide end mill, a starting point is 12,000 rpm and 1,500–2,500 mm/min feed for roughing, with 0.5–1.0 mm radial engagement and full depth if the tool allows. For stainless 316, drop the surface speed sharply: 2,000–3,500 rpm and 300–600 mm/min. These are starting ranges, not guarantees. Listen to the cut and adjust.
- 1Repair geometry firstFix open surfaces and duplicate faces before toolpath generation.
- 2Real tool dataEnter measured diameter, corner radius, and stick-out for every cutter.
- 3Work offset on a real datumPick a face or bore the operator can touch off accurately.
Toolpath strategy: rough, semi-finish, finish
Build toolpaths in passes, not one operation. A roughing pass removes bulk material with a larger stepover and leaves 0.3–0.5 mm of stock on walls and floors. A semi-finish pass equalizes that stock so the finishing cutter sees a consistent load. The finishing pass cuts to size with a small stepover, often 0.05–0.2 mm for a fine finish.
Use adaptive or trochoidal roughing when the material is tough or the tool is small. These paths keep radial engagement low and constant, which reduces heat and tool breakage. On deep pockets, add a helical entry instead of plunging straight down. A straight plunge on a carbide end mill in stainless is one of the fastest ways to chip a corner.
Climb milling is the default for most CNC finishing on modern machines with ball screws. It gives better surface finish and longer tool life. Conventional milling still has a place on older machines with backlash, or on castings and forgings with a hard skin that would otherwise dull the cutter edge on entry.
- 1Leave stock for finishing0.3–0.5 mm on walls and floors after roughing.
- 2Helical entry on pocketsAvoid straight plunges that shock the cutter tip.
- 3Climb mill by defaultBetter finish and tool life on machines with tight backlash.
Post-processing and validating the program
The post-processor converts internal toolpath data into the G code dialect your control reads. Fanuc, Siemens, Heidenhain, and Haas all differ in arc handling, canned cycles, and subprogram syntax. A post built for a 3-axis mill will not produce usable code for a 5-axis center with a rotary table. Verify the post matches the machine model and its optional features.
After posting, read the code. Check the first 20 lines and the last 20. Confirm the work offset, tool numbers, spindle direction, and safe Z height. Look for any G00 move that passes through the part, and confirm that the tool change position clears the fixture and rotary table.
Run a simulation in CAM, then dry-run on the machine with the tool offset away from the stock. Use single block and low feed override for the first pass. Watch the distance-to-go display on approach moves. This step is boring and it is the one that prevents most scrapped parts and broken tools.
- 1Match post to controlA generic post can output valid syntax the control rejects.
- 2Read head and tailCheck offsets, tool numbers, and safe Z before running.
- 3Dry run with offsetProve the path above the stock before cutting metal.
Step by step: how to make G code for CNC machine programs
- 11. Freeze the part design and drawingLock the CAD revision before programming. Add GD&T, datums, and surface finish notes. Any change after this point forces a toolpath rebuild.
- 22. Choose the machine and workholdingMatch axis count to feature access. For a part needing five faces, plan the vise or fixture sequence first. This decides how many setups the program needs.
- 33. Import the model and set stock, WCS, and toolsRepair geometry, define stock with 1–2 mm allowance, place the work coordinate system on a touchable datum, and load real tool data including stick-out.
- 44. Generate roughing, semi-finish, and finishing toolpathsRough with 0.3–0.5 mm stock left. Semi-finish to equalize load. Finish with 0.05–0.2 mm stepover for fine surfaces. Use helical entry on pockets.
- 55. Simulate and check for collisionsRun full machine simulation including holder and fixture. Look for gouges, rapid moves through material, and rotary table interference on 5-axis paths.
- 66. Post-process with the correct postSelect the post for the exact control model. Review arc output, canned cycles, and subprogram calls. Fix post issues at the post, not by hand-editing every file.
- 77. Dry run and prove out the first partRun above the stock with single block and reduced feed override. Measure the first article against the drawing before releasing the program to production.
- 88. Document and version the programSave the CAM file, posted code, setup sheet, and tool list under one revision number. Note any hand edits so the next run starts from a known state.
Manual programming vs CAM for making G code
Use this to decide which route fits the job.
| Factor | Manual G code | CAM-generated G code |
|---|---|---|
| Best for | Simple 2D profiles, one or two tools | Pockets, 3D surfaces, multi-tool setups |
| Programming time | Minutes for a short contour | Longer setup, faster for complex parts |
| Error risk | High on long hand calculations | Lower with simulation and collision check |
| 5-axis support | Impractical by hand | Standard output from CAM |
| Skill needed | Strong G code and control knowledge | CAM operation plus code review |
| Change handling | Edit lines by hand, easy to miss one | Rebuild toolpath, regenerate code |
| Typical use | Fixtures, simple lathe work, quick fixes | Production parts with tolerances |
When to program in-house and when to hand it off
If the part is simple, the machine is known, and you have proven posts, program it in-house. If the geometry needs 5-axis simultaneous motion or the deadline is tight, send the model to a shop that already has the machines, posts, and inspection in place.
Frequently asked questions
Can I make G code without CAM software?
Yes, for simple work. A straight profile, a facing pass, or a single drilled hole can be written by hand in a few lines. You need to know the control dialect and keep the moves simple.
Once the part has pockets, radii, or more than two tools, hand programming becomes slow and error-prone. CAM pays for itself on the first complex part.
What is the difference between G code and M code?
G codes set the machine mode: motion type, coordinate system, units, and compensation. M codes trigger discrete actions such as spindle on, coolant on, and tool change.
A line often carries both. G01 sets a feed move, M08 turns coolant on, and the coordinates follow in the same block.
How does G code affect part precision?
The code sets the path, but precision comes from the machine, tool, and setup. A correct program on a worn machine still misses tolerance.
Where code matters most is cutter compensation, arc accuracy, and consistent stock allowance between roughing and finishing. Errors there show up directly as dimensional drift.
What should I do if my G code produces a bad part?
Stop and measure the part against the drawing. Identify whether the error is dimensional, positional, or surface finish.
Check offsets first, then tool wear, then the program. Most first-article problems trace back to a work offset or tool length offset, not the toolpath itself.
Do CNC machining services generate G code for clients?
Yes. When you send a CAD model and drawing, the shop builds the CAM program, selects tooling, and proves out the process on its own machines.
At GreatLight, programming and setup are handled in-house across 127 high-precision CNC machines, including 16 simultaneous 5-axis centers. Uploads stay confidential and an NDA is available on request.
How long does it take to learn to make G code for a CNC machine?
Basic hand coding for simple profiles takes a few weeks of practice. Reading and editing CAM output confidently takes a few months.
Multi-axis programming is a longer path. Most engineers learn it on real jobs with simulation and a dry run, not from tutorials alone.
Send your CAD model and get a programmed, quoted part
Share your model and drawing. We return a quotation with DFM feedback within 12 hours, and production can start within 24 hours once the process is approved.
12-hour quote100% inspection before shipmentNDA on request