From 0a1e4758a14665b105cc88cdbd058138895cecd2 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Thu, 23 Nov 2023 13:55:56 +0100 Subject: [PATCH] Fix for #11727 : 2.7.0-rc2 Import ConfigBundle not pulling in all physical printers --- src/libslic3r/Preset.cpp | 44 ++++++++++++++++++++++++++++++++-- src/libslic3r/Preset.hpp | 4 +++- src/libslic3r/PresetBundle.cpp | 2 +- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/libslic3r/Preset.cpp b/src/libslic3r/Preset.cpp index 14c077f8d0..7557aa670f 100644 --- a/src/libslic3r/Preset.cpp +++ b/src/libslic3r/Preset.cpp @@ -46,6 +46,8 @@ #include "PlaceholderParser.hpp" #include "GCode/Thumbnails.hpp" +#include "PresetBundle.hpp" + using boost::property_tree::ptree; namespace Slic3r { @@ -1788,7 +1790,8 @@ std::string PhysicalPrinter::get_preset_name(std::string name) // *** PhysicalPrinterCollection *** // ----------------------------------- -PhysicalPrinterCollection::PhysicalPrinterCollection( const std::vector& keys) +PhysicalPrinterCollection::PhysicalPrinterCollection( const std::vector& keys, PresetBundle* preset_bundle) + :m_preset_bundle_owner(preset_bundle) { // Default config for a physical printer containing all key/value pairs of PhysicalPrinter::printer_options(). for (const std::string &key : keys) { @@ -1799,6 +1802,28 @@ PhysicalPrinterCollection::PhysicalPrinterCollection( const std::vector& preset_names, const PrinterPresetCollection& printer_presets) +{ + std::set new_names; + + bool was_renamed{ false }; + for (const std::string& preset_name : preset_names) { + const Preset* preset = printer_presets.find_preset(preset_name); + if (preset) + new_names.emplace(preset_name); + else { + if (const std::string* new_name = printer_presets.get_preset_name_renamed(preset_name)) { + BOOST_LOG_TRIVIAL(warning) << "Printer preset present " << preset_name << "was renamed to: " << *new_name << std::endl; + new_names.emplace(*new_name); + was_renamed = true; + } + } + } + + if (was_renamed) + preset_names = new_names; +} + // Load all printers found in dir_path. // Throws an exception on error. void PhysicalPrinterCollection::load_printers( @@ -1817,10 +1842,15 @@ void PhysicalPrinterCollection::load_printers( std::string name = dir_entry.path().filename().string(); // Remove the .ini suffix. name.erase(name.size() - 4); - if (this->find_printer(name, false)) { + if (PhysicalPrinter* found_printer = this->find_printer(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) << "Printer already present, not loading: " << name; + + // But some of used printer_preset might have been renamed. + // Check it and replace with new name(s) if it's needed + update_preset_names_if_were_renamed(found_printer->preset_names, m_preset_bundle_owner->printers); + continue; } try { @@ -1834,6 +1864,9 @@ void PhysicalPrinterCollection::load_printers( substitutions.push_back({ name, Preset::TYPE_PHYSICAL_PRINTER, PresetConfigSubstitutions::Source::UserFile, printer.file, std::move(config_substitutions) }); printer.update_from_config(config); printer.loaded = true; + // Some of used printer_preset might have been renamed. + // Check it and replace with new name(s) if it's needed + update_preset_names_if_were_renamed(printer.preset_names, m_preset_bundle_owner->printers); } catch (const std::ifstream::failure& err) { throw Slic3r::RuntimeError(std::string("The selected preset cannot be loaded: ") + printer.file + "\n\tReason: " + err.what()); @@ -1865,6 +1898,9 @@ void PhysicalPrinterCollection::load_printer(const std::string& path, const std: it->file = path; it->config = std::move(config); it->loaded = true; + // Some of used printer_preset might have been renamed. + // Check it and replace with new name(s) if it's needed + update_preset_names_if_were_renamed(it->preset_names, m_preset_bundle_owner->printers); if (select) this->select_printer(*it); @@ -2120,6 +2156,10 @@ void PhysicalPrinterCollection::select_printer(const std::string& full_name) m_selected_preset = *it->preset_names.begin(); else m_selected_preset = it->get_preset_name(full_name); + + // Check if selected preset wasn't renamed and replace it with new name + if (const std::string* new_name = m_preset_bundle_owner->printers.get_preset_name_renamed(m_selected_preset)) + m_selected_preset = *new_name; } void PhysicalPrinterCollection::select_printer(const std::string& printer_name, const std::string& preset_name) diff --git a/src/libslic3r/Preset.hpp b/src/libslic3r/Preset.hpp index d87b2ade9d..533c693812 100644 --- a/src/libslic3r/Preset.hpp +++ b/src/libslic3r/Preset.hpp @@ -729,7 +729,7 @@ protected: class PhysicalPrinterCollection { public: - PhysicalPrinterCollection(const std::vector& keys); + PhysicalPrinterCollection(const std::vector& keys, PresetBundle* preset_bundle); typedef std::deque::iterator Iterator; typedef std::deque::const_iterator ConstIterator; @@ -850,6 +850,8 @@ private: // Path to the directory to store the config files into. std::string m_dir_path; + + const PresetBundle* m_preset_bundle_owner{ nullptr }; }; diff --git a/src/libslic3r/PresetBundle.cpp b/src/libslic3r/PresetBundle.cpp index 64be1f78ed..943e5e9f63 100644 --- a/src/libslic3r/PresetBundle.cpp +++ b/src/libslic3r/PresetBundle.cpp @@ -50,7 +50,7 @@ PresetBundle::PresetBundle() : 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 -"), - physical_printers(PhysicalPrinter::printer_options()) + physical_printers(PhysicalPrinter::printer_options(), this) { // 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