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 (...) {