From 431eabd464b20942e7421ca036585cedbf8c25e7 Mon Sep 17 00:00:00 2001 From: "salt.wei" Date: Sun, 29 Jan 2023 11:47:57 +0800 Subject: [PATCH] ENH: use gcode processer to get precise layer number Using layer z to calculate total layer number is not precision, because for some models with sharp top layer, the top layer may be empty. Check the final layer change in final gcode file. This must be same with total layer number. Jira: Studio-2123 Signed-off-by: salt.wei Change-Id: I5d001a8bb9548c3fb5f67fda966051ac3bb16502 --- src/libslic3r/GCode.cpp | 2 +- src/libslic3r/GCode/GCodeProcessor.cpp | 13 ++++++++++--- src/libslic3r/GCode/GCodeProcessor.hpp | 5 +++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index ba2e19bfa..4fb3637b1 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -1482,7 +1482,7 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato //BBS: total estimated printing time file.write_format(";%s\n", GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Estimated_Printing_Time_Placeholder).c_str()); //BBS: total layer number - file.write_format("; total layer number: %d\n", m_layer_count); + file.write_format(";%s\n", GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Total_Layer_Number_Placeholder).c_str()); file.write_format("; HEADER_BLOCK_END\n\n"); //BBS: write global config at the beginning of gcode file because printer need these config information diff --git a/src/libslic3r/GCode/GCodeProcessor.cpp b/src/libslic3r/GCode/GCodeProcessor.cpp index 40912dcc3..22be036ae 100644 --- a/src/libslic3r/GCode/GCodeProcessor.cpp +++ b/src/libslic3r/GCode/GCodeProcessor.cpp @@ -54,7 +54,8 @@ const std::vector GCodeProcessor::Reserved_Tags = { " CUSTOM_GCODE", "_GP_FIRST_LINE_M73_PLACEHOLDER", "_GP_LAST_LINE_M73_PLACEHOLDER", - "_GP_ESTIMATED_PRINTING_TIME_PLACEHOLDER" + "_GP_ESTIMATED_PRINTING_TIME_PLACEHOLDER", + "_GP_TOTAL_LAYER_NUMBER_PLACEHOLDER" }; const std::string GCodeProcessor::Flush_Start_Tag = " FLUSH_START"; @@ -358,7 +359,7 @@ void GCodeProcessor::TimeProcessor::reset() machines[static_cast(PrintEstimatedStatistics::ETimeMode::Normal)].enabled = true; } -void GCodeProcessor::TimeProcessor::post_process(const std::string& filename, std::vector& moves, std::vector& lines_ends) +void GCodeProcessor::TimeProcessor::post_process(const std::string& filename, std::vector& moves, std::vector& lines_ends, size_t total_layer_num) { FilePtr in{ boost::nowide::fopen(filename.c_str(), "rb") }; if (in.f == nullptr) @@ -470,6 +471,12 @@ void GCodeProcessor::TimeProcessor::post_process(const std::string& filename, st } } } + //BBS: write total layer number + else if (line == reserved_tag(ETags::Total_Layer_Number_Placeholder)) { + char buf[128]; + sprintf(buf, "; total layer number: %zd\n", total_layer_num); + ret += buf; + } } if (! ret.empty()) @@ -1424,7 +1431,7 @@ void GCodeProcessor::finalize(bool post_process) #endif // ENABLE_GCODE_VIEWER_DATA_CHECKING if (post_process) - m_time_processor.post_process(m_result.filename, m_result.moves, m_result.lines_ends); + m_time_processor.post_process(m_result.filename, m_result.moves, m_result.lines_ends, m_layer_id); #if ENABLE_GCODE_VIEWER_STATISTICS m_result.time = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - m_start_time).count(); #endif // ENABLE_GCODE_VIEWER_STATISTICS diff --git a/src/libslic3r/GCode/GCodeProcessor.hpp b/src/libslic3r/GCode/GCodeProcessor.hpp index e95527605..d29b18f32 100644 --- a/src/libslic3r/GCode/GCodeProcessor.hpp +++ b/src/libslic3r/GCode/GCodeProcessor.hpp @@ -224,7 +224,8 @@ namespace Slic3r { Custom_Code, First_Line_M73_Placeholder, Last_Line_M73_Placeholder, - Estimated_Printing_Time_Placeholder + Estimated_Printing_Time_Placeholder, + Total_Layer_Number_Placeholder }; static const std::string& reserved_tag(ETags tag) { return Reserved_Tags[static_cast(tag)]; } @@ -422,7 +423,7 @@ namespace Slic3r { // post process the file with the given filename to add remaining time lines M73 // and updates moves' gcode ids accordingly - void post_process(const std::string& filename, std::vector& moves, std::vector& lines_ends); + void post_process(const std::string& filename, std::vector& moves, std::vector& lines_ends, size_t total_layer_num); }; struct UsedFilaments // filaments per ColorChange