CNC G-Code for Fanuc Milling Controllers

Hand-write and verify a Fanuc G-code milling program for a mounting plate.

Introduction

30 Second Summary

Every time you hit Post in Mastercam, the software translates your toolpaths into hundreds of lines of G-code. When something goes wrong at the machine, the CNC programmer who can read and edit that code is the one who fixes it.

In this project, you will hand-write a complete Fanuc G-code milling program for an aluminum mounting plate. You will then verify the program in Mastercam 2025 using its Backplot and Verify simulation tools.

What You'll Build

A two-tool CNC milling program in the Fanuc 0i dialect that profiles a 100mm x 60mm aluminum mounting plate with rounded corners and peck-drills four bolt holes.

By the end of this project, you'll have:

  • A hand-written G-code file (.nc) ready for a Fanuc 0i controller, complete with program structure, safety blocks, and inline comments.
  • Verified toolpath visualization in Mastercam using Backplot and Verify simulation.
  • A solid refresher on motion commands, arc programming, cutter compensation, and canned drilling cycles.
  • Secret Mission: Read and optimize Mastercam-posted G-code for the same part.

Are there any prerequisites?

This project assumes basic familiarity with CNC milling concepts such as axes, spindle speed, tools, and workholding. You also need access to Mastercam 2025 on a workstation and a BFW VMC with a Fanuc 0i-MD or 0i-MF controller available for reference.

Set Up Your Tools and Review the Part

Every CNC milling program is a sequence of G-code instructions that tells the machine exactly where to move and how to cut. Writing these instructions by hand gives you the fluency to read, edit, and troubleshoot programs directly at the controller.

In this step, you will create the file your program lives in, set up Mastercam 2025 as your verification tool, and review the part geometry that drives every line of code.

Why hand-write G-code when you have Mastercam?

Mastercam can generate G-code automatically through its post processor. Hand-writing builds the fluency to read that output, edit it at the controller, and troubleshoot issues on the shop floor without going back to the CAM workstation.

In this step, get ready to:
  • Create a new mounting_plate.nc file in your text editor.
  • Open Mastercam 2025 with a new Mill file for program verification.
  • Review the part dimensions, hole positions, and coordinate system.
Create your G-code file

Your entire G-code program will live in a single plain-text file with the .nc extension. This is the standard file format for CNC programs.

macOS

  • Press Cmd+Space to open Spotlight search.
  • Type TextEdit and press Enter to open it.
  • Click Format in the menu bar.
  • Click Make Plain Text.
  • Press Cmd+S to save.
  • In the Save As field, type mounting_plate.nc.
  • Choose your Desktop as the save location, then click Save.
  • If TextEdit asks about the file extension, confirm you want to use .nc by clicking Use .nc.

Disable the .txt extension default

If TextEdit keeps appending .txt to your filename, go to TextEdit > Settings > Open and Save and uncheck Add ".txt" extension to plain text files.

Windows

  • Press the Windows key to open search.
  • Type Notepad and press Enter to open it.
  • Press Ctrl+S to open the Save dialog.
  • In the File name field, type mounting_plate.nc.
  • Change the Save as type dropdown to All Files (*.*).
  • Choose your Desktop as the save location, then click Save.

Why change the Save as type?

Notepad defaults to saving as .txt files. Changing to All Files (*.*) lets Notepad save the file with the exact .nc extension you typed.

The title bar of your text editor should now display mounting_plate.nc.

File saved as .txt instead of .nc?

On macOS, make sure you switched to plain text mode (Format > Make Plain Text) before saving, and confirmed the .nc extension in the dialog.

On Windows, double-check that Save as type is set to All Files (*.*) instead of Text Documents (*.txt). If the file already saved as .txt, rename it in File Explorer to mounting_plate.nc.

Help me save my file with the .nc extension.

Open Mastercam 2025 and create a new Mill file

Mastercam is your verification tool for this project. You will load your G-code into Mastercam and use its Backplot and Verify features to visualize the toolpath before the program ever touches a machine.

  • On the Windows workstation, press the Windows key to open search.
  • Type Mastercam 2025 and press Enter to open it.
  • Click File in the menu bar.
  • Click New.
  • If prompted to choose a machine type, select Mill.

You should see a blank Mastercam workspace. You will not create toolpaths in Mastercam for this project. It is your verification tool only.

What are Backplot and Verify?

Mastercam offers three levels of toolpath verification. Backplot shows a basic visualization of the tool's motion path. Verify goes further by simulating stock removal and checking for interference.

You will use Backplot in the next step to confirm your linear moves, and Verify later to watch material being cut away from a solid stock model.

Review the part drawing and coordinate system

Before writing any G-code, you need a clear picture of the part you are programming. Every dimension and hole position translates directly into coordinates in your code.

The part is a 100mm x 60mm aluminum mounting plate, 10mm thick, machined from a 110 x 70 x 12mm stock blank. Here are the key specifications:

  • Finished part size: 100mm (X) x 60mm (Y) x 10mm (Z depth).
  • Stock size: 110 x 70 x 12mm aluminum.
  • Four R5 rounded corners (5mm radius arcs at each corner).
  • Four 8.5mm bolt holes at positions (15, 15), (85, 15), (85, 45), and (15, 45).
  • Two tools: T01 is a 10mm flat end mill for profiling, T02 is an 8.5mm drill for bolt holes.

What is a work coordinate system?

A work coordinate system defines where (0, 0, 0) sits on your stock. In this project, the program uses G54, which is the first work coordinate offset on Fanuc controllers. G54 tells the controller where the part origin is relative to the machine's home position.

For this part, the G54 origin sits at the bottom-left corner of the finished part, on the top surface of the stock.

Here is how the axes map to the part:

  • X runs left to right along the 100mm dimension.
  • Y runs front to back along the 60mm dimension.
  • Z points straight up from the top of the part. Cutting into the material means moving in the negative Z direction.

Why does Z zero sit on the top of the stock?

Setting Z0 at the top surface of the stock is standard shop practice. It means Z-negative values represent depth into the material. When you write Z-10.0 in your program, the tool cuts 10mm down from the surface. This convention makes it easy to visualize how deep the tool is cutting at any point.

