diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 6d0a076fde..00663a32d3 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -6460,6 +6460,9 @@ std::string GCode::retract(bool toolchange, bool is_last_retraction, LiftType li std::string GCode::set_extruder(unsigned int new_filament_id, double print_z, bool by_object) { + //init extrude + m_writer.init_extruder(new_filament_id); + int new_extruder_id = get_extruder_id(new_filament_id); if (!m_writer.need_toolchange(new_filament_id)) return ""; diff --git a/src/libslic3r/GCodeWriter.cpp b/src/libslic3r/GCodeWriter.cpp index 32dc859403..04d546c98f 100644 --- a/src/libslic3r/GCodeWriter.cpp +++ b/src/libslic3r/GCodeWriter.cpp @@ -994,6 +994,15 @@ std::string GCodeWriter::set_extruder(unsigned int filament_id) return this->need_toolchange(filament_id) ? this->toolchange(filament_id) : ""; } +void GCodeWriter::init_extruder(unsigned int filament_id) +{ + if (m_curr_extruder_id == -1 && filament_id != -1) { + auto filament_extruder_iter = Slic3r::lower_bound_by_predicate(m_filament_extruders.begin(), m_filament_extruders.end(), [filament_id](const Extruder &e) { return e.id() < filament_id; }); + assert(filament_extruder_iter != m_filament_extruders.end() && filament_extruder_iter->id() == filament_id); + m_curr_extruder_id = filament_extruder_iter->extruder_id(); + m_curr_filament_extruder[m_curr_extruder_id] = &*filament_extruder_iter; + } +} bool GCodeWriter::need_toolchange(unsigned int filament_id)const { diff --git a/src/libslic3r/GCodeWriter.hpp b/src/libslic3r/GCodeWriter.hpp index 0fa7fad4ee..720d89a4c1 100644 --- a/src/libslic3r/GCodeWriter.hpp +++ b/src/libslic3r/GCodeWriter.hpp @@ -64,6 +64,7 @@ public: // return false if this extruder was already selected bool need_toolchange(unsigned int filament_id) const; std::string set_extruder(unsigned int filament_id); + void init_extruder(unsigned int filament_id); // Prefix of the toolchange G-code line, to be used by the CoolingBuffer to separate sections of the G-code // printed with the same extruder. std::string toolchange_prefix() const;