Files
geMoldInsight/docs/GCODE_GENERATION.md
T
2026-02-17 01:35:09 +08:00

12 KiB

G代码生成方案设计文档

一、CAM模块架构

1.1 整体架构

┌─────────────────────────────────────────────────────────┐
│                    G代码生成系统                          │
├─────────────────────────────────────────────────────────┤
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────────┐  │
│  │  路径规划器   │  │  刀具选择器   │  │  后处理器       │  │
│  │ • 粗加工     │  │ • 刀具库     │  │ • Fanuc格式     │  │
│  │ • 精加工     │  │ • 切削参数    │  │ • Siemens格式   │  │
│  │ • 清角加工    │  │ • 寿命管理    │  │ • Haas格式      │  │
│  └─────────────┘  └─────────────┘  └─────────────────┘  │
├─────────────────────────────────────────────────────────┤
│                   加工策略层                             │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────────┐  │
│  │  型腔加工     │  │  电极加工     │  │  钻孔加工       │  │
│  │ • 等高线     │  │ • 铜电极      │  │ • 冷却孔        │  │
│  │ • 平行铣     │  │ • 石墨电极    │  │ • 顶针孔        │  │
│  │ • 螺旋铣     │  │ • 精密加工    │  │ • 螺丝孔        │  │
│  └─────────────┘  └─────────────┘  └─────────────────┘  │
├─────────────────────────────────────────────────────────┤
│                   几何处理层                             │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────────┐  │
│  │  STL处理器    │  │  网格优化器   │  │  碰撞检测       │  │
│  │ • STL导入    │  │ • 网格修复    │  │ • 刀具干涉      │  │
│  │ • 三角化     │  │ • 法线校正    │  │ • 机床限制      │  │
│  │ • 精度控制    │  │ • 简化优化    │  │ • 安全区域      │  │
│  └─────────────┘  └─────────────┘  └─────────────────┘  │
└─────────────────────────────────────────────────────────┘

二、核心功能模块

2.1 路径规划器 (PathPlanner)

粗加工策略

class RoughingStrategy:
    def adaptive_clearing(self, stock, tool):
        """自适应清根粗加工"""
        
    def pocket_milling(self, cavity, tool):
        """型腔铣削粗加工"""
        
    def rest_milling(self, previous_tool, current_tool):
        """残留区域加工"""

精加工策略

class FinishingStrategy:
    def contour_parallel(self, surface, stepover):
        """等高线精加工"""
        
    def spiral_milling(self, cavity, stepover):
        """螺旋精加工"""
        
    def pencil_milling(self, corners, tool):
        """清角精加工"""

2.2 刀具选择器 (ToolSelector)

刀具库配置

# config/tool_library.py

TOOL_LIBRARY = {
    "roughing_endmill_16mm": {
        "type": "endmill",
        "diameter": 16.0,
        "flute_length": 50.0,
        "shank_diameter": 16.0,
        "cutting_edges": 4,
        "material": "carbide",
        "speeds_feeds": {
            "cutting_speed": 120,    # m/min
            "feed_per_tooth": 0.15,  # mm/tooth
            "axial_depth": 8.0,      # mm
            "radial_depth": 12.0     # mm
        }
    },
    "finishing_ballnose_6mm": {
        "type": "ballnose",
        "diameter": 6.0,
        "flute_length": 25.0,
        "cutting_edges": 2,
        "material": "carbide",
        "speeds_feeds": {
            "cutting_speed": 150,
            "feed_per_tooth": 0.08,
            "axial_depth": 0.3,
            "radial_depth": 0.5
        }
    }
}

自动刀具选择算法

class AutoToolSelector:
    def select_roughing_tool(self, cavity_volume, material):
        """根据型腔体积选择粗加工刀具"""
        
    def select_finishing_tool(self, surface_quality, corner_radius):
        """根据表面质量要求选择精加工刀具"""
        
    def optimize_tool_sequence(self, operations):
        """优化刀具使用顺序"""

