From 117a6ace5a31b5f44a2b1e354d0c44cf5d2e2289 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Mon, 15 Mar 2021 11:27:24 +0100 Subject: [PATCH] G-code window - Fixed file mapping for gcode files generated by other slicers --- src/slic3r/GUI/GCodeViewer.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/slic3r/GUI/GCodeViewer.cpp b/src/slic3r/GUI/GCodeViewer.cpp index 7abcbdc41..17e3ac68c 100644 --- a/src/slic3r/GUI/GCodeViewer.cpp +++ b/src/slic3r/GUI/GCodeViewer.cpp @@ -308,15 +308,28 @@ void GCodeViewer::SequentialView::GCodeWindow::load_gcode() try { - boost::nowide::ifstream f(m_filename); - std::string line; // generate mapping for accessing data in file by line number + boost::nowide::ifstream f(m_filename); + + f.seekg(0, f.end); + uint64_t file_length = static_cast(f.tellg()); + f.seekg(0, f.beg); + + std::string line; uint64_t offset = 0; while (std::getline(f, line)) { - size_t line_length = static_cast(line.length()); + uint64_t line_length = static_cast(line.length()); m_lines_map.push_back({ offset, line_length }); offset += static_cast(line_length) + 1; - } + } + + if (offset != file_length) { + // if the final offset does not match with file length, lines are terminated with CR+LF + // so update all offsets accordingly + for (size_t i = 0; i < m_lines_map.size(); ++i) { + m_lines_map[i].first += static_cast(i); + } + } } catch (...) {