From c19ae886ec42cc0a887871955b3d3a919a0f27fa Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Thu, 21 Sep 2023 12:05:12 +0200 Subject: [PATCH] SPE-1899: Fixed out-of-synch of preview's moves slider, tool position and gcode window when export to binary gcode is enabled --- src/libslic3r/GCode/GCodeProcessor.cpp | 66 +++++--------------------- 1 file changed, 13 insertions(+), 53 deletions(-) diff --git a/src/libslic3r/GCode/GCodeProcessor.cpp b/src/libslic3r/GCode/GCodeProcessor.cpp index 690a0ed359..3b7f810268 100644 --- a/src/libslic3r/GCode/GCodeProcessor.cpp +++ b/src/libslic3r/GCode/GCodeProcessor.cpp @@ -4328,62 +4328,22 @@ void GCodeProcessor::post_process() out.close(); in.close(); + const std::string result_filename = m_result.filename; if (m_binarizer.is_enabled()) { - // updates m_result.lines_ends from binarized gcode file - m_result.lines_ends.clear(); - - FilePtr file(boost::nowide::fopen(out_path.c_str(), "rb")); - if (file.f != nullptr) { - fseek(file.f, 0, SEEK_END); - const long file_size = ftell(file.f); - rewind(file.f); - - // read file header - using namespace bgcode::core; - using namespace bgcode::binarize; - FileHeader file_header; - EResult res = read_header(*file.f, file_header, nullptr); - if (res == EResult::Success) { - // search first GCode block - BlockHeader block_header; - res = read_next_block_header(*file.f, file_header, block_header, EBlockType::GCode, nullptr, 0); - while (res == EResult::Success) { - GCodeBlock block; - res = block.read_data(*file.f, file_header, block_header); - if (res != EResult::Success) - break; - - // extract lines ends from block - std::vector& lines_ends = m_result.lines_ends.emplace_back(std::vector()); - for (size_t i = 0; i < block.raw_data.size(); ++i) { - if (block.raw_data[i] == '\n') - lines_ends.emplace_back(i + 1); - } - - if (ftell(file.f) == file_size) - break; - - // read next block header - res = read_next_block_header(*file.f, file_header, block_header, nullptr, 0); - if (res != EResult::Success) - break; - if (block_header.type != (uint16_t)EBlockType::GCode) { - res = EResult::InvalidBlockType; - break; - } - } - } - - if (res != EResult::Success && !m_result.lines_ends.empty() && !m_result.lines_ends.front().empty()) - // some error occourred, clear lines ends - m_result.lines_ends = { std::vector() }; - } + // The list of lines in the binary gcode is different from the original one. + // This requires to re-process the binarized file to be able to synchronize with it all the data needed by the preview, + // as gcode window, tool position and moves slider which relies on indexing the gcode lines. + reset(); + // the following call modifies m_result.filename + process_binary_file(out_path); + // restore the proper filename + m_result.filename = result_filename; } + else + export_lines.synchronize_moves(m_result); - export_lines.synchronize_moves(m_result); - - if (rename_file(out_path, m_result.filename)) - throw Slic3r::RuntimeError(std::string("Failed to rename the output G-code file from ") + out_path + " to " + m_result.filename + '\n' + + if (rename_file(out_path, result_filename)) + throw Slic3r::RuntimeError(std::string("Failed to rename the output G-code file from ") + out_path + " to " + result_filename + '\n' + "Is " + out_path + " locked?" + '\n'); }