Files
ViewDesignEngine/include/vde/core/cam_strategies.h
T

261 lines
10 KiB
C++
Raw Normal View History

#pragma once
/**
* @file cam_strategies.h
* @brief 完整 CAM 加工策略 — 粗加工、精加工、钻孔、刀具库、后处理、仿真
*
* 在 cam_toolpath.h 的基础刀路(contour/pocket)之上提供:
* - 层切粗加工 (roughing_toolpath)
* - 精加工平行/环绕 (finishing_toolpath)
* - 钻孔循环 G81/G83/G84 (drilling_toolpath)
* - 刀具结构与刀具库 (Tool, ToolLibrary)
* - 后处理器框架 (PostProcessor → FanucPost, SiemensPost, HeidenhainPost)
* - 加工仿真数据生成 (material_removal_simulation)
*
* @ingroup core
*/
#include "vde/core/point.h"
#include "vde/core/aabb.h"
#include "vde/core/cam_toolpath.h"
#include "vde/mesh/halfedge_mesh.h"
#include <vector>
#include <string>
#include <memory>
#include <functional>
#include <optional>
namespace vde::brep {
class BrepModel;
} // namespace vde::brep
namespace vde::core {
using core::Point3D;
using core::Vector3D;
// ═══════════════════════════════════════════════════════════════════════════
// 刀具定义
// ═══════════════════════════════════════════════════════════════════════════
/// 刀具类型枚举
enum class ToolType {
ENDMILL, ///< 平底铣刀
BALLNOSE, ///< 球头铣刀
DRILL, ///< 钻头
FACEMILL, ///< 面铣刀
TAP ///< 丝锥
};
/// 刀具参数结构体
struct Tool {
int id = 0; ///< 刀具编号
std::string name; ///< 刀具名称
ToolType type = ToolType::ENDMILL; ///< 刀具类型
double diameter = 10.0; ///< 刀具直径 (mm)
int flutes = 2; ///< 刃数
double length = 50.0; ///< 有效刃长 (mm)
double overall_length = 75.0; ///< 总长 (mm)
double shank_diameter = 10.0; ///< 柄径 (mm)
/// 计算推荐转速(基于切削速度 m/min)
[[nodiscard]] double spindle_rpm(double cutting_speed_m_per_min) const;
/// 计算推荐进给速度(mm/min,基于每齿进给量 mm/tooth
[[nodiscard]] double feed_rate_mm_per_min(double feed_per_tooth) const;
};
/// 刀具库 — 管理多个刀具
class ToolLibrary {
public:
/// 添加刀具,返回内部索引
int add_tool(const Tool& tool);
/// 按编号查找刀具
[[nodiscard]] std::optional<Tool> find_tool(int id) const;
/// 按名称查找刀具
[[nodiscard]] std::optional<Tool> find_tool(const std::string& name) const;
/// 列出所有刀具
[[nodiscard]] const std::vector<Tool>& list_tools() const { return tools_; }
/// 刀具数量
[[nodiscard]] size_t size() const { return tools_.size(); }
private:
std::vector<Tool> tools_;
};
// ═══════════════════════════════════════════════════════════════════════════
// 加工参数
// ═══════════════════════════════════════════════════════════════════════════
/// 粗加工参数
struct RoughingParams {
double step_down = 1.0; ///< 每层切深 (mm)
double step_over = 2.0; ///< 横向步距 (mm)
double stock_to_leave = 0.5; ///< 留量 (mm)
double feed_rate = 800.0; ///< 进给速度 (mm/min)
double safe_z = 10.0; ///< 安全高度 (mm)
};
/// 精加工参数
struct FinishingParams {
double step_over = 0.5; ///< 行距 (mm)
double feed_rate = 500.0; ///< 进给速度 (mm/min)
double safe_z = 10.0; ///< 安全高度 (mm)
double depth_of_cut = 0.0; ///< 精加工深度 (Z 坐标;0=最终 Z,负=往下)
};
/// 精加工策略类型
enum class FinishingStrategy {
PARALLEL, ///< 平行刀路 — 等距平行线
SPIRAL ///< 环绕刀路 — 轮廓向内偏移
};
/// 钻孔循环类型
enum class DrillingCycle {
G81, ///< 简单钻孔:G81 X.. Y.. Z.. R.. F..
G83, ///< 深孔啄钻:G83 X.. Y.. Z.. R.. Q.. F..
G84 ///< 攻丝: G84 X.. Y.. Z.. R.. F..
};
/// 钻孔点定义
struct DrillPoint {
Point3D position; ///< 孔位坐标
double depth = -10.0; ///< 钻深(Z 坐标)
double peck_depth = 2.0; ///< 啄钻每次深度 (G83 用)
};
// ═══════════════════════════════════════════════════════════════════════════
// 后处理器框架
// ═══════════════════════════════════════════════════════════════════════════
/// 抽象后处理器基类
class PostProcessor {
public:
virtual ~PostProcessor() = default;
/// 程序头部(初始化 G-code、换刀等)
[[nodiscard]] virtual std::string prologue(const Toolpath& tp) const = 0;
/// 程序尾部(退刀、程序结束)
[[nodiscard]] virtual std::string epilogue(const Toolpath& tp) const = 0;
/// 格式化一条 G-code 行
[[nodiscard]] virtual std::string format_gcode(const PathSegment& seg) const = 0;
/// 完整后处理:prologue + segments + epilogue
[[nodiscard]] std::string post_process(const Toolpath& tp) const;
};
/// Fanuc 控制器后处理器
class FanucPost : public PostProcessor {
public:
[[nodiscard]] std::string prologue(const Toolpath& tp) const override;
[[nodiscard]] std::string epilogue(const Toolpath& tp) const override;
[[nodiscard]] std::string format_gcode(const PathSegment& seg) const override;
};
/// Siemens (Sinumerik) 控制器后处理器
class SiemensPost : public PostProcessor {
public:
[[nodiscard]] std::string prologue(const Toolpath& tp) const override;
[[nodiscard]] std::string epilogue(const Toolpath& tp) const override;
[[nodiscard]] std::string format_gcode(const PathSegment& seg) const override;
};
/// Heidenhain 控制器后处理器
class HeidenhainPost : public PostProcessor {
public:
[[nodiscard]] std::string prologue(const Toolpath& tp) const override;
[[nodiscard]] std::string epilogue(const Toolpath& tp) const override;
[[nodiscard]] std::string format_gcode(const PathSegment& seg) const override;
};
// ═══════════════════════════════════════════════════════════════════════════
// 加工策略函数
// ═══════════════════════════════════════════════════════════════════════════
/// 层切粗加工刀路
///
/// 对 B-Rep 实体进行 Z 层切粗加工:
/// 1. 计算模型的 Z 范围
/// 2. 从安全高度开始,以 step_down 步距逐层往下切
/// 3. 每层将模型轮廓投影到当前 Z 高度,偏移 stock_to_leave 生成刀路
///
/// @param body B-Rep 实体模型
/// @param tool 使用的刀具
/// @param params 粗加工参数
/// @return 粗加工刀路
///
/// @ingroup core
[[nodiscard]] Toolpath roughing_toolpath(
const brep::BrepModel& body,
const Tool& tool,
const RoughingParams& params);
/// 精加工刀路
///
/// 对 B-Rep 实体生成精加工刀路,支持两种策略:
/// - PARALLEL:等距平行线覆盖整个加工面
/// - SPIRAL:从轮廓向内逐步偏移
///
/// @param body B-Rep 实体模型
/// @param tool 使用的刀具
/// @param params 精加工参数
/// @param strategy 加工策略(PARALLEL 或 SPIRAL
/// @return 精加工刀路
///
/// @ingroup core
[[nodiscard]] Toolpath finishing_toolpath(
const brep::BrepModel& body,
const Tool& tool,
const FinishingParams& params,
FinishingStrategy strategy = FinishingStrategy::PARALLEL);
/// 钻孔循环刀路
///
/// 对一组钻孔点生成指定钻孔循环的刀路。
/// - G81:简单钻孔
/// - G83:深孔啄钻(带 Q 参数)
/// - G84:攻丝
///
/// @param points 钻孔点列表
/// @param tool 使用的钻头/丝锥
/// @param cycle 钻孔循环类型
/// @param safe_z 安全高度 (mm)
/// @param feed_rate 进给速度 (mm/min)
/// @return 钻孔刀路
///
/// @ingroup core
[[nodiscard]] Toolpath drilling_toolpath(
const std::vector<DrillPoint>& points,
const Tool& tool,
DrillingCycle cycle = DrillingCycle::G81,
double safe_z = 10.0,
double feed_rate = 200.0);
// ═══════════════════════════════════════════════════════════════════════════
// 加工仿真
// ═══════════════════════════════════════════════════════════════════════════
/// 加工仿真结果
struct SimulationResult {
mesh::HalfedgeMesh remaining_mesh; ///< 加工后剩余材料网格
mesh::HalfedgeMesh removed_mesh; ///< 被切除材料网格
double volume_removed = 0.0; ///< 切除体积 (mm³)
double volume_remaining = 0.0; ///< 剩余体积 (mm³)
int steps = 0; ///< 仿真步数
};
/// 材料去除仿真
///
/// 给定毛坯 B-Rep 和刀路,模拟刀具沿刀路切割材料的过程,
/// 输出剩余材料网格、切除材料网格和体积统计。
///
/// @param body 毛坯 B-Rep 实体
/// @param toolpath 加工刀路
/// @param tool 使用的刀具(用于确定切削截面)
/// @return 仿真结果
///
/// @ingroup core
[[nodiscard]] SimulationResult material_removal_simulation(
const brep::BrepModel& body,
const Toolpath& toolpath,
const Tool& tool);
} // namespace vde::core