From 07148d2dfdc0f97e095a846b5529e80e1793efd5 Mon Sep 17 00:00:00 2001 From: "xun.zhang" Date: Thu, 5 Dec 2024 17:10:28 +0800 Subject: [PATCH] ENH: set filament retract params even if nil 1.Always set filament retract params to filament_num size.In gcode export module, we can always use filament idx to get retract params 2. add logic in update_filament_maps_to_config to update the retraction related params which can be overiden by filament jira:NONE Signed-off-by: xun.zhang Change-Id: Ia45dd1401aa3565d062d5da1c9f4a2ba8966f693 (cherry picked from commit 4b083d8d8220b8f65a1b804688cb2d6e238eb4e6) --- src/libslic3r/Config.hpp | 12 +++++++----- src/libslic3r/Print.cpp | 18 ++++++++++++++++++ src/libslic3r/PrintApply.cpp | 29 +++++++---------------------- src/libslic3r/PrintConfig.cpp | 35 +++++++++++++++++++++++++++++++++++ src/libslic3r/PrintConfig.hpp | 3 +++ 5 files changed, 70 insertions(+), 27 deletions(-) diff --git a/src/libslic3r/Config.hpp b/src/libslic3r/Config.hpp index 096958ad37..33f5dba65e 100644 --- a/src/libslic3r/Config.hpp +++ b/src/libslic3r/Config.hpp @@ -286,7 +286,7 @@ public: return *this != *rhs; } // Apply an override option, possibly a nullable one. - virtual bool apply_override(const ConfigOption *rhs) { + virtual bool apply_override(const ConfigOption *rhs, std::vector& default_index) { if (*this == *rhs) return false; *this = *rhs; @@ -583,7 +583,7 @@ public: return false; } // Apply an override option, possibly a nullable one. - bool apply_override(const ConfigOption *rhs) override { + bool apply_override(const ConfigOption *rhs, std::vector& default_index) override { if (this->nullable()) throw ConfigurationError("Cannot override a nullable ConfigOption."); if (rhs->type() != this->type()) @@ -607,14 +607,16 @@ public: else this->values.resize(rhs_vec->size(), this->values.front()); - bool modified = false; - auto default_value = this->values[0]; + assert(default_index.size() == rhs_vec->size()); + + bool modified = false; + std::vector default_value = this->values; for (size_t i = 0; i < rhs_vec->size(); ++i) { if (!rhs_vec->is_nil(i)) { this->values[i] = rhs_vec->values[i]; modified = true; } else { - this->values[i] = default_value; + this->values[i] = default_value[default_index[i]-1]; } } return modified; diff --git a/src/libslic3r/Print.cpp b/src/libslic3r/Print.cpp index 86fb02f92c..03b38a5538 100644 --- a/src/libslic3r/Print.cpp +++ b/src/libslic3r/Print.cpp @@ -2651,8 +2651,26 @@ void Print::update_filament_maps_to_config(std::vector f_maps) m_full_print_config = m_ori_full_print_config; m_full_print_config.update_values_to_printer_extruders_for_multiple_filaments(m_full_print_config, filament_options_with_variant, "filament_self_index", "filament_extruder_variant"); + const std::vector &extruder_retract_keys = print_config_def.extruder_retract_keys(); + const std::string filament_prefix = "filament_"; + t_config_option_keys print_diff; + DynamicPrintConfig filament_overrides; + for (auto& opt_key: extruder_retract_keys) + { + const ConfigOption *opt_new_filament = m_full_print_config.option(filament_prefix + opt_key); + const ConfigOption *opt_new_machine = m_full_print_config.option(opt_key); + const ConfigOption *opt_old_machine = m_config.option(opt_key); + + if (opt_new_filament) + compute_filament_override_value(opt_key, opt_old_machine, opt_new_machine, opt_new_filament, m_full_print_config, print_diff, filament_overrides, f_maps); + } + t_config_option_keys keys(filament_options_with_variant.begin(), filament_options_with_variant.end()); m_config.apply_only(m_full_print_config, keys, true); + if (!print_diff.empty()) { + m_placeholder_parser.apply_config(filament_overrides); + m_config.apply(filament_overrides); + } } m_has_auto_filament_map_result = true; } diff --git a/src/libslic3r/PrintApply.cpp b/src/libslic3r/PrintApply.cpp index 30635f98d1..29983f0fc9 100644 --- a/src/libslic3r/PrintApply.cpp +++ b/src/libslic3r/PrintApply.cpp @@ -222,7 +222,8 @@ static t_config_option_keys print_config_diffs( const PrintConfig ¤t_config, const DynamicPrintConfig &new_full_config, DynamicPrintConfig &filament_overrides, - int plate_index) + int plate_index, + std::vector& filament_maps) { const std::vector &extruder_retract_keys = print_config_def.extruder_retract_keys(); const std::string filament_prefix = "filament_"; @@ -236,26 +237,9 @@ static t_config_option_keys print_config_diffs( //FIXME This may happen when executing some test cases. continue; const ConfigOption *opt_new_filament = std::binary_search(extruder_retract_keys.begin(), extruder_retract_keys.end(), opt_key) ? new_full_config.option(filament_prefix + opt_key) : nullptr; - if (opt_new_filament != nullptr && ! opt_new_filament->is_nil()) { - // An extruder retract override is available at some of the filament presets. - bool overriden = opt_new->overriden_by(opt_new_filament); - if (overriden || *opt_old != *opt_new) { - auto opt_copy = opt_new->clone(); - if (!((opt_key == "long_retractions_when_cut" || opt_key == "retraction_distances_when_cut") - && new_full_config.option("enable_long_retraction_when_cut")->value != LongRectrationLevel::EnableFilament)) // ugly code, remove it later if firmware supports - opt_copy->apply_override(opt_new_filament); - bool changed = *opt_old != *opt_copy; - if (changed) - print_diff.emplace_back(opt_key); - if (changed || overriden) { - if ((opt_key == "long_retractions_when_cut" || opt_key == "retraction_distances_when_cut") - && new_full_config.option("enable_long_retraction_when_cut")->value != LongRectrationLevel::EnableFilament) - continue; - // filament_overrides will be applied to the placeholder parser, which layers these parameters over full_print_config. - filament_overrides.set_key_value(opt_key, opt_copy); - } else - delete opt_copy; - } + + if (opt_new_filament != nullptr) { + compute_filament_override_value(opt_key, opt_old, opt_new, opt_new_filament, new_full_config, print_diff, filament_overrides, filament_maps); } else if (*opt_new != *opt_old) { //BBS: add plate_index logic for wipe_tower_x/wipe_tower_y if (!opt_key.compare("wipe_tower_x") || !opt_key.compare("wipe_tower_y")) { @@ -1154,11 +1138,12 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_ m_ori_full_print_config = new_full_config; new_full_config.update_values_to_printer_extruders_for_multiple_filaments(new_full_config, filament_options_with_variant, "filament_self_index", "filament_extruder_variant"); + std::vector filament_maps = new_full_config.option("filament_map")->values; // Find modified keys of the various configs. Resolve overrides extruder retract values by filament profiles. DynamicPrintConfig filament_overrides; //BBS: add plate index - t_config_option_keys print_diff = print_config_diffs(m_config, new_full_config, filament_overrides, this->m_plate_index); + t_config_option_keys print_diff = print_config_diffs(m_config, new_full_config, filament_overrides, this->m_plate_index, filament_maps); t_config_option_keys full_config_diff = full_print_config_diffs(m_full_print_config, new_full_config, this->m_plate_index); // Collect changes to object and region configs. t_config_option_keys object_diff = m_default_object_config.diff(new_full_config); diff --git a/src/libslic3r/PrintConfig.cpp b/src/libslic3r/PrintConfig.cpp index 6c81a7adda..f0abe78465 100644 --- a/src/libslic3r/PrintConfig.cpp +++ b/src/libslic3r/PrintConfig.cpp @@ -8553,6 +8553,41 @@ void DynamicPrintConfig::update_non_diff_values_to_base_config(DynamicPrintConfi return; } +void compute_filament_override_value(const std::string& opt_key, const ConfigOption *opt_old_machine, const ConfigOption *opt_new_machine, const ConfigOption *opt_new_filament, const DynamicPrintConfig& new_full_config, + t_config_option_keys& diff_keys, DynamicPrintConfig& filament_overrides, std::vector& f_maps) +{ + bool is_nil = opt_new_filament->is_nil(); + + // ugly code, for these params, we should ignore the value in filament params + ConfigOptionBoolsNullable opt_long_retraction_default; + if (opt_key == "long_retractions_when_cut" && new_full_config.option("enable_long_retraction_when_cut")->value != LongRectrationLevel::EnableFilament) { + auto ptr = dynamic_cast(opt_new_filament); + for (size_t idx = 0; idx < ptr->values.size(); ++idx) + opt_long_retraction_default.values.push_back(ptr->nil_value()); + opt_new_filament = &opt_long_retraction_default; + } + + ConfigOptionFloatsNullable opt_retraction_distance_default; + if (opt_key == "retraction_distances_when_cut" && new_full_config.option("enable_long_retraction_when_cut")->value != LongRectrationLevel::EnableFilament) { + auto ptr = dynamic_cast(opt_new_filament); + for (size_t idx = 0; idx < ptr->values.size(); ++idx) + opt_long_retraction_default.values.push_back(ptr->nil_value()); + opt_new_filament = &opt_retraction_distance_default; + } + + auto opt_copy = opt_new_machine->clone(); + opt_copy->apply_override(opt_new_filament, f_maps); + bool changed = *opt_old_machine != *opt_copy; + + if (changed) { + diff_keys.emplace_back(opt_key); + filament_overrides.set_key_value(opt_key, opt_copy); + } + else + delete opt_copy; +} + + //BBS: pass map to recording all invalid valies //FIXME localize this function. std::map validate(const FullPrintConfig &cfg, bool under_cli) diff --git a/src/libslic3r/PrintConfig.hpp b/src/libslic3r/PrintConfig.hpp index 31d000a50c..fb406e8dca 100644 --- a/src/libslic3r/PrintConfig.hpp +++ b/src/libslic3r/PrintConfig.hpp @@ -628,6 +628,9 @@ extern std::set filament_options_with_variant; extern std::set printer_options_with_variant_1; extern std::set printer_options_with_variant_2; +extern void compute_filament_override_value(const std::string& opt_key, const ConfigOption *opt_old_machine, const ConfigOption *opt_new_machine, const ConfigOption *opt_new_filament, const DynamicPrintConfig& new_full_config, + t_config_option_keys& diff_keys, DynamicPrintConfig& filament_overrides, std::vector& f_maps); + void handle_legacy_sla(DynamicPrintConfig &config); class StaticPrintConfig : public StaticConfig