Your tools are ready and you have a clear picture of the part geometry. Next up, you will write the program header, the safety block, and your first linear moves.

Write the Program Header, Safety Block, and Linear Moves

Your text editor and Mastercam are ready. Now it is time to write actual G-code. Every Fanuc program follows a specific structure: a header that identifies the program and documents the job, a safety block that resets the controller to a known state, and then the actual machining commands.

In this step, you will write the program header with inline comments, add the safety line that protects against unexpected controller states, set up Tool 1 (your 10mm end mill), and program simple straight-line moves along the part edges. You will then load the file into Mastercam and use Backplot to visually confirm the toolpath.

In this step, get ready to:
  • Write the program number, comment header, and safety line.
  • Program the tool change, spindle start, and approach sequence for Tool 1.
  • Add straight-line profile cuts and verify the toolpath in Mastercam Backplot.
Write the program number, comment header, and safety line

Every Fanuc program starts with a tape-start character (%), followed by a program number. Comment lines in parentheses document the job so anyone at the machine knows exactly what the program does, what stock it expects, and which tools it uses.

After the comments, a safety line resets the controller to a known state. This is critical because modal G-codes persist between programs. Without it, leftover settings from a previous job could cause the machine to behave unpredictably.

  • Switch back to your text editor with mounting_plate.nc open.
  • Type the following program header, comment block, and safety line:
%
O1001 (MOUNTING PLATE)
(MATERIAL: ALUMINUM 6061)
(STOCK: 110 X 70 X 12MM)
(FINISHED PART: 100 X 60MM, 10MM THICK)
(ORIGIN: BOTTOM-LEFT CORNER, TOP OF PART)
(T01 - 10MM FLAT END MILL)
(T02 - 8.5MM DRILL)

(--- TOOL 1: PROFILE MILLING ---)
N10 G21 G17 G40 G49 G80 G90 (SAFETY LINE)

What does each part do?

  • % is the tape-start character. It tells the controller that a program follows.
  • O1001 (MOUNTING PLATE) assigns program number 1001 with a descriptive comment. The controller uses the O-number to identify and recall the program.
  • The comment lines in parentheses are ignored by the controller. They document the job for anyone reading the code at the machine.
  • N10 is a sequence number. Numbering by 10 leaves room to insert lines later without renumbering the whole program.

What does the safety line do?

The safety line G21 G17 G40 G49 G80 G90 resets six critical controller settings to a known state:

  • G21 sets metric units (millimeters).
  • G17 selects the XY plane for arcs.
  • G40 cancels cutter radius compensation.
  • G49 cancels tool length compensation.
  • G80 cancels any active canned cycle.
  • G90 sets absolute positioning.
  • Review your file in the text editor. You should see the % tape start at the top, the program number O1001, six comment lines, and the safety line starting with N10.

Code not looking right?

  • If you are on Mac, make sure TextEdit is in plain text mode (Format > Make Plain Text). Rich text mode adds invisible formatting that breaks G-code files.
  • Make sure every comment is wrapped in parentheses with no nested parentheses inside. Fanuc controllers treat unmatched parentheses as errors.
  • Check that the % sits alone on the very first line with nothing else on that line.

Help me troubleshoot my G-code program header and safety line in mounting_plate.nc.

Write the tool setup commands

With the controller in a known state, you need to tell it which work offset to use, which tool to load, and how fast to spin the spindle. These commands prepare the machine before any cutting begins.

  • Add the following tool setup lines directly below the safety line in mounting_plate.nc:
N20 G54 (SELECT WORK OFFSET 1)
N30 T01 M06 (LOAD TOOL 1)
N40 S3000 M03 (SPINDLE ON CW AT 3000 RPM)
N50 G43 H01 Z50.0 (TOOL LENGTH COMP, RAPID TO SAFE Z)
N60 M08 (COOLANT ON)

What does this code do?

  • G54 selects work coordinate system 1. This tells the controller where the part origin is located on the machine table. You set this during touch-off before running the program.
  • T01 M06 loads Tool 1 (your 10mm flat end mill) into the spindle. T01 selects the tool and M06 executes the tool change.
  • S3000 M03 starts the spindle clockwise at 3000 RPM.
  • G43 H01 Z50.0 activates tool length compensation using the offset stored in register H01. It also rapids the tool to Z50.0, a safe height well above the part.
  • M08 turns on coolant flood.

Why does the H-code match the tool number?

The H-code in G43 H01 references the controller's tool offset table, where each tool's measured length is stored. Matching H01 to T01 (and later H02 to T02) is standard practice because it keeps the relationship between tools and offsets obvious. Mismatched H-codes are a common source of crashes on real machines.

You won't see a visible change yet. The next chunk adds the approach moves that position the tool for cutting.

Now add the approach sequence that moves the tool from its safe height down to the cutting position. The tool approaches from below the part (in Y) so that it enters the material cleanly when cutter compensation is added in a later step.

  • Add a blank line after M08, then type the following approach sequence:
(APPROACH FROM BELOW THE PART)
N70 G00 X50.0 Y-15.0 (RAPID TO START POSITION)
N80 G00 Z2.0 (RAPID DOWN TO CLEARANCE)
N90 G01 Z-10.0 F100 (PLUNGE TO FULL DEPTH)

What does this code do?

  • G00 X50.0 Y-15.0 rapids the tool to a point below the bottom edge of the part (Y-15.0 is 15mm south of the part origin). The tool starts at the midpoint of the bottom edge so it can sweep the full profile.
  • G00 Z2.0 rapids down to 2mm above the part surface. This is the clearance height.
  • G01 Z-10.0 F100 switches to linear interpolation (feed move) and plunges the tool to full depth at 100 mm/min. The Z value is negative because the part origin is on the top surface and Z points up.

What is the difference between G00 and G01?

G00 is rapid positioning. The machine moves as fast as it can to get the tool into position. You never use G00 when the tool is touching the workpiece because the speed is uncontrolled.

G01 is linear interpolation. The machine moves in a straight line at the feed rate you specify with F. This is for cutting moves where you need control over how fast the tool moves through the material.

