FIX: invalid params in user preset load

jira: NONE

Signed-off-by: xun.zhang <xun.zhang@bambulab.com>
Change-Id: I7e9cdabb2d9c285db841c810f32aee5943b4db82
(cherry picked from commit ff8d764e7256cec1e7cd1d4d3d034590eb846f45)
(cherry picked from commit fb0461f3459afcfc80165442277ba8c54e232a39)
This commit is contained in:
xun.zhang
2025-06-04 18:13:58 +08:00
committed by Noisyfox
parent 8c64808ae5
commit 3c051f4180
5 changed files with 97 additions and 21 deletions

View File

@@ -152,6 +152,51 @@ static const std::unordered_map<std::string, std::string> pre_family_model_map {
{ "SL1", "SL1" },
}};
// 中间版本兼容性处理如果是nil值先改成default值再进行扩展
void extend_default_config_length(DynamicPrintConfig& config, const DynamicPrintConfig& defaults)
{
constexpr int default_param_length = 1;
int filament_variant_length = default_param_length;
int process_variant_length = default_param_length;
int machine_variant_length = default_param_length;
if(config.has("filament_extruder_variant"))
filament_variant_length = config.option<ConfigOptionStrings>("filament_extruder_variant")->size();
if(config.has("print_extruder_variant"))
process_variant_length = config.option<ConfigOptionStrings>("print_extruder_variant")->size();
if(config.has("printer_extruder_variant"))
machine_variant_length = config.option<ConfigOptionStrings>("printer_extruder_variant")->size();
auto replace_nil_and_resize = [&](const std::string & key, int length){
ConfigOption* raw_ptr = config.option(key);
ConfigOptionVectorBase* opt_vec = static_cast<ConfigOptionVectorBase *>(raw_ptr);
if(raw_ptr->is_nil() && defaults.has(key) && std::find(filament_extruder_override_keys.begin(), filament_extruder_override_keys.end(), key) == filament_extruder_override_keys.end()){
opt_vec->clear();
opt_vec->resize(length, defaults.option(key));
}
else{
opt_vec->resize(length, raw_ptr);
}
};
for(auto& key :config.keys()){
if(auto iter = print_options_with_variant.find(key); iter != print_options_with_variant.end()){
replace_nil_and_resize(key, process_variant_length);
}
else if(auto iter = filament_options_with_variant.find(key); iter != filament_options_with_variant.end()){
replace_nil_and_resize(key, filament_variant_length);
}
else if(auto iter = printer_options_with_variant_1.find(key); iter != printer_options_with_variant_1.end()){
replace_nil_and_resize(key, machine_variant_length);
}
else if(auto iter = printer_options_with_variant_2.find(key); iter != printer_options_with_variant_2.end()){
replace_nil_and_resize(key, machine_variant_length * 2);
}
}
}
VendorProfile VendorProfile::from_ini(const ptree &tree, const boost::filesystem::path &path, bool load_all)
{
static const std::string printer_model_key = "printer_model:";
@@ -1265,6 +1310,7 @@ void PresetCollection::load_presets(
// Find a default preset for the config. The PrintPresetCollection provides different default preset based on the "printer_technology" field.
preset.config = default_preset.config;
preset.config.apply(std::move(config));
extend_default_config_length(preset.config, default_preset.config);
}
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " load preset: " << name << " and filament_id: " << preset.filament_id << " and base_id: " << preset.base_id;
@@ -1821,6 +1867,7 @@ bool PresetCollection::load_user_preset(std::string name, std::map<std::string,
}
// Find a default preset for the config. The PrintPresetCollection provides different default preset based on the "printer_technology" field.
new_config = default_preset.config;
extend_default_config_length(new_config, default_preset.config);
}
if (inherit_preset) {
std::string extruder_id_name, extruder_variant_name;

View File

@@ -90,6 +90,8 @@ extern int get_values_from_json(std::string file_path, std::vector<std::string>&
extern ConfigFileType guess_config_file_type(const boost::property_tree::ptree &tree);
extern void extend_default_config_length(DynamicPrintConfig& config, const DynamicPrintConfig& defaults);
class VendorProfile
{
public:

View File

@@ -81,7 +81,16 @@ PresetBundle::PresetBundle()
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();
{
auto& default_config = this->filaments.default_preset().config;
for(const std::string& opt_key : default_config.keys()){
ConfigOption* opt = default_config.optptr(opt_key, false);
bool is_override_key = std::find(filament_extruder_override_keys.begin(),filament_extruder_override_keys.end(), opt_key) != filament_extruder_override_keys.end();
if(!is_override_key || !opt->nullable())
continue;
opt->deserialize("nil",ForwardCompatibilitySubstitutionRule::Disable);
}
}
this->sla_materials.default_preset().config.optptr("sla_material_settings_id", true);
this->sla_materials.default_preset().compatible_printers_condition();
@@ -902,6 +911,7 @@ bool PresetBundle::import_json_presets(PresetsConfigSubstitutions & s
}
if (inherit_preset) {
new_config = inherit_preset->config;
new_config.apply(std::move(config));
} else {
// We support custom root preset now
auto inherits_config2 = dynamic_cast<ConfigOptionString *>(inherits_config);
@@ -913,8 +923,9 @@ bool PresetBundle::import_json_presets(PresetsConfigSubstitutions & s
// Find a default preset for the config. The PrintPresetCollection provides different default preset based on the "printer_technology" field.
const Preset &default_preset = collection->default_preset_for(config);
new_config = default_preset.config;
new_config.apply(std::move(config));
extend_default_config_length(new_config, default_preset.config);
}
new_config.apply(std::move(config));
Preset &preset = collection->load_preset(collection->path_from_name(name, inherit_preset == nullptr), name, std::move(new_config), false);
if (key_values.find(BBL_JSON_KEY_FILAMENT_ID) != key_values.end())

View File

@@ -54,6 +54,30 @@ namespace Slic3r {
#define L(s) (s)
#define _(s) Slic3r::I18N::translate(s)
const std::vector<std::string> filament_extruder_override_keys = {
// floats
"filament_retraction_length",
"filament_z_hop",
"filament_z_hop_types",
"filament_retract_lift_above",
"filament_retract_lift_below",
"filament_retract_lift_enforce",
"filament_retraction_speed",
"filament_deretraction_speed",
"filament_retract_restart_extra",
"filament_retraction_minimum_travel",
// BBS: floats
"filament_wipe_distance",
// bools
"filament_retract_when_changing_layer",
"filament_wipe",
// percents
"filament_retract_before_wipe",
"filament_long_retractions_when_cut",
"filament_retraction_distances_when_cut"
};
size_t get_extruder_index(const GCodeConfig& config, unsigned int filament_id)
{
if (filament_id < config.filament_map.size()) {
@@ -6408,21 +6432,12 @@ void PrintConfigDef::init_fff_params()
def->set_default_value(new ConfigOptionPercent(85));
// Declare retract values for filament profile, overriding the printer's extruder profile.
for (const char *opt_key : {
// floats
"retraction_length", "z_hop", "z_hop_types", "retract_lift_above", "retract_lift_below", "retract_lift_enforce", "retraction_speed", "deretraction_speed", "retract_restart_extra", "retraction_minimum_travel",
// BBS: floats
"wipe_distance",
// bools
"retract_when_changing_layer", "wipe",
// percents
"retract_before_wipe",
"long_retractions_when_cut",
"retraction_distances_when_cut"
}) {
auto it_opt = options.find(opt_key);
for (auto& opt_key : filament_extruder_override_keys) {
const std::string filament_prefix = "filament_";
std::string extruder_raw_key = opt_key.substr(opt_key.find(filament_prefix) + filament_prefix.length());
auto it_opt = options.find(extruder_raw_key);
assert(it_opt != options.end());
def = this->add_nullable(std::string("filament_") + opt_key, it_opt->second.type);
def = this->add_nullable(opt_key, it_opt->second.type);
def->label = it_opt->second.label;
def->full_label = it_opt->second.full_label;
def->tooltip = it_opt->second.tooltip;
@@ -6433,10 +6448,10 @@ void PrintConfigDef::init_fff_params()
def->min = it_opt->second.min;
def->max = it_opt->second.max;
//BBS: shown specific filament retract config because we hide the machine retract into comDevelop mode
if ((strcmp(opt_key, "retraction_length") == 0) ||
(strcmp(opt_key, "z_hop") == 0)||
(strcmp(opt_key, "long_retractions_when_cut") == 0)||
(strcmp(opt_key, "retraction_distances_when_cut") == 0))
if (opt_key =="filament_retraction_length"||
opt_key=="filament_z_hop" ||
opt_key== "filament_long_retractions_when_cut" ||
opt_key=="filament_retraction_distances_when_cut")
def->mode = comSimple;
else
def->mode = comAdvanced;
@@ -7780,7 +7795,6 @@ size_t DynamicPrintConfig::get_parameter_size(const std::string& param_name, siz
if (nozzle_volume_type_opt) {
volume_type_size = nozzle_volume_type_opt->values.size();
}
bool flag = (param_name == "nozzle_volume");
if (printer_options_with_variant_1.count(param_name) > 0) {
return extruder_nums * volume_type_size;
}

View File

@@ -475,6 +475,8 @@ static std::string get_bed_temp_1st_layer_key(const BedType type)
return "";
}
extern const std::vector<std::string> filament_extruder_override_keys;
// for parse extruder_ams_count
extern std::vector<std::map<int, int>> get_extruder_ams_count(const std::vector<std::string> &strs);
extern std::vector<std::string> save_extruder_ams_count_to_string(const std::vector<std::map<int, int>> &extruder_ams_count);