From f4f1eb5f9b29bae3e1171fd832666d5efe2939b5 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Tue, 13 Feb 2024 13:40:19 +0100 Subject: [PATCH] Information about extruders count is added for each of printer profiles + Added JsonUtils to post-process written json. --- src/libslic3r/CMakeLists.txt | 2 ++ src/libslic3r/ProfilesSharingUtils.cpp | 29 +++++++++++++------------- src/libslic3r/Utils/JsonUtils.cpp | 22 +++++++++++++++++++ src/libslic3r/Utils/JsonUtils.hpp | 12 +++++++++++ 4 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 src/libslic3r/Utils/JsonUtils.cpp create mode 100644 src/libslic3r/Utils/JsonUtils.hpp diff --git a/src/libslic3r/CMakeLists.txt b/src/libslic3r/CMakeLists.txt index c260991bf2..103b3d9f7e 100644 --- a/src/libslic3r/CMakeLists.txt +++ b/src/libslic3r/CMakeLists.txt @@ -512,6 +512,8 @@ set(SLIC3R_SOURCES ProfilesSharingUtils.cpp Utils/DirectoriesUtils.hpp Utils/DirectoriesUtils.cpp + Utils/JsonUtils.hpp + Utils/JsonUtils.cpp ) if (APPLE) diff --git a/src/libslic3r/ProfilesSharingUtils.cpp b/src/libslic3r/ProfilesSharingUtils.cpp index 8de4828477..79d46e1e1a 100644 --- a/src/libslic3r/ProfilesSharingUtils.cpp +++ b/src/libslic3r/ProfilesSharingUtils.cpp @@ -8,8 +8,8 @@ #include "PrintConfig.hpp" #include "PresetBundle.hpp" #include "Utils/DirectoriesUtils.hpp" +#include "Utils/JsonUtils.hpp" -#include #include namespace Slic3r { @@ -116,9 +116,7 @@ static std::string get_printer_profiles(const VendorProfile* vendor_profile, data_node.add_child("printer_profiles", printer_profiles_node); // Serialize the tree into JSON and return it. - std::stringstream ss; - pt::write_json(ss, data_node); - return ss.str(); + return write_json_with_post_process(data_node); } } @@ -161,10 +159,14 @@ static bool is_compatible_preset(const Preset& printer_preset, const PrinterAttr printer_preset.config.opt_string("printer_variant") == attr.variant_name; } -static void add_profile_node(pt::ptree& printer_profiles_node, const std::string& preset_name) +static void add_profile_node(pt::ptree& printer_profiles_node, const std::string& preset_name, const int extruders_cnt) { pt::ptree profile_node; - profile_node.put("", preset_name); + + profile_node.put("name", preset_name); + if (extruders_cnt > 0) + profile_node.put("extruders_cnt", extruders_cnt); + printer_profiles_node.push_back(std::make_pair("", profile_node)); } @@ -178,13 +180,16 @@ static void get_printer_profiles_node(pt::ptree& printer_profiles_node, for (const Preset& printer_preset : printer_presets) { + int extruders_cnt = printer_preset.printer_technology() == ptSLA ? 0 : + printer_preset.config.option("nozzle_diameter")->values.size(); + if (printer_preset.is_user()) { const Preset* parent_preset = printer_presets.get_preset_parent(printer_preset); if (parent_preset && printer_preset.is_visible && is_compatible_preset(*parent_preset, attr)) - add_profile_node(user_printer_profiles_node, printer_preset.name); + add_profile_node(user_printer_profiles_node, printer_preset.name, extruders_cnt); } else if (printer_preset.is_visible && is_compatible_preset(printer_preset, attr)) - add_profile_node(printer_profiles_node, printer_preset.name); + add_profile_node(printer_profiles_node, printer_preset.name, extruders_cnt); } } @@ -266,9 +271,7 @@ std::string get_json_printer_models(PrinterTechnology printer_technology) root.add_child("printer_models", vendor_node); // Serialize the tree into JSON and return it. - std::stringstream ss; - pt::write_json(ss, root); - return ss.str(); + return write_json_with_post_process(root); } static std::string get_installed_print_and_filament_profiles(const PresetBundle* preset_bundle, const Preset* printer_preset) @@ -335,9 +338,7 @@ static std::string get_installed_print_and_filament_profiles(const PresetBundle* tree.add_child("user_print_profiles", user_print_profiles); // Serialize the tree into JSON and return it. - std::stringstream ss; - pt::write_json(ss, tree); - return ss.str(); + return write_json_with_post_process(tree); } std::string get_json_print_filament_profiles(const std::string& printer_profile) diff --git a/src/libslic3r/Utils/JsonUtils.cpp b/src/libslic3r/Utils/JsonUtils.cpp new file mode 100644 index 0000000000..0419075987 --- /dev/null +++ b/src/libslic3r/Utils/JsonUtils.cpp @@ -0,0 +1,22 @@ +#include "JsonUtils.hpp" + +namespace Slic3r { + +namespace pt = boost::property_tree; + +std::string write_json_with_post_process(const pt::ptree& ptree) +{ + std::stringstream oss; + pt::write_json(oss, ptree); + + // fix json-out to show node values as a string just for string nodes + std::regex reg("\\\"([0-9]+\\.{0,1}[0-9]*)\\\""); // code is borrowed from https://stackoverflow.com/questions/2855741/why-does-boost-property-tree-write-json-save-everything-as-string-is-it-possibl + std::string result = std::regex_replace(oss.str(), reg, "$1"); + + boost::replace_all(result, "\"true\"", "true"); + boost::replace_all(result, "\"false\"", "false"); + + return result; +} + +} // namespace Slic3r diff --git a/src/libslic3r/Utils/JsonUtils.hpp b/src/libslic3r/Utils/JsonUtils.hpp new file mode 100644 index 0000000000..08745a620c --- /dev/null +++ b/src/libslic3r/Utils/JsonUtils.hpp @@ -0,0 +1,12 @@ +#ifndef slic3r_JsonUtils_hpp_ +#define slic3r_JsonUtils_hpp_ + +#include + +namespace Slic3r { + +std::string write_json_with_post_process(const boost::property_tree::ptree& ptree); + +} // namespace Slic3r + +#endif // slic3r_jsonUtils_hpp_