1) Storing the physical_printer_settings_id into the 3MF, AMF, GCode.

2) Activating the physical_printer_settings_id when loading from 3MF, AMF, GCode.
   The physical printer is only activated if it references the printer_settings_id
   loaded from the same file.
3) When loading the presets from 3MF, AMF, GCode, the "external" profiles
   are no more created for profiles which differ from the local profiles
   the loaded profiles reference. Instead, the referenced profile is activated
   and modified with the loaded preset. If the referenced profile does not
   exist, but the profile refers to a system profile with the "inherits"
   fileds, the system profile is loaded and modified instead.
   This works for all profiles with the exception of multi-extruder
   printer with multiple filament profiles modified. In that case
   the first modified filament profile will be loaded as modified,
   while the other modified profiles will be loaded as "external".

This should fix
Physical printer + 3mf file, wrong preset used to generate gcode #5178
and possibly
https://github.com/prusa3d/PrusaSlicer/issues/5272
This commit is contained in:
Vojtech Bubnik 2020-12-04 10:48:44 +01:00
parent 15b56c9950
commit e42e25c933
8 changed files with 118 additions and 57 deletions

View file

@ -731,8 +731,9 @@ static bool profile_print_params_same(const DynamicPrintConfig &cfg_old, const D
// 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(
// Only a single profile could be edited at at the same time, which introduces complexity when loading
// filament profiles for multi-extruder printers.
std::pair<Preset*, bool> 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.
@ -742,14 +743,23 @@ Preset& PresetCollection::load_external_preset(
// Config to initialize the preset from.
const DynamicPrintConfig &config,
// Select the preset after loading?
bool select)
LoadAndSelect 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);
std::string &inherits = Preset::inherits(cfg);
if (select == LoadAndSelect::Never) {
// Some filament profile has been selected and modified already.
// Check whether this profile is equal to the modified edited profile.
const Preset &edited = this->get_edited_preset();
if ((edited.name == original_name || edited.name == inherits) && profile_print_params_same(edited.config, cfg))
// Just point to that already selected and edited profile.
return std::make_pair(&(*this->find_preset_internal(edited.name)), false);
}
// Is there a preset already loaded with the name stored inside the config?
std::deque<Preset>::iterator it = this->find_preset_internal(original_name);
bool found = it != m_presets.end() && it->name == original_name;
std::deque<Preset>::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);
@ -757,19 +767,40 @@ Preset& PresetCollection::load_external_preset(
}
if (found && profile_print_params_same(it->config, cfg)) {
// The preset exists and it matches the values stored inside config.
if (select)
if (select == LoadAndSelect::Always)
this->select_preset(it - m_presets.begin());
return *it;
return std::make_pair(&(*it), false);
}
// 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;
if (! found && select != LoadAndSelect::Never && ! inherits.empty()) {
// Try to use a system profile as a base to select the system profile
// and override its settings with the loaded ones.
assert(it == m_presets.end());
it = this->find_preset_internal(inherits);
found = it != m_presets.end() && it->name == inherits;
if (found && profile_print_params_same(it->config, cfg)) {
// The system preset exists and it matches the values stored inside config.
if (select == LoadAndSelect::Always)
this->select_preset(it - m_presets.begin());
return std::make_pair(&(*it), false);
}
}
if (found) {
if (select != LoadAndSelect::Never) {
// Select the existing preset and override it with new values, so that
// the differences will be shown in the preset editor against the referenced profile.
this->select_preset(it - m_presets.begin());
this->get_edited_preset().config.apply(config);
this->update_dirty();
assert(this->get_edited_preset().is_dirty);
return std::make_pair(&(*it), this->get_edited_preset().is_dirty);
}
if (inherits.empty()) {
// Update the "inherits" field.
// There is a profile with the same name already loaded. Should we update the "inherits" field?
inherits = it->vendor ? it->name : it->inherits();
}
}
// The external preset does not match an internal preset, load the external preset.
std::string new_name;
for (size_t idx = 0;; ++ idx) {
@ -790,19 +821,19 @@ Preset& PresetCollection::load_external_preset(
break;
if (profile_print_params_same(it->config, cfg)) {
// The preset exists and it matches the values stored inside config.
if (select)
if (select == LoadAndSelect::Always)
this->select_preset(it - m_presets.begin());
return *it;
return std::make_pair(&(*it), false);
}
// Form another profile name.
}
// Insert a new profile.
Preset &preset = this->load_preset(path, new_name, std::move(cfg), select);
Preset &preset = this->load_preset(path, new_name, std::move(cfg), select == LoadAndSelect::Always);
preset.is_external = true;
if (&this->get_selected_preset() == &preset)
this->get_edited_preset().is_external = true;
return preset;
return std::make_pair(&preset, false);
}
Preset& PresetCollection::load_preset(const std::string &path, const std::string &name, DynamicPrintConfig &&config, bool select)