You won't see a visible change yet. The next chunk adds the profile cuts that you will verify in Backplot.

Unsure about the negative Y and Z values?

The program origin sits at the bottom-left corner of the finished part, on the top surface. Y-15.0 is 15mm below (south of) the part edge. Z-10.0 is 10mm below the top surface (the full thickness of the part). Both values are negative because they are below the origin in their respective axes.

Help me understand the coordinate system and negative values in my G-code approach sequence.

Write the straight-line profile cuts and verify in Backplot

With the tool at full depth, you can now program the profile cuts. For this step, you will write straight-line moves along the bottom and right edges of the part only. Arcs for the rounded corners come in the next step.

  • Add the following lines directly below the plunge line (N90) in mounting_plate.nc:
N100 G01 X95.0 (BOTTOM EDGE TO RIGHT CORNER)
N110 G01 Y55.0 (RIGHT EDGE)
N120 G00 Z50.0 (RAPID RETRACT TO SAFE Z)

What does this code do?

  • G01 X95.0 feeds the tool along the bottom edge of the part from X50.0 to X95.0. The Y value stays the same because only the X axis moves.
  • G01 Y55.0 feeds the tool up the right edge of the part. The X value stays at 95.0 because only the Y axis moves.
  • G00 Z50.0 rapids the tool back up to the safe height. You always retract to a safe Z before making any lateral rapid moves to prevent the tool from dragging through the workpiece or fixture.
  • Save mounting_plate.nc in your text editor.
  • Switch to Mastercam 2025.
  • Click File in the top menu, then click Open.
  • Navigate to your mounting_plate.nc file and open it.
  • Run Backplot to visualize the toolpath.

You should see the toolpath rendered with the rapid moves (G00) displayed differently from the feed moves (G01). The tool should move to the start position, plunge down, cut along the bottom edge to the right, cut up the right edge, and retract.

Backplot not showing the toolpath?

  • Make sure you saved the file before opening it in Mastercam. Mastercam reads the file from disk, not from your text editor's unsaved buffer.
  • Confirm the file has the .nc extension. If your text editor added .txt to the filename, rename the file so it ends in .nc only.
  • Check for missing parentheses in your comments. An unmatched parenthesis causes the controller parser to choke on the line and skip subsequent moves.

Help me troubleshoot why Backplot isn't displaying my G-code toolpath in Mastercam.

✔️ Awesome, I've got everything!

Great. Double check you have saved mounting_plate.nc before moving on.

ⓧ I'd like to double check the full code

Here is the complete mounting_plate.nc file as it should look at the end of this step. Compare it carefully with your file:

%
O1001 (MOUNTING PLATE)
(MATERIAL: ALUMINUM 6061)
(STOCK: 110 X 70 X 12MM)
(FINISHED PART: 100 X 60MM, 10MM THICK)
(ORIGIN: BOTTOM-LEFT CORNER, TOP OF PART)
(T01 - 10MM FLAT END MILL)
(T02 - 8.5MM DRILL)

(--- TOOL 1: PROFILE MILLING ---)
N10 G21 G17 G40 G49 G80 G90 (SAFETY LINE)
N20 G54 (SELECT WORK OFFSET 1)
N30 T01 M06 (LOAD TOOL 1)
N40 S3000 M03 (SPINDLE ON CW AT 3000 RPM)
N50 G43 H01 Z50.0 (TOOL LENGTH COMP, RAPID TO SAFE Z)
N60 M08 (COOLANT ON)

(APPROACH FROM BELOW THE PART)
N70 G00 X50.0 Y-15.0 (RAPID TO START POSITION)
N80 G00 Z2.0 (RAPID DOWN TO CLEARANCE)
N90 G01 Z-10.0 F100 (PLUNGE TO FULL DEPTH)
N100 G01 X95.0 (BOTTOM EDGE TO RIGHT CORNER)
N110 G01 Y55.0 (RIGHT EDGE)
N120 G00 Z50.0 (RAPID RETRACT TO SAFE Z)

Your first G-code lines are verified and cutting in a straight line. Next up, you will add circular arcs at each corner and apply cutter radius compensation so the program describes the part geometry instead of the tool center path.

Add Arcs and Cutter Compensation

In the previous step, you wrote straight-line moves and verified them in Backplot. Your tool can rapid and feed in straight lines, but the profile still has sharp 90-degree corners where the part drawing calls for smooth R5 rounded transitions.

In this step, you will replace those sharp turns with circular arcs using G03 and add cutter radius compensation so the program describes the part geometry, not the tool center path.

In this step, get ready to:
  • Replace sharp corner transitions with G03 arcs using the R format.
  • Add cutter radius compensation with G41 and G40 to offset the tool from the part edge.
  • Verify the updated profile in Mastercam Backplot.
Add the four corner arcs

The mounting plate has R5 rounded corners, not sharp 90-degree turns. To cut a smooth curve, the controller needs a circular interpolation command instead of a straight-line move.

Because the profile path travels counterclockwise around the part (bottom edge right, right edge up, top edge left, left edge down), every corner arc also sweeps counterclockwise. That makes each one a G03 arc.

  • Return to your text editor with mounting_plate.nc open.
  • Find these three lines that come after the plunge move on N90:
N100 G01 X95.0 (BOTTOM EDGE TO RIGHT CORNER)
N110 G01 Y55.0 (RIGHT EDGE)
N120 G00 Z50.0 (RAPID RETRACT TO SAFE Z)
  • Delete all three lines. You will replace them with the complete profile path including arcs.
  • After the N90 plunge line, add the following profile code:
(PROFILE CUT CCW FROM MID-BOTTOM EDGE)
N110 G01 X95.0 (BOTTOM EDGE TO RIGHT CORNER)
N120 G03 X100.0 Y5.0 R5.0 (BOTTOM-RIGHT CORNER ARC)
N130 G01 Y55.0 (RIGHT EDGE)
N140 G03 X95.0 Y60.0 R5.0 (TOP-RIGHT CORNER ARC)
N150 G01 X5.0 (TOP EDGE)
N160 G03 X0.0 Y55.0 R5.0 (TOP-LEFT CORNER ARC)
N170 G01 Y5.0 (LEFT EDGE)
N180 G03 X5.0 Y0.0 R5.0 (BOTTOM-LEFT CORNER ARC)
N190 G01 X50.0 (BOTTOM EDGE BACK TO START POINT)

