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 <xun.zhang@bambulab.com>
Change-Id: Ia45dd1401aa3565d062d5da1c9f4a2ba8966f693
This commit is contained in:
xun.zhang 2024-12-05 17:10:28 +08:00 committed by lane.wei
parent ee99497fd7
commit 4b083d8d82
5 changed files with 69 additions and 27 deletions

View File

@ -292,7 +292,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<int>& default_index) {
if (*this == *rhs)
return false;
*this = *rhs;
@ -686,7 +686,7 @@ public:
}
// Apply an override option, possibly a nullable one.
// assume lhs is not nullable even it is nullable
bool apply_override(const ConfigOption *rhs) override {
bool apply_override(const ConfigOption *rhs, std::vector<int>& default_index) override {
//if (this->nullable())
// throw ConfigurationError("Cannot override a nullable ConfigOption.");
if (rhs->type() != this->type())
@ -710,14 +710,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<T> 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;

View File

@ -2371,8 +2371,26 @@ void Print::update_filament_maps_to_config(std::vector<int> 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<std::string> &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;
}

View File

@ -217,7 +217,8 @@ static t_config_option_keys print_config_diffs(
const PrintConfig &current_config,
const DynamicPrintConfig &new_full_config,
DynamicPrintConfig &filament_overrides,
int plate_index)
int plate_index,
std::vector<int>& filament_maps)
{
const std::vector<std::string> &extruder_retract_keys = print_config_def.extruder_retract_keys();
const std::string filament_prefix = "filament_";
@ -231,26 +232,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<ConfigOptionInt>("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<ConfigOptionInt>("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")) {
@ -1150,11 +1134,12 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_
print_variant_index[e_index] = e_index;
}
}
std::vector<int> filament_maps = new_full_config.option<ConfigOptionInts>("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);

View File

@ -6664,6 +6664,41 @@ void update_static_print_config_from_dynamic(ConfigBase& config, const DynamicPr
config.apply(dest_config, true);
}
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<int>& 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<ConfigOptionInt>("enable_long_retraction_when_cut")->value != LongRectrationLevel::EnableFilament) {
auto ptr = dynamic_cast<const ConfigOptionBoolsNullable*>(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<ConfigOptionInt>("enable_long_retraction_when_cut")->value != LongRectrationLevel::EnableFilament) {
auto ptr = dynamic_cast<const ConfigOptionFloatsNullable*>(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<std::string, std::string> validate(const FullPrintConfig &cfg, bool under_cli)

View File

@ -531,6 +531,8 @@ extern std::set<std::string> printer_options_with_variant_2;
extern std::set<std::string> empty_options;
extern void update_static_print_config_from_dynamic(ConfigBase& config, const DynamicPrintConfig& dest_config, std::vector<int> variant_index, std::set<std::string>& key_set1, int stride = 1);
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<int>& f_maps);
void handle_legacy_sla(DynamicPrintConfig &config);