2.3 后处理器 (PostProcessor)

支持的数控系统

# config/post_processors.py

SUPPORTED_CONTROLLERS = {
    "fanuc": {
        "g_code_dialect": "fanuc",
        "circular_interpolation": "G02/G03",
        "tool_change": "M06",
        "coolant": "M08/M09"
    },
    "siemens": {
        "g_code_dialect": "sinumerik",
        "circular_interpolation": "G02/G03",
        "tool_change": "T... M06",
        "coolant": "M08/M09"
    },
    "haas": {
        "g_code_dialect": "haas",
        "circular_interpolation": "G02/G03",
        "tool_change": "M06",
        "coolant": "M08/M09"
    },
    "heidenhain": {
        "g_code_dialect": "heidenhain",
        "circular_interpolation": "DR+",
        "tool_change": "TOOL CALL",
        "coolant": "M08/M09"
    }
}

三、加工工艺流程

3.1 标准加工序列

型腔加工流程

PROCESS_SEQUENCE = [
    # 阶段1: 粗加工
    {
        "operation": "roughing",
        "tool": "roughing_endmill_16mm",
        "strategy": "adaptive_clearing",
        "stock_allowance": 0.5
    },
    
    # 阶段2: 半精加工
    {
        "operation": "semi_finishing", 
        "tool": "endmill_10mm",
        "strategy": "contour_parallel",
        "stock_allowance": 0.1
    },
    
    # 阶段3: 精加工
    {
        "operation": "finishing",
        "tool": "ballnose_6mm", 
        "strategy": "spiral_milling",
        "stepover": 0.3
    },
    
    # 阶段4: 清角加工
    {
        "operation": "corner_cleaning",
        "tool": "ballnose_3mm",
        "strategy": "pencil_milling"
    }
]

3.2 电极加工流程

铜电极加工

ELECTRODE_PROCESS = [
    # 粗加工
    {"tool": "endmill_8mm", "strategy": "roughing"},
    
    # 精加工
    {"tool": "ballnose_3mm", "strategy": "finishing"},
    
    # 放电区域加工
    {"tool": "ballnose_1mm", "strategy": "detail_milling"}
]

四、G代码格式规范

4.1 基本G代码结构

% 程序开始
O1000 (模具型腔加工程序)
G17 G40 G49 G80 G90 (安全初始化)
G54 (工件坐标系)

(=== 粗加工 ===)
T01 M06 (换刀: 16mm端铣刀)
G43 H01 Z100.0 (刀具长度补偿)
S1800 M03 (主轴启动)
G00 X0 Y0 Z10.0 (快速定位)
M08 (冷却液开)

G01 Z-5.0 F500 (下刀)
G01 X50.0 Y30.0 F800 (直线插补)
G02 X60.0 Y40.0 I10.0 J0.0 F600 (圆弧插补)

(=== 精加工 ===)  
T02 M06 (换刀: 6mm球头刀)
G43 H02 Z100.0
S3000 M03

(程序结束)
M09 (冷却液关)
M05 (主轴停)
G28 G91 Z0 (Z轴回零)
G28 G91 X0 Y0 (XY轴回零)
M30 (程序结束)
% 程序结束

4.2 加工参数计算

切削参数计算

class CuttingParameters:
    def calculate_spindle_speed(self, tool_diameter, cutting_speed):
        """计算主轴转速"""
        # N = (1000 * Vc) / (π * D)
        return (1000 * cutting_speed) / (math.pi * tool_diameter)
    
    def calculate_feed_rate(self, spindle_speed, feed_per_tooth, teeth):
        """计算进给速率"""
        # F = N * fz * z
        return spindle_speed * feed_per_tooth * teeth
    
    def calculate_machining_time(self, toolpath_length, feed_rate):
        """估算加工时间"""
        return toolpath_length / feed_rate * 60  # 分钟

