| | |
| | | // 构造函数 |
| | | RecipeManager::RecipeManager() : m_recipeFolder("Recipe") {} |
| | | |
| | | // 设置配方文件夹 |
| | | void RecipeManager::setRecipeFolder(const std::string& folderPath) { |
| | | m_recipeFolder = folderPath; |
| | | } |
| | | |
| | | // 加载配方(如果文件不存在,加载默认数据) |
| | | bool RecipeManager::loadRecipe(const std::string& recipeName) { |
| | | std::string filePath = m_recipeFolder + "/" + recipeName + ".xml"; |
| | | pugi::xml_document doc; |
| | | |
| | | if (!doc.load_file(filePath.c_str())) { |
| | | std::cerr << "Recipe file not found: " << filePath << ". Loading default recipe." << std::endl; |
| | | generateDefaultRecipe(); |
| | | return false; // 文件不存在,但加载了默认数据 |
| | | } |
| | | |
| | | // 加载轴信息 |
| | | bool RecipeManager::loadAxes(pugi::xml_node axesNode) { |
| | | m_axes.clear(); |
| | | |
| | | auto recipe = doc.child("Recipe"); |
| | | for (auto axisNode : recipe.child("Axes").children("Axis")) { |
| | | for (auto axisNode : axesNode.children("Axis")) { |
| | | AxisInfo axisInfo; |
| | | axisInfo.id = axisNode.attribute("id").as_int(); |
| | | axisInfo.number = axisNode.attribute("number").value(); |
| | | axisInfo.description = axisNode.attribute("description").value(); |
| | | axisInfo.startAddress = axisNode.attribute("start_address").value(); |
| | | //axisInfo.maxPositioningSpeed = axisNode.attribute("maxPositioningSpeed").as_double(); |
| | | //axisInfo.maxManualSpeed = axisNode.attribute("maxManualSpeed").as_double(); |
| | | |
| | | // 加载 ValueRange 值 |
| | | axisInfo.jogDistance = ValueRange( |
| | |
| | | return true; |
| | | } |
| | | |
| | | // 保存配方 |
| | | bool RecipeManager::saveRecipe(const std::string& recipeName) { |
| | | // 生成文件路径 |
| | | std::string filePath = m_recipeFolder + "/" + recipeName + ".xml"; |
| | | |
| | | // 创建 XML 文档对象 |
| | | pugi::xml_document doc; |
| | | |
| | | // 如果轴数据为空,生成默认配方 |
| | | if (m_axes.empty()) { |
| | | generateDefaultRecipe(); |
| | | } |
| | | |
| | | // 添加配方根节点 |
| | | auto recipe = doc.append_child("Recipe"); |
| | | |
| | | // 添加轴列表节点 |
| | | auto axesNode = recipe.append_child("Axes"); |
| | | |
| | | // 遍历所有轴数据并写入 XML |
| | | // 保存轴信息 |
| | | void RecipeManager::saveAxes(pugi::xml_node& axesNode) { |
| | | for (const auto& axisEntry : m_axes) { |
| | | const AxisInfo& axisInfo = axisEntry.second; |
| | | |
| | |
| | | axisNode.append_attribute("number") = axisInfo.number.c_str(); |
| | | axisNode.append_attribute("description") = axisInfo.description.c_str(); |
| | | axisNode.append_attribute("start_address") = axisInfo.startAddress.c_str(); |
| | | //axisNode.append_attribute("maxPositioningSpeed") = axisInfo.maxPositioningSpeed; |
| | | //axisNode.append_attribute("maxManualSpeed") = axisInfo.maxManualSpeed; |
| | | |
| | | // 保存 ValueRange 值 |
| | | auto jog_distance = axisNode.append_child("jog_distance"); |
| | |
| | | positionNode.append_attribute("current") = position.range.currentValue; |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 设置配方文件夹 |
| | | void RecipeManager::setRecipeFolder(const std::string& folderPath) { |
| | | m_recipeFolder = folderPath; |
| | | } |
| | | |
| | | // 加载配方(如果文件不存在,加载默认数据) |
| | | bool RecipeManager::loadRecipe(const std::string& recipeName) { |
| | | std::string filePath = m_recipeFolder + "/" + recipeName + ".xml"; |
| | | pugi::xml_document doc; |
| | | |
| | | if (!doc.load_file(filePath.c_str())) { |
| | | std::cerr << "Recipe file not found: " << filePath << ". Loading default recipe." << std::endl; |
| | | generateDefaultRecipe(); |
| | | return false; // 文件不存在,但加载了默认数据 |
| | | } |
| | | |
| | | auto recipeNode = doc.child("Recipe"); |
| | | auto axesNode = recipeNode.child("Axes"); |
| | | loadAxes(axesNode); // 加载轴信息 |
| | | |
| | | return true; |
| | | } |
| | | |
| | | // 保存配方 |
| | | bool RecipeManager::saveRecipe(const std::string& recipeName) { |
| | | // 生成文件路径 |
| | | std::string filePath = m_recipeFolder + "/" + recipeName + ".xml"; |
| | | |
| | | // 创建 XML 文档对象 |
| | | pugi::xml_document doc; |
| | | |
| | | // 如果轴数据为空,生成默认配方 |
| | | if (m_axes.empty()) { |
| | | generateDefaultRecipe(); |
| | | } |
| | | |
| | | // 添加配方根节点 |
| | | auto recipeNode = doc.append_child("Recipe"); |
| | | |
| | | // 添加轴信息 |
| | | auto axesNode = recipeNode.append_child("Axes"); |
| | | saveAxes(axesNode); |
| | | |
| | | // 保存 XML 文件 |
| | | return doc.save_file(filePath.c_str()); |