GreatLight CNC Machining Factory logo
CNC Machining
Rapid Prototyping
Materials
Industries
News
About GL

Get Instant Quote

Programming Guide

Basic Knowledge of CNC Factory Programming

A working guide to how a CNC factory turns a 3D model into machine code: G-code structure, work offsets, tool data, CAM setup and the 5-axis rules that decide whether a part runs clean. Written for design and manufacturing engineers who review programs or release drawings to a shop.

G-code and M-codeWork offsetsCAM setup5-axis rules
Basics of G-code knowledge of CNC machines
Overview

What Programming Controls on the Shop Floor

Programming is the layer between the CAD model and the spindle. It sets where the part sits, how fast the tool moves, and how the machine checks that the cut is where it should be.

Fundamentals

G-code, M-code and the Structure of a Program

A CNC program is a numbered list of blocks. Each block holds one or more words: a letter address plus a value. G01 X50.0 Y30.0 F800 tells the machine to move in a straight line to that point at 800 mm/min. G00 is the rapid move, G02 and G03 are circular interpolation clockwise and counter-clockwise. G17/G18/G19 pick the plane, and G20/G21 pick inch or metric input.

M-codes handle machine functions that are not motion: M03 spindle on clockwise, M05 spindle stop, M08 coolant on, M30 end of program. Feed and speed come from S and F words. On a modern control these can be overridden at the panel, so the program should not sit at 100% of the tool's limit.

The order inside a block matters less than the order of blocks. Approach, cut, retract, then index. A program that retracts through stock will break a tool even if every coordinate is correct. This is why we simulate the full toolpath and check the setup sheet before the first cut, not just the final geometry.

  • 1
    One block, one intentKeep motion, feed and tool change on separate lines so the operator can read the sequence.
  • 2
    Use canned cyclesG81/G83 for drilling and G84 for tapping cut programming time and reduce typing errors.
  • 3
    Subprograms for repeated featuresBolt patterns and pocket arrays run from one subprogram with an offset call.
  • 4
    Keep a setup sheetTool list, offsets and stock size on one page next to the machine.
Setup

Work Offsets, Tool Length and the Datum You Choose

Work offsets (G54 to G59 on most controls) define where the part origin sits relative to machine home. Get this wrong and every feature shifts by the same amount. The datum should match the drawing: usually a corner or a bore, not an arbitrary point in space. For five-sided parts we set a second offset after each rotation so the operator does not touch the origin again.

Tool length offsets tell the control how far the tip of each tool is from the gauge line. They are measured on a presetter or touched off in the machine. Diameter wear offsets compensate for flank wear on the cutter. Both matter more on long runs: a 0.02 mm wear change over 500 parts is a scrap pile.

Fixtures decide how repeatable the datum is. A vise stop with a hard jaw repeats to a few microns. A soft jaw cut in place repeats better. When a part has no flat face to clamp, we add a machining tab or a sacrificial boss, then remove it in a second operation. That is a programming decision, not just a fixturing one.

Reference

Common G-codes and What They Do

The codes that show up in almost every milling program.

CodeFunctionWhen it matters
G00Rapid positioningApproach and retract; check clearance height
G01Linear interpolationAll straight cuts, with F feed
G02 / G03Circular interpolationBores, fillets, radii; needs I, J or R
G17 / G18 / G19Plane selectionArc direction and cutter comp plane
G40 / G41 / G42Cutter compensationOff, left, right of the path
G43Tool length compensationApplies the H offset after a tool change
G54–G59Work coordinate systemsPart origin for first and second ops
G73 / G83Peck drilling cyclesDeep holes; avoids chip packing
G84Tapping cycleSynchronized spindle and feed
G90 / G91Absolute / incrementalAbsolute for features, incremental for patterns
CAM

From CAM Setup to Verified Toolpath

CAM software takes the solid model and the stock model, then builds operations: face, rough, semi-finish, finish, drill. The programmer picks the tool, the stepover, the stepdown, the lead-in and the clearance plane. In a five-axis job the tool axis is a variable, not a constant, so the programmer also sets the lead and tilt angles.

Stock model accuracy is the part most people skip. If the CAM stock does not match the actual saw cut, the first roughing pass may be air or may be a full-width cut. We model stock to the real billet size and note the saw allowance on the setup sheet.

Verification runs in two layers: a CAM simulation that catches gouges and collisions, then a machine simulation that checks the actual head, table and fixture envelope. On a five-axis machine with a Ø400 mm rotary table, the second layer is not optional. A trunnion collision is expensive.