五、碰撞检测与安全

5.1 碰撞检测算法

class CollisionDetector:
    def check_tool_holder_collision(self, toolpath, stock, tool_geometry):
        """检测刀柄碰撞"""
        
    def check_rapid_moves(self, rapid_paths, obstacles):
        """检测快速移动碰撞"""
        
    def verify_machine_limits(self, coordinates, machine_limits):
        """验证机床行程限制"""

5.2 安全策略

SAFETY_STRATEGIES = {
    "retract_height": 10.0,           # 抬刀高度
    "safe_clearance": 5.0,            # 安全距离
    "max_feed_rate": 5000.0,          # 最大进给速率
    "max_spindle_speed": 24000.0,     # 最大主轴转速
    "emergency_stop_conditions": [
        "tool_breakage",
        "collision_detected", 
        "over_temperature"
    ]
}

六、仿真与验证

6.1 刀具路径仿真

class ToolpathSimulator:
    def visualize_toolpath(self, gcode_file):
        """可视化刀具路径"""
        
    def simulate_material_removal(self, toolpath, stock):
        """模拟材料去除"""
        
    def detect_gouging(self, toolpath, design_surface):
        """检测过切"""

6.2 加工质量分析

class QualityAnalyzer:
    def analyze_surface_roughness(self, toolpath, stepover):
        """分析表面粗糙度"""
        
    def check_dimensional_accuracy(self, simulated_part, design):
        """检查尺寸精度"""
        
    def estimate_tool_wear(self, cutting_length, material):
        """估算刀具磨损"""

七、文件输出格式

7.1 G代码文件结构

# src/core/gcode_writer.py

class GCodeWriter:
    def write_program_header(self, program_number, program_name):
        """写入程序头"""
        
    def write_tool_change(self, tool_number, tool_description):
        """写入换刀指令"""
        
    def write_motion_commands(self, toolpath, feed_rate):
        """写入运动指令"""
        
    def write_program_footer(self):
        """写入程序尾"""

7.2 工艺文档输出

class ProcessDocumentation:
    def generate_setup_sheet(self, operations, tools, materials):
        """生成加工作业指导书"""
        
    def generate_tool_list(self, tools_used):
        """生成刀具清单"""
        
    def generate_inspection_report(self, quality_metrics):
        """生成检测报告"""

八、实施计划

8.1 第一阶段:基础路径生成(3-4周)

  1. 实现STL几何导入和预处理
  2. 开发基础粗精加工算法
  3. 创建Fanuc格式后处理器

8.2 第二阶段:高级功能(4-5周)

  1. 实现碰撞检测和安全策略
  2. 开发刀具路径优化算法
  3. 添加多轴加工支持

8.3 第三阶段:完善优化(2-3周)

  1. 集成加工仿真功能
  2. 优化性能和用户体验
  3. 添加更多数控系统支持

九、技术选型

9.1 几何处理库

  • FreeCAD Path Workbench - 开源CAM解决方案
  • PyCAM - 纯Python CAM库
  • OpenCASCADE - 几何内核,用于复杂运算

9.2 可视化库

  • PyVista - 3D数据可视化
  • Matplotlib - 2D图表和路径显示
  • Plotly - 交互式3D可视化

9.3 数值计算

  • NumPy - 数值计算基础
  • SciPy - 科学计算和优化
  • OpenGL - 实时3D渲染

十、质量保证

10.1 测试策略

  • 单元测试 - 每个算法模块单独测试
  • 集成测试 - 完整加工流程测试
  • 回归测试 - 确保新功能不破坏现有功能

10.2 验证标准

  • G代码语法正确性 - 符合数控系统规范
  • 加工安全性 - 无碰撞风险
  • 加工质量 - 满足精度要求
  • 加工效率 - 优化加工时间

文档版本:v1.0
创建日期:2026-02-17