What does this code do?

The profile alternates between straight-line moves (G01) and counterclockwise arcs (G03). Each arc uses the R5.0 format to specify a 5mm radius that smoothly transitions between the two adjoining straight edges.

The path starts at the midpoint of the bottom edge (X50.0), travels right, then sweeps counterclockwise around all four corners, and returns to the same X50.0 midpoint. Using G02 here would produce clockwise arcs that cut into the part instead of rounding the outside corners.

You will not see a change yet. The next chunk adds cutter compensation that completes the profile section.

Why G03 and not G02?

When traversing an outside profile counterclockwise, the corner arcs are G03 (counterclockwise). Each arc smoothly transitions the tool direction by 90 degrees in the CCW direction. Using G02 would produce an incorrect arc that cuts into the part.

Add cutter radius compensation

Right now, the programmed coordinates describe the exact part edges. The controller would drive the tool center directly along those edges, cutting the part 5mm too small (half the tool diameter on each side).

Cutter radius compensation tells the controller to offset the tool by the amount stored in its offset table. You program the part geometry, and the controller figures out where the tool center needs to be.

  • Between the N90 plunge line and the N110 profile start, insert the cutter compensation lead-in by adding this code:
(CUTTER COMP ON - LEAD IN TO PROFILE)
N100 G01 G41 D01 Y0.0 F300 (COMP LEFT ON, LEAD IN TO BOTTOM EDGE)
  • After the last profile line (N190), add the lead-out and retract by adding this code:
(CUTTER COMP OFF - LEAD OUT)
N200 G01 G40 Y-15.0 (COMP OFF, LEAD OUT FROM PART)
N210 G00 Z50.0 (RAPID RETRACT TO SAFE Z)

What does this code do?

  • G41 activates cutter compensation left. This offsets the tool to the left of the programmed path, looking in the direction of travel. For a counterclockwise outside profile, "left" puts the tool on the outside of the part.
  • D01 references the tool radius offset stored in the controller's offset table. The controller reads the tool's actual radius from offset register 1.
  • G40 cancels cutter compensation. The lead-out move (Y-15.0) pulls the tool away from the part before compensation turns off.
  • Both the lead-in and lead-out moves must be straight lines (G01) and longer than the tool radius (5mm). The 15mm lead moves satisfy this requirement.

Why climb milling with G41?

For an outside profile going counterclockwise, G41 (cutter comp left) places the tool on the outside of the part. This results in climb milling, which is generally preferred for aluminum because it produces better surface finish and less tool deflection.

You will not see a change yet. The next substep saves the file and verifies the full profile in Backplot.

Save and Backplot in Mastercam
  • Save mounting_plate.nc in your text editor.
  • Switch back to Mastercam.
  • Load the updated .nc file and run Backplot to see your toolpath.

You should see smooth arcs at each of the four corners. The tool center path should be offset outward from the programmed part geometry by the tool radius (5mm).

What should I see?

Backplot draws rapid moves and feed moves in different colors. You should see the tool approach from below the part (Y-15.0), lead into the bottom edge, trace the full rectangle with rounded corners counterclockwise, lead out below the part, and retract to Z50.0.

The four corners should be smooth curves, not sharp turns. The tool center path sits outside the part outline because cutter compensation pushes it outward.

Arcs look wrong or tool cuts into the part?

  • Check that you used G03 (counterclockwise) and not G02 (clockwise) for every corner arc. A G02 arc here would curve inward and gouge the part.
  • Confirm that G41 (comp left) is active, not G42 (comp right). For a CCW outside profile, G42 would offset the tool into the part.
  • Verify that the lead-in (N100) and lead-out (N200) moves are both G01 straight lines. The controller requires straight-line lead moves when turning compensation on or off.

Still stuck? Help me figure out why my Backplot is showing an incorrect profile with arcs and cutter compensation.

✔️ Awesome, I've got everything!

Great. Double check you have saved mounting_plate.nc before moving on.

ⓧ I'd like to double check the full code

Here is the complete mounting_plate.nc file so far. Compare it line by line with your file.

%
O1001 (MOUNTING PLATE)
(MATERIAL: ALUMINUM 6061)
(STOCK: 110 X 70 X 12MM)
(FINISHED PART: 100 X 60MM, 10MM THICK)
(ORIGIN: BOTTOM-LEFT CORNER, TOP OF PART)
(T01 - 10MM FLAT END MILL)
(T02 - 8.5MM DRILL)

(--- TOOL 1: PROFILE MILLING ---)
N10 G21 G17 G40 G49 G80 G90 (SAFETY LINE)
N20 G54 (SELECT WORK OFFSET 1)
N30 T01 M06 (LOAD TOOL 1)
N40 S3000 M03 (SPINDLE ON CW AT 3000 RPM)
N50 G43 H01 Z50.0 (TOOL LENGTH COMP, RAPID TO SAFE Z)
N60 M08 (COOLANT ON)

(APPROACH FROM BELOW THE PART)
N70 G00 X50.0 Y-15.0 (RAPID TO START POSITION)
N80 G00 Z2.0 (RAPID DOWN TO CLEARANCE)
N90 G01 Z-10.0 F100 (PLUNGE TO FULL DEPTH)

(CUTTER COMP ON - LEAD IN TO PROFILE)
N100 G01 G41 D01 Y0.0 F300 (COMP LEFT ON, LEAD IN TO BOTTOM EDGE)

(PROFILE CUT CCW FROM MID-BOTTOM EDGE)
N110 G01 X95.0 (BOTTOM EDGE TO RIGHT CORNER)
N120 G03 X100.0 Y5.0 R5.0 (BOTTOM-RIGHT CORNER ARC)
N130 G01 Y55.0 (RIGHT EDGE)
N140 G03 X95.0 Y60.0 R5.0 (TOP-RIGHT CORNER ARC)
N150 G01 X5.0 (TOP EDGE)
N160 G03 X0.0 Y55.0 R5.0 (TOP-LEFT CORNER ARC)
N170 G01 Y5.0 (LEFT EDGE)
N180 G03 X5.0 Y0.0 R5.0 (BOTTOM-LEFT CORNER ARC)
N190 G01 X50.0 (BOTTOM EDGE BACK TO START POINT)

