SPE-1899: Fixed out-of-synch of preview's moves slider, tool position and gcode window when export to binary gcode is enabled

This commit is contained in:
enricoturri1966 2023-09-21 12:05:12 +02:00
parent 758ef7b03c
commit c19ae886ec

View File

@ -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<size_t>& lines_ends = m_result.lines_ends.emplace_back(std::vector<size_t>());
for (size_t i = 0; i < block.raw_data.size(); ++i) {
if (block.raw_data[i] == '\n')
lines_ends.emplace_back(i + 1);
// 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;
}
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<size_t>() };
}
}
else
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');
}