Skip to content

Commit

Permalink
Fix for #11727 : 2.7.0-rc2 Import ConfigBundle not pulling in all phy…
Browse files Browse the repository at this point in the history
…sical printers
  • Loading branch information
YuSanka committed Nov 23, 2023
1 parent b3a2bac commit 0a1e475
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
44 changes: 42 additions & 2 deletions src/libslic3r/Preset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
#include "PlaceholderParser.hpp"
#include "GCode/Thumbnails.hpp"

#include "PresetBundle.hpp"

using boost::property_tree::ptree;

namespace Slic3r {
Expand Down Expand Up @@ -1788,7 +1790,8 @@ std::string PhysicalPrinter::get_preset_name(std::string name)
// *** PhysicalPrinterCollection ***
// -----------------------------------

PhysicalPrinterCollection::PhysicalPrinterCollection( const std::vector<std::string>& keys)
PhysicalPrinterCollection::PhysicalPrinterCollection( const std::vector<std::string>& 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) {
Expand All @@ -1799,6 +1802,28 @@ PhysicalPrinterCollection::PhysicalPrinterCollection( const std::vector<std::str
}
}

static void update_preset_names_if_were_renamed(std::set<std::string>& preset_names, const PrinterPresetCollection& printer_presets)
{
std::set<std::string> 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(
Expand All @@ -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 {
Expand All @@ -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());
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/libslic3r/Preset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ class PhysicalPrinter
class PhysicalPrinterCollection
{
public:
PhysicalPrinterCollection(const std::vector<std::string>& keys);
PhysicalPrinterCollection(const std::vector<std::string>& keys, PresetBundle* preset_bundle);

typedef std::deque<PhysicalPrinter>::iterator Iterator;
typedef std::deque<PhysicalPrinter>::const_iterator ConstIterator;
Expand Down Expand Up @@ -850,6 +850,8 @@ class PhysicalPrinterCollection

// Path to the directory to store the config files into.
std::string m_dir_path;

const PresetBundle* m_preset_bundle_owner{ nullptr };
};


Expand Down
2 changes: 1 addition & 1 deletion src/libslic3r/PresetBundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ PresetBundle::PresetBundle() :
sla_materials(Preset::TYPE_SLA_MATERIAL, Preset::sla_material_options(), static_cast<const SLAMaterialConfig&>(SLAFullPrintConfig::defaults())),
sla_prints(Preset::TYPE_SLA_PRINT, Preset::sla_print_options(), static_cast<const SLAPrintObjectConfig&>(SLAFullPrintConfig::defaults())),
printers(Preset::TYPE_PRINTER, Preset::printer_options(), static_cast<const PrintRegionConfig&>(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
Expand Down

0 comments on commit 0a1e475

Please sign in to comment.