(CUTTER COMP OFF - LEAD OUT)
N200 G01 G40 Y-15.0 (COMP OFF, LEAD OUT FROM PART)
N210 G00 Z50.0 (RAPID RETRACT TO SAFE Z)

Your profile is complete with smooth arcs and proper cutter compensation. Next up, you will add the drilling cycle for the four bolt holes using a canned peck drilling cycle.

Add Drilling with Canned Cycles

Your profile toolpath is verified and looking sharp. The end mill has traced the full outline of the mounting plate with smooth arcs at every corner and proper cutter compensation.

Now it is time to switch tools and drill four bolt holes. Instead of manually coding every plunge, retract, and repositioning move, you will use a canned cycle. This is a pre-programmed drilling routine built into the Fanuc controller that handles the entire drilling sequence in a single command.

In this step, get ready to:
  • End the Tool 1 section and set up Tool 2 for drilling.
  • Program a G83 peck drilling cycle for four bolt holes.
  • Write the program ending sequence and verify the complete program in Backplot.
End Tool 1 and set up the drill

The profile milling is done, but the Tool 1 section needs proper shutdown commands before you can switch to a new tool. Every tool section ends with coolant off and spindle stop.

  • In your text editor, find the last line of the Tool 1 section in mounting_plate.nc:
N210 G00 Z50.0 (RAPID RETRACT TO SAFE Z)
  • Add the following lines directly below N210 to end Tool 1 and begin the Tool 2 section:
N220 M09 (COOLANT OFF)
N230 M05 (SPINDLE STOP)

(--- TOOL 2: DRILL BOLT HOLES ---)
N240 T02 M06 (LOAD TOOL 2)
N250 G21 G17 G40 G49 G80 G90 (SAFETY LINE)
N260 G54 (SELECT WORK OFFSET 1)
N270 S1200 M03 (SPINDLE ON CW AT 1200 RPM)
N280 G43 H02 Z50.0 (TOOL LENGTH COMP, RAPID TO SAFE Z)
N290 M08 (COOLANT ON)

What does this code do?

  • M09 turns off coolant and M05 stops the spindle. These two lines close out the Tool 1 section cleanly.
  • T02 M06 loads the 8.5mm drill into the spindle.
  • N250 repeats the full safety line. This resets the controller to a known state after the tool change.
  • G43 H02 Z50.0 activates tool length compensation using offset register H02 and rapids the drill to a safe height of 50mm above the part.

Why repeat the safety line after a tool change?

Modal G-codes persist until something changes them. After a tool change, the controller state depends on what the previous tool section left behind.

Repeating G21 G17 G40 G49 G80 G90 guarantees a clean state every time. This is standard practice on every shop floor.

  • Save mounting_plate.nc.
  • You will not see a change in Backplot yet. The next chunk adds the actual drilling moves.

Not sure where to add the new lines?

  • Make sure you are adding directly below N210 G00 Z50.0. N220 should be the very next line after it.
  • Check that your sequence numbers continue from N220 onward in increments of 10. Skipping or duplicating a number can confuse the controller.
  • Help me figure out where to add the Tool 2 setup lines in my G-code program.
Program the peck drilling cycle

With the drill loaded and spinning, you need to move it to each hole position and run a peck drilling cycle. Peck drilling plunges the drill a small increment at a time, retracting between pecks to clear chips from the hole.

  • Add the following lines directly below N290 M08 (COOLANT ON) to program all four bolt holes:
(PECK DRILL 4 BOLT HOLES)
N300 G00 X15.0 Y15.0 (RAPID TO FIRST HOLE)
N310 G99 G83 Z-12.0 R2.0 Q3.0 F80 (PECK DRILL CYCLE)
N320 X85.0 Y15.0 (SECOND HOLE)
N330 X85.0 Y45.0 (THIRD HOLE)
N340 G98 X15.0 Y45.0 (FOURTH HOLE, RETRACT TO INITIAL Z)
N350 G80 (CANCEL CANNED CYCLE)

What does this code do?

  • N300 rapids the drill to the first hole at position (15, 15).
  • G83 starts the peck drilling cycle. Z-12.0 is the final hole depth, going through the 10mm part plus 2mm clearance below. R2.0 is the retract plane, 2mm above the part surface. Q3.0 is the peck depth, meaning the drill cuts 3mm, retracts to clear chips, then cuts another 3mm.
  • G99 tells the cycle to retract to the R plane between holes. This is faster than retracting all the way up.
  • N320 through N330 only specify new X/Y positions. The G83 cycle repeats automatically at each new position because canned cycles are modal.
  • N340 switches to G98 for the last hole. This retracts the drill to the initial Z height (50mm) instead of just the R plane, giving safe clearance before the tool moves away.
  • G80 cancels the canned cycle. Without this line, the controller would try to drill at every subsequent position the tool moves to.

What is the difference between G98 and G99?

G99 retracts the drill to the R plane (2mm above the part) between holes. This is fast because the drill only travels a short distance before repositioning to the next hole.

G98 retracts the drill all the way up to the initial Z height (the Z position before the cycle started). Use G98 on the last hole or when the drill needs to clear clamps or fixtures on its way to the next position.

  • Save mounting_plate.nc.
  • The drilling moves are in the code, but the program still needs an ending sequence before you can Backplot the complete file.

Not sure about the G83 parameters?

  • Z-12.0 is the final depth. The hole goes through the 10mm part plus 2mm clearance below.
  • Q3.0 is the peck increment. The drill cuts 3mm at a time, retracting between each peck to clear chips.
  • Make sure G80 appears on its own line after the last hole. Missing G80 leaves the canned cycle active, which causes unexpected drilling on the next move.
  • Help me understand the G83 peck drilling parameters in my Fanuc program.