Post-processing converts the CAM output into the dialect of the control. Fanuc, Siemens, Heidenhain and Mazak each read slightly different code. The post must match the machine, including its rotary axis direction and its safe retract behavior. A post copied from another machine is a common source of scrapped first articles.

  • 1
    Tool library disciplineOne library per machine family, with real stick-out and holder geometry.
  • 2
    Feeds and speeds from dataStart from the cutter supplier's chip load, then adjust for the material and setup rigidity.
  • 3
    Rest machiningUse the previous tool's remaining stock so small tools cut only what is left.
  • 4
    First-article checkMeasure the critical features before the run continues.
5-axis

Five-Axis Programming Adds Two Variables

A three-axis machine moves X, Y and Z. A five-axis machine adds two rotary axes: either the table tilts (A and C) or the head tilts, or both. That lets the tool reach undercuts, drill angled holes in one setup, and keep a short, stiff tool on a deep cavity. It also means the programmer must think about the pivot distance, the rotary center and the machine's singularity points.

The most common five-axis mistake is programming a toolpath that passes near the rotary center. The C-axis can spin very fast there for a small linear move, and the surface finish suffers. Programmers avoid this by tilting the part or by choosing a different lead angle so the tool stays away from the pole.

Simultaneous five-axis is not always the answer. For a part with holes on four faces, 3+2 positioning is faster to program, easier to verify, and often cheaper per part. We reserve simultaneous motion for contoured surfaces, impellers, and features that cannot be reached any other way. The choice should come from the geometry, not from the machine list.

Tolerances

Tolerance, Finish and Process Capability

A drawing tolerance of ±0.005 mm is achievable on a rigid setup with a sharp tool and a stable thermal environment. It is not achievable across a long unsupported bore or on a thin wall that deflects under cutting force. Before programming, we look at the feature, not the title block: how far is it from the datum, how much material surrounds it, and can the tool reach it without a long overhang.

Surface finish follows the same logic. A fine finish of Ra 0.2–0.8 μm needs a small stepover, a high spindle speed and a tool with a large nose radius. A high finish of Ra 0.8–1.6 μm is typical for a well-run finishing pass. As-machined at Ra 1.6–3.2 μm is fine for most functional surfaces. If the drawing calls for a mirror finish on a deep pocket, the cost is in the tooling and the cycle time, not in the code itself.

We program to the middle of the tolerance band where the process allows, not to the nominal. That leaves room for tool wear and thermal drift over a long run. Critical features are inspected with the same datum used in the program, so the numbers agree.

Selection

Which Machine Setup Fits the Part

A quick guide to picking the machine and the programming approach.

Part featureTypical setupProgramming note
Prismatic part, features on one face3-axis mill, vise or soft jawsSimple G-code, one work offset
Features on four sides4-axis or 3+2Second offset per rotation; verify clearance
Contoured surface, undercutSimultaneous 5-axisWatch rotary center and tool axis limits
Turned shaft with milled flatsMill-turn centerOne program, one datum, no re-chuck
Thin wall or long bore3-axis with supportLight stepdown; check deflection
Large plate up to 4,000 mmLarge-travel 3-axisPlan datum and clamping before first cut
FAQs

Common Questions from Engineers

Do I need to send G-code with my CAD file?

No. Send the 3D model, the 2D drawing with tolerances, and the material and finish spec. We write the program for the machine that will run the job.

If you already have a proven program and a post for our control, we can review it. Otherwise we regenerate from the model so the setup sheet and offsets match our tooling.

How do you decide between 3-axis, 4-axis and 5-axis?

We look at how many faces carry features, whether the tool can reach them without a long overhang, and how much repositioning the part needs. Fewer setups mean tighter location between features.

A part with holes on four sides usually runs as 3+2 positioning. Simultaneous five-axis is reserved for contoured or undercut surfaces where a three-axis approach would need multiple fixtures.

What tolerance can a CNC factory hold in normal production?

We work to ±0.005 mm on features that support it, with the right tool and a rigid setup. Thin walls, long bores and deep pockets need a wider band or extra support.

We flag features that are tight for their geometry during the DFM review, before the program is released.

How is the part datum chosen?

The datum comes from the drawing and the function of the part. A mounting face, a bore or a corner that locates in the assembly is usually the best choice.

For multi-setup parts we carry the same datum through every operation, so inspection and machining use the same reference.

Can you machine a one-off prototype and a production run from the same program?

Yes. There is no minimum order quantity, so a single prototype and a 10,000-part run are both possible. The prototype program is often kept as the base and optimized for cycle time before the production run.

Tool life and chip evacuation are the main changes between the two versions.

Send a Model, Get a Program Review

Upload your CAD and drawing. We return a quotation and a free DFM analysis within 12 hours, with notes on datum choice, tolerances and the machine setup we would use.

12-hour quoteFree DFM analysis100% inspection before shipmentNDA on request

Trusted by engineers and manufacturers worldwide

Tesla Ford Motor Company BYD Auto Denso Magna International Boeing Airbus Medtronic KUKA FANUC