From c83319437f5804c6017200ef63cc123199181af5 Mon Sep 17 00:00:00 2001 From: Erwan MATHIEU Date: Wed, 5 Feb 2025 11:11:36 +0100 Subject: [PATCH] Properly apply retraction limitations during travel CURA-11978 --- include/gcodeExport.h | 3 ++- src/LayerPlan.cpp | 18 ++++++++++++------ src/gcodeExport.cpp | 24 +++++++++++++----------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/include/gcodeExport.h b/include/gcodeExport.h index 21cacdc357..1ac1cf510c 100644 --- a/include/gcodeExport.h +++ b/include/gcodeExport.h @@ -552,8 +552,9 @@ class GCodeExport : public NoCopy * @param force Indicates whether we should force the retraction to happen regardless of the maximum allowed retraction count * @param extruder_switch Indicates whether we retract for an extruder switch * @param retract_distance A specific absolute retraction distance to be used, or nullopt to use the one in the config + * @return True if the retraction has been processed normally, false if it was skipped because of limitations */ - void writeRetraction(const RetractionConfig& config, bool force = false, bool extruder_switch = false, const std::optional retract_distance = std::nullopt); + bool writeRetraction(const RetractionConfig& config, bool force = false, bool extruder_switch = false, const std::optional retract_distance = std::nullopt); /*! * Start a z hop with the given \p hop_height. diff --git a/src/LayerPlan.cpp b/src/LayerPlan.cpp index 67f8550480..d3f2540440 100644 --- a/src/LayerPlan.cpp +++ b/src/LayerPlan.cpp @@ -3160,11 +3160,17 @@ void LayerPlan::writeGCode(GCodeExport& gcode) computeAntiOozeAmounts(gcode, extruder, path, z_hop_height, retraction_config, retraction_amounts, priming_amounts); } - gcode.writeRetraction( - retraction_config->retraction_config, - false, - false, - retraction_amounts.has_value() ? std::optional{ retraction_amounts->amount_while_still } : std::nullopt); + if (! gcode.writeRetraction( + retraction_config->retraction_config, + false, + false, + retraction_amounts.has_value() ? std::make_optional(retraction_amounts->amount_while_still) : std::nullopt)) + { + // Retraction was cancelled because of limitations, so also cancel retraction/priming during travel + retraction_amounts.reset(); + priming_amounts.reset(); + } + if (path.retract_for_nozzle_switch) { constexpr bool force = true; @@ -3177,7 +3183,7 @@ void LayerPlan::writeGCode(GCodeExport& gcode) gcode.writeZhopStart( z_hop_height, 0.0, - retraction_amounts.has_value() ? retraction_amounts->z_hop.amount : 0.0, + retraction_amounts.has_value() ? std::make_optional(retraction_amounts->z_hop.amount) : std::nullopt, retraction_amounts.has_value() ? retraction_amounts->z_hop.ratio : 0.0_r); z_hop_height = retraction_config->retraction_config.zHop; // back to normal z hop } diff --git a/src/gcodeExport.cpp b/src/gcodeExport.cpp index 36adca13a8..25b9b347f8 100644 --- a/src/gcodeExport.cpp +++ b/src/gcodeExport.cpp @@ -1214,7 +1214,7 @@ void GCodeExport::writeUnretractionAndPrime() } } -void GCodeExport::writeRetraction(const RetractionConfig& config, bool force, bool extruder_switch, const std::optional retract_distance) +bool GCodeExport::writeRetraction(const RetractionConfig& config, bool force, bool extruder_switch, const std::optional retract_distance) { ExtruderTrainAttributes& extr_attr = extruder_attr_[current_extruder_]; @@ -1228,13 +1228,7 @@ void GCodeExport::writeRetraction(const RetractionConfig& config, bool force, bo } extr_attr.retraction_e_amount_current_ = 1.0; // 1.0 is a stub; BFB doesn't use the actual retracted amount; retraction is performed by firmware } - return; - } - - RetractionAmounts retraction_amounts = computeRetractionAmounts(extr_attr, retract_distance.value_or(config.distance)); - if (! retraction_amounts.has_retraction()) - { - return; + return true; } { // handle retraction limitation @@ -1248,12 +1242,12 @@ void GCodeExport::writeRetraction(const RetractionConfig& config, bool force, bo } if (! force && config.retraction_count_max <= 0) { - return; + return false; } if (! force && extruded_volume_at_previous_n_retractions.size() == config.retraction_count_max && current_extruded_volume < extruded_volume_at_previous_n_retractions.back() + config.retraction_extrusion_window * extr_attr.filament_area_) { - return; + return false; } extruded_volume_at_previous_n_retractions.push_front(current_extruded_volume); if (extruded_volume_at_previous_n_retractions.size() == config.retraction_count_max + 1) @@ -1262,11 +1256,17 @@ void GCodeExport::writeRetraction(const RetractionConfig& config, bool force, bo } } + RetractionAmounts retraction_amounts = computeRetractionAmounts(extr_attr, retract_distance.value_or(config.distance)); + if (! retraction_amounts.has_retraction()) + { + return true; + } + if (extr_attr.machine_firmware_retract_) { if (extruder_switch && extr_attr.retraction_e_amount_current_) { - return; + return true; } *output_stream_ << "G10"; if (extruder_switch && flavor_ == EGCodeFlavor::REPETIER) @@ -1301,6 +1301,8 @@ void GCodeExport::writeRetraction(const RetractionConfig& config, bool force, bo } extr_attr.prime_volume_ += config.prime_volume; + + return true; } void GCodeExport::writeZhopStart(const coord_t hop_height, Velocity speed /*= 0*/, const std::optional retract_distance, const Ratio& retract_ratio)