From 540b800a5c1c76b25a809aac18f319e6a23093b7 Mon Sep 17 00:00:00 2001 From: Nohus Date: Thu, 13 Oct 2022 06:01:20 +0200 Subject: [PATCH] SPE-1922: Place M600 after unretract to prevent printer returns at the position that was already printed with the previous color. This fixes #2672 caused by the default implementation of M600 that returns back to XY position before M600. --- src/libslic3r/GCode.cpp | 14 +++++++++++++- src/libslic3r/GCode.hpp | 2 ++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 5a9253a8e4..583281dddd 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -2249,7 +2249,13 @@ LayerResult GCodeGenerator::process_layer( if (single_object_instance_idx == size_t(-1)) { // Normal (non-sequential) print. - gcode += ProcessLayer::emit_custom_gcode_per_print_z(*this, layer_tools.custom_gcode, m_writer.extruder()->id(), first_extruder_id, print.config()); + std::string custom_gcode = ProcessLayer::emit_custom_gcode_per_print_z(*this, layer_tools.custom_gcode, m_writer.extruder()->id(), first_extruder_id, print.config()); + if (layer_tools.custom_gcode != nullptr && layer_tools.custom_gcode->type == CustomGCode::ColorChange) { + // We have a color change to do on this layer, but we want to do it immediately before the first extrusion instead of now, in order to fix GH #2672 + m_pending_pre_extrusion_gcode = custom_gcode; + } else { + gcode += custom_gcode; + } } // Extrude skirt at the print_z of the raft layers and normal object layers // not at the print_z of the interlaced support material layers. @@ -3010,6 +3016,12 @@ std::string GCodeGenerator::_extrude( // compensate retraction gcode += this->unretract(); + if (!m_pending_pre_extrusion_gcode.empty()) { + // There is G-Code that is due to be inserted before an extrusion starts. Insert it. + gcode += m_pending_pre_extrusion_gcode; + m_pending_pre_extrusion_gcode.clear(); + } + // adjust acceleration if (m_config.default_acceleration.value > 0) { double acceleration; diff --git a/src/libslic3r/GCode.hpp b/src/libslic3r/GCode.hpp index 1548af4513..3f2d3685fd 100644 --- a/src/libslic3r/GCode.hpp +++ b/src/libslic3r/GCode.hpp @@ -458,6 +458,8 @@ private: bool m_brim_done; // Flag indicating whether the nozzle temperature changes from 1st to 2nd layer were performed. bool m_second_layer_things_done; + // G-code that is due to be written before the next extrusion + std::string m_pending_pre_extrusion_gcode; // Pointer to currently exporting PrintObject and instance index. GCode::PrintObjectInstance m_current_instance;