// 示例 CAM 策略/后处理插件(VDE 微内核 P2 / 插件 ABI 1.2;纯 C ABI,不链接内核)。 // // 演示 VdeCamStrategyPlugin 扩展点:输入 VDE 序列化模型 + 参数 JSON,输出 // Fanuc 风格 G-code 文本("演示级":在包围盒顶面做等距光栅刀路,不考虑毛坯/ // 残料/碰撞;目的是验证扩展点契约、宿主服务 body_from_serialized/body_bbox 与 // host->alloc 输出所有权)。 #include #include #include #include #include "vde/plugin/vde_plugin_api.h" namespace { int cam_fail(const VdePluginHost* host, int code, const char* message) { if (host && host->set_error) host->set_error(message); return code; } int validate_host(const VdePluginHost* host) { if (!host) return VDE_PLUGIN_ERR_INVALID_ARG; if (host->abi_major != VDE_PLUGIN_ABI_MAJOR) return cam_fail(host, VDE_PLUGIN_ERR_ABI, "sample_cam_post: 宿主 ABI major 不兼容"); if (!host->alloc || !host->free || !host->set_error) return cam_fail(host, VDE_PLUGIN_ERR_UNSUPPORTED, "sample_cam_post: 宿主缺少 alloc/free/set_error"); if (!host->body_from_serialized || !host->body_bbox || !host->body_destroy) return cam_fail(host, VDE_PLUGIN_ERR_UNSUPPORTED, "sample_cam_post: 宿主缺少 body_from_serialized/bbox/destroy"); return VDE_PLUGIN_OK; } double json_number(const char* text, const char* key, double fallback) { if (!text || !key) return fallback; const std::string pattern = std::string("\"") + key + "\""; const char* pos = std::strstr(text, pattern.c_str()); if (!pos) return fallback; pos += pattern.size(); while (*pos == ' ' || *pos == '\t' || *pos == '\r' || *pos == '\n') ++pos; if (*pos != ':') return fallback; ++pos; char* end = nullptr; const double value = std::strtod(pos, &end); if (end == pos) return fallback; return value; } int cam_invoke(const VdePluginHost* host, const void* model_data, size_t model_size, const char* args_json, VdeBuffer* out) { if (out) { out->data = nullptr; out->size = 0; } if (!out) return cam_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "sample_cam_post: out 为空"); const int host_rc = validate_host(host); if (host_rc != VDE_PLUGIN_OK) return host_rc; if (!model_data || model_size == 0) return cam_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "sample_cam_post: 模型数据为空"); VdeBody body = host->body_from_serialized(model_data, model_size); if (!body) return cam_fail(host, VDE_PLUGIN_ERR_PARSE, "sample_cam_post: 无法反序列化模型(见宿主错误信息)"); double bmin[3] = {0.0, 0.0, 0.0}; double bmax[3] = {0.0, 0.0, 0.0}; const int rc = host->body_bbox(body, bmin, bmax); host->body_destroy(body); if (rc != VDE_PLUGIN_OK) return cam_fail(host, VDE_PLUGIN_ERR_INTERNAL, "sample_cam_post: body_bbox 失败"); const double stepover = json_number(args_json, "stepover", 5.0); const double feed = json_number(args_json, "feed", 300.0); const double rapid = json_number(args_json, "rapid", 1000.0); const double clearance = json_number(args_json, "clearance", 5.0); const double safe_step = (stepover > 1e-6) ? stepover : 5.0; const double top_z = bmax[2] + ((clearance > 0.0) ? clearance : 5.0); const double x0 = bmin[0]; const double x1 = bmax[0]; const double y0 = bmin[1]; const double y1 = bmax[1]; std::string code; char line[256]; code += "; VDE sample CAM post (fanuc-style, demo only)\n"; std::snprintf(line, sizeof(line), "; stock: X[%.6g,%.6g] Y[%.6g,%.6g] Z[%.6g,%.6g]\n", bmin[0], bmax[0], bmin[1], bmax[1], bmin[2], bmax[2]); code += line; code += "G21 G90 G17 G40 G49\n"; std::snprintf(line, sizeof(line), "G0 Z%.6g\n", top_z); code += line; std::snprintf(line, sizeof(line), "G0 X%.6g Y%.6g\n", x0, y0); code += line; bool forward = true; int passes = 0; for (double y = y0; y <= y1 + 1e-9; y += safe_step) { const double target_x = forward ? x1 : x0; std::snprintf(line, sizeof(line), "G1 X%.6g Y%.6g F%.6g\n", target_x, y, feed); code += line; ++passes; if (y + safe_step > y1 + 1e-9) break; std::snprintf(line, sizeof(line), "G1 Y%.6g F%.6g\n", y + safe_step, feed); code += line; forward = !forward; } std::snprintf(line, sizeof(line), "G0 Z%.6g\n", top_z + 10.0); code += line; code += "M30\n"; code += "; postprocessor: fanuc; machine: generic-3axis\n"; void* buffer = host->alloc(code.size() + 1); if (!buffer) return cam_fail(host, VDE_PLUGIN_ERR_BUFFER, "sample_cam_post: 输出缓冲分配失败"); std::memcpy(buffer, code.c_str(), code.size() + 1); out->data = buffer; out->size = code.size(); (void)rapid; (void)passes; return VDE_PLUGIN_OK; } const char* cam_describe() { return "VDE sample CAM post-processor: Fanuc-style raster G-code (demo only, ABI 1.2)"; } const VdeCamStrategyPlugin kStrategy = [] { VdeCamStrategyPlugin strategy{}; strategy.struct_size = static_cast(sizeof(VdeCamStrategyPlugin)); strategy.strategy_id = "cam.post.gcode"; strategy.postprocessor_id = "fanuc"; strategy.machine_id = "generic-3axis"; strategy.invoke = &cam_invoke; strategy.describe = &cam_describe; return strategy; }(); } // namespace extern "C" { VDE_PLUGIN_EXPORT int vde_plugin_query(uint32_t host_abi_major, VdePluginInfo* info) { if (!info || info->struct_size < sizeof(VdePluginInfo)) return VDE_PLUGIN_ERR_ABI; if (host_abi_major != VDE_PLUGIN_ABI_MAJOR) return VDE_PLUGIN_ERR_ABI; info->id = "com.vde.sample.cam_post"; info->name = "VDE Sample CAM Fanuc Post-processor"; info->version = "0.1.0"; info->kind = VDE_PLUGIN_KIND_CAM_STRATEGY; info->abi_major = VDE_PLUGIN_ABI_MAJOR; info->abi_minor = VDE_PLUGIN_ABI_MINOR; info->capabilities = "cam_strategy:post.gcode"; info->license = "Apache-2.0"; return VDE_PLUGIN_OK; } VDE_PLUGIN_EXPORT int vde_plugin_init(const VdePluginHost* host, VdePluginRegistration* reg) { if (!reg || reg->struct_size < sizeof(VdePluginRegistration)) return VDE_PLUGIN_ERR_ABI; const int host_rc = validate_host(host); if (host_rc != VDE_PLUGIN_OK) return host_rc; reg->kind = VDE_PLUGIN_KIND_CAM_STRATEGY; reg->format = nullptr; reg->cae_solver = nullptr; reg->cam_strategy = &kStrategy; return VDE_PLUGIN_OK; } VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {} } // extern "C"