Write the program ending sequence and verify

Every G-code program needs a clean ending sequence that retracts the tool, shuts down the spindle and coolant, and sends the machine axes back to their home position.

  • Add the following lines directly below N350 G80 (CANCEL CANNED CYCLE) to finish the program:
(PROGRAM END)
N360 G00 Z50.0 (RETRACT TO SAFE Z)
N370 M09 (COOLANT OFF)
N380 M05 (SPINDLE STOP)
N390 G28 G91 Z0 (HOME Z AXIS)
N400 G28 X0 Y0 (HOME X AND Y)
N410 M30 (PROGRAM END AND REWIND)
%

What does this code do?

  • N360 rapids the tool up to 50mm, safely clear of the part and any fixtures.
  • M09 and M05 turn off coolant and stop the spindle, just like at the end of Tool 1.
  • G28 G91 Z0 homes the Z axis first. G91 switches to incremental mode so Z0 means zero incremental movement before heading to the machine reference point. Z is always homed before X and Y to prevent the tool from dragging through the part.
  • G28 X0 Y0 homes the X and Y axes after Z is safely clear.
  • M30 ends the program and rewinds it back to the beginning, ready for the next run.
  • The closing % is the tape end marker, matching the % at the very top of the file.
  • Save mounting_plate.nc.
  • Switch back to Mastercam. Load the updated .nc file and run Backplot.
  • You should see the profile toolpath from Tool 1 (the contour with arcs) and four drill point markers from Tool 2. Rapid moves and feed moves should display differently in the Backplot view.

Backplot not showing drill points?

  • Make sure the file ends with % on its own line. The controller uses % as tape start and tape end markers.
  • Check that M30 appears before the closing %. Without M30, the program has no defined endpoint.
  • Confirm G80 appears at N350 after the last hole. If it is missing, the controller may interpret subsequent moves as drill positions.
  • Help me troubleshoot why my drill points are not showing in Mastercam Backplot.

✔️ Awesome, I've got everything!

Great. Double check you have saved mounting_plate.nc and your Backplot shows both the profile and drill points.

ⓧ I'd like to double check the full code

Here is the complete mounting_plate.nc file at this point. Compare it with yours line by line.

%
O1001 (MOUNTING PLATE)
(MATERIAL: ALUMINUM 6061)
(STOCK: 110 X 70 X 12MM)
(FINISHED PART: 100 X 60MM, 10MM THICK)
(ORIGIN: BOTTOM-LEFT CORNER, TOP OF PART)
(T01 - 10MM FLAT END MILL)
(T02 - 8.5MM DRILL)

(--- TOOL 1: PROFILE MILLING ---)
N10 G21 G17 G40 G49 G80 G90 (SAFETY LINE)
N20 G54 (SELECT WORK OFFSET 1)
N30 T01 M06 (LOAD TOOL 1)
N40 S3000 M03 (SPINDLE ON CW AT 3000 RPM)
N50 G43 H01 Z50.0 (TOOL LENGTH COMP, RAPID TO SAFE Z)
N60 M08 (COOLANT ON)

(APPROACH FROM BELOW THE PART)
N70 G00 X50.0 Y-15.0 (RAPID TO START POSITION)
N80 G00 Z2.0 (RAPID DOWN TO CLEARANCE)
N90 G01 Z-10.0 F100 (PLUNGE TO FULL DEPTH)

(CUTTER COMP ON - LEAD IN TO PROFILE)
N100 G01 G41 D01 Y0.0 F300 (COMP LEFT ON, LEAD IN TO BOTTOM EDGE)

(PROFILE CUT CCW FROM MID-BOTTOM EDGE)
N110 G01 X95.0 (BOTTOM EDGE TO RIGHT CORNER)
N120 G03 X100.0 Y5.0 R5.0 (BOTTOM-RIGHT CORNER ARC)
N130 G01 Y55.0 (RIGHT EDGE)
N140 G03 X95.0 Y60.0 R5.0 (TOP-RIGHT CORNER ARC)
N150 G01 X5.0 (TOP EDGE)
N160 G03 X0.0 Y55.0 R5.0 (TOP-LEFT CORNER ARC)
N170 G01 Y5.0 (LEFT EDGE)
N180 G03 X5.0 Y0.0 R5.0 (BOTTOM-LEFT CORNER ARC)
N190 G01 X50.0 (BOTTOM EDGE BACK TO START POINT)

(CUTTER COMP OFF - LEAD OUT)
N200 G01 G40 Y-15.0 (COMP OFF, LEAD OUT FROM PART)
N210 G00 Z50.0 (RAPID RETRACT TO SAFE Z)
N220 M09 (COOLANT OFF)
N230 M05 (SPINDLE STOP)

(--- TOOL 2: DRILL BOLT HOLES ---)
N240 T02 M06 (LOAD TOOL 2)
N250 G21 G17 G40 G49 G80 G90 (SAFETY LINE)
N260 G54 (SELECT WORK OFFSET 1)
N270 S1200 M03 (SPINDLE ON CW AT 1200 RPM)
N280 G43 H02 Z50.0 (TOOL LENGTH COMP, RAPID TO SAFE Z)
N290 M08 (COOLANT ON)

(PECK DRILL 4 BOLT HOLES)
N300 G00 X15.0 Y15.0 (RAPID TO FIRST HOLE)
N310 G99 G83 Z-12.0 R2.0 Q3.0 F80 (PECK DRILL CYCLE)
N320 X85.0 Y15.0 (SECOND HOLE)
N330 X85.0 Y45.0 (THIRD HOLE)
N340 G98 X15.0 Y45.0 (FOURTH HOLE, RETRACT TO INITIAL Z)
N350 G80 (CANCEL CANNED CYCLE)

(PROGRAM END)
N360 G00 Z50.0 (RETRACT TO SAFE Z)
N370 M09 (COOLANT OFF)
N380 M05 (SPINDLE STOP)
N390 G28 G91 Z0 (HOME Z AXIS)
N400 G28 X0 Y0 (HOME X AND Y)
N410 M30 (PROGRAM END AND REWIND)
%

