#include #include "vde_cam/gcode_import.h" #include "vde_cam/cam_toolpath.h" #include "vde_cam/cam_5axis.h" #include "vde/core/point.h" #include "vde/foundation/utf8_path.h" #include #include #include #include #include #include #ifndef VDE_TEST_TEMP_DIR #define VDE_TEST_TEMP_DIR "./" #endif using namespace vde::cam; using namespace vde::core; namespace { // Wave21/A55:UTF-8 路径回归辅助——判定字符串是否含非 ASCII 字节。 bool a55_has_non_ascii(const std::string& s) { for (const char c : s) { if (static_cast(c) >= 0x80) return true; } return false; } } // namespace // =========================================================================== // 测试1:直线插补 (G00 / G01) // =========================================================================== TEST(GcodeImportTest, LinearMoves) { const char* gcode = R"( G90 G21 G17 G00 X0 Y0 Z10 G01 X50 Y0 Z-2 F800 G01 X50 Y30 G01 X0 Y30 G01 X0 Y0 G00 Z10 )"; auto tp = import_gcode_from_string(gcode); EXPECT_EQ(tp.name, "Imported G-code"); EXPECT_GE(tp.segments.size(), 5u) << "Should have at least 5 moves"; } // =========================================================================== // 测试2:圆弧插补 (G02 顺时针 / G03 逆时针) // =========================================================================== TEST(GcodeImportTest, ArcMoves) { // G02: 顺时针圆弧 — 从 (10,0) 到 (0,10),圆心 (0,0),所以 I=-10, J=0 // G03: 逆时针圆弧 — 从 (0,10) 到 (10,0),圆心 (0,0),所以 I=0, J=-10 const char* gcode = R"( G90 G21 G17 G00 X10 Y0 G01 Z-1 F500 G02 X0 Y10 I-10 J0 F600 G03 X10 Y0 I0 J-10 G00 Z10 )"; auto tp = import_gcode_from_string(gcode); EXPECT_GE(tp.segments.size(), 4u); // 查找圆弧段 bool found_cw = false, found_ccw = false; for (auto& seg : tp.segments) { if (seg.type == PathSegmentType::ArcCW) found_cw = true; if (seg.type == PathSegmentType::ArcCCW) found_ccw = true; } EXPECT_TRUE(found_cw) << "Should contain G02 (ArcCW) segment"; EXPECT_TRUE(found_ccw) << "Should contain G03 (ArcCCW) segment"; } // =========================================================================== // 测试3:绝对坐标 vs 增量坐标 (G90 / G91) // =========================================================================== TEST(GcodeImportTest, AbsoluteVsIncremental) { // G90 绝对模式下:G01 X10 Y20 → 移动到 (10, 20) // G91 增量模式下:G01 X5 Y-3 → 从当前位置偏移 (5, -3) const char* gcode = R"( G90 G21 G17 G00 X10 Y10 Z5 G91 G01 X5 Y0 Z0 F500 G01 X0 Y5 G01 X-5 Y0 G01 X0 Y-5 G90 G00 Z10 )"; auto tp = import_gcode_from_string(gcode); EXPECT_GE(tp.segments.size(), 5u); // 增量模式下各段的终点应是相对位移累加 // 起始 (10, 10, 5), G91后: // G01 X5 Y0 → (15, 10, 5) // G01 X0 Y5 → (15, 15, 5) // G01 X-5 Y0 → (10, 15, 5) // G01 X0 Y-5 → (10, 10, 5) // 验证最后一个线性切削段的终点约为 (10, 10, 5) Point3D last_end(0, 0, 0); for (auto& seg : tp.segments) { if (seg.type == PathSegmentType::Linear) { last_end = seg.end; } } EXPECT_NEAR(last_end.x(), 10.0, 0.01); EXPECT_NEAR(last_end.y(), 10.0, 0.01); } // =========================================================================== // 测试4:钻孔循环 G81(简单钻孔) // =========================================================================== TEST(GcodeImportTest, DrillCycleG81) { // G81: 快速到 R 平面 → 进给到 Z → 快速退回 const char* gcode = R"( G90 G21 G17 G00 Z10 G81 X10 Y20 Z-5 R2 F300 X30 Y20 X30 Y40 X10 Y40 G80 G00 Z10 )"; auto tp = import_gcode_from_string(gcode); // 每个孔: Rapid to R, Linear to Z, Rapid to R = 3 segments // 4 个孔 + 初始 G00 Z10 = 13 segments EXPECT_GE(tp.segments.size(), 13u) << "Should have drill cycle segments for 4 holes"; // 验证有进给到底部的 Linear 段 int feed_to_depth = 0; for (auto& seg : tp.segments) { if (seg.type == PathSegmentType::Linear && seg.end.z() < 0) { feed_to_depth++; } } EXPECT_EQ(feed_to_depth, 4) << "4 holes → 4 feed-to-depth moves"; } // =========================================================================== // 测试5:啄钻循环 G83 // =========================================================================== TEST(GcodeImportTest, PeckDrillCycleG83) { const char* gcode = R"( G90 G21 G17 G00 Z10 G83 X25 Y25 Z-6 R3 Q2 F250 G80 G00 Z10 )"; auto tp = import_gcode_from_string(gcode); EXPECT_GT(tp.segments.size(), 5u) << "G83 peck drill should generate more segments than G81"; // 验证有多种深度的 Linear 段(啄钻逐级下降) int linear_count = 0; for (auto& seg : tp.segments) { if (seg.type == PathSegmentType::Linear) { linear_count++; } } // Z 从 R=3 到 Z=-6,Q=2:3→1→-1→-3→-5→-6 = 5 次下刀 EXPECT_GE(linear_count, 5) << "Peck drill should have at least 5 feed moves"; } // =========================================================================== // 测试6:刀具选择与换刀 (T + M06) // =========================================================================== TEST(GcodeImportTest, ToolChange) { const char* gcode = R"( G90 G21 G17 T01 M06 S3000 M03 G00 X0 Y0 Z10 G01 Z-1 F500 G01 X100 G00 Z10 T02 M06 S5000 M03 G00 X100 Y50 Z10 G01 Z-2 F400 G01 X0 G00 Z10 M05 M30 )"; auto tp = import_gcode_from_string(gcode); EXPECT_GE(tp.segments.size(), 6u); // 验证运动段都存在 bool has_rapid = false, has_linear = false; for (auto& seg : tp.segments) { if (seg.type == PathSegmentType::Rapid) has_rapid = true; if (seg.type == PathSegmentType::Linear) has_linear = true; } EXPECT_TRUE(has_rapid); EXPECT_TRUE(has_linear); } // =========================================================================== // 测试7:单位切换 (G20 英寸 / G21 毫米) // =========================================================================== TEST(GcodeImportTest, UnitConversion) { // G20 英制模式:坐标值 × 25.4 转换为毫米 const char* gcode = R"( G90 G20 G17 G00 X0 Y0 Z0.5 G01 X2.0 Y0 Z-0.1 F10 G01 X2.0 Y1.0 G01 X0 Y1.0 G01 X0 Y0 G00 Z0.5 G21 G00 X0 Y0 Z10 )"; auto tp = import_gcode_from_string(gcode); EXPECT_GE(tp.segments.size(), 6u); // 验证 G20 下的坐标已转换为毫米:2.0 inch → 50.8 mm bool found_converted = false; for (auto& seg : tp.segments) { // 查找 X 接近 50.8mm(即 2 英寸)的段 if (std::abs(seg.end.x() - 50.8) < 0.1) { found_converted = true; } } EXPECT_TRUE(found_converted) << "G20 inch values should be converted to mm (×25.4)"; } // =========================================================================== // 测试8:注释跳过(括号注释和分号注释) // =========================================================================== TEST(GcodeImportTest, CommentSkipping) { const char* gcode = R"( (This is a header comment) G90 G21 G17 ; set units to mm (Move to start position) G00 X10 Y20 Z5 (rapid to safe height) G01 Z-1 F500 ; plunge cut G01 X50 Y20 ; horizontal cut G01 X50 Y50 G01 X10 Y50 G01 X10 Y20 G00 Z5 ; End of program M30 )"; auto tp = import_gcode_from_string(gcode); EXPECT_GE(tp.segments.size(), 6u); // 确保解析了运动指令(没有被注释污染) bool has_linear = false; for (auto& seg : tp.segments) { if (seg.type == PathSegmentType::Linear) { has_linear = true; break; } } EXPECT_TRUE(has_linear) << "Linear moves should be parsed despite comments"; } // =========================================================================== // 测试9:平面选择 (G17/G18/G19) // =========================================================================== TEST(GcodeImportTest, PlaneSelection) { // G18 (ZX 平面) 下的圆弧:使用 I 和 K 偏移 const char* gcode = R"( G90 G21 G18 G00 X10 Z10 G01 Y-1 F500 G02 X0 Z20 I-10 K0 F400 G03 X10 Z10 I0 K-10 G00 Y10 )"; auto tp = import_gcode_from_string(gcode); EXPECT_GE(tp.segments.size(), 4u); bool found_cw = false, found_ccw = false; for (auto& seg : tp.segments) { if (seg.type == PathSegmentType::ArcCW) found_cw = true; if (seg.type == PathSegmentType::ArcCCW) found_ccw = true; } EXPECT_TRUE(found_cw) << "G18 plane should allow G02 arc"; EXPECT_TRUE(found_ccw) << "G18 plane should allow G03 arc"; } // =========================================================================== // 测试10:空 G-code 安全和边界情况 // =========================================================================== TEST(GcodeImportTest, EmptyGcode) { // 空输入或纯注释不应崩溃 const char* gcode1 = ""; const char* gcode2 = "(Just a comment)"; const char* gcode3 = "N10 (empty)"; const char* gcode4 = "%"; auto tp1 = import_gcode_from_string(gcode1); auto tp2 = import_gcode_from_string(gcode2); auto tp3 = import_gcode_from_string(gcode3); auto tp4 = import_gcode_from_string(gcode4); // 空输入或无运动指令 → segments 为空(不应崩溃) EXPECT_EQ(tp1.segments.size(), 0u); } // ═══════════════════════════════════════════════════════════ // B-7 深度:5 轴后处理 G-code 往返 + swarf 侧刃过切解析校验 // ═══════════════════════════════════════════════════════════ TEST(GcodeImportTest, FiveAxisPostprocessRoundTrip) { // 构造带倾斜刀轴的 5 轴 CLData,覆盖 AC / BC / AB 三种机床构型 AxisToolpath5 tp; tp.name = "roundtrip"; for (int k = 0; k < 6; ++k) { const Vector3D axis = Vector3D(0.15 * k - 0.3, 0.1 * k - 0.2, 1.0).normalized(); tp.points.emplace_back(Point3D(10.0 + k, -5.0 + 0.5 * k, -2.0 - 0.25 * k), axis, 300.0 + 10.0 * k); } for (MachineAxisConfig mode : {MachineAxisConfig::AC, MachineAxisConfig::BC, MachineAxisConfig::AB}) { MachineConfig cfg; cfg.axis_config = mode; cfg.a_min = -180.0; cfg.a_max = 180.0; cfg.b_min = -180.0; cfg.b_max = 180.0; cfg.c_min = -9999.0; cfg.c_max = 9999.0; const std::string gcode = export_gcode_5axis(tp, cfg); EXPECT_NE(gcode.find("G90"), std::string::npos); EXPECT_NE(gcode.find("G1 X"), std::string::npos); const AxisToolpath5 back = import_gcode_5axis(gcode, cfg); ASSERT_EQ(back.size(), tp.size()); for (size_t i = 0; i < tp.size(); ++i) { EXPECT_NEAR(back.points[i].position.x(), tp.points[i].position.x(), 2e-6); EXPECT_NEAR(back.points[i].position.y(), tp.points[i].position.y(), 2e-6); EXPECT_NEAR(back.points[i].position.z(), tp.points[i].position.z(), 2e-6); EXPECT_NEAR(back.points[i].feed_rate, tp.points[i].feed_rate, 2e-6); EXPECT_NEAR((back.points[i].tool_axis - tp.points[i].tool_axis).norm(), 0.0, 1e-5); } } } // Wave21/A55:UTF-8 路径回归(Windows ANSI 代码页)——G-code 文件按中文路径导入。 // 修复前 import_gcode 的 std::ifstream(path) 在 Windows 上按 ACP 解释路径。 TEST(GcodeImportTest, ImportUtf8Path) { const std::string path = std::string(VDE_TEST_TEMP_DIR) + "vde_a55_刀路_①.nc"; ASSERT_TRUE(a55_has_non_ascii(path)); std::error_code ec; std::filesystem::remove(vde::foundation::path_from_utf8(path), ec); { std::ofstream out = vde::foundation::open_ofstream_utf8(path); ASSERT_TRUE(out.is_open()) << path; out << "G90 G21 G17\n" "G00 X0 Y0 Z10\n" "G01 X50 Y0 Z-2 F800\n" "G01 X50 Y30\n" "G01 X0 Y30\n" "G00 Z10\n"; } const Toolpath tp = import_gcode(path); EXPECT_GE(tp.segments.size(), 4u); EXPECT_EQ(tp.name, "Imported G-code"); // 不存在的非 ASCII 路径:抛 std::runtime_error(不崩溃、不静默) EXPECT_THROW(import_gcode(path + ".missing"), std::runtime_error); std::filesystem::remove(vde::foundation::path_from_utf8(path), ec); } TEST(GcodeImportTest, FiveAxisAxisForwardInverseExact) { // 正解/反解在三种构型上互逆(刀轴归一化后误差 < 1e-9) for (MachineAxisConfig mode : {MachineAxisConfig::AC, MachineAxisConfig::BC, MachineAxisConfig::AB}) { MachineConfig cfg; cfg.axis_config = mode; for (double a : {-30.0, 0.0, 25.0}) { for (double c : {-60.0, 0.0, 45.0}) { const Vector3D axis = tool_axis_from_machine_angles(cfg, a, 0.0, c).normalized(); const std::string g = export_gcode_5axis( AxisToolpath5{"one", {CLData(Point3D(0, 0, 0), axis)}}, cfg); const AxisToolpath5 back = import_gcode_5axis(g, cfg); ASSERT_EQ(back.size(), 1u); EXPECT_NEAR((back.points[0].tool_axis - axis).norm(), 0.0, 1e-9); } } } }