From 7da565f0582f470274d279e52daf0dd889f0de7d Mon Sep 17 00:00:00 2001 From: "qing.zhang" Date: Thu, 13 Jun 2024 16:03:24 +0800 Subject: [PATCH] ENH: remove the appended T cmd after change filament Jira: none Signed-off-by: qing.zhang Change-Id: Id5da64626b7343a71dcb38c06f5b5caf43ec40e2 --- src/libslic3r/GCode.cpp | 7 +-- src/libslic3r/GCode/GCodeProcessor.cpp | 59 +++++++++++++++++++++++++- src/libslic3r/GCode/GCodeProcessor.hpp | 1 + src/libslic3r/GCodeWriter.cpp | 6 ++- src/libslic3r/GCodeWriter.hpp | 3 ++ 5 files changed, 70 insertions(+), 6 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index b827b25d2..f2a413c45 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -1123,7 +1123,7 @@ void GCode::do_export(Print* print, const char* path, GCodeProcessorResult* resu BOOST_LOG_TRIVIAL(info) << boost::format("Will export G-code to %1% soon")%path; GCodeProcessor::s_IsBBLPrinter = print->is_BBL_Printer(); - + m_writer.set_is_bbl_printer(print->is_BBL_Printer()); print->set_started(psGCodeExport); // check if any custom gcode contains keywords used by the gcode processor to @@ -5466,9 +5466,10 @@ std::string GCode::set_extruder(unsigned int extruder_id, double print_z, bool b m_writer.reset_e(); } - // We inform the writer about what is happening, but we may not use the resulting gcode. + //BBS: don't add T[next extruder] if there is no T cmd on filament change + //We inform the writer about what is happening, but we may not use the resulting gcode. std::string toolchange_command = m_writer.toolchange(extruder_id); - if (! custom_gcode_changes_tool(toolchange_gcode_parsed, m_writer.toolchange_prefix(), extruder_id)) + if (!custom_gcode_changes_tool(toolchange_gcode_parsed, m_writer.toolchange_prefix(), extruder_id)) gcode += toolchange_command; else { // user provided his own toolchange gcode, no need to do anything diff --git a/src/libslic3r/GCode/GCodeProcessor.cpp b/src/libslic3r/GCode/GCodeProcessor.cpp index 04bdc8875..6147220f5 100644 --- a/src/libslic3r/GCode/GCodeProcessor.cpp +++ b/src/libslic3r/GCode/GCodeProcessor.cpp @@ -2037,8 +2037,27 @@ void GCodeProcessor::process_gcode_line(const GCodeReader::GCodeLine& line, bool break; } break; - default: - break; + case 5: + switch (cmd[1]) { + case '1': + switch (cmd[2]) { + case '0': + switch (cmd[3]) { + case '2': + switch (cmd[4]) { + case '0': { + process_M1020(line); // Select Tool + break; + } + default: break; + } + default: break; + } + default: break; + } + default: break; + } + default: break; } break; case 't': @@ -4221,6 +4240,42 @@ void GCodeProcessor::process_T(const GCodeReader::GCodeLine& line) process_T(line.cmd()); } +void GCodeProcessor::process_M1020(const GCodeReader::GCodeLine &line) +{ + if (line.raw().length() > 5) { + std::string filament_id = line.raw().substr(7); + if (filament_id.empty()) + return; + + int eid = 0; + eid = std::stoi(filament_id); + if (eid < 0 || eid > 254) { + // M1020-1 is a valid gcode line for RepRap Firmwares (used to deselects all tools) + if ((m_flavor != gcfRepRapFirmware && m_flavor != gcfRepRapSprinter) || eid != -1) + BOOST_LOG_TRIVIAL(error) << "Invalid M1020 command (" << line.raw() << ")."; + } else { + unsigned char id = static_cast(eid); + if (m_extruder_id != id) { + if (id >= m_result.extruders_count) + BOOST_LOG_TRIVIAL(error) << "Invalid M1020 command (" << line.raw() << ")."; + else { + m_last_extruder_id = m_extruder_id; + process_filaments(CustomGCode::ToolChange); + m_extruder_id = id; + m_cp_color.current = m_extruder_colors[id]; + // BBS: increase filament change times + m_result.lock(); + m_result.print_statistics.total_filamentchanges++; + m_result.unlock(); + } + + // store tool change move + store_move_vertex(EMoveType::Tool_change); + } + } + } +} + void GCodeProcessor::process_T(const std::string_view command) { if (command.length() > 1) { diff --git a/src/libslic3r/GCode/GCodeProcessor.hpp b/src/libslic3r/GCode/GCodeProcessor.hpp index e33bd4c95..3836425a7 100644 --- a/src/libslic3r/GCode/GCodeProcessor.hpp +++ b/src/libslic3r/GCode/GCodeProcessor.hpp @@ -956,6 +956,7 @@ namespace Slic3r { // Processes T line (Select Tool) void process_T(const GCodeReader::GCodeLine& line); void process_T(const std::string_view command); + void process_M1020(const GCodeReader::GCodeLine &line); //BBS: different path_type is only used for arc move void store_move_vertex(EMoveType type, EMovePathType path_type = EMovePathType::Noop_move); diff --git a/src/libslic3r/GCodeWriter.cpp b/src/libslic3r/GCodeWriter.cpp index f5489e521..927d13339 100644 --- a/src/libslic3r/GCodeWriter.cpp +++ b/src/libslic3r/GCodeWriter.cpp @@ -305,7 +305,11 @@ std::string GCodeWriter::toolchange(unsigned int extruder_id) // if we are running a single-extruder setup, just set the extruder and return nothing std::ostringstream gcode; if (this->multiple_extruders) { - gcode << this->toolchange_prefix() << extruder_id; + // BBS + if (this->m_is_bbl_printer) + gcode << "M1020 S" << extruder_id; + else + gcode << this->toolchange_prefix() << extruder_id; //BBS if (GCodeWriter::full_gcode_comment) gcode << " ; change extruder"; diff --git a/src/libslic3r/GCodeWriter.hpp b/src/libslic3r/GCodeWriter.hpp index bb8ddb001..709d124a6 100644 --- a/src/libslic3r/GCodeWriter.hpp +++ b/src/libslic3r/GCodeWriter.hpp @@ -106,6 +106,7 @@ public: //BBS: void set_current_position_clear(bool clear) { m_is_current_pos_clear = clear; }; bool is_current_position_clear() const { return m_is_current_pos_clear; }; + void set_is_bbl_printer(bool is_bbl_printer) { m_is_bbl_printer = is_bbl_printer; }; //BBS: static const bool full_gcode_comment; //Radian threshold of slope for lazy lift and spiral lift; @@ -140,6 +141,8 @@ private: double m_x_offset{ 0 }; double m_y_offset{ 0 }; double m_current_speed{ 0 }; + bool m_is_bbl_printer = false; + std::string m_gcode_label_objects_start; std::string m_gcode_label_objects_end;