Your program is complete. Every line of code for both tools is written, and Backplot confirms the toolpaths look right. Next up, you will use Mastercam's Verify simulation to watch material actually being removed from the stock model.

Verify the Complete Program

Your mounting plate program is fully written. Both tool sections have been confirmed in Backplot, so you know the toolpath geometry is correct.

Now it is time to prove that the program actually produces the right part. Mastercam's Verify tool goes beyond Backplot by rendering a solid stock model and removing material as the simulation plays. This is your last line of defense before code touches a controller.

In this step, get ready to:
  • Run a Verify simulation with stock dimensions matching the raw aluminum.
  • Check for common G-code errors in the program.
  • Desk check the complete program structure line by line.
Run the Verify simulation in Mastercam

Backplot shows you where the tool travels. Verify goes further by starting with a solid block of stock and carving material away as the simulation runs. If the tool cuts where it should not, you see it immediately.

  • Switch back to Mastercam 2025 on your workstation.
  • Make sure your mounting_plate.nc file is loaded. If you still have it open from the previous step, you are ready to go. Otherwise, reload it using File > Open or the NC editor.
  • Open the stock setup in your machine group properties (in the Toolpath Manager pane, expand the properties and select Stock Setup).
  • Set the stock to a rectangular block with dimensions 110 x 70 x 12 mm (X, Y, Z). These match the raw aluminum stock from the part drawing.
  • Click OK to confirm the stock dimensions.
  • Run Verify instead of Backplot. The Verify button is on the same toolbar where you ran Backplot earlier.
  • Press Play and watch the simulation. Tool 1 (the 10mm end mill) should cut the profile around the plate, and Tool 2 (the 8.5mm drill) should drill four holes.

What is the difference between Backplot and Verify?

Backplot draws the toolpath as lines in space. It shows direction and feed type (rapid vs. cutting) but has no concept of the stock material.

Verify renders a 3D solid block and removes material in real time as the simulation runs. It checks for stock interference and shows you the finished part shape. If the tool gouges the part or collides with the stock, you see it.

When the simulation finishes, rotate the model to inspect all sides. The finished shape should be a 100 x 60mm plate with smooth R5 rounded corners and four through holes at positions (15, 15), (85, 15), (85, 45), and (15, 45).

Part shape looks wrong in Verify?

Double-check that your stock dimensions are set to 110 x 70 x 12mm. If the stock is too small, the tool will appear to cut into empty space. If it is too large, extra material will remain on the edges.

Also confirm that the stock origin is aligned with your program's G54 origin (bottom-left corner, top of part surface). A misaligned origin shifts every cut.

Help me troubleshoot my Verify simulation. The part shape does not match the expected 100x60mm mounting plate with R5 corners and four bolt holes.

Check for common G-code errors

A clean Verify simulation is a good sign, but certain G-code mistakes can hide in the program without showing obvious visual problems. These errors surface later when the code runs on a real controller.

  • Return to your text editor and open mounting_plate.nc.
  • Confirm that G80 appears on line N350 after the last drilling hole. A missing G80 would leave the canned cycle active. Any subsequent move could trigger an unexpected drilling operation.
  • Confirm that all four corner arcs use G03 (counterclockwise). Using G02 instead would cut the arc in the wrong direction and gouge into the part.
  • Check that the retract heights are Z50.0 on lines N210 and N360. A retract height that is too low risks dragging the tool across the part or fixture during rapids.
  • Confirm that tool length compensation matches the tool numbers: G43 H01 on line N50 for Tool 1, and G43 H02 on line N280 for Tool 2. A mismatched H-code applies the wrong tool length offset, which shifts every Z move.
  • Look at the Verify display one more time. Check for any collision or gouge warnings. If everything is clean, your program is verified.

Why is a missing G80 so dangerous?

G83 is a modal code. Once activated, it stays active until explicitly cancelled with G80. If you forget G80, any G00 or G01 move after the drilling section could be interpreted as another drilling cycle by the controller. The tool would plunge into the part at an unintended location.

Desk check the complete program

Experienced CNC programmers read through every line of a program before loading it onto a controller. This "desk check" catches logic errors that simulations sometimes miss, like a wrong feed rate or a missing coolant command.

  • Scroll to the top of mounting_plate.nc in your text editor.
  • Read through the program from % (tape start) to % (tape end). For each line, state what the machine does.
  • Confirm the program follows this structure in order:
  • Header comments (program number, material, stock size, tools).
  • Safety block (N10: G21 G17 G40 G49 G80 G90).
  • Tool 1 section: work offset, tool change, spindle on, tool length comp, coolant, approach, cutter comp on (G41 D01), full CCW profile with four G03 arcs, cutter comp off (G40), retract, coolant off, spindle stop.
  • Tool 2 section: tool change, safety line, work offset, spindle on, tool length comp, coolant, rapid to first hole, G83 peck drill cycle with G99/G98 retract control, G80 cancel, retract, coolant off, spindle stop.
  • Program end: retract to safe Z, G28 home Z first then X/Y, M30 program end and rewind.

Why does Z home before X and Y?

Line N390 homes the Z axis first with G28 G91 Z0. This lifts the tool to a safe height before line N400 moves X and Y with G28 X0 Y0. If X and Y moved first while the tool was still at cutting depth, the tool would drag through the part or fixture.

Read each line aloud

Say what the machine does for every line: "N70, rapid to X50 Y-15, that is the start position below the part." Reading aloud forces you to process each command instead of skimming. This is how experienced programmers catch subtle mistakes before code reaches the shop floor.

✔️ Awesome, I've got everything!

Great. Your program is verified and desk-checked. Make sure mounting_plate.nc is saved.

ⓧ I'd like to double check the full code

Here is the complete mounting_plate.nc program. Compare your file against this line by line.

%
O1001 (MOUNTING PLATE)
(MATERIAL: ALUMINUM 6061)
(STOCK: 110 X 70 X 12MM)
(FINISHED PART: 100 X 60MM, 10MM THICK)
(ORIGIN: BOTTOM-LEFT CORNER, TOP OF PART)
(T01 - 10MM FLAT END MILL)
(T02 - 8.5MM DRILL)

