From 19c4f3260429ba6db33411fd78b6bfc74ac2b8e9 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Tue, 16 Jun 2020 16:58:41 +0200 Subject: Preset and PresetBundle are moved to the _libslic3r_ folder --- src/slic3r/CMakeLists.txt | 4 - src/slic3r/Config/Snapshot.cpp | 3 +- src/slic3r/GUI/3DBed.cpp | 2 +- src/slic3r/GUI/ConfigManipulation.cpp | 2 +- src/slic3r/GUI/ConfigWizard_private.hpp | 2 +- src/slic3r/GUI/GLCanvas3D.cpp | 2 +- src/slic3r/GUI/GUI_App.cpp | 4 +- src/slic3r/GUI/GUI_App.hpp | 2 +- src/slic3r/GUI/GUI_ObjectLayers.cpp | 2 +- src/slic3r/GUI/GUI_ObjectList.cpp | 2 +- src/slic3r/GUI/GUI_ObjectManipulation.cpp | 2 +- src/slic3r/GUI/GUI_ObjectSettings.cpp | 2 +- src/slic3r/GUI/GUI_Preview.cpp | 2 +- src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp | 2 +- src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp | 2 +- src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp | 2 +- src/slic3r/GUI/Gizmos/GLGizmosCommon.cpp | 2 +- src/slic3r/GUI/Gizmos/GLGizmosManager.cpp | 2 +- src/slic3r/GUI/I18N.hpp | 2 +- src/slic3r/GUI/Jobs/SLAImportJob.cpp | 2 +- src/slic3r/GUI/MainFrame.cpp | 2 +- src/slic3r/GUI/Mouse3DController.cpp | 2 +- src/slic3r/GUI/Plater.cpp | 4 +- src/slic3r/GUI/Plater.hpp | 2 +- src/slic3r/GUI/Preset.cpp | 1451 ------------------------- src/slic3r/GUI/Preset.hpp | 714 ------------ src/slic3r/GUI/PresetBundle.cpp | 1497 -------------------------- src/slic3r/GUI/PresetBundle.hpp | 163 --- src/slic3r/GUI/PresetComboBoxes.cpp | 2 +- src/slic3r/GUI/PresetComboBoxes.hpp | 2 +- src/slic3r/GUI/PresetHints.cpp | 1 - src/slic3r/GUI/PresetHints.hpp | 2 +- src/slic3r/GUI/Search.cpp | 2 +- src/slic3r/GUI/Search.hpp | 2 +- src/slic3r/GUI/Tab.cpp | 3 +- src/slic3r/GUI/Tab.hpp | 2 +- src/slic3r/Utils/PresetUpdater.cpp | 2 +- 37 files changed, 34 insertions(+), 3864 deletions(-) delete mode 100644 src/slic3r/GUI/Preset.cpp delete mode 100644 src/slic3r/GUI/Preset.hpp delete mode 100644 src/slic3r/GUI/PresetBundle.cpp delete mode 100644 src/slic3r/GUI/PresetBundle.hpp (limited to 'src/slic3r') diff --git a/src/slic3r/CMakeLists.txt b/src/slic3r/CMakeLists.txt index 98389e7da..49e069285 100644 --- a/src/slic3r/CMakeLists.txt +++ b/src/slic3r/CMakeLists.txt @@ -61,10 +61,6 @@ set(SLIC3R_GUI_SOURCES GUI/GLToolbar.cpp GUI/Preferences.cpp GUI/Preferences.hpp - GUI/Preset.cpp - GUI/Preset.hpp - GUI/PresetBundle.cpp - GUI/PresetBundle.hpp GUI/PresetHints.cpp GUI/PresetHints.hpp GUI/GUI.cpp diff --git a/src/slic3r/Config/Snapshot.cpp b/src/slic3r/Config/Snapshot.cpp index 2264afa7d..f7d313418 100644 --- a/src/slic3r/Config/Snapshot.cpp +++ b/src/slic3r/Config/Snapshot.cpp @@ -1,6 +1,5 @@ #include "Snapshot.hpp" #include "../GUI/AppConfig.hpp" -#include "../GUI/PresetBundle.hpp" #include @@ -11,7 +10,7 @@ #include #include - +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/libslic3r.h" #include "libslic3r/Time.hpp" #include "libslic3r/Config.hpp" diff --git a/src/slic3r/GUI/3DBed.cpp b/src/slic3r/GUI/3DBed.cpp index 6c070ca99..f2f9f6301 100644 --- a/src/slic3r/GUI/3DBed.cpp +++ b/src/slic3r/GUI/3DBed.cpp @@ -7,7 +7,7 @@ #include "libslic3r/BoundingBox.hpp" #include "GUI_App.hpp" -#include "PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include "GLCanvas3D.hpp" #include diff --git a/src/slic3r/GUI/ConfigManipulation.cpp b/src/slic3r/GUI/ConfigManipulation.cpp index a0df4c659..cd8463a77 100644 --- a/src/slic3r/GUI/ConfigManipulation.cpp +++ b/src/slic3r/GUI/ConfigManipulation.cpp @@ -2,7 +2,7 @@ #include "ConfigManipulation.hpp" #include "I18N.hpp" #include "GUI_App.hpp" -#include "PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include diff --git a/src/slic3r/GUI/ConfigWizard_private.hpp b/src/slic3r/GUI/ConfigWizard_private.hpp index c99c5952b..be2919861 100644 --- a/src/slic3r/GUI/ConfigWizard_private.hpp +++ b/src/slic3r/GUI/ConfigWizard_private.hpp @@ -20,9 +20,9 @@ #include #include "libslic3r/PrintConfig.hpp" +#include "libslic3r/PresetBundle.hpp" #include "slic3r/Utils/PresetUpdater.hpp" #include "AppConfig.hpp" -#include "PresetBundle.hpp" #include "BedShapeDialog.hpp" #include "GUI.hpp" #include "wxExtensions.hpp" diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index b4e672c4f..b5cd9fb2a 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -13,11 +13,11 @@ #include "libslic3r/Utils.hpp" #include "libslic3r/Technologies.hpp" #include "libslic3r/Tesselate.hpp" +#include "libslic3r/PresetBundle.hpp" #include "slic3r/GUI/3DScene.hpp" #include "slic3r/GUI/BackgroundSlicingProcess.hpp" #include "slic3r/GUI/GLShader.hpp" #include "slic3r/GUI/GUI.hpp" -#include "slic3r/GUI/PresetBundle.hpp" #include "slic3r/GUI/Tab.hpp" #include "slic3r/GUI/GUI_Preview.hpp" #include "slic3r/GUI/OpenGLManager.hpp" diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 3c000f62e..157875e70 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -31,11 +31,11 @@ #include "libslic3r/Utils.hpp" #include "libslic3r/Model.hpp" #include "libslic3r/I18N.hpp" +#include "libslic3r/PresetBundle.hpp" #include "GUI.hpp" #include "GUI_Utils.hpp" #include "AppConfig.hpp" -#include "PresetBundle.hpp" #include "3DScene.hpp" #include "MainFrame.hpp" #include "Plater.hpp" @@ -935,7 +935,7 @@ bool GUI_App::load_language(wxString language, bool initial) m_imgui->set_language(into_u8(language_info->CanonicalName)); //FIXME This is a temporary workaround, the correct solution is to switch to "C" locale during file import / export only. wxSetlocale(LC_NUMERIC, "C"); - Preset::update_suffix_modified(); + Preset::update_suffix_modified((" (" + _L("modified") + ")").ToUTF8().data()); return true; } diff --git a/src/slic3r/GUI/GUI_App.hpp b/src/slic3r/GUI/GUI_App.hpp index c2b257f45..23567695c 100644 --- a/src/slic3r/GUI/GUI_App.hpp +++ b/src/slic3r/GUI/GUI_App.hpp @@ -3,10 +3,10 @@ #include #include -#include "Preset.hpp" #include "ImGuiWrapper.hpp" #include "ConfigWizard.hpp" #include "OpenGLManager.hpp" +#include "libslic3r/Preset.hpp" #include #include diff --git a/src/slic3r/GUI/GUI_ObjectLayers.cpp b/src/slic3r/GUI/GUI_ObjectLayers.cpp index b1a5512d4..90a725fbf 100644 --- a/src/slic3r/GUI/GUI_ObjectLayers.cpp +++ b/src/slic3r/GUI/GUI_ObjectLayers.cpp @@ -3,7 +3,7 @@ #include "OptionsGroup.hpp" #include "GUI_App.hpp" -#include "PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/Model.hpp" #include "GLCanvas3D.hpp" #include "Plater.hpp" diff --git a/src/slic3r/GUI/GUI_ObjectList.cpp b/src/slic3r/GUI/GUI_ObjectList.cpp index 2f201180a..b87565b03 100644 --- a/src/slic3r/GUI/GUI_ObjectList.cpp +++ b/src/slic3r/GUI/GUI_ObjectList.cpp @@ -1,4 +1,5 @@ #include "libslic3r/libslic3r.h" +#include "libslic3r/PresetBundle.hpp" #include "GUI_ObjectList.hpp" #include "GUI_ObjectManipulation.hpp" #include "GUI_ObjectLayers.hpp" @@ -7,7 +8,6 @@ #include "Plater.hpp" #include "OptionsGroup.hpp" -#include "PresetBundle.hpp" #include "Tab.hpp" #include "wxExtensions.hpp" #include "libslic3r/Model.hpp" diff --git a/src/slic3r/GUI/GUI_ObjectManipulation.cpp b/src/slic3r/GUI/GUI_ObjectManipulation.cpp index 2c35fc316..7243e8c73 100644 --- a/src/slic3r/GUI/GUI_ObjectManipulation.cpp +++ b/src/slic3r/GUI/GUI_ObjectManipulation.cpp @@ -6,7 +6,7 @@ #include "OptionsGroup.hpp" #include "GUI_App.hpp" #include "wxExtensions.hpp" -#include "PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/Model.hpp" #include "libslic3r/Geometry.hpp" #include "Selection.hpp" diff --git a/src/slic3r/GUI/GUI_ObjectSettings.cpp b/src/slic3r/GUI/GUI_ObjectSettings.cpp index ef78123a4..398cd51d4 100644 --- a/src/slic3r/GUI/GUI_ObjectSettings.cpp +++ b/src/slic3r/GUI/GUI_ObjectSettings.cpp @@ -4,8 +4,8 @@ #include "OptionsGroup.hpp" #include "GUI_App.hpp" #include "wxExtensions.hpp" -#include "PresetBundle.hpp" #include "Plater.hpp" +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/Model.hpp" #include diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index c1e8b4c33..f068ef37d 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -8,7 +8,7 @@ #include "BackgroundSlicingProcess.hpp" #include "OpenGLManager.hpp" #include "GLCanvas3D.hpp" -#include "PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include "DoubleSlider.hpp" #include "Plater.hpp" diff --git a/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp b/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp index cd4285724..7aa516845 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp @@ -6,9 +6,9 @@ #include #include "slic3r/GUI/GUI_App.hpp" -#include "slic3r/GUI/PresetBundle.hpp" #include "slic3r/GUI/Camera.hpp" #include "slic3r/GUI/Plater.hpp" +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/Model.hpp" diff --git a/src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp b/src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp index 658db64ca..273384da2 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp @@ -9,7 +9,7 @@ #include "slic3r/GUI/GUI_ObjectSettings.hpp" #include "slic3r/GUI/GUI_ObjectList.hpp" #include "slic3r/GUI/Plater.hpp" -#include "slic3r/GUI/PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/Model.hpp" diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp index 908fe27b1..2856bb35d 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp @@ -16,7 +16,7 @@ #include "slic3r/GUI/GUI_ObjectSettings.hpp" #include "slic3r/GUI/GUI_ObjectList.hpp" #include "slic3r/GUI/Plater.hpp" -#include "slic3r/GUI/PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/SLAPrint.hpp" diff --git a/src/slic3r/GUI/Gizmos/GLGizmosCommon.cpp b/src/slic3r/GUI/Gizmos/GLGizmosCommon.cpp index 051e9cf88..6742f5cde 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmosCommon.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmosCommon.cpp @@ -8,7 +8,7 @@ #include "slic3r/GUI/Camera.hpp" #include "slic3r/GUI/Plater.hpp" -#include "slic3r/GUI/PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include diff --git a/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp b/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp index 511c68735..c33ba2850 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp @@ -5,7 +5,6 @@ #include "slic3r/GUI/Camera.hpp" #include "slic3r/GUI/GUI_App.hpp" #include "slic3r/GUI/GUI_ObjectManipulation.hpp" -#include "slic3r/GUI/PresetBundle.hpp" #include "slic3r/GUI/Plater.hpp" #include "slic3r/Utils/UndoRedo.hpp" @@ -19,6 +18,7 @@ #include "slic3r/GUI/Gizmos/GLGizmoHollow.hpp" #include "libslic3r/Model.hpp" +#include "libslic3r/PresetBundle.hpp" #include diff --git a/src/slic3r/GUI/I18N.hpp b/src/slic3r/GUI/I18N.hpp index 25e46930b..7bad6880e 100644 --- a/src/slic3r/GUI/I18N.hpp +++ b/src/slic3r/GUI/I18N.hpp @@ -12,7 +12,7 @@ #ifndef L // !!! If you needed to translate some wxString, -// !!! please use _(L(string)) +// !!! please use _L(string) // !!! _() - is a standard wxWidgets macro to translate // !!! L() is used only for marking localizable string // !!! It will be used in "xgettext" to create a Locating Message Catalog. diff --git a/src/slic3r/GUI/Jobs/SLAImportJob.cpp b/src/slic3r/GUI/Jobs/SLAImportJob.cpp index 4e9f08ff2..cc779df2a 100644 --- a/src/slic3r/GUI/Jobs/SLAImportJob.cpp +++ b/src/slic3r/GUI/Jobs/SLAImportJob.cpp @@ -4,11 +4,11 @@ #include "slic3r/GUI/GUI_App.hpp" #include "slic3r/GUI/AppConfig.hpp" #include "slic3r/GUI/Plater.hpp" -#include "slic3r/GUI/PresetBundle.hpp" #include "slic3r/GUI/GUI_ObjectList.hpp" #include "slic3r/Utils/SLAImport.hpp" #include "libslic3r/Model.hpp" +#include "libslic3r/PresetBundle.hpp" #include #include diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index d4ce21fc0..08caf299b 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -15,9 +15,9 @@ #include "libslic3r/Print.hpp" #include "libslic3r/Polygon.hpp" #include "libslic3r/SLAPrint.hpp" +#include "libslic3r/PresetBundle.hpp" #include "Tab.hpp" -#include "PresetBundle.hpp" #include "ProgressStatusBar.hpp" #include "3DScene.hpp" #include "AppConfig.hpp" diff --git a/src/slic3r/GUI/Mouse3DController.cpp b/src/slic3r/GUI/Mouse3DController.cpp index baa9356b6..91d2414d5 100644 --- a/src/slic3r/GUI/Mouse3DController.cpp +++ b/src/slic3r/GUI/Mouse3DController.cpp @@ -1,9 +1,9 @@ #include "libslic3r/libslic3r.h" +#include "libslic3r/PresetBundle.hpp" #include "Mouse3DController.hpp" #include "Camera.hpp" #include "GUI_App.hpp" -#include "PresetBundle.hpp" #include "AppConfig.hpp" #include "GLCanvas3D.hpp" #include "Plater.hpp" diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 339badc96..a78683bd4 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -43,6 +43,7 @@ #include "libslic3r/PrintConfig.hpp" #include "libslic3r/SLAPrint.hpp" #include "libslic3r/Utils.hpp" +#include "libslic3r/PresetBundle.hpp" #include "GUI.hpp" #include "GUI_App.hpp" @@ -65,7 +66,6 @@ #include "Jobs/ArrangeJob.hpp" #include "Jobs/RotoptimizeJob.hpp" #include "Jobs/SLAImportJob.hpp" -#include "PresetBundle.hpp" #include "BackgroundSlicingProcess.hpp" #include "ProgressStatusBar.hpp" #include "PrintHostDialogs.hpp" @@ -2120,6 +2120,8 @@ std::vector Plater::priv::load_files(const std::vector& input_ if (!config.empty()) { Preset::normalize(config); wxGetApp().preset_bundle->load_config_model(filename.string(), std::move(config)); + if (printer_technology == ptFFF) + CustomGCode::update_custom_gcode_per_print_z_from_config(model.custom_gcode_per_print_z, &wxGetApp().preset_bundle->project_config); wxGetApp().load_current_presets(); is_project_file = true; } diff --git a/src/slic3r/GUI/Plater.hpp b/src/slic3r/GUI/Plater.hpp index d2acc7632..38fc67982 100644 --- a/src/slic3r/GUI/Plater.hpp +++ b/src/slic3r/GUI/Plater.hpp @@ -7,9 +7,9 @@ #include -#include "Preset.hpp" #include "Selection.hpp" +#include "libslic3r/Preset.hpp" #include "libslic3r/BoundingBox.hpp" #include "Jobs/Job.hpp" #include "Search.hpp" diff --git a/src/slic3r/GUI/Preset.cpp b/src/slic3r/GUI/Preset.cpp deleted file mode 100644 index 883dc438a..000000000 --- a/src/slic3r/GUI/Preset.cpp +++ /dev/null @@ -1,1451 +0,0 @@ -#include - -#include "Preset.hpp" -#include "AppConfig.hpp" -#include "I18N.hpp" - -#ifdef _MSC_VER - #define WIN32_LEAN_AND_MEAN - #define NOMINMAX - #include -#endif /* _MSC_VER */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "libslic3r/libslic3r.h" -#include "libslic3r/Utils.hpp" -#include "libslic3r/PlaceholderParser.hpp" - -using boost::property_tree::ptree; - -namespace Slic3r { - -ConfigFileType guess_config_file_type(const ptree &tree) -{ - size_t app_config = 0; - size_t bundle = 0; - size_t config = 0; - for (const ptree::value_type &v : tree) { - if (v.second.empty()) { - if (v.first == "background_processing" || - v.first == "last_output_path" || - v.first == "no_controller" || - v.first == "no_defaults") - ++ app_config; - else if (v.first == "nozzle_diameter" || - v.first == "filament_diameter") - ++ config; - } else if (boost::algorithm::starts_with(v.first, "print:") || - boost::algorithm::starts_with(v.first, "filament:") || - boost::algorithm::starts_with(v.first, "printer:") || - v.first == "settings") - ++ bundle; - else if (v.first == "presets") { - ++ app_config; - ++ bundle; - } else if (v.first == "recent") { - for (auto &kvp : v.second) - if (kvp.first == "config_directory" || kvp.first == "skein_directory") - ++ app_config; - } - } - return (app_config > bundle && app_config > config) ? CONFIG_FILE_TYPE_APP_CONFIG : - (bundle > config) ? CONFIG_FILE_TYPE_CONFIG_BUNDLE : CONFIG_FILE_TYPE_CONFIG; -} - - -VendorProfile VendorProfile::from_ini(const boost::filesystem::path &path, bool load_all) -{ - ptree tree; - boost::filesystem::ifstream ifs(path); - boost::property_tree::read_ini(ifs, tree); - return VendorProfile::from_ini(tree, path, load_all); -} - -static const std::unordered_map pre_family_model_map {{ - { "MK3", "MK3" }, - { "MK3MMU2", "MK3" }, - { "MK2.5", "MK2.5" }, - { "MK2.5MMU2", "MK2.5" }, - { "MK2S", "MK2" }, - { "MK2SMM", "MK2" }, - { "SL1", "SL1" }, -}}; - -VendorProfile VendorProfile::from_ini(const ptree &tree, const boost::filesystem::path &path, bool load_all) -{ - static const std::string printer_model_key = "printer_model:"; - static const std::string filaments_section = "default_filaments"; - static const std::string materials_section = "default_sla_materials"; - - const std::string id = path.stem().string(); - - if (! boost::filesystem::exists(path)) { - throw std::runtime_error((boost::format("Cannot load Vendor Config Bundle `%1%`: File not found: `%2%`.") % id % path).str()); - } - - VendorProfile res(id); - - // Helper to get compulsory fields - auto get_or_throw = [&](const ptree &tree, const std::string &key) -> ptree::const_assoc_iterator - { - auto res = tree.find(key); - if (res == tree.not_found()) { - throw std::runtime_error((boost::format("Vendor Config Bundle `%1%` is not valid: Missing secion or key: `%2%`.") % id % key).str()); - } - return res; - }; - - // Load the header - const auto &vendor_section = get_or_throw(tree, "vendor")->second; - res.name = get_or_throw(vendor_section, "name")->second.data(); - - auto config_version_str = get_or_throw(vendor_section, "config_version")->second.data(); - auto config_version = Semver::parse(config_version_str); - if (! config_version) { - throw std::runtime_error((boost::format("Vendor Config Bundle `%1%` is not valid: Cannot parse config_version: `%2%`.") % id % config_version_str).str()); - } else { - res.config_version = std::move(*config_version); - } - - // Load URLs - const auto config_update_url = vendor_section.find("config_update_url"); - if (config_update_url != vendor_section.not_found()) { - res.config_update_url = config_update_url->second.data(); - } - - const auto changelog_url = vendor_section.find("changelog_url"); - if (changelog_url != vendor_section.not_found()) { - res.changelog_url = changelog_url->second.data(); - } - - if (! load_all) { - return res; - } - - // Load printer models - for (auto §ion : tree) { - if (boost::starts_with(section.first, printer_model_key)) { - VendorProfile::PrinterModel model; - model.id = section.first.substr(printer_model_key.size()); - model.name = section.second.get("name", model.id); - - const char *technology_fallback = boost::algorithm::starts_with(model.id, "SL") ? "SLA" : "FFF"; - - auto technology_field = section.second.get("technology", technology_fallback); - if (! ConfigOptionEnum::from_string(technology_field, model.technology)) { - BOOST_LOG_TRIVIAL(error) << boost::format("Vendor bundle: `%1%`: Invalid printer technology field: `%2%`") % id % technology_field; - model.technology = ptFFF; - } - - model.family = section.second.get("family", std::string()); - if (model.family.empty() && res.name == "Prusa Research") { - // If no family is specified, it can be inferred for known printers - const auto from_pre_map = pre_family_model_map.find(model.id); - if (from_pre_map != pre_family_model_map.end()) { model.family = from_pre_map->second; } - } -#if 0 - // Remove SLA printers from the initial alpha. - if (model.technology == ptSLA) - continue; -#endif - section.second.get("variants", ""); - const auto variants_field = section.second.get("variants", ""); - std::vector variants; - if (Slic3r::unescape_strings_cstyle(variants_field, variants)) { - for (const std::string &variant_name : variants) { - if (model.variant(variant_name) == nullptr) - model.variants.emplace_back(VendorProfile::PrinterVariant(variant_name)); - } - } else { - BOOST_LOG_TRIVIAL(error) << boost::format("Vendor bundle: `%1%`: Malformed variants field: `%2%`") % id % variants_field; - } - auto default_materials_field = section.second.get("default_materials", ""); - if (default_materials_field.empty()) - default_materials_field = section.second.get("default_filaments", ""); - if (Slic3r::unescape_strings_cstyle(default_materials_field, model.default_materials)) { - Slic3r::sort_remove_duplicates(model.default_materials); - if (! model.default_materials.empty() && model.default_materials.front().empty()) - // An empty material was inserted into the list of default materials. Remove it. - model.default_materials.erase(model.default_materials.begin()); - } else { - BOOST_LOG_TRIVIAL(error) << boost::format("Vendor bundle: `%1%`: Malformed default_materials field: `%2%`") % id % default_materials_field; - } - model.bed_model = section.second.get("bed_model", ""); - model.bed_texture = section.second.get("bed_texture", ""); - if (! model.id.empty() && ! model.variants.empty()) - res.models.push_back(std::move(model)); - } - } - - // Load filaments and sla materials to be installed by default - const auto filaments = tree.find(filaments_section); - if (filaments != tree.not_found()) { - for (auto &pair : filaments->second) { - if (pair.second.data() == "1") { - res.default_filaments.insert(pair.first); - } - } - } - const auto materials = tree.find(materials_section); - if (materials != tree.not_found()) { - for (auto &pair : materials->second) { - if (pair.second.data() == "1") { - res.default_sla_materials.insert(pair.first); - } - } - } - - return res; -} - -std::vector VendorProfile::families() const -{ - std::vector res; - unsigned num_familiies = 0; - - for (auto &model : models) { - if (std::find(res.begin(), res.end(), model.family) == res.end()) { - res.push_back(model.family); - num_familiies++; - } - } - - return res; -} - -// Suffix to be added to a modified preset name in the combo box. -static std::string g_suffix_modified = " (modified)"; -const std::string& Preset::suffix_modified() -{ - return g_suffix_modified; -} - -void Preset::update_suffix_modified() -{ - g_suffix_modified = (" (" + _(L("modified")) + ")").ToUTF8().data(); -} -// Remove an optional "(modified)" suffix from a name. -// This converts a UI name to a unique preset identifier. -std::string Preset::remove_suffix_modified(const std::string &name) -{ - return boost::algorithm::ends_with(name, g_suffix_modified) ? - name.substr(0, name.size() - g_suffix_modified.size()) : - name; -} - -// Update new extruder fields at the printer profile. -void Preset::normalize(DynamicPrintConfig &config) -{ - auto *nozzle_diameter = dynamic_cast(config.option("nozzle_diameter")); - if (nozzle_diameter != nullptr) - // Loaded the FFF Printer settings. Verify, that all extruder dependent values have enough values. - config.set_num_extruders((unsigned int)nozzle_diameter->values.size()); - if (config.option("filament_diameter") != nullptr) { - // This config contains single or multiple filament presets. - // Ensure that the filament preset vector options contain the correct number of values. - size_t n = (nozzle_diameter == nullptr) ? 1 : nozzle_diameter->values.size(); - const auto &defaults = FullPrintConfig::defaults(); - for (const std::string &key : Preset::filament_options()) { - if (key == "compatible_prints" || key == "compatible_printers") - continue; - auto *opt = config.option(key, false); - /*assert(opt != nullptr); - assert(opt->is_vector());*/ - if (opt != nullptr && opt->is_vector()) - static_cast(opt)->resize(n, defaults.option(key)); - } - // The following keys are mandatory for the UI, but they are not part of FullPrintConfig, therefore they are handled separately. - for (const std::string &key : { "filament_settings_id" }) { - auto *opt = config.option(key, false); - assert(opt == nullptr || opt->type() == coStrings); - if (opt != nullptr && opt->type() == coStrings) - static_cast(opt)->values.resize(n, std::string()); - } - } -} - -std::string Preset::remove_invalid_keys(DynamicPrintConfig &config, const DynamicPrintConfig &default_config) -{ - std::string incorrect_keys; - for (const std::string &key : config.keys()) - if (! default_config.has(key)) { - if (incorrect_keys.empty()) - incorrect_keys = key; - else { - incorrect_keys += ", "; - incorrect_keys += key; - } - config.erase(key); - } - return incorrect_keys; -} - -void Preset::save() -{ - this->config.save(this->file); -} - -// Return a label of this preset, consisting of a name and a "(modified)" suffix, if this preset is dirty. -std::string Preset::label() const -{ - return this->name + (this->is_dirty ? g_suffix_modified : ""); -} - -bool is_compatible_with_print(const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_print, const PresetWithVendorProfile &active_printer) -{ - if (preset.vendor != nullptr && preset.vendor != active_printer.vendor) - // The current profile has a vendor assigned and it is different from the active print's vendor. - return false; - auto &condition = preset.preset.compatible_prints_condition(); - auto *compatible_prints = dynamic_cast(preset.preset.config.option("compatible_prints")); - bool has_compatible_prints = compatible_prints != nullptr && ! compatible_prints->values.empty(); - if (! has_compatible_prints && ! condition.empty()) { - try { - return PlaceholderParser::evaluate_boolean_expression(condition, active_print.preset.config); - } catch (const std::runtime_error &err) { - //FIXME in case of an error, return "compatible with everything". - printf("Preset::is_compatible_with_print - parsing error of compatible_prints_condition %s:\n%s\n", active_print.preset.name.c_str(), err.what()); - return true; - } - } - return preset.preset.is_default || active_print.preset.name.empty() || ! has_compatible_prints || - std::find(compatible_prints->values.begin(), compatible_prints->values.end(), active_print.preset.name) != - compatible_prints->values.end(); -} - -bool is_compatible_with_printer(const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_printer, const DynamicPrintConfig *extra_config) -{ - if (preset.vendor != nullptr && preset.vendor != active_printer.vendor) - // The current profile has a vendor assigned and it is different from the active print's vendor. - return false; - auto &condition = preset.preset.compatible_printers_condition(); - auto *compatible_printers = dynamic_cast(preset.preset.config.option("compatible_printers")); - bool has_compatible_printers = compatible_printers != nullptr && ! compatible_printers->values.empty(); - if (! has_compatible_printers && ! condition.empty()) { - try { - return PlaceholderParser::evaluate_boolean_expression(condition, active_printer.preset.config, extra_config); - } catch (const std::runtime_error &err) { - //FIXME in case of an error, return "compatible with everything". - printf("Preset::is_compatible_with_printer - parsing error of compatible_printers_condition %s:\n%s\n", active_printer.preset.name.c_str(), err.what()); - return true; - } - } - return preset.preset.is_default || active_printer.preset.name.empty() || ! has_compatible_printers || - std::find(compatible_printers->values.begin(), compatible_printers->values.end(), active_printer.preset.name) != - compatible_printers->values.end(); -} - -bool is_compatible_with_printer(const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_printer) -{ - DynamicPrintConfig config; - config.set_key_value("printer_preset", new ConfigOptionString(active_printer.preset.name)); - const ConfigOption *opt = active_printer.preset.config.option("nozzle_diameter"); - if (opt) - config.set_key_value("num_extruders", new ConfigOptionInt((int)static_cast(opt)->values.size())); - return is_compatible_with_printer(preset, active_printer, &config); -} - -void Preset::set_visible_from_appconfig(const AppConfig &app_config) -{ - if (vendor == nullptr) { return; } - - if (type == TYPE_PRINTER) { - const std::string &model = config.opt_string("printer_model"); - const std::string &variant = config.opt_string("printer_variant"); - if (model.empty() || variant.empty()) - return; - is_visible = app_config.get_variant(vendor->id, model, variant); - } else if (type == TYPE_FILAMENT || type == TYPE_SLA_MATERIAL) { - const std::string §ion_name = (type == TYPE_FILAMENT) ? AppConfig::SECTION_FILAMENTS : AppConfig::SECTION_MATERIALS; - if (app_config.has_section(section_name)) { - // Check whether this profile is marked as "installed" in PrusaSlicer.ini, - // or whether a profile is marked as "installed", which this profile may have been renamed from. - const std::map &installed = app_config.get_section(section_name); - auto has = [&installed](const std::string &name) { - auto it = installed.find(name); - return it != installed.end() && ! it->second.empty(); - }; - is_visible = has(this->name); - for (auto it = this->renamed_from.begin(); ! is_visible && it != this->renamed_from.end(); ++ it) - is_visible = has(*it); - } - } -} - -const std::vector& Preset::print_options() -{ - static std::vector s_opts { - "layer_height", "first_layer_height", "perimeters", "spiral_vase", "slice_closing_radius", - "top_solid_layers", "top_solid_min_thickness", "bottom_solid_layers", "bottom_solid_min_thickness", - "extra_perimeters", "ensure_vertical_shell_thickness", "avoid_crossing_perimeters", "thin_walls", "overhangs", - "seam_position", "external_perimeters_first", "fill_density", "fill_pattern", "top_fill_pattern", "bottom_fill_pattern", - "infill_every_layers", "infill_only_where_needed", "solid_infill_every_layers", "fill_angle", "bridge_angle", - "solid_infill_below_area", "only_retract_when_crossing_perimeters", "infill_first", - "ironing", "ironing_type", "ironing_flowrate", "ironing_speed", "ironing_spacing", - "max_print_speed", "max_volumetric_speed", -#ifdef HAS_PRESSURE_EQUALIZER - "max_volumetric_extrusion_rate_slope_positive", "max_volumetric_extrusion_rate_slope_negative", -#endif /* HAS_PRESSURE_EQUALIZER */ - "perimeter_speed", "small_perimeter_speed", "external_perimeter_speed", "infill_speed", "solid_infill_speed", - "top_solid_infill_speed", "support_material_speed", "support_material_xy_spacing", "support_material_interface_speed", - "bridge_speed", "gap_fill_speed", "travel_speed", "first_layer_speed", "perimeter_acceleration", "infill_acceleration", - "bridge_acceleration", "first_layer_acceleration", "default_acceleration", "skirts", "skirt_distance", "skirt_height", "draft_shield", - "min_skirt_length", "brim_width", "support_material", "support_material_auto", "support_material_threshold", "support_material_enforce_layers", - "raft_layers", "support_material_pattern", "support_material_with_sheath", "support_material_spacing", - "support_material_synchronize_layers", "support_material_angle", "support_material_interface_layers", - "support_material_interface_spacing", "support_material_interface_contact_loops", "support_material_contact_distance", - "support_material_buildplate_only", "dont_support_bridges", "notes", "complete_objects", "extruder_clearance_radius", - "extruder_clearance_height", "gcode_comments", "gcode_label_objects", "output_filename_format", "post_process", "perimeter_extruder", - "infill_extruder", "solid_infill_extruder", "support_material_extruder", "support_material_interface_extruder", - "ooze_prevention", "standby_temperature_delta", "interface_shells", "extrusion_width", "first_layer_extrusion_width", - "perimeter_extrusion_width", "external_perimeter_extrusion_width", "infill_extrusion_width", "solid_infill_extrusion_width", - "top_infill_extrusion_width", "support_material_extrusion_width", "infill_overlap", "bridge_flow_ratio", "clip_multipart_objects", - "elefant_foot_compensation", "xy_size_compensation", "threads", "resolution", "wipe_tower", "wipe_tower_x", "wipe_tower_y", - "wipe_tower_width", "wipe_tower_rotation_angle", "wipe_tower_bridging", "single_extruder_multi_material_priming", - "wipe_tower_no_sparse_layers", "compatible_printers", "compatible_printers_condition", "inherits" - }; - return s_opts; -} - -const std::vector& Preset::filament_options() -{ - static std::vector s_opts { - "filament_colour", "filament_diameter", "filament_type", "filament_soluble", "filament_notes", "filament_max_volumetric_speed", - "extrusion_multiplier", "filament_density", "filament_cost", "filament_loading_speed", "filament_loading_speed_start", "filament_load_time", - "filament_unloading_speed", "filament_unloading_speed_start", "filament_unload_time", "filament_toolchange_delay", "filament_cooling_moves", - "filament_cooling_initial_speed", "filament_cooling_final_speed", "filament_ramming_parameters", "filament_minimal_purge_on_wipe_tower", - "temperature", "first_layer_temperature", "bed_temperature", "first_layer_bed_temperature", "fan_always_on", "cooling", "min_fan_speed", - "max_fan_speed", "bridge_fan_speed", "disable_fan_first_layers", "fan_below_layer_time", "slowdown_below_layer_time", "min_print_speed", - "start_filament_gcode", "end_filament_gcode", - // Retract overrides - "filament_retract_length", "filament_retract_lift", "filament_retract_lift_above", "filament_retract_lift_below", "filament_retract_speed", "filament_deretract_speed", "filament_retract_restart_extra", "filament_retract_before_travel", - "filament_retract_layer_change", "filament_wipe", "filament_retract_before_wipe", - // Profile compatibility - "filament_vendor", "compatible_prints", "compatible_prints_condition", "compatible_printers", "compatible_printers_condition", "inherits" - }; - return s_opts; -} - -const std::vector& Preset::printer_options() -{ - static std::vector s_opts; - if (s_opts.empty()) { - s_opts = { - "printer_technology", - "bed_shape", "bed_custom_texture", "bed_custom_model", "z_offset", "gcode_flavor", "use_relative_e_distances", "serial_port", "serial_speed", - "use_firmware_retraction", "use_volumetric_e", "variable_layer_height", - "host_type", "print_host", "printhost_apikey", "printhost_cafile", - "single_extruder_multi_material", "start_gcode", "end_gcode", "before_layer_gcode", "layer_gcode", "toolchange_gcode", - "color_change_gcode", "pause_print_gcode", "template_custom_gcode", - "between_objects_gcode", "printer_vendor", "printer_model", "printer_variant", "printer_notes", "cooling_tube_retraction", - "cooling_tube_length", "high_current_on_filament_swap", "parking_pos_retraction", "extra_loading_move", "max_print_height", - "default_print_profile", "inherits", - "remaining_times", "silent_mode", "machine_max_acceleration_extruding", "machine_max_acceleration_retracting", - "machine_max_acceleration_x", "machine_max_acceleration_y", "machine_max_acceleration_z", "machine_max_acceleration_e", - "machine_max_feedrate_x", "machine_max_feedrate_y", "machine_max_feedrate_z", "machine_max_feedrate_e", - "machine_min_extruding_rate", "machine_min_travel_rate", - "machine_max_jerk_x", "machine_max_jerk_y", "machine_max_jerk_z", "machine_max_jerk_e", - "thumbnails" - }; - s_opts.insert(s_opts.end(), Preset::nozzle_options().begin(), Preset::nozzle_options().end()); - } - return s_opts; -} - -// The following nozzle options of a printer profile will be adjusted to match the size -// of the nozzle_diameter vector. -const std::vector& Preset::nozzle_options() -{ - return print_config_def.extruder_option_keys(); -} - -const std::vector& Preset::sla_print_options() -{ - static std::vector s_opts; - if (s_opts.empty()) { - s_opts = { - "layer_height", - "faded_layers", - "supports_enable", - "support_head_front_diameter", - "support_head_penetration", - "support_head_width", - "support_pillar_diameter", - "support_max_bridges_on_pillar", - "support_pillar_connection_mode", - "support_buildplate_only", - "support_pillar_widening_factor", - "support_base_diameter", - "support_base_height", - "support_base_safety_distance", - "support_critical_angle", - "support_max_bridge_length", - "support_max_pillar_link_distance", - "support_object_elevation", - "support_points_density_relative", - "support_points_minimal_distance", - "slice_closing_radius", - "pad_enable", - "pad_wall_thickness", - "pad_wall_height", - "pad_brim_size", - "pad_max_merge_distance", - // "pad_edge_radius", - "pad_wall_slope", - "pad_object_gap", - "pad_around_object", - "pad_around_object_everywhere", - "pad_object_connector_stride", - "pad_object_connector_width", - "pad_object_connector_penetration", - "hollowing_enable", - "hollowing_min_thickness", - "hollowing_quality", - "hollowing_closing_distance", - "output_filename_format", - "default_sla_print_profile", - "compatible_printers", - "compatible_printers_condition", - "inherits" - }; - } - return s_opts; -} - -const std::vector& Preset::sla_material_options() -{ - static std::vector s_opts; - if (s_opts.empty()) { - s_opts = { - "material_type", - "initial_layer_height", - "bottle_cost", - "bottle_volume", - "bottle_weight", - "material_density", - "exposure_time", - "initial_exposure_time", - "material_correction", - "material_notes", - "material_vendor", - "default_sla_material_profile", - "compatible_prints", "compatible_prints_condition", - "compatible_printers", "compatible_printers_condition", "inherits" - }; - } - return s_opts; -} - -const std::vector& Preset::sla_printer_options() -{ - static std::vector s_opts; - if (s_opts.empty()) { - s_opts = { - "printer_technology", - "bed_shape", "bed_custom_texture", "bed_custom_model", "max_print_height", - "display_width", "display_height", "display_pixels_x", "display_pixels_y", - "display_mirror_x", "display_mirror_y", - "display_orientation", - "fast_tilt_time", "slow_tilt_time", "area_fill", - "relative_correction", - "absolute_correction", - "elefant_foot_compensation", - "elefant_foot_min_width", - "gamma_correction", - "min_exposure_time", "max_exposure_time", - "min_initial_exposure_time", "max_initial_exposure_time", - "print_host", "printhost_apikey", "printhost_cafile", - "printer_notes", - "inherits" - }; - } - return s_opts; -} - -PresetCollection::PresetCollection(Preset::Type type, const std::vector &keys, const Slic3r::StaticPrintConfig &defaults, const std::string &default_name) : - m_type(type), - m_edited_preset(type, "", false), - m_idx_selected(0) -{ - // Insert just the default preset. - this->add_default_preset(keys, defaults, default_name); - m_edited_preset.config.apply(m_presets.front().config); -} - -PresetCollection::~PresetCollection() -{ -} - -void PresetCollection::reset(bool delete_files) -{ - if (m_presets.size() > m_num_default_presets) { - if (delete_files) { - // Erase the preset files. - for (Preset &preset : m_presets) - if (! preset.is_default && ! preset.is_external && ! preset.is_system) - boost::nowide::remove(preset.file.c_str()); - } - // Don't use m_presets.resize() here as it requires a default constructor for Preset. - m_presets.erase(m_presets.begin() + m_num_default_presets, m_presets.end()); - this->select_preset(0); - } - m_map_alias_to_profile_name.clear(); - m_map_system_profile_renamed.clear(); -} - -void PresetCollection::add_default_preset(const std::vector &keys, const Slic3r::StaticPrintConfig &defaults, const std::string &preset_name) -{ - // Insert just the default preset. - m_presets.emplace_back(Preset(this->type(), preset_name, true)); - m_presets.back().config.apply_only(defaults, keys.empty() ? defaults.keys() : keys); - m_presets.back().loaded = true; - ++ m_num_default_presets; -} - -// Load all presets found in dir_path. -// Throws an exception on error. -void PresetCollection::load_presets(const std::string &dir_path, const std::string &subdir) -{ - boost::filesystem::path dir = boost::filesystem::canonical(boost::filesystem::path(dir_path) / subdir).make_preferred(); - m_dir_path = dir.string(); - std::string errors_cummulative; - // Store the loaded presets into a new vector, otherwise the binary search for already existing presets would be broken. - // (see the "Preset already present, not loading" message). - std::deque presets_loaded; - for (auto &dir_entry : boost::filesystem::directory_iterator(dir)) - if (Slic3r::is_ini_file(dir_entry)) { - std::string name = dir_entry.path().filename().string(); - // Remove the .ini suffix. - name.erase(name.size() - 4); - if (this->find_preset(name, false)) { - // This happens when there's is a preset (most likely legacy one) with the same name as a system preset - // that's already been loaded from a bundle. - BOOST_LOG_TRIVIAL(warning) << "Preset already present, not loading: " << name; - continue; - } - try { - Preset preset(m_type, name, false); - preset.file = dir_entry.path().string(); - // Load the preset file, apply preset values on top of defaults. - try { - DynamicPrintConfig config; - config.load_from_ini(preset.file); - // Find a default preset for the config. The PrintPresetCollection provides different default preset based on the "printer_technology" field. - const Preset &default_preset = this->default_preset_for(config); - preset.config = default_preset.config; - preset.config.apply(std::move(config)); - Preset::normalize(preset.config); - // Report configuration fields, which are misplaced into a wrong group. - std::string incorrect_keys = Preset::remove_invalid_keys(config, default_preset.config); - if (! incorrect_keys.empty()) - BOOST_LOG_TRIVIAL(error) << "Error in a preset file: The preset \"" << - preset.file << "\" contains the following incorrect keys: " << incorrect_keys << ", which were removed"; - preset.loaded = true; - } catch (const std::ifstream::failure &err) { - throw std::runtime_error(std::string("The selected preset cannot be loaded: ") + preset.file + "\n\tReason: " + err.what()); - } catch (const std::runtime_error &err) { - throw std::runtime_error(std::string("Failed loading the preset file: ") + preset.file + "\n\tReason: " + err.what()); - } - presets_loaded.emplace_back(preset); - } catch (const std::runtime_error &err) { - errors_cummulative += err.what(); - errors_cummulative += "\n"; - } - } - m_presets.insert(m_presets.end(), std::make_move_iterator(presets_loaded.begin()), std::make_move_iterator(presets_loaded.end())); - std::sort(m_presets.begin() + m_num_default_presets, m_presets.end()); - this->select_preset(first_visible_idx()); - if (! errors_cummulative.empty()) - throw std::runtime_error(errors_cummulative); -} - -// Load a preset from an already parsed config file, insert it into the sorted sequence of presets -// and select it, losing previous modifications. -Preset& PresetCollection::load_preset(const std::string &path, const std::string &name, const DynamicPrintConfig &config, bool select) -{ - DynamicPrintConfig cfg(this->default_preset().config); - cfg.apply_only(config, cfg.keys(), true); - return this->load_preset(path, name, std::move(cfg), select); -} - -enum class ProfileHostParams -{ - Same, - Different, - Anonymized, -}; - -static ProfileHostParams profile_host_params_same_or_anonymized(const DynamicPrintConfig &cfg_old, const DynamicPrintConfig &cfg_new) -{ - auto opt_print_host_old = cfg_old.option("print_host"); - auto opt_printhost_apikey_old = cfg_old.option("printhost_apikey"); - auto opt_printhost_cafile_old = cfg_old.option("printhost_cafile"); - - auto opt_print_host_new = cfg_new.option("print_host"); - auto opt_printhost_apikey_new = cfg_new.option("printhost_apikey"); - auto opt_printhost_cafile_new = cfg_new.option("printhost_cafile"); - - // If the new print host data is undefined, use the old data. - bool new_print_host_undefined = (opt_print_host_new == nullptr || opt_print_host_new ->empty()) && - (opt_printhost_apikey_new == nullptr || opt_printhost_apikey_new ->empty()) && - (opt_printhost_cafile_new == nullptr || opt_printhost_cafile_new ->empty()); - if (new_print_host_undefined) - return ProfileHostParams::Anonymized; - - auto opt_same = [](const ConfigOptionString *l, const ConfigOptionString *r) { - return ((l == nullptr || l->empty()) && (r == nullptr || r->empty())) || - (l != nullptr && r != nullptr && l->value == r->value); - }; - return (opt_same(opt_print_host_old, opt_print_host_new) && opt_same(opt_printhost_apikey_old, opt_printhost_apikey_new) && - opt_same(opt_printhost_cafile_old, opt_printhost_cafile_new)) ? ProfileHostParams::Same : ProfileHostParams::Different; -} - -static bool profile_print_params_same(const DynamicPrintConfig &cfg_old, const DynamicPrintConfig &cfg_new) -{ - t_config_option_keys diff = cfg_old.diff(cfg_new); - // Following keys are used by the UI, not by the slicing core, therefore they are not important - // when comparing profiles for equality. Ignore them. - for (const char *key : { "compatible_prints", "compatible_prints_condition", - "compatible_printers", "compatible_printers_condition", "inherits", - "print_settings_id", "filament_settings_id", "sla_print_settings_id", "sla_material_settings_id", "printer_settings_id", - "printer_model", "printer_variant", "default_print_profile", "default_filament_profile", "default_sla_print_profile", "default_sla_material_profile", - "print_host", "printhost_apikey", "printhost_cafile" }) - diff.erase(std::remove(diff.begin(), diff.end(), key), diff.end()); - // Preset with the same name as stored inside the config exists. - return diff.empty() && profile_host_params_same_or_anonymized(cfg_old, cfg_new) != ProfileHostParams::Different; -} - -// Load a preset from an already parsed config file, insert it into the sorted sequence of presets -// and select it, losing previous modifications. -// In case -Preset& PresetCollection::load_external_preset( - // Path to the profile source file (a G-code, an AMF or 3MF file, a config file) - const std::string &path, - // Name of the profile, derived from the source file name. - const std::string &name, - // Original name of the profile, extracted from the loaded config. Empty, if the name has not been stored. - const std::string &original_name, - // Config to initialize the preset from. - const DynamicPrintConfig &config, - // Select the preset after loading? - bool select) -{ - // Load the preset over a default preset, so that the missing fields are filled in from the default preset. - DynamicPrintConfig cfg(this->default_preset_for(config).config); - cfg.apply_only(config, cfg.keys(), true); - // Is there a preset already loaded with the name stored inside the config? - std::deque::iterator it = this->find_preset_internal(original_name); - bool found = it != m_presets.end() && it->name == original_name; - if (! found) { - // Try to match the original_name against the "renamed_from" profile names of loaded system profiles. - it = this->find_preset_renamed(original_name); - found = it != m_presets.end(); - } - if (found) { - if (profile_print_params_same(it->config, cfg)) { - // The preset exists and it matches the values stored inside config. - if (select) - this->select_preset(it - m_presets.begin()); - return *it; - } - if (profile_host_params_same_or_anonymized(it->config, cfg) == ProfileHostParams::Anonymized) { - // The project being loaded is anonymized. Replace the empty host keys of the loaded profile with the data from the original profile. - // See "Octoprint Settings when Opening a .3MF file" GH issue #3244 - auto opt_update = [it, &cfg](const std::string &opt_key) { - auto opt = it->config.option(opt_key); - if (opt != nullptr) - cfg.set_key_value(opt_key, opt->clone()); - }; - opt_update("print_host"); - opt_update("printhost_apikey"); - opt_update("printhost_cafile"); - } - } - // Update the "inherits" field. - std::string &inherits = Preset::inherits(cfg); - if (found && inherits.empty()) { - // There is a profile with the same name already loaded. Should we update the "inherits" field? - if (it->vendor == nullptr) - inherits = it->inherits(); - else - inherits = it->name; - } - // The external preset does not match an internal preset, load the external preset. - std::string new_name; - for (size_t idx = 0;; ++ idx) { - std::string suffix; - if (original_name.empty()) { - if (idx > 0) - suffix = " (" + std::to_string(idx) + ")"; - } else { - if (idx == 0) - suffix = " (" + original_name + ")"; - else - suffix = " (" + original_name + "-" + std::to_string(idx) + ")"; - } - new_name = name + suffix; - it = this->find_preset_internal(new_name); - if (it == m_presets.end() || it->name != new_name) - // Unique profile name. Insert a new profile. - break; - if (profile_print_params_same(it->config, cfg)) { - // The preset exists and it matches the values stored inside config. - if (select) - this->select_preset(it - m_presets.begin()); - return *it; - } - // Form another profile name. - } - // Insert a new profile. - Preset &preset = this->load_preset(path, new_name, std::move(cfg), select); - preset.is_external = true; - if (&this->get_selected_preset() == &preset) - this->get_edited_preset().is_external = true; - - return preset; -} - -Preset& PresetCollection::load_preset(const std::string &path, const std::string &name, DynamicPrintConfig &&config, bool select) -{ - auto it = this->find_preset_internal(name); - if (it == m_presets.end() || it->name != name) { - // The preset was not found. Create a new preset. - it = m_presets.emplace(it, Preset(m_type, name, false)); - } - Preset &preset = *it; - preset.file = path; - preset.config = std::move(config); - preset.loaded = true; - preset.is_dirty = false; - if (select) - this->select_preset_by_name(name, true); - return preset; -} - -void PresetCollection::save_current_preset(const std::string &new_name, bool detach) -{ - // 1) Find the preset with a new_name or create a new one, - // initialize it with the edited config. - auto it = this->find_preset_internal(new_name); - if (it != m_presets.end() && it->name == new_name) { - // Preset with the same name found. - Preset &preset = *it; - if (preset.is_default || preset.is_external || preset.is_system) - // Cannot overwrite the default preset. - return; - // Overwriting an existing preset. - preset.config = std::move(m_edited_preset.config); - // The newly saved preset will be activated -> make it visible. - preset.is_visible = true; - if (detach) { - // Clear the link to the parent profile. - preset.vendor = nullptr; - preset.inherits().clear(); - preset.alias.clear(); - preset.renamed_from.clear(); - } - } else { - // Creating a new preset. - Preset &preset = *m_presets.insert(it, m_edited_preset); - std::string &inherits = preset.inherits(); - std::string old_name = preset.name; - preset.name = new_name; - preset.file = this->path_from_name(new_name); - preset.vendor = nullptr; - preset.alias.clear(); - preset.renamed_from.clear(); - if (detach) { - // Clear the link to the parent profile. - inherits.clear(); - } else if (preset.is_system) { - // Inheriting from a system preset. - inherits = /* preset.vendor->name + "/" + */ old_name; - } else if (inherits.empty()) { - // Inheriting from a user preset. Link the new preset to the old preset. - // inherits = old_name; - } else { - // Inherited from a user preset. Just maintain the "inherited" flag, - // meaning it will inherit from either the system preset, or the inherited user preset. - } - preset.is_default = false; - preset.is_system = false; - preset.is_external = false; - // The newly saved preset will be activated -> make it visible. - preset.is_visible = true; - // Just system presets have aliases - preset.alias.clear(); - } - // 2) Activate the saved preset. - this->select_preset_by_name(new_name, true); - // 2) Store the active preset to disk. - this->get_selected_preset().save(); -} - -bool PresetCollection::delete_current_preset() -{ - const Preset &selected = this->get_selected_preset(); - if (selected.is_default) - return false; - if (! selected.is_external && ! selected.is_system) { - // Erase the preset file. - boost::nowide::remove(selected.file.c_str()); - } - // Remove the preset from the list. - m_presets.erase(m_presets.begin() + m_idx_selected); - // Find the next visible preset. - size_t new_selected_idx = m_idx_selected; - if (new_selected_idx < m_presets.size()) - for (; new_selected_idx < m_presets.size() && ! m_presets[new_selected_idx].is_visible; ++ new_selected_idx) ; - if (new_selected_idx == m_presets.size()) - for (--new_selected_idx; new_selected_idx > 0 && !m_presets[new_selected_idx].is_visible; --new_selected_idx); - this->select_preset(new_selected_idx); - return true; -} - -bool PresetCollection::delete_preset(const std::string& name) -{ - auto it = this->find_preset_internal(name); - - const Preset& preset = *it; - if (preset.is_default) - return false; - if (!preset.is_external && !preset.is_system) { - // Erase the preset file. - boost::nowide::remove(preset.file.c_str()); - } - m_presets.erase(it); - return true; -} - -const Preset* PresetCollection::get_selected_preset_parent() const -{ - if (this->get_selected_idx() == size_t(-1)) - // This preset collection has no preset activated yet. Only the get_edited_preset() is valid. - return nullptr; - - const Preset &selected_preset = this->get_selected_preset(); - if (selected_preset.is_system || selected_preset.is_default) - return &selected_preset; - - const Preset &edited_preset = this->get_edited_preset(); - const std::string &inherits = edited_preset.inherits(); - const Preset *preset = nullptr; - if (inherits.empty()) { - if (selected_preset.is_external) - return nullptr; - preset = &this->default_preset(m_type == Preset::Type::TYPE_PRINTER && edited_preset.printer_technology() == ptSLA ? 1 : 0); - } else - preset = this->find_preset(inherits, false); - if (preset == nullptr) { - // Resolve the "renamed_from" field. - assert(! inherits.empty()); - auto it = this->find_preset_renamed(inherits); - if (it != m_presets.end()) - preset = &(*it); - } - return (preset == nullptr/* || preset->is_default*/ || preset->is_external) ? nullptr : preset; -} - -const Preset* PresetCollection::get_preset_parent(const Preset& child) const -{ - const std::string &inherits = child.inherits(); - if (inherits.empty()) -// return this->get_selected_preset().is_system ? &this->get_selected_preset() : nullptr; - return nullptr; - const Preset* preset = this->find_preset(inherits, false); - if (preset == nullptr) { - auto it = this->find_preset_renamed(inherits); - if (it != m_presets.end()) - preset = &(*it); - } - return (preset == nullptr/* || preset->is_default */|| preset->is_external) ? nullptr : preset; -} - -// Return vendor of the first parent profile, for which the vendor is defined, or null if such profile does not exist. -PresetWithVendorProfile PresetCollection::get_preset_with_vendor_profile(const Preset &preset) const -{ - const Preset *p = &preset; - const VendorProfile *v = nullptr; - do { - if (p->vendor != nullptr) { - v = p->vendor; - break; - } - p = this->get_preset_parent(*p); - } while (p != nullptr); - return PresetWithVendorProfile(preset, v); -} - -const std::string& PresetCollection::get_preset_name_by_alias(const std::string& alias) const -{ - for ( - // Find the 1st profile name with the alias. - auto it = Slic3r::lower_bound_by_predicate(m_map_alias_to_profile_name.begin(), m_map_alias_to_profile_name.end(), [&alias](auto &l){ return l.first < alias; }); - // Continue over all profile names with the same alias. - it != m_map_alias_to_profile_name.end() && it->first == alias; ++ it) - if (auto it_preset = this->find_preset_internal(it->second); - it_preset != m_presets.end() && it_preset->name == it->second && - it_preset->is_visible && (it_preset->is_compatible || size_t(it_preset - m_presets.begin()) == m_idx_selected)) - return it_preset->name; - return alias; -} - -const std::string* PresetCollection::get_preset_name_renamed(const std::string &old_name) const -{ - auto it_renamed = m_map_system_profile_renamed.find(old_name); - if (it_renamed != m_map_system_profile_renamed.end()) - return &it_renamed->second; - return nullptr; -} - -const std::string& PresetCollection::get_suffix_modified() { - return g_suffix_modified; -} - -// Return a preset by its name. If the preset is active, a temporary copy is returned. -// If a preset is not found by its name, null is returned. -Preset* PresetCollection::find_preset(const std::string &name, bool first_visible_if_not_found) -{ - Preset key(m_type, name, false); - auto it = this->find_preset_internal(name); - // Ensure that a temporary copy is returned if the preset found is currently selected. - return (it != m_presets.end() && it->name == key.name) ? &this->preset(it - m_presets.begin()) : - first_visible_if_not_found ? &this->first_visible() : nullptr; -} - -// Return index of the first visible preset. Certainly at least the '- default -' preset shall be visible. -size_t PresetCollection::first_visible_idx() const -{ - size_t idx = m_default_suppressed ? m_num_default_presets : 0; - for (; idx < this->m_presets.size(); ++ idx) - if (m_presets[idx].is_visible) - break; - if (idx == m_presets.size()) - idx = 0; - return idx; -} - -void PresetCollection::set_default_suppressed(bool default_suppressed) -{ - if (m_default_suppressed != default_suppressed) { - m_default_suppressed = default_suppressed; - bool default_visible = ! default_suppressed || m_idx_selected < m_num_default_presets; - for (size_t i = 0; i < m_num_default_presets; ++ i) - m_presets[i].is_visible = default_visible; - } -} - -size_t PresetCollection::update_compatible_internal(const PresetWithVendorProfile &active_printer, const PresetWithVendorProfile *active_print, PresetSelectCompatibleType unselect_if_incompatible) -{ - DynamicPrintConfig config; - config.set_key_value("printer_preset", new ConfigOptionString(active_printer.preset.name)); - const ConfigOption *opt = active_printer.preset.config.option("nozzle_diameter"); - if (opt) - config.set_key_value("num_extruders", new ConfigOptionInt((int)static_cast(opt)->values.size())); - bool some_compatible = false; - for (size_t idx_preset = m_num_default_presets; idx_preset < m_presets.size(); ++ idx_preset) { - bool selected = idx_preset == m_idx_selected; - Preset &preset_selected = m_presets[idx_preset]; - Preset &preset_edited = selected ? m_edited_preset : preset_selected; - - const PresetWithVendorProfile this_preset_with_vendor_profile = this->get_preset_with_vendor_profile(preset_edited); - bool was_compatible = preset_edited.is_compatible; - preset_edited.is_compatible = is_compatible_with_printer(this_preset_with_vendor_profile, active_printer, &config); - some_compatible |= preset_edited.is_compatible; - if (active_print != nullptr) - preset_edited.is_compatible &= is_compatible_with_print(this_preset_with_vendor_profile, *active_print, active_printer); - if (! preset_edited.is_compatible && selected && - (unselect_if_incompatible == PresetSelectCompatibleType::Always || (unselect_if_incompatible == PresetSelectCompatibleType::OnlyIfWasCompatible && was_compatible))) - m_idx_selected = size_t(-1); - if (selected) - preset_selected.is_compatible = preset_edited.is_compatible; - } - // Update visibility of the default profiles here if the defaults are suppressed, the current profile is not compatible and we don't want to select another compatible profile. - if (m_idx_selected >= m_num_default_presets && m_default_suppressed) - for (size_t i = 0; i < m_num_default_presets; ++ i) - m_presets[i].is_visible = ! some_compatible; - return m_idx_selected; -} - -// Save the preset under a new name. If the name is different from the old one, -// a new preset is stored into the list of presets. -// All presets are marked as not modified and the new preset is activated. -//void PresetCollection::save_current_preset(const std::string &new_name); - -// Delete the current preset, activate the first visible preset. -//void PresetCollection::delete_current_preset(); - -// Update a dirty flag of the current preset -// Return true if the dirty flag changed. -bool PresetCollection::update_dirty() -{ - bool was_dirty = this->get_selected_preset().is_dirty; - bool is_dirty = current_is_dirty(); - this->get_selected_preset().is_dirty = is_dirty; - this->get_edited_preset().is_dirty = is_dirty; - - return was_dirty != is_dirty; -} - -template -void add_correct_opts_to_diff(const std::string &opt_key, t_config_option_keys& vec, const ConfigBase &other, const ConfigBase &this_c) -{ - const T* opt_init = static_cast(other.option(opt_key)); - const T* opt_cur = static_cast(this_c.option(opt_key)); - int opt_init_max_id = opt_init->values.size() - 1; - for (int i = 0; i < int(opt_cur->values.size()); i++) - { - int init_id = i <= opt_init_max_id ? i : 0; - if (opt_cur->values[i] != opt_init->values[init_id]) - vec.emplace_back(opt_key + "#" + std::to_string(i)); - } -} - -// Use deep_diff to correct return of changed options, considering individual options for each extruder. -inline t_config_option_keys deep_diff(const ConfigBase &config_this, const ConfigBase &config_other) -{ - t_config_option_keys diff; - for (const t_config_option_key &opt_key : config_this.keys()) { - const ConfigOption *this_opt = config_this.option(opt_key); - const ConfigOption *other_opt = config_other.option(opt_key); - if (this_opt != nullptr && other_opt != nullptr && *this_opt != *other_opt) - { - if (opt_key == "bed_shape" || opt_key == "thumbnails" || opt_key == "compatible_prints" || opt_key == "compatible_printers") { - diff.emplace_back(opt_key); - continue; - } - switch (other_opt->type()) - { - case coInts: add_correct_opts_to_diff(opt_key, diff, config_other, config_this); break; - case coBools: add_correct_opts_to_diff(opt_key, diff, config_other, config_this); break; - case coFloats: add_correct_opts_to_diff(opt_key, diff, config_other, config_this); break; - case coStrings: add_correct_opts_to_diff(opt_key, diff, config_other, config_this); break; - case coPercents:add_correct_opts_to_diff(opt_key, diff, config_other, config_this); break; - case coPoints: add_correct_opts_to_diff(opt_key, diff, config_other, config_this); break; - default: diff.emplace_back(opt_key); break; - } - } - } - return diff; -} - -std::vector PresetCollection::dirty_options(const Preset *edited, const Preset *reference, const bool deep_compare /*= false*/) -{ - std::vector changed; - if (edited != nullptr && reference != nullptr) { - changed = deep_compare ? - deep_diff(edited->config, reference->config) : - reference->config.diff(edited->config); - // The "compatible_printers" option key is handled differently from the others: - // It is not mandatory. If the key is missing, it means it is compatible with any printer. - // If the key exists and it is empty, it means it is compatible with no printer. - std::initializer_list optional_keys { "compatible_prints", "compatible_printers" }; - for (auto &opt_key : optional_keys) { - if (reference->config.has(opt_key) != edited->config.has(opt_key)) - changed.emplace_back(opt_key); - } - } - return changed; -} - -// Select a new preset. This resets all the edits done to the currently selected preset. -// If the preset with index idx does not exist, a first visible preset is selected. -Preset& PresetCollection::select_preset(size_t idx) -{ - for (Preset &preset : m_presets) - preset.is_dirty = false; - if (idx >= m_presets.size()) - idx = first_visible_idx(); - m_idx_selected = idx; - m_edited_preset = m_presets[idx]; - bool default_visible = ! m_default_suppressed || m_idx_selected < m_num_default_presets; - for (size_t i = 0; i < m_num_default_presets; ++i) - m_presets[i].is_visible = default_visible; - return m_presets[idx]; -} - -bool PresetCollection::select_preset_by_name(const std::string &name_w_suffix, bool force) -{ - std::string name = Preset::remove_suffix_modified(name_w_suffix); - // 1) Try to find the preset by its name. - auto it = this->find_preset_internal(name); - size_t idx = 0; - if (it != m_presets.end() && it->name == name && it->is_visible) - // Preset found by its name and it is visible. - idx = it - m_presets.begin(); - else { - // Find the first visible preset. - for (size_t i = m_default_suppressed ? m_num_default_presets : 0; i < m_presets.size(); ++ i) - if (m_presets[i].is_visible) { - idx = i; - break; - } - // If the first visible preset was not found, return the 0th element, which is the default preset. - } - - // 2) Select the new preset. - if (m_idx_selected != idx || force) { - this->select_preset(idx); - return true; - } - - return false; -} - -bool PresetCollection::select_preset_by_name_strict(const std::string &name) -{ - // 1) Try to find the preset by its name. - auto it = this->find_preset_internal(name); - size_t idx = (size_t)-1; - if (it != m_presets.end() && it->name == name && it->is_visible) - // Preset found by its name. - idx = it - m_presets.begin(); - // 2) Select the new preset. - if (idx != (size_t)-1) { - this->select_preset(idx); - return true; - } - m_idx_selected = idx; - return false; -} - -// Merge one vendor's presets with the other vendor's presets, report duplicates. -std::vector PresetCollection::merge_presets(PresetCollection &&other, const VendorMap &new_vendors) -{ - std::vector duplicates; - for (Preset &preset : other.m_presets) { - if (preset.is_default || preset.is_external) - continue; - Preset key(m_type, preset.name); - auto it = std::lower_bound(m_presets.begin() + m_num_default_presets, m_presets.end(), key); - if (it == m_presets.end() || it->name != preset.name) { - if (preset.vendor != nullptr) { - // Re-assign a pointer to the vendor structure in the new PresetBundle. - auto it = new_vendors.find(preset.vendor->id); - assert(it != new_vendors.end()); - preset.vendor = &it->second; - } - this->m_presets.emplace(it, std::move(preset)); - } else - duplicates.emplace_back(std::move(preset.name)); - } - return duplicates; -} - -void PresetCollection::update_map_alias_to_profile_name() -{ - m_map_alias_to_profile_name.clear(); - for (const Preset &preset : m_presets) - m_map_alias_to_profile_name.emplace_back(preset.alias, preset.name); - std::sort(m_map_alias_to_profile_name.begin(), m_map_alias_to_profile_name.end(), [](auto &l, auto &r) { return l.first < r.first; }); -} - -void PresetCollection::update_map_system_profile_renamed() -{ - m_map_system_profile_renamed.clear(); - for (Preset &preset : m_presets) - for (const std::string &renamed_from : preset.renamed_from) { - const auto [it, success] = m_map_system_profile_renamed.insert(std::pair(renamed_from, preset.name)); - if (! success) - BOOST_LOG_TRIVIAL(error) << boost::format("Preset name \"%1%\" was marked as renamed from \"%2%\", though preset name \"%3%\" was marked as renamed from \"%2%\" as well.") % preset.name % renamed_from % it->second; - } -} - -std::string PresetCollection::name() const -{ - switch (this->type()) { - case Preset::TYPE_PRINT: return L("print"); - case Preset::TYPE_FILAMENT: return L("filament"); - case Preset::TYPE_SLA_PRINT: return L("SLA print"); - case Preset::TYPE_SLA_MATERIAL: return L("SLA material"); - case Preset::TYPE_PRINTER: return L("printer"); - default: return "invalid"; - } -} - -std::string PresetCollection::section_name() const -{ - switch (this->type()) { - case Preset::TYPE_PRINT: return "print"; - case Preset::TYPE_FILAMENT: return "filament"; - case Preset::TYPE_SLA_PRINT: return "sla_print"; - case Preset::TYPE_SLA_MATERIAL: return "sla_material"; - case Preset::TYPE_PRINTER: return "printer"; - default: return "invalid"; - } -} - -std::vector PresetCollection::system_preset_names() const -{ - size_t num = 0; - for (const Preset &preset : m_presets) - if (preset.is_system) - ++ num; - std::vector out; - out.reserve(num); - for (const Preset &preset : m_presets) - if (preset.is_system) - out.emplace_back(preset.name); - std::sort(out.begin(), out.end()); - return out; -} - -// Generate a file path from a profile name. Add the ".ini" suffix if it is missing. -std::string PresetCollection::path_from_name(const std::string &new_name) const -{ - std::string file_name = boost::iends_with(new_name, ".ini") ? new_name : (new_name + ".ini"); - return (boost::filesystem::path(m_dir_path) / file_name).make_preferred().string(); -} - -const Preset& PrinterPresetCollection::default_preset_for(const DynamicPrintConfig &config) const -{ - const ConfigOptionEnumGeneric *opt_printer_technology = config.opt("printer_technology"); - return this->default_preset((opt_printer_technology == nullptr || opt_printer_technology->value == ptFFF) ? 0 : 1); -} - -const Preset* PrinterPresetCollection::find_by_model_id(const std::string &model_id) const -{ - if (model_id.empty()) { return nullptr; } - - const auto it = std::find_if(cbegin(), cend(), [&](const Preset &preset) { - return preset.config.opt_string("printer_model") == model_id; - }); - - return it != cend() ? &*it : nullptr; -} -/* -PhysicalPrinter& PhysicalPrinterCollection::load_external_printer( - // Path to the profile source file (a G-code, an AMF or 3MF file, a config file) - const std::string& path, - // Name of the profile, derived from the source file name. - const std::string& name, - // Original name of the profile, extracted from the loaded config. Empty, if the name has not been stored. - const std::string& original_name, - // Config to initialize the preset from. - const DynamicPrintConfig& config, - // Select the preset after loading? - bool select) -{ - // Load the preset over a default preset, so that the missing fields are filled in from the default preset. - DynamicPrintConfig cfg(this->default_printer().config); - cfg.apply_only(config, cfg.keys(), true); - // Is there a preset already loaded with the name stored inside the config? - std::deque::iterator it = this->find_printer_internal(original_name); - bool found = it != m_printers.end() && it->name == original_name; - if (!found) { - // Try to match the original_name against the "renamed_from" profile names of loaded system profiles. - / * - it = this->find_preset_renamed(original_name); - found = it != m_presets.end(); - * / - } - if (found) { - if (profile_print_params_same(it->config, cfg)) { - // The preset exists and it matches the values stored inside config. - if (select) - this->select_printer(it - m_printers.begin()); - return *it; - } - if (profile_host_params_same_or_anonymized(it->config, cfg) == ProfileHostParams::Anonymized) { - // The project being loaded is anonymized. Replace the empty host keys of the loaded profile with the data from the original profile. - // See "Octoprint Settings when Opening a .3MF file" GH issue #3244 - auto opt_update = [it, &cfg](const std::string& opt_key) { - auto opt = it->config.option(opt_key); - if (opt != nullptr) - cfg.set_key_value(opt_key, opt->clone()); - }; - opt_update("print_host"); - opt_update("printhost_apikey"); - opt_update("printhost_cafile"); - } - } - // The external preset does not match an internal preset, load the external preset. - std::string new_name; - for (size_t idx = 0;; ++idx) { - std::string suffix; - if (original_name.empty()) { - if (idx > 0) - suffix = " (" + std::to_string(idx) + ")"; - } - else { - if (idx == 0) - suffix = " (" + original_name + ")"; - else - suffix = " (" + original_name + "-" + std::to_string(idx) + ")"; - } - new_name = name + suffix; - it = this->find_printer_internal(new_name); - if (it == m_printers.end() || it->name != new_name) - // Unique profile name. Insert a new profile. - break; - if (profile_print_params_same(it->config, cfg)) { - // The preset exists and it matches the values stored inside config. - if (select) - this->select_printer(it - m_printers.begin()); - return *it; - } - // Form another profile name. - } - // Insert a new profile. - PhysicalPrinter& printer = this->load_printer(path, new_name, std::move(cfg), select); - - return printer; -} - -void PhysicalPrinterCollection::save_printer(const std::string& new_name) -{ - // 1) Find the printer with a new_name or create a new one, - // initialize it with the edited config. - auto it = this->find_printer_internal(new_name); - if (it != m_printers.end() && it->name == new_name) { - // Preset with the same name found. - PhysicalPrinter& printer = *it; - // Overwriting an existing preset. - printer.config = std::move(m_edited_printer.config); - } - else { - // Creating a new printer. - PhysicalPrinter& printer = *m_printers.insert(it, m_edited_printer); - std::string old_name = printer.name; - printer.name = new_name; - } - // 2) Activate the saved preset. - this->select_printer_by_name(new_name, true); - // 3) Store the active preset to disk. - this->get_selected_preset().save(); -} -*/ -namespace PresetUtils { - const VendorProfile::PrinterModel* system_printer_model(const Preset &preset) - { - const VendorProfile::PrinterModel *out = nullptr; - if (preset.vendor != nullptr) { - auto *printer_model = preset.config.opt("printer_model"); - if (printer_model != nullptr && ! printer_model->value.empty()) { - auto it = std::find_if(preset.vendor->models.begin(), preset.vendor->models.end(), [printer_model](const VendorProfile::PrinterModel &pm) { return pm.id == printer_model->value; }); - if (it != preset.vendor->models.end()) - out = &(*it); - } - } - return out; - } -} // namespace PresetUtils - -} // namespace Slic3r diff --git a/src/slic3r/GUI/Preset.hpp b/src/slic3r/GUI/Preset.hpp deleted file mode 100644 index 8a8fa024b..000000000 --- a/src/slic3r/GUI/Preset.hpp +++ /dev/null @@ -1,714 +0,0 @@ -#ifndef slic3r_Preset_hpp_ -#define slic3r_Preset_hpp_ - -#include -#include -#include - -#include -#include - -#include "libslic3r/libslic3r.h" -#include "libslic3r/PrintConfig.hpp" -#include "libslic3r/Semver.hpp" - -class wxBitmap; -class wxBitmapComboBox; -class wxChoice; -class wxItemContainer; -class wxString; -class wxWindow; - -namespace Slic3r { - -class AppConfig; -class PresetBundle; - -enum ConfigFileType -{ - CONFIG_FILE_TYPE_UNKNOWN, - CONFIG_FILE_TYPE_APP_CONFIG, - CONFIG_FILE_TYPE_CONFIG, - CONFIG_FILE_TYPE_CONFIG_BUNDLE, -}; - -extern ConfigFileType guess_config_file_type(const boost::property_tree::ptree &tree); - -class VendorProfile -{ -public: - std::string name; - std::string id; - Semver config_version; - std::string config_update_url; - std::string changelog_url; - - struct PrinterVariant { - PrinterVariant() {} - PrinterVariant(const std::string &name) : name(name) {} - std::string name; - }; - - struct PrinterModel { - PrinterModel() {} - std::string id; - std::string name; - PrinterTechnology technology; - std::string family; - std::vector variants; - std::vector default_materials; - // Vendor & Printer Model specific print bed model & texture. - std::string bed_model; - std::string bed_texture; - - PrinterVariant* variant(const std::string &name) { - for (auto &v : this->variants) - if (v.name == name) - return &v; - return nullptr; - } - - const PrinterVariant* variant(const std::string &name) const { return const_cast(this)->variant(name); } - }; - std::vector models; - - std::set default_filaments; - std::set default_sla_materials; - - VendorProfile() {} - VendorProfile(std::string id) : id(std::move(id)) {} - - bool valid() const { return ! name.empty() && ! id.empty() && config_version.valid(); } - - // Load VendorProfile from an ini file. - // If `load_all` is false, only the header with basic info (name, version, URLs) is loaded. - static VendorProfile from_ini(const boost::filesystem::path &path, bool load_all=true); - static VendorProfile from_ini(const boost::property_tree::ptree &tree, const boost::filesystem::path &path, bool load_all=true); - - size_t num_variants() const { size_t n = 0; for (auto &model : models) n += model.variants.size(); return n; } - std::vector families() const; - - bool operator< (const VendorProfile &rhs) const { return this->id < rhs.id; } - bool operator==(const VendorProfile &rhs) const { return this->id == rhs.id; } -}; - -class Preset; - -// Helper to hold a profile with its vendor definition, where the vendor definition may have been extracted from a parent system preset. -// The parent preset is only accessible through PresetCollection, therefore to allow definition of the various is_compatible_with methods -// outside of the PresetCollection, this composite is returned by PresetCollection::get_preset_with_vendor_profile() when needed. -struct PresetWithVendorProfile { - PresetWithVendorProfile(const Preset &preset, const VendorProfile *vendor) : preset(preset), vendor(vendor) {} - const Preset &preset; - const VendorProfile *vendor; -}; - -// Note: it is imporant that map is used here rather than unordered_map, -// because we need iterators to not be invalidated, -// because Preset and the ConfigWizard hold pointers to VendorProfiles. -// XXX: maybe set is enough (cf. changes in Wizard) -typedef std::map VendorMap; - -class Preset -{ -public: - enum Type - { - TYPE_INVALID, - TYPE_PRINT, - TYPE_SLA_PRINT, - TYPE_FILAMENT, - TYPE_SLA_MATERIAL, - TYPE_PRINTER, - TYPE_COUNT, - }; - - Preset(Type type, const std::string &name, bool is_default = false) : type(type), is_default(is_default), name(name) {} - - Type type = TYPE_INVALID; - - // The preset represents a "default" set of properties, - // pulled from the default values of the PrintConfig (see PrintConfigDef for their definitions). - bool is_default; - // External preset points to a configuration, which has been loaded but not imported - // into the Slic3r default configuration location. - bool is_external = false; - // System preset is read-only. - bool is_system = false; - // Preset is visible, if it is associated with a printer model / variant that is enabled in the AppConfig - // or if it has no printer model / variant association. - // Also the "default" preset is only visible, if it is the only preset in the list. - bool is_visible = true; - // Has this preset been modified? - bool is_dirty = false; - // Is this preset compatible with the currently active printer? - bool is_compatible = true; - - bool is_user() const { return ! this->is_default && ! this->is_system; } - - // Name of the preset, usually derived form the file name. - std::string name; - // File name of the preset. This could be a Print / Filament / Printer preset, - // or a Configuration file bundling the Print + Filament + Printer presets (in that case is_external and possibly is_system will be true), - // or it could be a G-code (again, is_external will be true). - std::string file; - // If this is a system profile, then there should be a vendor data available to display at the UI. - const VendorProfile *vendor = nullptr; - - // Has this profile been loaded? - bool loaded = false; - - // Configuration data, loaded from a file, or set from the defaults. - DynamicPrintConfig config; - - // Alias of the preset - std::string alias; - // List of profile names, from which this profile was renamed at some point of time. - // This list is then used to match profiles by their names when loaded from .gcode, .3mf, .amf, - // and to match the "inherits" field of user profiles with updated system profiles. - std::vector renamed_from; - - void save(); - - // Return a label of this preset, consisting of a name and a "(modified)" suffix, if this preset is dirty. - std::string label() const; - - // Set the is_dirty flag if the provided config is different from the active one. - void set_dirty(const DynamicPrintConfig &config) { this->is_dirty = ! this->config.diff(config).empty(); } - void set_dirty(bool dirty = true) { this->is_dirty = dirty; } - void reset_dirty() { this->is_dirty = false; } - - // Returns the name of the preset, from which this preset inherits. - static std::string& inherits(DynamicPrintConfig &cfg) { return cfg.option("inherits", true)->value; } - std::string& inherits() { return Preset::inherits(this->config); } - const std::string& inherits() const { return Preset::inherits(const_cast(this)->config); } - - // Returns the "compatible_prints_condition". - static std::string& compatible_prints_condition(DynamicPrintConfig &cfg) { return cfg.option("compatible_prints_condition", true)->value; } - std::string& compatible_prints_condition() { - assert(this->type == TYPE_FILAMENT || this->type == TYPE_SLA_MATERIAL); - return Preset::compatible_prints_condition(this->config); - } - const std::string& compatible_prints_condition() const { return const_cast(this)->compatible_prints_condition(); } - - // Returns the "compatible_printers_condition". - static std::string& compatible_printers_condition(DynamicPrintConfig &cfg) { return cfg.option("compatible_printers_condition", true)->value; } - std::string& compatible_printers_condition() { - assert(this->type == TYPE_PRINT || this->type == TYPE_SLA_PRINT || this->type == TYPE_FILAMENT || this->type == TYPE_SLA_MATERIAL); - return Preset::compatible_printers_condition(this->config); - } - const std::string& compatible_printers_condition() const { return const_cast(this)->compatible_printers_condition(); } - - // Return a printer technology, return ptFFF if the printer technology is not set. - static PrinterTechnology printer_technology(const DynamicPrintConfig &cfg) { - auto *opt = cfg.option>("printer_technology"); - // The following assert may trigger when importing some legacy profile, - // but it is safer to keep it here to capture the cases where the "printer_technology" key is queried, where it should not. -// assert(opt != nullptr); - return (opt == nullptr) ? ptFFF : opt->value; - } - PrinterTechnology printer_technology() const { return Preset::printer_technology(this->config); } - // This call returns a reference, it may add a new entry into the DynamicPrintConfig. - PrinterTechnology& printer_technology_ref() { return this->config.option>("printer_technology", true)->value; } - - // Set is_visible according to application config - void set_visible_from_appconfig(const AppConfig &app_config); - - // Resize the extruder specific fields, initialize them with the content of the 1st extruder. - void set_num_extruders(unsigned int n) { this->config.set_num_extruders(n); } - - // Sort lexicographically by a preset name. The preset name shall be unique across a single PresetCollection. - bool operator<(const Preset &other) const { return this->name < other.name; } - - static const std::vector& print_options(); - static const std::vector& filament_options(); - // Printer options contain the nozzle options. - static const std::vector& printer_options(); - // Nozzle options of the printer options. - static const std::vector& nozzle_options(); - - static const std::vector& sla_printer_options(); - static const std::vector& sla_material_options(); - static const std::vector& sla_print_options(); - - static void update_suffix_modified(); - static const std::string& suffix_modified(); - static std::string remove_suffix_modified(const std::string& name); - static void normalize(DynamicPrintConfig &config); - // Report configuration fields, which are misplaced into a wrong group, remove them from the config. - static std::string remove_invalid_keys(DynamicPrintConfig &config, const DynamicPrintConfig &default_config); - -protected: - friend class PresetCollection; - friend class PresetBundle; -}; - -bool is_compatible_with_print (const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_print, const PresetWithVendorProfile &active_printer); -bool is_compatible_with_printer(const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_printer, const DynamicPrintConfig *extra_config); -bool is_compatible_with_printer(const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_printer); - -enum class PresetSelectCompatibleType { - // Never select a compatible preset if the newly selected profile is not compatible. - Never, - // Only select a compatible preset if the active profile used to be compatible, but it is no more. - OnlyIfWasCompatible, - // Always select a compatible preset if the active profile is no more compatible. - Always -}; - -// Collections of presets of the same type (one of the Print, Filament or Printer type). -class PresetCollection -{ -public: - // Initialize the PresetCollection with the "- default -" preset. - PresetCollection(Preset::Type type, const std::vector &keys, const Slic3r::StaticPrintConfig &defaults, const std::string &default_name = "- default -"); - ~PresetCollection(); - - typedef std::deque::iterator Iterator; - typedef std::deque::const_iterator ConstIterator; - Iterator begin() { return m_presets.begin() + m_num_default_presets; } - ConstIterator begin() const { return m_presets.cbegin() + m_num_default_presets; } - ConstIterator cbegin() const { return m_presets.cbegin() + m_num_default_presets; } - Iterator end() { return m_presets.end(); } - ConstIterator end() const { return m_presets.cend(); } - ConstIterator cend() const { return m_presets.cend(); } - - void reset(bool delete_files); - - Preset::Type type() const { return m_type; } - // Name, to be used on the screen and in error messages. Not localized. - std::string name() const; - // Name, to be used as a section name in config bundle, and as a folder name for presets. - std::string section_name() const; - const std::deque& operator()() const { return m_presets; } - - // Add default preset at the start of the collection, increment the m_default_preset counter. - void add_default_preset(const std::vector &keys, const Slic3r::StaticPrintConfig &defaults, const std::string &preset_name); - - // Load ini files of the particular type from the provided directory path. - void load_presets(const std::string &dir_path, const std::string &subdir); - - // Load a preset from an already parsed config file, insert it into the sorted sequence of presets - // and select it, losing previous modifications. - Preset& load_preset(const std::string &path, const std::string &name, const DynamicPrintConfig &config, bool select = true); - Preset& load_preset(const std::string &path, const std::string &name, DynamicPrintConfig &&config, bool select = true); - - Preset& load_external_preset( - // Path to the profile source file (a G-code, an AMF or 3MF file, a config file) - const std::string &path, - // Name of the profile, derived from the source file name. - const std::string &name, - // Original name of the profile, extracted from the loaded config. Empty, if the name has not been stored. - const std::string &original_name, - // Config to initialize the preset from. - const DynamicPrintConfig &config, - // Select the preset after loading? - bool select = true); - - // Save the preset under a new name. If the name is different from the old one, - // a new preset is stored into the list of presets. - // All presets are marked as not modified and the new preset is activated. - void save_current_preset(const std::string &new_name, bool detach = false); - - // Delete the current preset, activate the first visible preset. - // returns true if the preset was deleted successfully. - bool delete_current_preset(); - // Delete the current preset, activate the first visible preset. - // returns true if the preset was deleted successfully. - bool delete_preset(const std::string& name); - - // Enable / disable the "- default -" preset. - void set_default_suppressed(bool default_suppressed); - bool is_default_suppressed() const { return m_default_suppressed; } - - // Select a preset. If an invalid index is provided, the first visible preset is selected. - Preset& select_preset(size_t idx); - // Return the selected preset, without the user modifications applied. - Preset& get_selected_preset() { return m_presets[m_idx_selected]; } - const Preset& get_selected_preset() const { return m_presets[m_idx_selected]; } - size_t get_selected_idx() const { return m_idx_selected; } - // Returns the name of the selected preset, or an empty string if no preset is selected. - std::string get_selected_preset_name() const { return (m_idx_selected == size_t(-1)) ? std::string() : this->get_selected_preset().name; } - // For the current edited preset, return the parent preset if there is one. - // If there is no parent preset, nullptr is returned. - // The parent preset may be a system preset or a user preset, which will be - // reflected by the UI. - const Preset* get_selected_preset_parent() const; - // Get parent preset for a child preset, based on the "inherits" field of a child, - // where the "inherits" profile name is searched for in both m_presets and m_map_system_profile_renamed. - const Preset* get_preset_parent(const Preset& child) const; - // Return the selected preset including the user modifications. - Preset& get_edited_preset() { return m_edited_preset; } - const Preset& get_edited_preset() const { return m_edited_preset; } - - // Return vendor of the first parent profile, for which the vendor is defined, or null if such profile does not exist. - PresetWithVendorProfile get_preset_with_vendor_profile(const Preset &preset) const; - PresetWithVendorProfile get_edited_preset_with_vendor_profile() const { return this->get_preset_with_vendor_profile(this->get_edited_preset()); } - - const std::string& get_preset_name_by_alias(const std::string& alias) const; - const std::string* get_preset_name_renamed(const std::string &old_name) const; - - // used to update preset_choice from Tab - const std::deque& get_presets() const { return m_presets; } - size_t get_idx_selected() { return m_idx_selected; } - static const std::string& get_suffix_modified(); - - // Return a preset possibly with modifications. - Preset& default_preset(size_t idx = 0) { assert(idx < m_num_default_presets); return m_presets[idx]; } - const Preset& default_preset(size_t idx = 0) const { assert(idx < m_num_default_presets); return m_presets[idx]; } - virtual const Preset& default_preset_for(const DynamicPrintConfig & /* config */) const { return this->default_preset(); } - // Return a preset by an index. If the preset is active, a temporary copy is returned. - Preset& preset(size_t idx) { return (idx == m_idx_selected) ? m_edited_preset : m_presets[idx]; } - const Preset& preset(size_t idx) const { return const_cast(this)->preset(idx); } - void discard_current_changes() { m_presets[m_idx_selected].reset_dirty(); m_edited_preset = m_presets[m_idx_selected]; } - - // Return a preset by its name. If the preset is active, a temporary copy is returned. - // If a preset is not found by its name, null is returned. - Preset* find_preset(const std::string &name, bool first_visible_if_not_found = false); - const Preset* find_preset(const std::string &name, bool first_visible_if_not_found = false) const - { return const_cast(this)->find_preset(name, first_visible_if_not_found); } - - size_t first_visible_idx() const; - // Return index of the first compatible preset. Certainly at least the '- default -' preset shall be compatible. - // If one of the prefered_alternates is compatible, select it. - template - size_t first_compatible_idx(PreferedCondition prefered_condition) const - { - size_t i = m_default_suppressed ? m_num_default_presets : 0; - size_t n = this->m_presets.size(); - size_t i_compatible = n; - for (; i < n; ++ i) - // Since we use the filament selection from Wizard, it's needed to control the preset visibility too - if (m_presets[i].is_compatible && m_presets[i].is_visible) { - if (prefered_condition(m_presets[i].name)) - return i; - if (i_compatible == n) - // Store the first compatible profile into i_compatible. - i_compatible = i; - } - return (i_compatible == n) ? 0 : i_compatible; - } - // Return index of the first compatible preset. Certainly at least the '- default -' preset shall be compatible. - size_t first_compatible_idx() const { return this->first_compatible_idx([](const std::string&){return true;}); } - - // Return index of the first visible preset. Certainly at least the '- default -' preset shall be visible. - // Return the first visible preset. Certainly at least the '- default -' preset shall be visible. - Preset& first_visible() { return this->preset(this->first_visible_idx()); } - const Preset& first_visible() const { return this->preset(this->first_visible_idx()); } - Preset& first_compatible() { return this->preset(this->first_compatible_idx()); } - template - Preset& first_compatible(PreferedCondition prefered_condition) { return this->preset(this->first_compatible_idx(prefered_condition)); } - const Preset& first_compatible() const { return this->preset(this->first_compatible_idx()); } - - // Return number of presets including the "- default -" preset. - size_t size() const { return m_presets.size(); } - bool has_defaults_only() const { return m_presets.size() <= m_num_default_presets; } - - // For Print / Filament presets, disable those, which are not compatible with the printer. - template - void update_compatible(const PresetWithVendorProfile &active_printer, const PresetWithVendorProfile *active_print, PresetSelectCompatibleType select_other_if_incompatible, PreferedCondition prefered_condition) - { - if (this->update_compatible_internal(active_printer, active_print, select_other_if_incompatible) == (size_t)-1) - // Find some other compatible preset, or the "-- default --" preset. - this->select_preset(this->first_compatible_idx(prefered_condition)); - } - void update_compatible(const PresetWithVendorProfile &active_printer, const PresetWithVendorProfile *active_print, PresetSelectCompatibleType select_other_if_incompatible) - { this->update_compatible(active_printer, active_print, select_other_if_incompatible, [](const std::string&){return true;}); } - - size_t num_visible() const { return std::count_if(m_presets.begin(), m_presets.end(), [](const Preset &preset){return preset.is_visible;}); } - - // Compare the content of get_selected_preset() with get_edited_preset() configs, return true if they differ. - bool current_is_dirty() const { return ! this->current_dirty_options().empty(); } - // Compare the content of get_selected_preset() with get_edited_preset() configs, return the list of keys where they differ. - std::vector current_dirty_options(const bool deep_compare = false) const - { return dirty_options(&this->get_edited_preset(), &this->get_selected_preset(), deep_compare); } - // Compare the content of get_selected_preset() with get_edited_preset() configs, return the list of keys where they differ. - std::vector current_different_from_parent_options(const bool deep_compare = false) const - { return dirty_options(&this->get_edited_preset(), this->get_selected_preset_parent(), deep_compare); } - - // Return a sorted list of system preset names. - std::vector system_preset_names() const; - - // Update a dirty flag of the current preset - // Return true if the dirty flag changed. - bool update_dirty(); - - // Select a profile by its name. Return true if the selection changed. - // Without force, the selection is only updated if the index changes. - // With force, the changes are reverted if the new index is the same as the old index. - bool select_preset_by_name(const std::string &name, bool force); - - // Generate a file path from a profile name. Add the ".ini" suffix if it is missing. - std::string path_from_name(const std::string &new_name) const; - - size_t num_default_presets() { return m_num_default_presets; } - -protected: - // Select a preset, if it exists. If it does not exist, select an invalid (-1) index. - // This is a temporary state, which shall be fixed immediately by the following step. - bool select_preset_by_name_strict(const std::string &name); - - // Merge one vendor's presets with the other vendor's presets, report duplicates. - std::vector merge_presets(PresetCollection &&other, const VendorMap &new_vendors); - - // Update m_map_alias_to_profile_name from loaded system profiles. - void update_map_alias_to_profile_name(); - - // Update m_map_system_profile_renamed from loaded system profiles. - void update_map_system_profile_renamed(); - -private: - PresetCollection(); - PresetCollection(const PresetCollection &other); - PresetCollection& operator=(const PresetCollection &other); - - // Find a preset position in the sorted list of presets. - // The "-- default -- " preset is always the first, so it needs - // to be handled differently. - // If a preset does not exist, an iterator is returned indicating where to insert a preset with the same name. - std::deque::iterator find_preset_internal(const std::string &name) - { - Preset key(m_type, name); - auto it = std::lower_bound(m_presets.begin() + m_num_default_presets, m_presets.end(), key); - if (it == m_presets.end() || it->name != name) { - // Preset has not been not found in the sorted list of non-default presets. Try the defaults. - for (size_t i = 0; i < m_num_default_presets; ++ i) - if (m_presets[i].name == name) { - it = m_presets.begin() + i; - break; - } - } - return it; - } - std::deque::const_iterator find_preset_internal(const std::string &name) const - { return const_cast(this)->find_preset_internal(name); } - std::deque::iterator find_preset_renamed(const std::string &name) { - auto it_renamed = m_map_system_profile_renamed.find(name); - auto it = (it_renamed == m_map_system_profile_renamed.end()) ? m_presets.end() : this->find_preset_internal(it_renamed->second); - assert((it_renamed == m_map_system_profile_renamed.end()) || (it != m_presets.end() && it->name == it_renamed->second)); - return it; - } - std::deque::const_iterator find_preset_renamed(const std::string &name) const - { return const_cast(this)->find_preset_renamed(name); } - - size_t update_compatible_internal(const PresetWithVendorProfile &active_printer, const PresetWithVendorProfile *active_print, PresetSelectCompatibleType unselect_if_incompatible); - - static std::vector dirty_options(const Preset *edited, const Preset *reference, const bool is_printer_type = false); - - // Type of this PresetCollection: TYPE_PRINT, TYPE_FILAMENT or TYPE_PRINTER. - Preset::Type m_type; - // List of presets, starting with the "- default -" preset. - // Use deque to force the container to allocate an object per each entry, - // so that the addresses of the presets don't change during resizing of the container. - std::deque m_presets; - // System profiles may have aliases. Map to the full profile name. - std::vector> m_map_alias_to_profile_name; - // Map from old system profile name to a current system profile name. - std::map m_map_system_profile_renamed; - // Initially this preset contains a copy of the selected preset. Later on, this copy may be modified by the user. - Preset m_edited_preset; - // Selected preset. - size_t m_idx_selected; - // Is the "- default -" preset suppressed? - bool m_default_suppressed = true; - size_t m_num_default_presets = 0; - - // Path to the directory to store the config files into. - std::string m_dir_path; - - // to access select_preset_by_name_strict() - friend class PresetBundle; -}; - -// Printer supports the FFF and SLA technologies, with different set of configuration values, -// therefore this PresetCollection needs to handle two defaults. -class PrinterPresetCollection : public PresetCollection -{ -public: - PrinterPresetCollection(Preset::Type type, const std::vector &keys, const Slic3r::StaticPrintConfig &defaults, const std::string &default_name = "- default -") : - PresetCollection(type, keys, defaults, default_name) {} - const Preset& default_preset_for(const DynamicPrintConfig &config) const override; - - const Preset* find_by_model_id(const std::string &model_id) const; -}; - -namespace PresetUtils { - // PrinterModel of a system profile, from which this preset is derived, or null if it is not derived from a system profile. - const VendorProfile::PrinterModel* system_printer_model(const Preset &preset); -} // namespace PresetUtils - - -////////////////////////////////////////////////////////////////////// - -class PhysicalPrinter -{ -public: - PhysicalPrinter(const std::string& name) : name(name) {} - - // Name of the Physical Printer, usually derived form the file name. - std::string name; - // File name of the Physical Printer. - std::string file; - // Name of the related Printer preset - std::string preset_name; - - // Has this profile been loaded? - bool loaded = false; - - // Configuration data, loaded from a file, or set from the defaults. - DynamicPrintConfig config; - - void save() { this->config.save(this->file); } - - // Return a printer technology, return ptFFF if the printer technology is not set. - static PrinterTechnology printer_technology(const DynamicPrintConfig& cfg) { - auto* opt = cfg.option>("printer_technology"); - // The following assert may trigger when importing some legacy profile, - // but it is safer to keep it here to capture the cases where the "printer_technology" key is queried, where it should not. - return (opt == nullptr) ? ptFFF : opt->value; - } - PrinterTechnology printer_technology() const { return printer_technology(this->config); } - - // Sort lexicographically by a preset name. The preset name shall be unique across a single PresetCollection. - bool operator<(const Preset& other) const { return this->name < other.name; } - -protected: - friend class PhysicalPrinterCollection; -}; -/* -// Collections of presets of the same type (one of the Print, Filament or Printer type). -class PhysicalPrinterCollection -{ -public: - // Initialize the PresetCollection with the "- default -" preset. - PhysicalPrinterCollection(const std::vector& keys) : m_idx_selected(0) {} - ~PhysicalPrinterCollection() {} - - typedef std::deque::iterator Iterator; - typedef std::deque::const_iterator ConstIterator; - Iterator begin() { return m_printers.begin(); } - ConstIterator begin() const { return m_printers.cbegin(); } - ConstIterator cbegin() const { return m_printers.cbegin(); } - Iterator end() { return m_printers.end(); } - ConstIterator end() const { return m_printers.cend(); } - ConstIterator cend() const { return m_printers.cend(); } - - void reset(bool delete_files) {}; - - const std::deque& operator()() const { return m_printers; } - - // Load ini files of the particular type from the provided directory path. - void load_printers(const std::string& dir_path, const std::string& subdir){}; - - // Load a preset from an already parsed config file, insert it into the sorted sequence of presets - // and select it, losing previous modifications. - PhysicalPrinter& load_printer(const std::string& path, const std::string& name, const DynamicPrintConfig& config, bool select = true); - PhysicalPrinter& load_printer(const std::string& path, const std::string& name, DynamicPrintConfig&& config, bool select = true); - - PhysicalPrinter& load_external_printer( - // Path to the profile source file (a G-code, an AMF or 3MF file, a config file) - const std::string& path, - // Name of the profile, derived from the source file name. - const std::string& name, - // Original name of the profile, extracted from the loaded config. Empty, if the name has not been stored. - const std::string& original_name, - // Config to initialize the preset from. - const DynamicPrintConfig& config, - // Select the preset after loading? - bool select = true); - - // Save the printer under a new name. If the name is different from the old one, - // a new printer is stored into the list of printers. - // ? New printer is activated. - void save_printer(const std::string& new_name); - - // Delete the current preset, activate the first visible preset. - // returns true if the preset was deleted successfully. - bool delete_current_printer() {return true;} - // Delete the current preset, activate the first visible preset. - // returns true if the preset was deleted successfully. - bool delete_printer(const std::string& name) { return true; } - - // Select a printer. If an invalid index is provided, the first visible printer is selected. - PhysicalPrinter& select_printer(size_t idx); - // Return the selected preset, without the user modifications applied. - PhysicalPrinter& get_selected_preset() { return m_printers[m_idx_selected]; } - const PhysicalPrinter& get_selected_preset() const { return m_printers[m_idx_selected]; } - size_t get_selected_idx() const { return m_idx_selected; } - // Returns the name of the selected preset, or an empty string if no preset is selected. - std::string get_selected_preset_name() const { return (m_idx_selected == size_t(-1)) ? std::string() : this->get_selected_preset().name; } - PhysicalPrinter& get_edited_preset() { return m_edited_printer; } - const PhysicalPrinter& get_edited_preset() const { return m_edited_printer; } - - // Return a preset possibly with modifications. - PhysicalPrinter& default_printer(size_t idx = 0) { return m_printers[idx]; } - const PhysicalPrinter& default_printer(size_t idx = 0) const { return m_printers[idx]; } - - // used to update preset_choice from Tab - const std::deque& get_presets() const { return m_printers; } - size_t get_idx_selected() { return m_idx_selected; } - - // Return a preset by an index. If the preset is active, a temporary copy is returned. - PhysicalPrinter& printer(size_t idx) { return (idx == m_idx_selected) ? m_edited_printer : m_printers[idx]; } - const PhysicalPrinter& printer(size_t idx) const { return const_cast(this)->printer(idx); } - - // Return a preset by its name. If the preset is active, a temporary copy is returned. - // If a preset is not found by its name, null is returned. - PhysicalPrinter* find_printer(const std::string& name, bool first_visible_if_not_found = false); - const PhysicalPrinter* find_printer(const std::string& name, bool first_visible_if_not_found = false) const - { - return const_cast(this)->find_printer(name, first_visible_if_not_found); - } - - // Return number of presets including the "- default -" preset. - size_t size() const { return m_printers.size(); } - - // Select a profile by its name. Return true if the selection changed. - // Without force, the selection is only updated if the index changes. - // With force, the changes are reverted if the new index is the same as the old index. - bool select_printer_by_name(const std::string& name, bool force) {}; - - // Generate a file path from a profile name. Add the ".ini" suffix if it is missing. - std::string path_from_name(const std::string& new_name) const; - -private: -// PhysicalPrinterCollection(); - PhysicalPrinterCollection(const PhysicalPrinterCollection& other); - PhysicalPrinterCollection& operator=(const PhysicalPrinterCollection& other); - - // Find a preset position in the sorted list of presets. - // The "-- default -- " preset is always the first, so it needs - // to be handled differently. - // If a preset does not exist, an iterator is returned indicating where to insert a preset with the same name. - std::deque::iterator find_printer_internal(const std::string& name) - { - PhysicalPrinter key(name); - auto it = std::lower_bound(m_printers.begin()+0, m_printers.end(), key); - return it; - } - std::deque::const_iterator find_printer_internal(const std::string& name) const - { - return const_cast(this)->find_printer_internal(name); - } - - static std::vector dirty_options(const Preset* edited, const Preset* reference, const bool is_printer_type = false); - - // List of presets, starting with the "- default -" preset. - // Use deque to force the container to allocate an object per each entry, - // so that the addresses of the presets don't change during resizing of the container. - std::deque m_printers; - // Initially this printer contains a copy of the selected printer. Later on, this copy may be modified by the user. - PhysicalPrinter m_edited_printer; - // Selected preset. - size_t m_idx_selected; - - // Path to the directory to store the config files into. - std::string m_dir_path; -}; - -////////////////////////////////////////////////////////////////////// -*/ - -} // namespace Slic3r - -#endif /* slic3r_Preset_hpp_ */ diff --git a/src/slic3r/GUI/PresetBundle.cpp b/src/slic3r/GUI/PresetBundle.cpp deleted file mode 100644 index 024884b00..000000000 --- a/src/slic3r/GUI/PresetBundle.cpp +++ /dev/null @@ -1,1497 +0,0 @@ -#include - -#include "PresetBundle.hpp" -#include "Plater.hpp" - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "libslic3r/libslic3r.h" -#include "libslic3r/Utils.hpp" -#include "libslic3r/Model.hpp" -#include "GUI_App.hpp" -#include "libslic3r/CustomGCode.hpp" - - -// Store the print/filament/printer presets into a "presets" subdirectory of the Slic3rPE config dir. -// This breaks compatibility with the upstream Slic3r if the --datadir is used to switch between the two versions. -// #define SLIC3R_PROFILE_USE_PRESETS_SUBDIR - -namespace Slic3r { - -static std::vector s_project_options { - "colorprint_heights", - "wiping_volumes_extruders", - "wiping_volumes_matrix" -}; - -const char *PresetBundle::PRUSA_BUNDLE = "PrusaResearch"; - -PresetBundle::PresetBundle() : - prints(Preset::TYPE_PRINT, Preset::print_options(), static_cast(FullPrintConfig::defaults())), - filaments(Preset::TYPE_FILAMENT, Preset::filament_options(), static_cast(FullPrintConfig::defaults())), - sla_materials(Preset::TYPE_SLA_MATERIAL, Preset::sla_material_options(), static_cast(SLAFullPrintConfig::defaults())), - sla_prints(Preset::TYPE_SLA_PRINT, Preset::sla_print_options(), static_cast(SLAFullPrintConfig::defaults())), - printers(Preset::TYPE_PRINTER, Preset::printer_options(), static_cast(FullPrintConfig::defaults()), "- default FFF -") -{ - if (wxImage::FindHandler(wxBITMAP_TYPE_PNG) == nullptr) - wxImage::AddHandler(new wxPNGHandler); - - // The following keys are handled by the UI, they do not have a counterpart in any StaticPrintConfig derived classes, - // therefore they need to be handled differently. As they have no counterpart in StaticPrintConfig, they are not being - // initialized based on PrintConfigDef(), but to empty values (zeros, empty vectors, empty strings). - // - // "compatible_printers", "compatible_printers_condition", "inherits", - // "print_settings_id", "filament_settings_id", "printer_settings_id", - // "printer_vendor", "printer_model", "printer_variant", "default_print_profile", "default_filament_profile" - - // Create the ID config keys, as they are not part of the Static print config classes. - this->prints.default_preset().config.optptr("print_settings_id", true); - this->prints.default_preset().compatible_printers_condition(); - this->prints.default_preset().inherits(); - - this->filaments.default_preset().config.option("filament_settings_id", true)->values = { "" }; - this->filaments.default_preset().compatible_printers_condition(); - this->filaments.default_preset().inherits(); - // Set all the nullable values to nils. - this->filaments.default_preset().config.null_nullables(); - - this->sla_materials.default_preset().config.optptr("sla_material_settings_id", true); - this->sla_materials.default_preset().compatible_printers_condition(); - this->sla_materials.default_preset().inherits(); - - this->sla_prints.default_preset().config.optptr("sla_print_settings_id", true); - this->sla_prints.default_preset().config.opt_string("output_filename_format", true) = "[input_filename_base].sl1"; - this->sla_prints.default_preset().compatible_printers_condition(); - this->sla_prints.default_preset().inherits(); - - this->printers.add_default_preset(Preset::sla_printer_options(), static_cast(SLAFullPrintConfig::defaults()), "- default SLA -"); - this->printers.preset(1).printer_technology_ref() = ptSLA; - for (size_t i = 0; i < 2; ++ i) { - // The following ugly switch is to avoid printers.preset(0) to return the edited instance, as the 0th default is the current one. - Preset &preset = this->printers.default_preset(i); - preset.config.optptr("printer_settings_id", true); - preset.config.optptr("printer_vendor", true); - preset.config.optptr("printer_model", true); - preset.config.optptr("printer_variant", true); - preset.config.optptr("thumbnails", true); - if (i == 0) { - preset.config.optptr("default_print_profile", true); - preset.config.option("default_filament_profile", true)->values = { "" }; - } - else { - preset.config.optptr("default_sla_print_profile", true); - preset.config.optptr("default_sla_material_profile", true); - } - // default_sla_material_profile - preset.inherits(); - } - - // Re-activate the default presets, so their "edited" preset copies will be updated with the additional configuration values above. - this->prints .select_preset(0); - this->sla_prints .select_preset(0); - this->filaments .select_preset(0); - this->sla_materials.select_preset(0); - this->printers .select_preset(0); - - this->project_config.apply_only(FullPrintConfig::defaults(), s_project_options); -} - -PresetBundle::~PresetBundle() -{ -} - -void PresetBundle::reset(bool delete_files) -{ - // Clear the existing presets, delete their respective files. - this->vendors.clear(); - this->prints .reset(delete_files); - this->sla_prints .reset(delete_files); - this->filaments .reset(delete_files); - this->sla_materials.reset(delete_files); - this->printers .reset(delete_files); - this->filament_presets.clear(); - this->filament_presets.emplace_back(this->filaments.get_selected_preset_name()); - this->obsolete_presets.prints.clear(); - this->obsolete_presets.sla_prints.clear(); - this->obsolete_presets.filaments.clear(); - this->obsolete_presets.sla_materials.clear(); - this->obsolete_presets.printers.clear(); -} - -void PresetBundle::setup_directories() -{ - boost::filesystem::path data_dir = boost::filesystem::path(Slic3r::data_dir()); - std::initializer_list paths = { - data_dir, - data_dir / "vendor", - data_dir / "cache", -#ifdef SLIC3R_PROFILE_USE_PRESETS_SUBDIR - // Store the print/filament/printer presets into a "presets" directory. - data_dir / "presets", - data_dir / "presets" / "print", - data_dir / "presets" / "filament", - data_dir / "presets" / "sla_print", - data_dir / "presets" / "sla_material", - data_dir / "presets" / "printer" -#else - // Store the print/filament/printer presets at the same location as the upstream Slic3r. - data_dir / "print", - data_dir / "filament", - data_dir / "sla_print", - data_dir / "sla_material", - data_dir / "printer" -#endif - }; - for (const boost::filesystem::path &path : paths) { - boost::filesystem::path subdir = path; - subdir.make_preferred(); - if (! boost::filesystem::is_directory(subdir) && - ! boost::filesystem::create_directory(subdir)) - throw std::runtime_error(std::string("Slic3r was unable to create its data directory at ") + subdir.string()); - } -} - -void PresetBundle::load_presets(AppConfig &config, const std::string &preferred_model_id) -{ - // First load the vendor specific system presets. - std::string errors_cummulative = this->load_system_presets(); - - const std::string dir_user_presets = data_dir() -#ifdef SLIC3R_PROFILE_USE_PRESETS_SUBDIR - // Store the print/filament/printer presets into a "presets" directory. - + "/presets" -#else - // Store the print/filament/printer presets at the same location as the upstream Slic3r. -#endif - ; - try { - this->prints.load_presets(dir_user_presets, "print"); - } catch (const std::runtime_error &err) { - errors_cummulative += err.what(); - } - try { - this->sla_prints.load_presets(dir_user_presets, "sla_print"); - } catch (const std::runtime_error &err) { - errors_cummulative += err.what(); - } - try { - this->filaments.load_presets(dir_user_presets, "filament"); - } catch (const std::runtime_error &err) { - errors_cummulative += err.what(); - } - try { - this->sla_materials.load_presets(dir_user_presets, "sla_material"); - } catch (const std::runtime_error &err) { - errors_cummulative += err.what(); - } - try { - this->printers.load_presets(dir_user_presets, "printer"); - } catch (const std::runtime_error &err) { - errors_cummulative += err.what(); - } - this->update_multi_material_filament_presets(); - this->update_compatible(PresetSelectCompatibleType::Never); - if (! errors_cummulative.empty()) - throw std::runtime_error(errors_cummulative); - - this->load_selections(config, preferred_model_id); -} - -// Load system presets into this PresetBundle. -// For each vendor, there will be a single PresetBundle loaded. -std::string PresetBundle::load_system_presets() -{ - // Here the vendor specific read only Config Bundles are stored. - boost::filesystem::path dir = (boost::filesystem::path(data_dir()) / "vendor").make_preferred(); - std::string errors_cummulative; - bool first = true; - for (auto &dir_entry : boost::filesystem::directory_iterator(dir)) - if (Slic3r::is_ini_file(dir_entry)) { - std::string name = dir_entry.path().filename().string(); - // Remove the .ini suffix. - name.erase(name.size() - 4); - try { - // Load the config bundle, flatten it. - if (first) { - // Reset this PresetBundle and load the first vendor config. - this->load_configbundle(dir_entry.path().string(), LOAD_CFGBNDLE_SYSTEM); - first = false; - } else { - // Load the other vendor configs, merge them with this PresetBundle. - // Report duplicate profiles. - PresetBundle other; - other.load_configbundle(dir_entry.path().string(), LOAD_CFGBNDLE_SYSTEM); - std::vector duplicates = this->merge_presets(std::move(other)); - if (! duplicates.empty()) { - errors_cummulative += "Vendor configuration file " + name + " contains the following presets with names used by other vendors: "; - for (size_t i = 0; i < duplicates.size(); ++ i) { - if (i > 0) - errors_cummulative += ", "; - errors_cummulative += duplicates[i]; - } - } - } - } catch (const std::runtime_error &err) { - errors_cummulative += err.what(); - errors_cummulative += "\n"; - } - } - if (first) { - // No config bundle loaded, reset. - this->reset(false); - } - - this->update_system_maps(); - return errors_cummulative; -} - -// Merge one vendor's presets with the other vendor's presets, report duplicates. -std::vector PresetBundle::merge_presets(PresetBundle &&other) -{ - this->vendors.insert(other.vendors.begin(), other.vendors.end()); - std::vector duplicate_prints = this->prints .merge_presets(std::move(other.prints), this->vendors); - std::vector duplicate_sla_prints = this->sla_prints .merge_presets(std::move(other.sla_prints), this->vendors); - std::vector duplicate_filaments = this->filaments .merge_presets(std::move(other.filaments), this->vendors); - std::vector duplicate_sla_materials = this->sla_materials.merge_presets(std::move(other.sla_materials), this->vendors); - std::vector duplicate_printers = this->printers .merge_presets(std::move(other.printers), this->vendors); - append(this->obsolete_presets.prints, std::move(other.obsolete_presets.prints)); - append(this->obsolete_presets.sla_prints, std::move(other.obsolete_presets.sla_prints)); - append(this->obsolete_presets.filaments, std::move(other.obsolete_presets.filaments)); - append(this->obsolete_presets.sla_materials, std::move(other.obsolete_presets.sla_materials)); - append(this->obsolete_presets.printers, std::move(other.obsolete_presets.printers)); - append(duplicate_prints, std::move(duplicate_sla_prints)); - append(duplicate_prints, std::move(duplicate_filaments)); - append(duplicate_prints, std::move(duplicate_sla_materials)); - append(duplicate_prints, std::move(duplicate_printers)); - return duplicate_prints; -} - -void PresetBundle::update_system_maps() -{ - this->prints .update_map_system_profile_renamed(); - this->sla_prints .update_map_system_profile_renamed(); - this->filaments .update_map_system_profile_renamed(); - this->sla_materials.update_map_system_profile_renamed(); - this->printers .update_map_system_profile_renamed(); - - this->prints .update_map_alias_to_profile_name(); - this->sla_prints .update_map_alias_to_profile_name(); - this->filaments .update_map_alias_to_profile_name(); - this->sla_materials.update_map_alias_to_profile_name(); -} - -static inline std::string remove_ini_suffix(const std::string &name) -{ - std::string out = name; - if (boost::iends_with(out, ".ini")) - out.erase(out.end() - 4, out.end()); - return out; -} - -// Set the "enabled" flag for printer vendors, printer models and printer variants -// based on the user configuration. -// If the "vendor" section is missing, enable all models and variants of the particular vendor. -void PresetBundle::load_installed_printers(const AppConfig &config) -{ - this->update_system_maps(); - for (auto &preset : printers) - preset.set_visible_from_appconfig(config); -} - -const std::string& PresetBundle::get_preset_name_by_alias( const Preset::Type& preset_type, const std::string& alias) const -{ - // there are not aliases for Printers profiles - if (preset_type == Preset::TYPE_PRINTER || preset_type == Preset::TYPE_INVALID) - return alias; - - const PresetCollection& presets = preset_type == Preset::TYPE_PRINT ? prints : - preset_type == Preset::TYPE_SLA_PRINT ? sla_prints : - preset_type == Preset::TYPE_FILAMENT ? filaments : - sla_materials; - - return presets.get_preset_name_by_alias(alias); -} - -void PresetBundle::load_installed_filaments(AppConfig &config) -{ - if (! config.has_section(AppConfig::SECTION_FILAMENTS)) { - // Compatibility with the PrusaSlicer 2.1.1 and older, where the filament profiles were not installable yet. - // Find all filament profiles, which are compatible with installed printers, and act as if these filament profiles - // were installed. - std::unordered_set compatible_filaments; - for (const Preset &printer : printers) - if (printer.is_visible && printer.printer_technology() == ptFFF) { - const PresetWithVendorProfile printer_with_vendor_profile = printers.get_preset_with_vendor_profile(printer); - for (const Preset &filament : filaments) - if (filament.is_system && is_compatible_with_printer(filaments.get_preset_with_vendor_profile(filament), printer_with_vendor_profile)) - compatible_filaments.insert(&filament); - } - // and mark these filaments as installed, therefore this code will not be executed at the next start of the application. - for (const auto &filament: compatible_filaments) - config.set(AppConfig::SECTION_FILAMENTS, filament->name, "1"); - } - - for (auto &preset : filaments) - preset.set_visible_from_appconfig(config); -} - -void PresetBundle::load_installed_sla_materials(AppConfig &config) -{ - if (! config.has_section(AppConfig::SECTION_MATERIALS)) { - std::unordered_set comp_sla_materials; - // Compatibility with the PrusaSlicer 2.1.1 and older, where the SLA material profiles were not installable yet. - // Find all SLA material profiles, which are compatible with installed printers, and act as if these SLA material profiles - // were installed. - for (const Preset &printer : printers) - if (printer.is_visible && printer.printer_technology() == ptSLA) { - const PresetWithVendorProfile printer_with_vendor_profile = printers.get_preset_with_vendor_profile(printer); - for (const Preset &material : sla_materials) - if (material.is_system && is_compatible_with_printer(sla_materials.get_preset_with_vendor_profile(material), printer_with_vendor_profile)) - comp_sla_materials.insert(&material); - } - // and mark these SLA materials as installed, therefore this code will not be executed at the next start of the application. - for (const auto &material: comp_sla_materials) - config.set(AppConfig::SECTION_MATERIALS, material->name, "1"); - } - - for (auto &preset : sla_materials) - preset.set_visible_from_appconfig(config); -} - -// Load selections (current print, current filaments, current printer) from config.ini -// This is done on application start up or after updates are applied. -void PresetBundle::load_selections(AppConfig &config, const std::string &preferred_model_id) -{ - // Update visibility of presets based on application vendor / model / variant configuration. - this->load_installed_printers(config); - - // Update visibility of filament and sla material presets - this->load_installed_filaments(config); - this->load_installed_sla_materials(config); - - // Parse the initial print / filament / printer profile names. - std::string initial_print_profile_name = remove_ini_suffix(config.get("presets", "print")); - std::string initial_sla_print_profile_name = remove_ini_suffix(config.get("presets", "sla_print")); - std::string initial_filament_profile_name = remove_ini_suffix(config.get("presets", "filament")); - std::string initial_sla_material_profile_name = remove_ini_suffix(config.get("presets", "sla_material")); - std::string initial_printer_profile_name = remove_ini_suffix(config.get("presets", "printer")); - - // Activate print / filament / printer profiles from either the config, - // or from the preferred_model_id suggestion passed in by ConfigWizard. - // If the printer profile enumerated by the config are not visible, select an alternate preset. - // Do not select alternate profiles for the print / filament profiles as those presets - // will be selected by the following call of this->update_compatible(PresetSelectCompatibleType::Always). - - const Preset *initial_printer = printers.find_preset(initial_printer_profile_name); - const Preset *preferred_printer = printers.find_by_model_id(preferred_model_id); - printers.select_preset_by_name( - (preferred_printer != nullptr && (initial_printer == nullptr || !initial_printer->is_visible)) ? - preferred_printer->name : - initial_printer_profile_name, - true); - - // Selects the profile, leaves it to -1 if the initial profile name is empty or if it was not found. - prints.select_preset_by_name_strict(initial_print_profile_name); - filaments.select_preset_by_name_strict(initial_filament_profile_name); - sla_prints.select_preset_by_name_strict(initial_sla_print_profile_name); - sla_materials.select_preset_by_name_strict(initial_sla_material_profile_name); - - // Load the names of the other filament profiles selected for a multi-material printer. - // Load it even if the current printer technology is SLA. - // The possibly excessive filament names will be later removed with this->update_multi_material_filament_presets() - // once the FFF technology gets selected. - this->filament_presets = { filaments.get_selected_preset_name() }; - for (unsigned int i = 1; i < 1000; ++ i) { - char name[64]; - sprintf(name, "filament_%u", i); - if (! config.has("presets", name)) - break; - this->filament_presets.emplace_back(remove_ini_suffix(config.get("presets", name))); - } - - // Update visibility of presets based on their compatibility with the active printer. - // Always try to select a compatible print and filament preset to the current printer preset, - // as the application may have been closed with an active "external" preset, which does not - // exist. - this->update_compatible(PresetSelectCompatibleType::Always); - this->update_multi_material_filament_presets(); -} - -// Export selections (current print, current filaments, current printer) into config.ini -void PresetBundle::export_selections(AppConfig &config) -{ - assert(this->printers.get_edited_preset().printer_technology() != ptFFF || filament_presets.size() >= 1); - assert(this->printers.get_edited_preset().printer_technology() != ptFFF || filament_presets.size() > 1 || filaments.get_selected_preset_name() == filament_presets.front()); - config.clear_section("presets"); - config.set("presets", "print", prints.get_selected_preset_name()); - config.set("presets", "filament", filament_presets.front()); - for (unsigned i = 1; i < filament_presets.size(); ++i) { - char name[64]; - sprintf(name, "filament_%u", i); - config.set("presets", name, filament_presets[i]); - } - - config.set("presets", "sla_print", sla_prints.get_selected_preset_name()); - config.set("presets", "sla_material", sla_materials.get_selected_preset_name()); - config.set("presets", "printer", printers.get_selected_preset_name()); -} - -DynamicPrintConfig PresetBundle::full_config() const -{ - return (this->printers.get_edited_preset().printer_technology() == ptFFF) ? - this->full_fff_config() : - this->full_sla_config(); -} - -DynamicPrintConfig PresetBundle::full_config_secure() const -{ - DynamicPrintConfig config = this->full_config(); - config.erase("print_host"); - config.erase("printhost_apikey"); - config.erase("printhost_cafile"); - return config; -} - -DynamicPrintConfig PresetBundle::full_fff_config() const -{ - DynamicPrintConfig out; - out.apply(FullPrintConfig::defaults()); - out.apply(this->prints.get_edited_preset().config); - // Add the default filament preset to have the "filament_preset_id" defined. - out.apply(this->filaments.default_preset().config); - out.apply(this->printers.get_edited_preset().config); - out.apply(this->project_config); - - auto *nozzle_diameter = dynamic_cast(out.option("nozzle_diameter")); - size_t num_extruders = nozzle_diameter->values.size(); - // Collect the "compatible_printers_condition" and "inherits" values over all presets (print, filaments, printers) into a single vector. - std::vector compatible_printers_condition; - std::vector compatible_prints_condition; - std::vector inherits; - compatible_printers_condition.emplace_back(this->prints.get_edited_preset().compatible_printers_condition()); - inherits .emplace_back(this->prints.get_edited_preset().inherits()); - - if (num_extruders <= 1) { - out.apply(this->filaments.get_edited_preset().config); - compatible_printers_condition.emplace_back(this->filaments.get_edited_preset().compatible_printers_condition()); - compatible_prints_condition .emplace_back(this->filaments.get_edited_preset().compatible_prints_condition()); - inherits .emplace_back(this->filaments.get_edited_preset().inherits()); - } else { - // Retrieve filament presets and build a single config object for them. - // First collect the filament configurations based on the user selection of this->filament_presets. - // Here this->filaments.find_preset() and this->filaments.first_visible() return the edited copy of the preset if active. - std::vector filament_configs; - for (const std::string &filament_preset_name : this->filament_presets) - filament_configs.emplace_back(&this->filaments.find_preset(filament_preset_name, true)->config); - while (filament_configs.size() < num_extruders) - filament_configs.emplace_back(&this->filaments.first_visible().config); - for (const DynamicPrintConfig *cfg : filament_configs) { - // The compatible_prints/printers_condition() returns a reference to configuration key, which may not yet exist. - DynamicPrintConfig &cfg_rw = *const_cast(cfg); - compatible_printers_condition.emplace_back(Preset::compatible_printers_condition(cfg_rw)); - compatible_prints_condition .emplace_back(Preset::compatible_prints_condition(cfg_rw)); - inherits .emplace_back(Preset::inherits(cfg_rw)); - } - // Option values to set a ConfigOptionVector from. - std::vector filament_opts(num_extruders, nullptr); - // loop through options and apply them to the resulting config. - for (const t_config_option_key &key : this->filaments.default_preset().config.keys()) { - if (key == "compatible_prints" || key == "compatible_printers") - continue; - // Get a destination option. - ConfigOption *opt_dst = out.option(key, false); - if (opt_dst->is_scalar()) { - // Get an option, do not create if it does not exist. - const ConfigOption *opt_src = filament_configs.front()->option(key); - if (opt_src != nullptr) - opt_dst->set(opt_src); - } else { - // Setting a vector value from all filament_configs. - for (size_t i = 0; i < filament_opts.size(); ++ i) - filament_opts[i] = filament_configs[i]->option(key); - static_cast(opt_dst)->set(filament_opts); - } - } - } - - // Don't store the "compatible_printers_condition" for the printer profile, there is none. - inherits.emplace_back(this->printers.get_edited_preset().inherits()); - - // These value types clash between the print and filament profiles. They should be renamed. - out.erase("compatible_prints"); - out.erase("compatible_prints_condition"); - out.erase("compatible_printers"); - out.erase("compatible_printers_condition"); - out.erase("inherits"); - - static const char *keys[] = { "perimeter", "infill", "solid_infill", "support_material", "support_material_interface" }; - for (size_t i = 0; i < sizeof(keys) / sizeof(keys[0]); ++ i) { - std::string key = std::string(keys[i]) + "_extruder"; - auto *opt = dynamic_cast(out.option(key, false)); - assert(opt != nullptr); - opt->value = boost::algorithm::clamp(opt->value, 0, int(num_extruders)); - } - - out.option("print_settings_id", true)->value = this->prints.get_selected_preset_name(); - out.option("filament_settings_id", true)->values = this->filament_presets; - out.option("printer_settings_id", true)->value = this->printers.get_selected_preset_name(); - - // Serialize the collected "compatible_printers_condition" and "inherits" fields. - // There will be 1 + num_exturders fields for "inherits" and 2 + num_extruders for "compatible_printers_condition" stored. - // The vector will not be stored if all fields are empty strings. - auto add_if_some_non_empty = [&out](std::vector &&values, const std::string &key) { - bool nonempty = false; - for (const std::string &v : values) - if (! v.empty()) { - nonempty = true; - break; - } - if (nonempty) - out.set_key_value(key, new ConfigOptionStrings(std::move(values))); - }; - add_if_some_non_empty(std::move(compatible_printers_condition), "compatible_printers_condition_cummulative"); - add_if_some_non_empty(std::move(compatible_prints_condition), "compatible_prints_condition_cummulative"); - add_if_some_non_empty(std::move(inherits), "inherits_cummulative"); - - out.option("printer_technology", true)->value = ptFFF; - return out; -} - -DynamicPrintConfig PresetBundle::full_sla_config() const -{ - DynamicPrintConfig out; - out.apply(SLAFullPrintConfig::defaults()); - out.apply(this->sla_prints.get_edited_preset().config); - out.apply(this->sla_materials.get_edited_preset().config); - out.apply(this->printers.get_edited_preset().config); - // There are no project configuration values as of now, the project_config is reserved for FFF printers. -// out.apply(this->project_config); - - // Collect the "compatible_printers_condition" and "inherits" values over all presets (sla_prints, sla_materials, printers) into a single vector. - std::vector compatible_printers_condition; - std::vector compatible_prints_condition; - std::vector inherits; - compatible_printers_condition.emplace_back(this->sla_prints.get_edited_preset().compatible_printers_condition()); - inherits .emplace_back(this->sla_prints.get_edited_preset().inherits()); - compatible_printers_condition.emplace_back(this->sla_materials.get_edited_preset().compatible_printers_condition()); - compatible_prints_condition .emplace_back(this->sla_materials.get_edited_preset().compatible_prints_condition()); - inherits .emplace_back(this->sla_materials.get_edited_preset().inherits()); - inherits .emplace_back(this->printers.get_edited_preset().inherits()); - - // These two value types clash between the print and filament profiles. They should be renamed. - out.erase("compatible_printers"); - out.erase("compatible_printers_condition"); - out.erase("inherits"); - - out.option("sla_print_settings_id", true)->value = this->sla_prints.get_selected_preset_name(); - out.option("sla_material_settings_id", true)->value = this->sla_materials.get_selected_preset_name(); - out.option("printer_settings_id", true)->value = this->printers.get_selected_preset_name(); - - // Serialize the collected "compatible_printers_condition" and "inherits" fields. - // There will be 1 + num_exturders fields for "inherits" and 2 + num_extruders for "compatible_printers_condition" stored. - // The vector will not be stored if all fields are empty strings. - auto add_if_some_non_empty = [&out](std::vector &&values, const std::string &key) { - bool nonempty = false; - for (const std::string &v : values) - if (! v.empty()) { - nonempty = true; - break; - } - if (nonempty) - out.set_key_value(key, new ConfigOptionStrings(std::move(values))); - }; - add_if_some_non_empty(std::move(compatible_printers_condition), "compatible_printers_condition_cummulative"); - add_if_some_non_empty(std::move(compatible_prints_condition), "compatible_prints_condition_cummulative"); - add_if_some_non_empty(std::move(inherits), "inherits_cummulative"); - - out.option("printer_technology", true)->value = ptSLA; - return out; -} - -// Load an external config file containing the print, filament and printer presets. -// Instead of a config file, a G-code may be loaded containing the full set of parameters. -// In the future the configuration will likely be read from an AMF file as well. -// If the file is loaded successfully, its print / filament / printer profiles will be activated. -void PresetBundle::load_config_file(const std::string &path) -{ - if (boost::iends_with(path, ".gcode") || boost::iends_with(path, ".g")) { - DynamicPrintConfig config; - config.apply(FullPrintConfig::defaults()); - config.load_from_gcode_file(path); - Preset::normalize(config); - load_config_file_config(path, true, std::move(config)); - return; - } - - // 1) Try to load the config file into a boost property tree. - boost::property_tree::ptree tree; - try { - boost::nowide::ifstream ifs(path); - boost::property_tree::read_ini(ifs, tree); - } catch (const std::ifstream::failure &err) { - throw std::runtime_error(std::string("The Config Bundle cannot be loaded: ") + path + "\n\tReason: " + err.what()); - } catch (const boost::property_tree::file_parser_error &err) { - throw std::runtime_error((boost::format("Failed loading the Config Bundle \"%1%\": %2% at line %3%") - % err.filename() % err.message() % err.line()).str()); - } catch (const std::runtime_error &err) { - throw std::runtime_error(std::string("Failed loading the preset file: ") + path + "\n\tReason: " + err.what()); - } - - // 2) Continue based on the type of the configuration file. - ConfigFileType config_file_type = guess_config_file_type(tree); - switch (config_file_type) { - case CONFIG_FILE_TYPE_UNKNOWN: - throw std::runtime_error(std::string("Unknown configuration file type: ") + path); - case CONFIG_FILE_TYPE_APP_CONFIG: - throw std::runtime_error(std::string("Invalid configuration file: ") + path + ". This is an application config file."); - case CONFIG_FILE_TYPE_CONFIG: - { - // Initialize a config from full defaults. - DynamicPrintConfig config; - config.apply(FullPrintConfig::defaults()); - config.load(tree); - Preset::normalize(config); - load_config_file_config(path, true, std::move(config)); - break; - } - case CONFIG_FILE_TYPE_CONFIG_BUNDLE: - load_config_file_config_bundle(path, tree); - break; - } -} - -// Load a config file from a boost property_tree. This is a private method called from load_config_file. -void PresetBundle::load_config_file_config(const std::string &name_or_path, bool is_external, DynamicPrintConfig &&config) -{ - PrinterTechnology printer_technology = Preset::printer_technology(config); - - // The "compatible_printers" field should not have been exported into a config.ini or a G-code anyway, - // but some of the alpha versions of Slic3r did. - { - ConfigOption *opt_compatible = config.optptr("compatible_printers"); - if (opt_compatible != nullptr) { - assert(opt_compatible->type() == coStrings); - if (opt_compatible->type() == coStrings) - static_cast(opt_compatible)->values.clear(); - } - } - - size_t num_extruders = (printer_technology == ptFFF) ? - std::min(config.option("nozzle_diameter" )->values.size(), - config.option("filament_diameter")->values.size()) : - // 1 SLA material - 1; - // Make a copy of the "compatible_printers_condition_cummulative" and "inherits_cummulative" vectors, which - // accumulate values over all presets (print, filaments, printers). - // These values will be distributed into their particular presets when loading. - std::vector compatible_printers_condition_values = std::move(config.option("compatible_printers_condition_cummulative", true)->values); - std::vector compatible_prints_condition_values = std::move(config.option("compatible_prints_condition_cummulative", true)->values); - std::vector inherits_values = std::move(config.option("inherits_cummulative", true)->values); - std::string &compatible_printers_condition = Preset::compatible_printers_condition(config); - std::string &compatible_prints_condition = Preset::compatible_prints_condition(config); - std::string &inherits = Preset::inherits(config); - compatible_printers_condition_values.resize(num_extruders + 2, std::string()); - compatible_prints_condition_values.resize(num_extruders, std::string()); - inherits_values.resize(num_extruders + 2, std::string()); - // The "default_filament_profile" will be later extracted into the printer profile. - switch (printer_technology) { - case ptFFF: - config.option("default_print_profile", true); - config.option("default_filament_profile", true)->values.resize(num_extruders, std::string()); - break; - case ptSLA: - config.option("default_sla_print_profile", true); - config.option("default_sla_material_profile", true); - break; - default: break; - } - - // 1) Create a name from the file name. - // Keep the suffix (.ini, .gcode, .amf, .3mf etc) to differentiate it from the normal profiles. - std::string name = is_external ? boost::filesystem::path(name_or_path).filename().string() : name_or_path; - - // 2) If the loading succeeded, split and load the config into print / filament / printer settings. - // First load the print and printer presets. - - auto load_preset = - [&config, &inherits, &inherits_values, - &compatible_printers_condition, &compatible_printers_condition_values, - &compatible_prints_condition, &compatible_prints_condition_values, - is_external, &name, &name_or_path] - (PresetCollection &presets, size_t idx, const std::string &key) { - // Split the "compatible_printers_condition" and "inherits" values one by one from a single vector to the print & printer profiles. - inherits = inherits_values[idx]; - compatible_printers_condition = compatible_printers_condition_values[idx]; - if (idx > 0 && idx - 1 < compatible_prints_condition_values.size()) - compatible_prints_condition = compatible_prints_condition_values[idx - 1]; - if (is_external) - presets.load_external_preset(name_or_path, name, config.opt_string(key, true), config); - else - presets.load_preset(presets.path_from_name(name), name, config).save(); - }; - - switch (Preset::printer_technology(config)) { - case ptFFF: - { - load_preset(this->prints, 0, "print_settings_id"); - load_preset(this->printers, num_extruders + 1, "printer_settings_id"); - - // 3) Now load the filaments. If there are multiple filament presets, split them and load them. - auto old_filament_profile_names = config.option("filament_settings_id", true); - old_filament_profile_names->values.resize(num_extruders, std::string()); - - if (num_extruders <= 1) { - // Split the "compatible_printers_condition" and "inherits" from the cummulative vectors to separate filament presets. - inherits = inherits_values[1]; - compatible_printers_condition = compatible_printers_condition_values[1]; - compatible_prints_condition = compatible_prints_condition_values.front(); - Preset *loaded = nullptr; - if (is_external) { - loaded = &this->filaments.load_external_preset(name_or_path, name, old_filament_profile_names->values.front(), config); - } else { - loaded = &this->filaments.load_preset(this->filaments.path_from_name(name), name, config); - loaded->save(); - } - this->filament_presets.clear(); - this->filament_presets.emplace_back(loaded->name); - } else { - // Split the filament presets, load each of them separately. - std::vector configs(num_extruders, this->filaments.default_preset().config); - // loop through options and scatter them into configs. - for (const t_config_option_key &key : this->filaments.default_preset().config.keys()) { - const ConfigOption *other_opt = config.option(key); - if (other_opt == nullptr) - continue; - if (other_opt->is_scalar()) { - for (size_t i = 0; i < configs.size(); ++ i) - configs[i].option(key, false)->set(other_opt); - } else if (key != "compatible_printers" && key != "compatible_prints") { - for (size_t i = 0; i < configs.size(); ++ i) - static_cast(configs[i].option(key, false))->set_at(other_opt, 0, i); - } - } - // Load the configs into this->filaments and make them active. - this->filament_presets = std::vector(configs.size()); - // To avoid incorrect selection of the first filament preset (means a value of Preset->m_idx_selected) - // in a case when next added preset take a place of previosly selected preset, - // we should add presets from last to first - for (int i = (int)configs.size()-1; i >= 0; i--) { - DynamicPrintConfig &cfg = configs[i]; - // Split the "compatible_printers_condition" and "inherits" from the cummulative vectors to separate filament presets. - cfg.opt_string("compatible_printers_condition", true) = compatible_printers_condition_values[i + 1]; - cfg.opt_string("compatible_prints_condition", true) = compatible_prints_condition_values[i]; - cfg.opt_string("inherits", true) = inherits_values[i + 1]; - // Load all filament presets, but only select the first one in the preset dialog. - Preset *loaded = nullptr; - if (is_external) - loaded = &this->filaments.load_external_preset(name_or_path, name, - (i < int(old_filament_profile_names->values.size())) ? old_filament_profile_names->values[i] : "", - std::move(cfg), i == 0); - else { - // Used by the config wizard when creating a custom setup. - // Therefore this block should only be called for a single extruder. - char suffix[64]; - if (i == 0) - suffix[0] = 0; - else - sprintf(suffix, "%d", (int)i); - std::string new_name = name + suffix; - loaded = &this->filaments.load_preset(this->filaments.path_from_name(new_name), - new_name, std::move(cfg), i == 0); - loaded->save(); - } - this->filament_presets[i] = loaded->name; - } - } - // 4) Load the project config values (the per extruder wipe matrix etc). - this->project_config.apply_only(config, s_project_options); - - CustomGCode::update_custom_gcode_per_print_z_from_config(GUI::wxGetApp().plater()->model().custom_gcode_per_print_z, &this->project_config); - - break; - } - case ptSLA: - load_preset(this->sla_prints, 0, "sla_print_settings_id"); - load_preset(this->sla_materials, 1, "sla_material_settings_id"); - load_preset(this->printers, 2, "printer_settings_id"); - break; - default: break; - } - - this->update_compatible(PresetSelectCompatibleType::Never); -} - -// Load the active configuration of a config bundle from a boost property_tree. This is a private method called from load_config_file. -void PresetBundle::load_config_file_config_bundle(const std::string &path, const boost::property_tree::ptree &tree) -{ - // 1) Load the config bundle into a temp data. - PresetBundle tmp_bundle; - // Load the config bundle, don't save the loaded presets to user profile directory. - tmp_bundle.load_configbundle(path, 0); - std::string bundle_name = std::string(" - ") + boost::filesystem::path(path).filename().string(); - - // 2) Extract active configs from the config bundle, copy them and activate them in this bundle. - auto load_one = [this, &path, &bundle_name](PresetCollection &collection_dst, PresetCollection &collection_src, const std::string &preset_name_src, bool activate) -> std::string { - Preset *preset_src = collection_src.find_preset(preset_name_src, false); - Preset *preset_dst = collection_dst.find_preset(preset_name_src, false); - assert(preset_src != nullptr); - std::string preset_name_dst; - if (preset_dst != nullptr && preset_dst->is_default) { - // No need to copy a default preset, it always exists in collection_dst. - if (activate) - collection_dst.select_preset(0); - return preset_name_src; - } else if (preset_dst != nullptr && preset_src->config == preset_dst->config) { - // Don't save as the config exists in the current bundle and its content is the same. - return preset_name_src; - } else { - // Generate a new unique name. - preset_name_dst = preset_name_src + bundle_name; - Preset *preset_dup = nullptr; - for (size_t i = 1; (preset_dup = collection_dst.find_preset(preset_name_dst, false)) != nullptr; ++ i) { - if (preset_src->config == preset_dup->config) - // The preset has been already copied into collection_dst. - return preset_name_dst; - // Try to generate another name. - char buf[64]; - sprintf(buf, " (%d)", (int)i); - preset_name_dst = preset_name_src + buf + bundle_name; - } - } - assert(! preset_name_dst.empty()); - // Save preset_src->config into collection_dst under preset_name_dst. - // The "compatible_printers" field should not have been exported into a config.ini or a G-code anyway, - // but some of the alpha versions of Slic3r did. - ConfigOption *opt_compatible = preset_src->config.optptr("compatible_printers"); - if (opt_compatible != nullptr) { - assert(opt_compatible->type() == coStrings); - if (opt_compatible->type() == coStrings) - static_cast(opt_compatible)->values.clear(); - } - collection_dst.load_preset(path, preset_name_dst, std::move(preset_src->config), activate).is_external = true; - return preset_name_dst; - }; - load_one(this->prints, tmp_bundle.prints, tmp_bundle.prints .get_selected_preset_name(), true); - load_one(this->sla_prints, tmp_bundle.sla_prints, tmp_bundle.sla_prints .get_selected_preset_name(), true); - load_one(this->filaments, tmp_bundle.filaments, tmp_bundle.filaments .get_selected_preset_name(), true); - load_one(this->sla_materials, tmp_bundle.sla_materials, tmp_bundle.sla_materials.get_selected_preset_name(), true); - load_one(this->printers, tmp_bundle.printers, tmp_bundle.printers .get_selected_preset_name(), true); - this->update_multi_material_filament_presets(); - for (size_t i = 1; i < std::min(tmp_bundle.filament_presets.size(), this->filament_presets.size()); ++ i) - this->filament_presets[i] = load_one(this->filaments, tmp_bundle.filaments, tmp_bundle.filament_presets[i], false); - - this->update_compatible(PresetSelectCompatibleType::Never); -} - -// Process the Config Bundle loaded as a Boost property tree. -// For each print, filament and printer preset (group defined by group_name), apply the inherited presets. -// The presets starting with '*' are considered non-terminal and they are -// removed through the flattening process by this function. -// This function will never fail, but it will produce error messages through boost::log. -// system_profiles will not be flattened, and they will be kept inside the "inherits" field -static void flatten_configbundle_hierarchy(boost::property_tree::ptree &tree, const std::string &group_name, const std::vector &system_profiles) -{ - namespace pt = boost::property_tree; - - // 1) For the group given by group_name, initialize the presets. - struct Prst { - Prst(const std::string &name, pt::ptree *node) : name(name), node(node) {} - // Name of this preset. If the name starts with '*', it is an intermediate preset, - // which will not make it into the result. - const std::string name; - // Link to the source boost property tree node, owned by tree. - pt::ptree *node; - // Link to the presets, from which this preset inherits. - std::vector inherits; - // Link to the presets, for which this preset is a direct parent. - std::vector parent_of; - // When running the Kahn's Topological sorting algorithm, this counter is decreased from inherits.size() to zero. - // A cycle is indicated, if the number does not drop to zero after the Kahn's algorithm finishes. - size_t num_incoming_edges_left = 0; - // Sorting by the name, to be used when inserted into std::set. - bool operator==(const Prst &rhs) const { return this->name == rhs.name; } - bool operator< (const Prst &rhs) const { return this->name < rhs.name; } - }; - // Find the presets, store them into a std::map, addressed by their names. - std::set presets; - std::string group_name_preset = group_name + ":"; - for (auto §ion : tree) - if (boost::starts_with(section.first, group_name_preset) && section.first.size() > group_name_preset.size()) - presets.emplace(section.first.substr(group_name_preset.size()), §ion.second); - // Fill in the "inherits" and "parent_of" members, report invalid inheritance fields. - for (const Prst &prst : presets) { - // Parse the list of comma separated values, possibly enclosed in quotes. - std::vector inherits_names; - std::vector inherits_system; - if (Slic3r::unescape_strings_cstyle(prst.node->get("inherits", ""), inherits_names)) { - // Resolve the inheritance by name. - std::vector &inherits_nodes = const_cast(prst).inherits; - for (const std::string &node_name : inherits_names) { - auto it_system = std::lower_bound(system_profiles.begin(), system_profiles.end(), node_name); - if (it_system != system_profiles.end() && *it_system == node_name) { - // Loading a user config budnle, this preset is derived from a system profile. - inherits_system.emplace_back(node_name); - } else { - auto it = presets.find(Prst(node_name, nullptr)); - if (it == presets.end()) - BOOST_LOG_TRIVIAL(error) << "flatten_configbundle_hierarchy: The preset " << prst.name << " inherits an unknown preset \"" << node_name << "\""; - else { - inherits_nodes.emplace_back(const_cast(&(*it))); - inherits_nodes.back()->parent_of.emplace_back(const_cast(&prst)); - } - } - } - } else { - BOOST_LOG_TRIVIAL(error) << "flatten_configbundle_hierarchy: The preset " << prst.name << " has an invalid \"inherits\" field"; - } - // Remove the "inherits" key, it has no meaning outside of the config bundle. - const_cast(prst.node)->erase("inherits"); - if (! inherits_system.empty()) { - // Loaded a user config bundle, where a profile inherits a system profile. - // User profile should be derived from a single system profile only. - assert(inherits_system.size() == 1); - if (inherits_system.size() > 1) - BOOST_LOG_TRIVIAL(error) << "flatten_configbundle_hierarchy: The preset " << prst.name << " inherits from more than single system preset"; - prst.node->put("inherits", Slic3r::escape_string_cstyle(inherits_system.front())); - } - } - - // 2) Create a linear ordering for the directed acyclic graph of preset inheritance. - // https://en.wikipedia.org/wiki/Topological_sorting - // Kahn's algorithm. - std::vector sorted; - { - // Initialize S with the set of all nodes with no incoming edge. - std::deque S; - for (const Prst &prst : presets) - if (prst.inherits.empty()) - S.emplace_back(const_cast(&prst)); - else - const_cast(&prst)->num_incoming_edges_left = prst.inherits.size(); - while (! S.empty()) { - Prst *n = S.front(); - S.pop_front(); - sorted.emplace_back(n); - for (Prst *m : n->parent_of) { - assert(m->num_incoming_edges_left > 0); - if (-- m->num_incoming_edges_left == 0) { - // We have visited all parents of m. - S.emplace_back(m); - } - } - } - if (sorted.size() < presets.size()) { - for (const Prst &prst : presets) - if (prst.num_incoming_edges_left) - BOOST_LOG_TRIVIAL(error) << "flatten_configbundle_hierarchy: The preset " << prst.name << " has cyclic dependencies"; - } - } - - // Apply the dependencies in their topological ordering. - for (Prst *prst : sorted) { - // Merge the preset nodes in their order of application. - // Iterate in a reverse order, so the last change will be placed first in merged. - for (auto it_inherits = prst->inherits.rbegin(); it_inherits != prst->inherits.rend(); ++ it_inherits) - for (auto it = (*it_inherits)->node->begin(); it != (*it_inherits)->node->end(); ++ it) - if (it->first == "renamed_from") { - // Don't inherit "renamed_from" flag, it does not make sense. The "renamed_from" flag only makes sense for a concrete preset. - if (boost::starts_with((*it_inherits)->name, "*")) - BOOST_LOG_TRIVIAL(error) << boost::format("Nonpublic intermediate preset %1% contains a \"renamed_from\" field, which is ignored") % (*it_inherits)->name; - } else if (prst->node->find(it->first) == prst->node->not_found()) - prst->node->add_child(it->first, it->second); - } - - // Remove the "internal" presets from the ptree. These presets are marked with '*'. - group_name_preset += '*'; - for (auto it_section = tree.begin(); it_section != tree.end(); ) { - if (boost::starts_with(it_section->first, group_name_preset) && it_section->first.size() > group_name_preset.size()) - // Remove the "internal" preset from the ptree. - it_section = tree.erase(it_section); - else - // Keep the preset. - ++ it_section; - } -} - -// preset_bundle is set when loading user config bundles, which must not overwrite the system profiles. -static void flatten_configbundle_hierarchy(boost::property_tree::ptree &tree, const PresetBundle *preset_bundle) -{ - flatten_configbundle_hierarchy(tree, "print", preset_bundle ? preset_bundle->prints.system_preset_names() : std::vector()); - flatten_configbundle_hierarchy(tree, "filament", preset_bundle ? preset_bundle->filaments.system_preset_names() : std::vector()); - flatten_configbundle_hierarchy(tree, "sla_print", preset_bundle ? preset_bundle->sla_prints.system_preset_names() : std::vector()); - flatten_configbundle_hierarchy(tree, "sla_material", preset_bundle ? preset_bundle->sla_materials.system_preset_names() : std::vector()); - flatten_configbundle_hierarchy(tree, "printer", preset_bundle ? preset_bundle->printers.system_preset_names() : std::vector()); -} - -// Load a config bundle file, into presets and store the loaded presets into separate files -// of the local configuration directory. -size_t PresetBundle::load_configbundle(const std::string &path, unsigned int flags) -{ - if (flags & (LOAD_CFGBNDLE_RESET_USER_PROFILE | LOAD_CFGBNDLE_SYSTEM)) - // Reset this bundle, delete user profile files if LOAD_CFGBNDLE_SAVE. - this->reset(flags & LOAD_CFGBNDLE_SAVE); - - // 1) Read the complete config file into a boost::property_tree. - namespace pt = boost::property_tree; - pt::ptree tree; - boost::nowide::ifstream ifs(path); - pt::read_ini(ifs, tree); - - const VendorProfile *vendor_profile = nullptr; - if (flags & (LOAD_CFGBNDLE_SYSTEM | LOAD_CFGBUNDLE_VENDOR_ONLY)) { - auto vp = VendorProfile::from_ini(tree, path); - if (vp.models.size() == 0) { - BOOST_LOG_TRIVIAL(error) << boost::format("Vendor bundle: `%1%`: No printer model defined.") % path; - return 0; - } else if (vp.num_variants() == 0) { - BOOST_LOG_TRIVIAL(error) << boost::format("Vendor bundle: `%1%`: No printer variant defined") % path; - return 0; - } - vendor_profile = &this->vendors.insert({vp.id, vp}).first->second; - } - - if (flags & LOAD_CFGBUNDLE_VENDOR_ONLY) { - return 0; - } - - // 1.5) Flatten the config bundle by applying the inheritance rules. Internal profiles (with names starting with '*') are removed. - // If loading a user config bundle, do not flatten with the system profiles, but keep the "inherits" flag intact. - flatten_configbundle_hierarchy(tree, ((flags & LOAD_CFGBNDLE_SYSTEM) == 0) ? this : nullptr); - - // 2) Parse the property_tree, extract the active preset names and the profiles, save them into local config files. - // Parse the obsolete preset names, to be deleted when upgrading from the old configuration structure. - std::vector loaded_prints; - std::vector loaded_filaments; - std::vector loaded_sla_prints; - std::vector loaded_sla_materials; - std::vector loaded_printers; - std::string active_print; - std::vector active_filaments; - std::string active_sla_print; - std::string active_sla_material; - std::string active_printer; - size_t presets_loaded = 0; - for (const auto §ion : tree) { - PresetCollection *presets = nullptr; - std::vector *loaded = nullptr; - std::string preset_name; - if (boost::starts_with(section.first, "print:")) { - presets = &this->prints; - loaded = &loaded_prints; - preset_name = section.first.substr(6); - } else if (boost::starts_with(section.first, "filament:")) { - presets = &this->filaments; - loaded = &loaded_filaments; - preset_name = section.first.substr(9); - } else if (boost::starts_with(section.first, "sla_print:")) { - presets = &this->sla_prints; - loaded = &loaded_sla_prints; - preset_name = section.first.substr(10); - } else if (boost::starts_with(section.first, "sla_material:")) { - presets = &this->sla_materials; - loaded = &loaded_sla_materials; - preset_name = section.first.substr(13); - } else if (boost::starts_with(section.first, "printer:")) { - presets = &this->printers; - loaded = &loaded_printers; - preset_name = section.first.substr(8); - } else if (section.first == "presets") { - // Load the names of the active presets. - for (auto &kvp : section.second) { - if (kvp.first == "print") { - active_print = kvp.second.data(); - } else if (boost::starts_with(kvp.first, "filament")) { - int idx = 0; - if (kvp.first == "filament" || sscanf(kvp.first.c_str(), "filament_%d", &idx) == 1) { - if (int(active_filaments.size()) <= idx) - active_filaments.resize(idx + 1, std::string()); - active_filaments[idx] = kvp.second.data(); - } - } else if (kvp.first == "sla_print") { - active_sla_print = kvp.second.data(); - } else if (kvp.first == "sla_material") { - active_sla_material = kvp.second.data(); - } else if (kvp.first == "printer") { - active_printer = kvp.second.data(); - } - } - } else if (section.first == "obsolete_presets") { - // Parse the names of obsolete presets. These presets will be deleted from user's - // profile directory on installation of this vendor preset. - for (auto &kvp : section.second) { - std::vector *dst = nullptr; - if (kvp.first == "print") - dst = &this->obsolete_presets.prints; - else if (kvp.first == "filament") - dst = &this->obsolete_presets.filaments; - else if (kvp.first == "sla_print") - dst = &this->obsolete_presets.sla_prints; - else if (kvp.first == "sla_material") - dst = &this->obsolete_presets.sla_materials; - else if (kvp.first == "printer") - dst = &this->obsolete_presets.printers; - if (dst) - unescape_strings_cstyle(kvp.second.data(), *dst); - } - } else if (section.first == "settings") { - // Load the settings. - for (auto &kvp : section.second) { - if (kvp.first == "autocenter") { - } - } - } else - // Ignore an unknown section. - continue; - if (presets != nullptr) { - // Load the print, filament or printer preset. - const DynamicPrintConfig *default_config = nullptr; - DynamicPrintConfig config; - std::string alias_name; - std::vector renamed_from; - auto parse_config_section = [§ion, &alias_name, &renamed_from, &path](DynamicPrintConfig &config) { - for (auto &kvp : section.second) { - if (kvp.first == "alias") - alias_name = kvp.second.data(); - else if (kvp.first == "renamed_from") { - if (! unescape_strings_cstyle(kvp.second.data(), renamed_from)) { - BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The preset \"" << - section.first << "\" contains invalid \"renamed_from\" key, which is being ignored."; - } - } - config.set_deserialize(kvp.first, kvp.second.data()); - } - }; - if (presets == &this->printers) { - // Select the default config based on the printer_technology field extracted from kvp. - DynamicPrintConfig config_src; - parse_config_section(config_src); - default_config = &presets->default_preset_for(config_src).config; - config = *default_config; - config.apply(config_src); - } else { - default_config = &presets->default_preset().config; - config = *default_config; - parse_config_section(config); - } - Preset::normalize(config); - // Report configuration fields, which are misplaced into a wrong group. - std::string incorrect_keys = Preset::remove_invalid_keys(config, *default_config); - if (! incorrect_keys.empty()) - BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" << - section.first << "\" contains the following incorrect keys: " << incorrect_keys << ", which were removed"; - if ((flags & LOAD_CFGBNDLE_SYSTEM) && presets == &printers) { - // Filter out printer presets, which are not mentioned in the vendor profile. - // These presets are considered not installed. - auto printer_model = config.opt_string("printer_model"); - if (printer_model.empty()) { - BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" << - section.first << "\" defines no printer model, it will be ignored."; - continue; - } - auto printer_variant = config.opt_string("printer_variant"); - if (printer_variant.empty()) { - BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" << - section.first << "\" defines no printer variant, it will be ignored."; - continue; - } - auto it_model = std::find_if(vendor_profile->models.cbegin(), vendor_profile->models.cend(), - [&](const VendorProfile::PrinterModel &m) { return m.id == printer_model; } - ); - if (it_model == vendor_profile->models.end()) { - BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" << - section.first << "\" defines invalid printer model \"" << printer_model << "\", it will be ignored."; - continue; - } - auto it_variant = it_model->variant(printer_variant); - if (it_variant == nullptr) { - BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" << - section.first << "\" defines invalid printer variant \"" << printer_variant << "\", it will be ignored."; - continue; - } - const Preset *preset_existing = presets->find_preset(section.first, false); - if (preset_existing != nullptr) { - BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" << - section.first << "\" has already been loaded from another Confing Bundle."; - continue; - } - } else if ((flags & LOAD_CFGBNDLE_SYSTEM) == 0) { - // This is a user config bundle. - const Preset *existing = presets->find_preset(preset_name, false); - if (existing != nullptr) { - if (existing->is_system) { - assert(existing->vendor != nullptr); - BOOST_LOG_TRIVIAL(error) << "Error in a user provided Config Bundle \"" << path << "\": The " << presets->name() << " preset \"" << - existing->name << "\" is a system preset of vendor " << existing->vendor->name << " and it will be ignored."; - continue; - } else { - assert(existing->vendor == nullptr); - BOOST_LOG_TRIVIAL(trace) << "A " << presets->name() << " preset \"" << existing->name << "\" was overwritten with a preset from user Config Bundle \"" << path << "\""; - } - } else { - BOOST_LOG_TRIVIAL(trace) << "A new " << presets->name() << " preset \"" << preset_name << "\" was imported from user Config Bundle \"" << path << "\""; - } - } - // Decide a full path to this .ini file. - auto file_name = boost::algorithm::iends_with(preset_name, ".ini") ? preset_name : preset_name + ".ini"; - auto file_path = (boost::filesystem::path(data_dir()) -#ifdef SLIC3R_PROFILE_USE_PRESETS_SUBDIR - // Store the print/filament/printer presets into a "presets" directory. - / "presets" -#else - // Store the print/filament/printer presets at the same location as the upstream Slic3r. -#endif - / presets->section_name() / file_name).make_preferred(); - // Load the preset into the list of presets, save it to disk. - Preset &loaded = presets->load_preset(file_path.string(), preset_name, std::move(config), false); - if (flags & LOAD_CFGBNDLE_SAVE) - loaded.save(); - if (flags & LOAD_CFGBNDLE_SYSTEM) { - loaded.is_system = true; - loaded.vendor = vendor_profile; - } - - // Derive the profile logical name aka alias from the preset name if the alias was not stated explicitely. - if (alias_name.empty()) { - size_t end_pos = preset_name.find_first_of("@"); - if (end_pos != std::string::npos) { - alias_name = preset_name.substr(0, end_pos); - if (renamed_from.empty()) - // Add the preset name with the '@' character removed into the "renamed_from" list. - renamed_from.emplace_back(alias_name + preset_name.substr(end_pos + 1)); - boost::trim_right(alias_name); - } - } - if (alias_name.empty()) - loaded.alias = preset_name; - else - loaded.alias = std::move(alias_name); - loaded.renamed_from = std::move(renamed_from); - - ++ presets_loaded; - } - } - - // 3) Activate the presets. - if ((flags & LOAD_CFGBNDLE_SYSTEM) == 0) { - if (! active_print.empty()) - prints.select_preset_by_name(active_print, true); - if (! active_sla_print.empty()) - sla_materials.select_preset_by_name(active_sla_print, true); - if (! active_sla_material.empty()) - sla_materials.select_preset_by_name(active_sla_material, true); - if (! active_printer.empty()) - printers.select_preset_by_name(active_printer, true); - // Activate the first filament preset. - if (! active_filaments.empty() && ! active_filaments.front().empty()) - filaments.select_preset_by_name(active_filaments.front(), true); - this->update_multi_material_filament_presets(); - for (size_t i = 0; i < std::min(this->filament_presets.size(), active_filaments.size()); ++ i) - this->filament_presets[i] = filaments.find_preset(active_filaments[i], true)->name; - this->update_compatible(PresetSelectCompatibleType::Never); - } - - return presets_loaded; -} - -void PresetBundle::update_multi_material_filament_presets() -{ - if (printers.get_edited_preset().printer_technology() != ptFFF) - return; - - // Verify and select the filament presets. - auto *nozzle_diameter = static_cast(printers.get_edited_preset().config.option("nozzle_diameter")); - size_t num_extruders = nozzle_diameter->values.size(); - // Verify validity of the current filament presets. - for (size_t i = 0; i < std::min(this->filament_presets.size(), num_extruders); ++ i) - this->filament_presets[i] = this->filaments.find_preset(this->filament_presets[i], true)->name; - // Append the rest of filament presets. - this->filament_presets.resize(num_extruders, this->filament_presets.empty() ? this->filaments.first_visible().name : this->filament_presets.back()); - - // Now verify if wiping_volumes_matrix has proper size (it is used to deduce number of extruders in wipe tower generator): - std::vector old_matrix = this->project_config.option("wiping_volumes_matrix")->values; - size_t old_number_of_extruders = size_t(sqrt(old_matrix.size())+EPSILON); - if (num_extruders != old_number_of_extruders) { - // First verify if purging volumes presets for each extruder matches number of extruders - std::vector& extruders = this->project_config.option("wiping_volumes_extruders")->values; - while (extruders.size() < 2*num_extruders) { - extruders.push_back(extruders.size()>1 ? extruders[0] : 50.); // copy the values from the first extruder - extruders.push_back(extruders.size()>1 ? extruders[1] : 50.); - } - while (extruders.size() > 2*num_extruders) { - extruders.pop_back(); - extruders.pop_back(); - } - - std::vector new_matrix; - for (unsigned int i=0;iproject_config.option("wiping_volumes_matrix")->values = new_matrix; - } -} - -void PresetBundle::update_compatible(PresetSelectCompatibleType select_other_print_if_incompatible, PresetSelectCompatibleType select_other_filament_if_incompatible) -{ - const Preset &printer_preset = this->printers.get_edited_preset(); - const PresetWithVendorProfile printer_preset_with_vendor_profile = this->printers.get_preset_with_vendor_profile(printer_preset); - - switch (printer_preset.printer_technology()) { - case ptFFF: - { - assert(printer_preset.config.has("default_print_profile")); - assert(printer_preset.config.has("default_filament_profile")); - const std::string &prefered_print_profile = printer_preset.config.opt_string("default_print_profile"); - const std::vector &prefered_filament_profiles = printer_preset.config.option("default_filament_profile")->values; - prefered_print_profile.empty() ? - this->prints.update_compatible(printer_preset_with_vendor_profile, nullptr, select_other_print_if_incompatible) : - this->prints.update_compatible(printer_preset_with_vendor_profile, nullptr, select_other_print_if_incompatible, - [&prefered_print_profile](const std::string& profile_name) { return profile_name == prefered_print_profile; }); - const PresetWithVendorProfile print_preset_with_vendor_profile = this->prints.get_edited_preset_with_vendor_profile(); - // Remember whether the filament profiles were compatible before updating the filament compatibility. - std::vector filament_preset_was_compatible(this->filament_presets.size(), false); - for (size_t idx = 0; idx < this->filament_presets.size(); ++ idx) { - std::string &filament_name = this->filament_presets[idx]; - Preset *preset = this->filaments.find_preset(filament_name, false); - filament_preset_was_compatible[idx] = preset != nullptr && preset->is_compatible; - } - prefered_filament_profiles.empty() ? - this->filaments.update_compatible(printer_preset_with_vendor_profile, &print_preset_with_vendor_profile, select_other_filament_if_incompatible) : - this->filaments.update_compatible(printer_preset_with_vendor_profile, &print_preset_with_vendor_profile, select_other_filament_if_incompatible, - [&prefered_filament_profiles](const std::string& profile_name) - { return std::find(prefered_filament_profiles.begin(), prefered_filament_profiles.end(), profile_name) != prefered_filament_profiles.end(); }); - if (select_other_filament_if_incompatible != PresetSelectCompatibleType::Never) { - // Verify validity of the current filament presets. - if (this->filament_presets.size() == 1) { - if (select_other_filament_if_incompatible == PresetSelectCompatibleType::Always || filament_preset_was_compatible.front()) - this->filament_presets.front() = this->filaments.get_edited_preset().name; - } else { - for (size_t idx = 0; idx < this->filament_presets.size(); ++ idx) { - std::string &filament_name = this->filament_presets[idx]; - Preset *preset = this->filaments.find_preset(filament_name, false); - if (preset == nullptr || (! preset->is_compatible && (select_other_filament_if_incompatible == PresetSelectCompatibleType::Always || filament_preset_was_compatible[idx]))) { - // Pick a compatible profile. If there are prefered_filament_profiles, use them. - if (prefered_filament_profiles.empty()) - filament_name = this->filaments.first_compatible().name; - else { - const std::string &preferred = (idx < prefered_filament_profiles.size()) ? - prefered_filament_profiles[idx] : prefered_filament_profiles.front(); - filament_name = this->filaments.first_compatible( - [&preferred](const std::string& profile_name) { return profile_name == preferred; }).name; - } - } - } - } - } - break; - } - case ptSLA: - { - assert(printer_preset.config.has("default_sla_print_profile")); - assert(printer_preset.config.has("default_sla_material_profile")); - const PresetWithVendorProfile sla_print_preset_with_vendor_profile = this->sla_prints.get_edited_preset_with_vendor_profile(); - const std::string &prefered_sla_print_profile = printer_preset.config.opt_string("default_sla_print_profile"); - (prefered_sla_print_profile.empty()) ? - this->sla_prints.update_compatible(printer_preset_with_vendor_profile, nullptr, select_other_print_if_incompatible) : - this->sla_prints.update_compatible(printer_preset_with_vendor_profile, nullptr, select_other_print_if_incompatible, - [&prefered_sla_print_profile](const std::string& profile_name){ return profile_name == prefered_sla_print_profile; }); - const std::string &prefered_sla_material_profile = printer_preset.config.opt_string("default_sla_material_profile"); - prefered_sla_material_profile.empty() ? - this->sla_materials.update_compatible(printer_preset_with_vendor_profile, &sla_print_preset_with_vendor_profile, select_other_filament_if_incompatible) : - this->sla_materials.update_compatible(printer_preset_with_vendor_profile, &sla_print_preset_with_vendor_profile, select_other_filament_if_incompatible, - [&prefered_sla_material_profile](const std::string& profile_name){ return profile_name == prefered_sla_material_profile; }); - break; - } - default: break; - } -} - -void PresetBundle::export_configbundle(const std::string &path, bool export_system_settings) -{ - boost::nowide::ofstream c; - c.open(path, std::ios::out | std::ios::trunc); - - // Put a comment at the first line including the time stamp and Slic3r version. - c << "# " << Slic3r::header_slic3r_generated() << std::endl; - - // Export the print, filament and printer profiles. - - for (const PresetCollection *presets : { - (const PresetCollection*)&this->prints, (const PresetCollection*)&this->filaments, - (const PresetCollection*)&this->sla_prints, (const PresetCollection*)&this->sla_materials, - (const PresetCollection*)&this->printers }) { - for (const Preset &preset : (*presets)()) { - if (preset.is_default || preset.is_external || (preset.is_system && ! export_system_settings)) - // Only export the common presets, not external files or the default preset. - continue; - c << std::endl << "[" << presets->section_name() << ":" << preset.name << "]" << std::endl; - for (const std::string &opt_key : preset.config.keys()) - c << opt_key << " = " << preset.config.opt_serialize(opt_key) << std::endl; - } - } - - // Export the names of the active presets. - c << std::endl << "[presets]" << std::endl; - c << "print = " << this->prints.get_selected_preset_name() << std::endl; - c << "sla_print = " << this->sla_prints.get_selected_preset_name() << std::endl; - c << "sla_material = " << this->sla_materials.get_selected_preset_name() << std::endl; - c << "printer = " << this->printers.get_selected_preset_name() << std::endl; - for (size_t i = 0; i < this->filament_presets.size(); ++ i) { - char suffix[64]; - if (i > 0) - sprintf(suffix, "_%d", (int)i); - else - suffix[0] = 0; - c << "filament" << suffix << " = " << this->filament_presets[i] << std::endl; - } - -#if 0 - // Export the following setting values from the provided setting repository. - static const char *settings_keys[] = { "autocenter" }; - c << "[settings]" << std::endl; - for (size_t i = 0; i < sizeof(settings_keys) / sizeof(settings_keys[0]); ++ i) - c << settings_keys[i] << " = " << settings.serialize(settings_keys[i]) << std::endl; -#endif - - c.close(); -} - -// Set the filament preset name. As the name could come from the UI selection box, -// an optional "(modified)" suffix will be removed from the filament name. -void PresetBundle::set_filament_preset(size_t idx, const std::string &name) -{ - if (idx >= filament_presets.size()) - filament_presets.resize(idx + 1, filaments.default_preset().name); - filament_presets[idx] = Preset::remove_suffix_modified(name); -} - -void PresetBundle::set_default_suppressed(bool default_suppressed) -{ - prints.set_default_suppressed(default_suppressed); - filaments.set_default_suppressed(default_suppressed); - sla_prints.set_default_suppressed(default_suppressed); - sla_materials.set_default_suppressed(default_suppressed); - printers.set_default_suppressed(default_suppressed); -} - -} // namespace Slic3r diff --git a/src/slic3r/GUI/PresetBundle.hpp b/src/slic3r/GUI/PresetBundle.hpp deleted file mode 100644 index 7d137bb7a..000000000 --- a/src/slic3r/GUI/PresetBundle.hpp +++ /dev/null @@ -1,163 +0,0 @@ -#ifndef slic3r_PresetBundle_hpp_ -#define slic3r_PresetBundle_hpp_ - -#include "AppConfig.hpp" -#include "Preset.hpp" - -#include -#include -#include - -class wxWindow; - -namespace Slic3r { - -// Bundle of Print + Filament + Printer presets. -class PresetBundle -{ -public: - PresetBundle(); - ~PresetBundle(); - - // Remove all the presets but the "-- default --". - // Optionally remove all the files referenced by the presets from the user profile directory. - void reset(bool delete_files); - - void setup_directories(); - - // Load ini files of all types (print, filament, printer) from Slic3r::data_dir() / presets. - // Load selections (current print, current filaments, current printer) from config.ini - // This is done just once on application start up. - void load_presets(AppConfig &config, const std::string &preferred_model_id = ""); - - // Export selections (current print, current filaments, current printer) into config.ini - void export_selections(AppConfig &config); - - PresetCollection prints; - PresetCollection sla_prints; - PresetCollection filaments; - PresetCollection sla_materials; - PresetCollection& materials(PrinterTechnology pt) { return pt == ptFFF ? this->filaments : this->sla_materials; } - const PresetCollection& materials(PrinterTechnology pt) const { return pt == ptFFF ? this->filaments : this->sla_materials; } - PrinterPresetCollection printers; - // Filament preset names for a multi-extruder or multi-material print. - // extruders.size() should be the same as printers.get_edited_preset().config.nozzle_diameter.size() - std::vector filament_presets; - - // The project configuration values are kept separated from the print/filament/printer preset, - // they are being serialized / deserialized from / to the .amf, .3mf, .config, .gcode, - // and they are being used by slicing core. - DynamicPrintConfig project_config; - - // There will be an entry for each system profile loaded, - // and the system profiles will point to the VendorProfile instances owned by PresetBundle::vendors. - VendorMap vendors; - - struct ObsoletePresets { - std::vector prints; - std::vector sla_prints; - std::vector filaments; - std::vector sla_materials; - std::vector printers; - }; - ObsoletePresets obsolete_presets; - - bool has_defauls_only() const - { return prints.has_defaults_only() && filaments.has_defaults_only() && printers.has_defaults_only(); } - - DynamicPrintConfig full_config() const; - // full_config() with the "printhost_apikey" and "printhost_cafile" removed. - DynamicPrintConfig full_config_secure() const; - - // Load user configuration and store it into the user profiles. - // This method is called by the configuration wizard. - void load_config(const std::string &name, DynamicPrintConfig config) - { this->load_config_file_config(name, false, std::move(config)); } - - // Load configuration that comes from a model file containing configuration, such as 3MF et al. - // This method is called by the Plater. - void load_config_model(const std::string &name, DynamicPrintConfig config) - { this->load_config_file_config(name, true, std::move(config)); } - - // Load an external config file containing the print, filament and printer presets. - // Instead of a config file, a G-code may be loaded containing the full set of parameters. - // In the future the configuration will likely be read from an AMF file as well. - // If the file is loaded successfully, its print / filament / printer profiles will be activated. - void load_config_file(const std::string &path); - - // Load a config bundle file, into presets and store the loaded presets into separate files - // of the local configuration directory. - // Load settings into the provided settings instance. - // Activate the presets stored in the config bundle. - // Returns the number of presets loaded successfully. - enum { - // Save the profiles, which have been loaded. - LOAD_CFGBNDLE_SAVE = 1, - // Delete all old config profiles before loading. - LOAD_CFGBNDLE_RESET_USER_PROFILE = 2, - // Load a system config bundle. - LOAD_CFGBNDLE_SYSTEM = 4, - LOAD_CFGBUNDLE_VENDOR_ONLY = 8, - }; - // Load the config bundle, store it to the user profile directory by default. - size_t load_configbundle(const std::string &path, unsigned int flags = LOAD_CFGBNDLE_SAVE); - - // Export a config bundle file containing all the presets and the names of the active presets. - void export_configbundle(const std::string &path, bool export_system_settings = false); - - // Enable / disable the "- default -" preset. - void set_default_suppressed(bool default_suppressed); - - // Set the filament preset name. As the name could come from the UI selection box, - // an optional "(modified)" suffix will be removed from the filament name. - void set_filament_preset(size_t idx, const std::string &name); - - // Read out the number of extruders from an active printer preset, - // update size and content of filament_presets. - void update_multi_material_filament_presets(); - - // Update the is_compatible flag of all print and filament presets depending on whether they are marked - // as compatible with the currently selected printer (and print in case of filament presets). - // Also updates the is_visible flag of each preset. - // If select_other_if_incompatible is true, then the print or filament preset is switched to some compatible - // preset if the current print or filament preset is not compatible. - void update_compatible(PresetSelectCompatibleType select_other_print_if_incompatible, PresetSelectCompatibleType select_other_filament_if_incompatible); - void update_compatible(PresetSelectCompatibleType select_other_if_incompatible) { this->update_compatible(select_other_if_incompatible, select_other_if_incompatible); } - - // Set the is_visible flag for printer vendors, printer models and printer variants - // based on the user configuration. - // If the "vendor" section is missing, enable all models and variants of the particular vendor. - void load_installed_printers(const AppConfig &config); - - const std::string& get_preset_name_by_alias(const Preset::Type& preset_type, const std::string& alias) const; - - static const char *PRUSA_BUNDLE; -private: - std::string load_system_presets(); - // Merge one vendor's presets with the other vendor's presets, report duplicates. - std::vector merge_presets(PresetBundle &&other); - // Update renamed_from and alias maps of system profiles. - void update_system_maps(); - - // Set the is_visible flag for filaments and sla materials, - // apply defaults based on enabled printers when no filaments/materials are installed. - void load_installed_filaments(AppConfig &config); - void load_installed_sla_materials(AppConfig &config); - - // Load selections (current print, current filaments, current printer) from config.ini - // This is done just once on application start up. - void load_selections(AppConfig &config, const std::string &preferred_model_id = ""); - - // Load print, filament & printer presets from a config. If it is an external config, then the name is extracted from the external path. - // and the external config is just referenced, not stored into user profile directory. - // If it is not an external config, then the config will be stored into the user profile directory. - void load_config_file_config(const std::string &name_or_path, bool is_external, DynamicPrintConfig &&config); - void load_config_file_config_bundle(const std::string &path, const boost::property_tree::ptree &tree); - - DynamicPrintConfig full_fff_config() const; - DynamicPrintConfig full_sla_config() const; -}; - -} // namespace Slic3r - -#endif /* slic3r_PresetBundle_hpp_ */ diff --git a/src/slic3r/GUI/PresetComboBoxes.cpp b/src/slic3r/GUI/PresetComboBoxes.cpp index 380edb48a..51b5a0c8d 100644 --- a/src/slic3r/GUI/PresetComboBoxes.cpp +++ b/src/slic3r/GUI/PresetComboBoxes.cpp @@ -15,6 +15,7 @@ #include "libslic3r/libslic3r.h" #include "libslic3r/PrintConfig.hpp" +#include "libslic3r/PresetBundle.hpp" #include "GUI.hpp" #include "GUI_App.hpp" @@ -22,7 +23,6 @@ #include "MainFrame.hpp" #include "format.hpp" #include "Tab.hpp" -#include "PresetBundle.hpp" #include "PrintHostDialogs.hpp" #include "ConfigWizard.hpp" #include "../Utils/ASCIIFolding.hpp" diff --git a/src/slic3r/GUI/PresetComboBoxes.hpp b/src/slic3r/GUI/PresetComboBoxes.hpp index 63110e432..3d9a13490 100644 --- a/src/slic3r/GUI/PresetComboBoxes.hpp +++ b/src/slic3r/GUI/PresetComboBoxes.hpp @@ -7,7 +7,7 @@ #include #include -#include "Preset.hpp" +#include "libslic3r/Preset.hpp" #include "wxExtensions.hpp" #include "GUI_Utils.hpp" diff --git a/src/slic3r/GUI/PresetHints.cpp b/src/slic3r/GUI/PresetHints.cpp index 24afeb526..c40c4c6ac 100644 --- a/src/slic3r/GUI/PresetHints.cpp +++ b/src/slic3r/GUI/PresetHints.cpp @@ -4,7 +4,6 @@ #include "libslic3r/Slicing.hpp" #include "libslic3r/libslic3r.h" -#include "PresetBundle.hpp" #include "PresetHints.hpp" #include diff --git a/src/slic3r/GUI/PresetHints.hpp b/src/slic3r/GUI/PresetHints.hpp index be049c2c8..a61310f40 100644 --- a/src/slic3r/GUI/PresetHints.hpp +++ b/src/slic3r/GUI/PresetHints.hpp @@ -3,7 +3,7 @@ #include -#include "PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" namespace Slic3r { diff --git a/src/slic3r/GUI/Search.cpp b/src/slic3r/GUI/Search.cpp index 613a39cce..242e3d725 100644 --- a/src/slic3r/GUI/Search.cpp +++ b/src/slic3r/GUI/Search.cpp @@ -9,10 +9,10 @@ #include "wx/dataview.h" #include "libslic3r/PrintConfig.hpp" +#include "libslic3r/PresetBundle.hpp" #include "GUI_App.hpp" #include "Plater.hpp" #include "Tab.hpp" -#include "PresetBundle.hpp" #define FTS_FUZZY_MATCH_IMPLEMENTATION #include "fts_fuzzy_match.h" diff --git a/src/slic3r/GUI/Search.hpp b/src/slic3r/GUI/Search.hpp index 9701e6808..8202222e9 100644 --- a/src/slic3r/GUI/Search.hpp +++ b/src/slic3r/GUI/Search.hpp @@ -14,8 +14,8 @@ #include #include "GUI_Utils.hpp" -#include "Preset.hpp" #include "wxExtensions.hpp" +#include "libslic3r/Preset.hpp" namespace Slic3r { diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index b128ec03d..88c11030d 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -1,8 +1,8 @@ // #include "libslic3r/GCodeSender.hpp" #include "slic3r/Utils/Serial.hpp" #include "Tab.hpp" -#include "PresetBundle.hpp" #include "PresetHints.hpp" +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/Utils.hpp" #include "libslic3r/Model.hpp" @@ -32,7 +32,6 @@ #include "GUI_App.hpp" #include "GUI_ObjectList.hpp" -#include "ConfigWizard.hpp" #include "Plater.hpp" #include "MainFrame.hpp" #include "format.hpp" diff --git a/src/slic3r/GUI/Tab.hpp b/src/slic3r/GUI/Tab.hpp index a0bf536c1..bc15efa35 100644 --- a/src/slic3r/GUI/Tab.hpp +++ b/src/slic3r/GUI/Tab.hpp @@ -33,8 +33,8 @@ #include "Event.hpp" #include "wxExtensions.hpp" #include "ConfigManipulation.hpp" -#include "Preset.hpp" #include "OptionsGroup.hpp" +#include "libslic3r/Preset.hpp" namespace Slic3r { namespace GUI { diff --git a/src/slic3r/Utils/PresetUpdater.cpp b/src/slic3r/Utils/PresetUpdater.cpp index c32613c46..dec251858 100644 --- a/src/slic3r/Utils/PresetUpdater.cpp +++ b/src/slic3r/Utils/PresetUpdater.cpp @@ -19,9 +19,9 @@ #include "libslic3r/libslic3r.h" #include "libslic3r/format.hpp" #include "libslic3r/Utils.hpp" +#include "libslic3r/PresetBundle.hpp" #include "slic3r/GUI/GUI.hpp" #include "slic3r/GUI/I18N.hpp" -#include "slic3r/GUI/PresetBundle.hpp" #include "slic3r/GUI/UpdateDialogs.hpp" #include "slic3r/GUI/ConfigWizard.hpp" #include "slic3r/GUI/GUI_App.hpp" -- cgit v1.2.3