3dSynth

Tutorials / Custom G-code

Open in the interactive tutorials reader

---
title: Tutorial 46 CNC vs FDM G-code Differences
description: One language, two dialects.
---

Tutorial 46 CNC vs FDM G-code Differences

1. Lesson Header

2. Concept Introduction

Subtractive vs Additive.
FDM adds material (E).
CNC removes material (Spindle S, Tool T).
The G-code is 90% the same (G1 X10), but the Machine Control Codes (M-codes) differ significantly.

3. Machine State Explanation

CNC Specifics.
- S[RPM]: Spindle Speed (e.g., S10000).
- M3: Spindle On (Clockwise).
- M4: Spindle On (Counter-Clockwise).
- M5: Spindle Stop.
- M6 T[Tool]: Tool Change.
- M7/M8: Coolant Mist/Flood.
- M9: Coolant Off.
- G54-G59: Work Coordinate Systems (WCS).

4. Command Breakdown

5. Minimal Working Example

The Milling Op.
1. Start Spindle.
2. Turn on Coolant.
3. Move to Safe Z.
4. Plunge into material.
5. Cut a square.
6. Retract.
7. Stop.

G21 G90 G54
M6 T1 ; Load Tool 1
S12000 M3 ; Spindle 12k CW
M8 ; Flood Coolant
G0 X0 Y0 Z10 ; Safe Height
G1 Z-2 F200 ; Plunge (Slow!)
G1 X10 F500 ; Cut
G1 Y10
G1 X0
G1 Y0
G0 Z10 ; Retract
M5 M9 ; Stop Spindle/Coolant

6. Visual Representation

Interactive preview is available in the interactive reader.

7. Build Exercise

Task: Write a Python script to generate a "Pocketing" toolpath.
Area: 20x20mm Square.
Depth: 5mm.
Tool Diameter: 3mm.
Stepdown: 1mm per pass.
Stepover: 1.5mm (50%).

Algorithm:
1. Loop Z from 0 to -5 by -1.
2. At each Z, spiral out from center to fill the 20x20 square.
3. Ensure Total_Width = 20 - Tool_Diameter.

8. Deep Insight Section

Feeds and Speeds.
In FDM, speed is limited by flow rate.
In CNC, speed is critical physics.
Too slow = Rubbing (Heat, Tool Dull).
Too fast = Chipping (Tool Break).
Chip_Load = Feed / (RPM * Flutes).
You must calculate RPM and Feedrate based on the material (Aluminum vs Wood).

9. Common Failure Modes

  1. Crash: Rapid move (G0) through material. Always retract Z before rapid XY moves!
  2. Workholding: Hitting the clamp with the tool. Define "Keep-Out Zones".

10. Real-World Application

Hybrid Manufacturing.
Machines that print metal (DED) and then mill it smooth in the same setup.
Requires switching between "Additive Mode" (Laser/Powder on) and "Subtractive Mode" (Spindle on).

11. Final Clean Version

The Pocket Generator:

tool_d = 3.0
pocket_w = 20.0
stepdown = 1.0
total_depth = 5.0

safe_z = 5.0
feed_z = 200
feed_xy = 800

width_cut = pocket_w - tool_d
z = 0

with open("pocket.nc", "w") as f:
    f.write("G21 G90 G54\n")
    f.write("S10000 M3\n")
    f.write(f"G0 Z{safe_z}\n")
    f.write("G0 X0 Y0\n") # Center
    
    while z > -total_depth:
        z -= stepdown
        if z < -total_depth: z = -total_depth
        
        f.write(f"G1 Z{z:.3f} F{feed_z}\n")
        
        # Spiral Out (Simplified: Concentric squares)
        current_w = 0
        while current_w < width_cut:
            current_w += tool_d * 0.5 # 50% Stepover
            if current_w > width_cut: current_w = width_cut
            
            half = current_w / 2
            f.write(f"G1 X{-half} Y{-half} F{feed_xy}\n")
            f.write(f"G1 X{half} Y{-half}\n")
            f.write(f"G1 X{half} Y{half}\n")
            f.write(f"G1 X{-half} Y{half}\n")
            f.write(f"G1 X{-half} Y{-half}\n") # Close loop
            
    f.write(f"G0 Z{safe_z}\n")
    f.write("M5 M30\n") # Stop and Rewind

12. Stretch Challenge

Challenge: Implement Lead-In / Lead-Out.
Plunging straight down (G1 Z-2) is bad for endmills (unless center-cutting).
Better: Ramp In (Zig-Zag down) or Helix In (Spiral down).
Calculate a helical entry path to reach depth.