(--- TOOL 1: PROFILE MILLING ---)
N10 G21 G17 G40 G49 G80 G90 (SAFETY LINE)
N20 G54 (SELECT WORK OFFSET 1)
N30 T01 M06 (LOAD TOOL 1)
N40 S3000 M03 (SPINDLE ON CW AT 3000 RPM)
N50 G43 H01 Z50.0 (TOOL LENGTH COMP, RAPID TO SAFE Z)
N60 M08 (COOLANT ON)

(APPROACH FROM BELOW THE PART)
N70 G00 X50.0 Y-15.0 (RAPID TO START POSITION)
N80 G00 Z2.0 (RAPID DOWN TO CLEARANCE)
N90 G01 Z-10.0 F100 (PLUNGE TO FULL DEPTH)

(CUTTER COMP ON - LEAD IN TO PROFILE)
N100 G01 G41 D01 Y0.0 F300 (COMP LEFT ON, LEAD IN TO BOTTOM EDGE)

(PROFILE CUT CCW FROM MID-BOTTOM EDGE)
N110 G01 X95.0 (BOTTOM EDGE TO RIGHT CORNER)
N120 G03 X100.0 Y5.0 R5.0 (BOTTOM-RIGHT CORNER ARC)
N130 G01 Y55.0 (RIGHT EDGE)
N140 G03 X95.0 Y60.0 R5.0 (TOP-RIGHT CORNER ARC)
N150 G01 X5.0 (TOP EDGE)
N160 G03 X0.0 Y55.0 R5.0 (TOP-LEFT CORNER ARC)
N170 G01 Y5.0 (LEFT EDGE)
N180 G03 X5.0 Y0.0 R5.0 (BOTTOM-LEFT CORNER ARC)
N190 G01 X50.0 (BOTTOM EDGE BACK TO START POINT)

(CUTTER COMP OFF - LEAD OUT)
N200 G01 G40 Y-15.0 (COMP OFF, LEAD OUT FROM PART)
N210 G00 Z50.0 (RAPID RETRACT TO SAFE Z)
N220 M09 (COOLANT OFF)
N230 M05 (SPINDLE STOP)

(--- TOOL 2: DRILL BOLT HOLES ---)
N240 T02 M06 (LOAD TOOL 2)
N250 G21 G17 G40 G49 G80 G90 (SAFETY LINE)
N260 G54 (SELECT WORK OFFSET 1)
N270 S1200 M03 (SPINDLE ON CW AT 1200 RPM)
N280 G43 H02 Z50.0 (TOOL LENGTH COMP, RAPID TO SAFE Z)
N290 M08 (COOLANT ON)

(PECK DRILL 4 BOLT HOLES)
N300 G00 X15.0 Y15.0 (RAPID TO FIRST HOLE)
N310 G99 G83 Z-12.0 R2.0 Q3.0 F80 (PECK DRILL CYCLE)
N320 X85.0 Y15.0 (SECOND HOLE)
N330 X85.0 Y45.0 (THIRD HOLE)
N340 G98 X15.0 Y45.0 (FOURTH HOLE, RETRACT TO INITIAL Z)
N350 G80 (CANCEL CANNED CYCLE)

(PROGRAM END)
N360 G00 Z50.0 (RETRACT TO SAFE Z)
N370 M09 (COOLANT OFF)
N380 M05 (SPINDLE STOP)
N390 G28 G91 Z0 (HOME Z AXIS)
N400 G28 X0 Y0 (HOME X AND Y)
N410 M30 (PROGRAM END AND REWIND)
%

Secret mission

Read and Optimize Mastercam-Posted G-Code

Mastercam generates G-code automatically when you post-process a toolpath. But that generated code often looks very different from what an experienced programmer writes by hand. In this mission, you will post-process the same mounting plate profile in Mastercam, compare the output to your hand-written program, and edit it for clarity and efficiency.

Clean Up Your Resources

Clean Up Your Resources

This project runs entirely locally using your existing Mastercam 2025 license and a text editor. There are no cloud resources or ongoing costs.

Resources you used:

  • mounting_plate.nc (your hand-written G-code program).
  • Mastercam Mill file (used for Backplot and Verify simulation).
  • Mastercam-posted .nc file (generated during the Secret Mission).

Keep everything running

No action needed. Choose this option if you plan to keep editing your G-code or experimenting with Mastercam simulation.

  • Your .nc files and Mastercam Mill file stay on your local machine and take up minimal disk space.
  • Reopen them anytime in your text editor or Mastercam to continue practicing.

Pause - I'll come back to this later

Close your applications to free up memory. Your files remain on disk for when you return.

  • Save any unsaved work in Mastercam and your text editor.
  • Close Mastercam 2025.
  • Close your text editor (TextEdit on macOS or Notepad on Windows).

Your .nc files will be right where you left them when you come back.

Delete - I don't want to use this again

Remove all project files from your machine and start fresh.

  • Open Finder (macOS) or File Explorer (Windows) and navigate to the folder where you saved mounting_plate.nc.
  • Delete mounting_plate.nc.
  • Delete the Mastercam-posted .nc file if you generated one during the Secret Mission.
  • In Mastercam, close your Mill file without saving. If you already saved it as a .mcam file, delete it from the same folder.
  • Close Mastercam 2025 and your text editor.

Nice Work!

Nice Work!

Nice work! You just hand-wrote a complete Fanuc G-code milling program for an aluminum mounting plate and verified it in Mastercam before it ever touches a machine.

You've learned how to:

  • Written a complete Fanuc G-code program from scratch with proper program structure, safety blocks, header comments, and a clean ending sequence that follows real shop-floor conventions.
  • Profiled a part outline using linear interpolation (G00/G01), circular arcs (G03), and cutter radius compensation (G41/G40) so the program describes part geometry instead of tool center paths.
  • Drilled four bolt holes using a G83 peck drilling canned cycle with G98/G99 retract control, and verified the complete two-tool program using Mastercam's Backplot and Verify simulation before it touches a machine.
  • Secret Mission: Read Mastercam-posted G-code, compared it with your hand-written program, and edited it for clarity and efficiency.

Ready to quiz yourself?