Fixed visualization of G-code lines in G-code viewer (3D view).

Improved speed of parsing external G-code.
This commit is contained in:
Vojtech Bubnik 2021-09-21 15:30:37 +02:00
parent 116fd0526b
commit ac7674b85a
7 changed files with 163 additions and 73 deletions

View file

@ -343,18 +343,6 @@ void GCodeProcessor::TimeProcessor::reset()
machines[static_cast<size_t>(PrintEstimatedStatistics::ETimeMode::Normal)].enabled = true; machines[static_cast<size_t>(PrintEstimatedStatistics::ETimeMode::Normal)].enabled = true;
} }
struct FilePtr {
FilePtr(FILE *f) : f(f) {}
~FilePtr() { this->close(); }
void close() {
if (this->f) {
::fclose(this->f);
this->f = nullptr;
}
}
FILE* f = nullptr;
};
void GCodeProcessor::TimeProcessor::post_process(const std::string& filename, std::vector<MoveVertex>& moves, std::vector<size_t>& lines_ends) void GCodeProcessor::TimeProcessor::post_process(const std::string& filename, std::vector<MoveVertex>& moves, std::vector<size_t>& lines_ends)
{ {
FilePtr in{ boost::nowide::fopen(filename.c_str(), "rb") }; FilePtr in{ boost::nowide::fopen(filename.c_str(), "rb") };
@ -755,11 +743,11 @@ void GCodeProcessor::Result::reset() {
#endif // ENABLE_GCODE_VIEWER_STATISTICS #endif // ENABLE_GCODE_VIEWER_STATISTICS
const std::vector<std::pair<GCodeProcessor::EProducer, std::string>> GCodeProcessor::Producers = { const std::vector<std::pair<GCodeProcessor::EProducer, std::string>> GCodeProcessor::Producers = {
{ EProducer::PrusaSlicer, "PrusaSlicer" }, { EProducer::PrusaSlicer, "generated by PrusaSlicer" },
{ EProducer::Slic3rPE, "Slic3r Prusa Edition" }, { EProducer::Slic3rPE, "generated by Slic3r Prusa Edition" },
{ EProducer::Slic3r, "Slic3r" }, { EProducer::Slic3r, "generated by Slic3r" },
{ EProducer::Cura, "Cura_SteamEngine" }, { EProducer::Cura, "Cura_SteamEngine" },
{ EProducer::Simplify3D, "Simplify3D" }, { EProducer::Simplify3D, "G-Code generated by Simplify3D(R)" },
{ EProducer::CraftWare, "CraftWare" }, { EProducer::CraftWare, "CraftWare" },
{ EProducer::ideaMaker, "ideaMaker" }, { EProducer::ideaMaker, "ideaMaker" },
{ EProducer::KissSlicer, "KISSlicer" } { EProducer::KissSlicer, "KISSlicer" }
@ -1191,6 +1179,16 @@ void GCodeProcessor::reset()
#endif // ENABLE_GCODE_VIEWER_DATA_CHECKING #endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
} }
static inline const char* skip_whitespaces(const char *begin, const char *end) {
for (; begin != end && (*begin == ' ' || *begin == '\t'); ++ begin);
return begin;
}
static inline const char* remove_eols(const char *begin, const char *end) {
for (; begin != end && (*(end - 1) == '\r' || *(end - 1) == '\n'); -- end);
return end;
}
void GCodeProcessor::process_file(const std::string& filename, std::function<void()> cancel_callback) void GCodeProcessor::process_file(const std::string& filename, std::function<void()> cancel_callback)
{ {
CNumericLocalesSetter locales_setter; CNumericLocalesSetter locales_setter;
@ -1202,14 +1200,16 @@ void GCodeProcessor::process_file(const std::string& filename, std::function<voi
// pre-processing // pre-processing
// parse the gcode file to detect its producer // parse the gcode file to detect its producer
{ {
m_parser.parse_file(filename, [this](GCodeReader& reader, const GCodeReader::GCodeLine& line) { m_parser.parse_file_raw(filename, [this](GCodeReader& reader, const char *begin, const char *end) {
const std::string_view cmd = line.cmd(); begin = skip_whitespaces(begin, end);
if (cmd.empty()) { if (begin != end && *begin == ';') {
const std::string_view comment = line.comment(); // Comment.
if (comment.length() > 1 && detect_producer(comment)) begin = skip_whitespaces(++ begin, end);
end = remove_eols(begin, end);
if (begin != end && detect_producer(std::string_view(begin, end - begin)))
m_parser.quit_parsing(); m_parser.quit_parsing();
} }
}); });
m_parser.reset(); m_parser.reset();
// if the gcode was produced by PrusaSlicer, // if the gcode was produced by PrusaSlicer,
@ -1374,9 +1374,11 @@ void GCodeProcessor::apply_config_simplify3d(const std::string& filename)
}; };
BedSize bed_size; BedSize bed_size;
bool producer_detected = false;
m_parser.parse_file(filename, [this, &bed_size](GCodeReader& reader, const GCodeReader::GCodeLine& line) { m_parser.parse_file_raw(filename, [this, &bed_size, &producer_detected](GCodeReader& reader, const char* begin, const char* end) {
auto extract_double = [](const std::string& cmt, const std::string& key, double& out) {
auto extract_double = [](const std::string_view cmt, const std::string& key, double& out) {
size_t pos = cmt.find(key); size_t pos = cmt.find(key);
if (pos != cmt.npos) { if (pos != cmt.npos) {
pos = cmt.find(',', pos); pos = cmt.find(',', pos);
@ -1388,12 +1390,12 @@ void GCodeProcessor::apply_config_simplify3d(const std::string& filename)
return false; return false;
}; };
auto extract_floats = [](const std::string& cmt, const std::string& key, std::vector<float>& out) { auto extract_floats = [](const std::string_view cmt, const std::string& key, std::vector<float>& out) {
size_t pos = cmt.find(key); size_t pos = cmt.find(key);
if (pos != cmt.npos) { if (pos != cmt.npos) {
pos = cmt.find(',', pos); pos = cmt.find(',', pos);
if (pos != cmt.npos) { if (pos != cmt.npos) {
std::string data_str = cmt.substr(pos + 1); const std::string_view data_str = cmt.substr(pos + 1);
std::vector<std::string> values_str; std::vector<std::string> values_str;
boost::split(values_str, data_str, boost::is_any_of("|,"), boost::token_compress_on); boost::split(values_str, data_str, boost::is_any_of("|,"), boost::token_compress_on);
for (const std::string& s : values_str) { for (const std::string& s : values_str) {
@ -1404,28 +1406,39 @@ void GCodeProcessor::apply_config_simplify3d(const std::string& filename)
} }
return false; return false;
}; };
const std::string& comment = line.raw(); begin = skip_whitespaces(begin, end);
if (comment.length() > 2 && comment.front() == ';') { end = remove_eols(begin, end);
if (bed_size.x == 0.0 && comment.find("strokeXoverride") != comment.npos) if (begin != end)
extract_double(comment, "strokeXoverride", bed_size.x); if (*begin == ';') {
else if (bed_size.y == 0.0 && comment.find("strokeYoverride") != comment.npos) // Comment.
extract_double(comment, "strokeYoverride", bed_size.y); begin = skip_whitespaces(++ begin, end);
else if (comment.find("filamentDiameters") != comment.npos) { if (begin != end) {
m_result.filament_diameters.clear(); std::string_view comment(begin, end - begin);
extract_floats(comment, "filamentDiameters", m_result.filament_diameters); if (producer_detected) {
if (bed_size.x == 0.0 && comment.find("strokeXoverride") != comment.npos)
extract_double(comment, "strokeXoverride", bed_size.x);
else if (bed_size.y == 0.0 && comment.find("strokeYoverride") != comment.npos)
extract_double(comment, "strokeYoverride", bed_size.y);
else if (comment.find("filamentDiameters") != comment.npos) {
m_result.filament_diameters.clear();
extract_floats(comment, "filamentDiameters", m_result.filament_diameters);
} else if (comment.find("filamentDensities") != comment.npos) {
m_result.filament_densities.clear();
extract_floats(comment, "filamentDensities", m_result.filament_densities);
} else if (comment.find("extruderDiameter") != comment.npos) {
std::vector<float> extruder_diameters;
extract_floats(comment, "extruderDiameter", extruder_diameters);
m_result.extruders_count = extruder_diameters.size();
}
} else if (boost::starts_with(comment, "G-Code generated by Simplify3D(R)"))
producer_detected = true;
}
} else {
// Some non-empty G-code line detected, stop parsing config comments.
reader.quit_parsing();
} }
else if (comment.find("filamentDensities") != comment.npos) { });
m_result.filament_densities.clear();
extract_floats(comment, "filamentDensities", m_result.filament_densities);
}
else if (comment.find("extruderDiameter") != comment.npos) {
std::vector<float> extruder_diameters;
extract_floats(comment, "extruderDiameter", extruder_diameters);
m_result.extruders_count = extruder_diameters.size();
}
}
});
if (m_result.extruders_count == 0) if (m_result.extruders_count == 0)
m_result.extruders_count = std::max<size_t>(1, std::min(m_result.filament_diameters.size(), m_result.filament_densities.size())); m_result.extruders_count = std::max<size_t>(1, std::min(m_result.filament_diameters.size(), m_result.filament_densities.size()));

View file

@ -5,6 +5,7 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include "Utils.hpp"
#include "LocalesUtils.hpp" #include "LocalesUtils.hpp"
@ -126,44 +127,92 @@ void GCodeReader::update_coordinates(GCodeLine &gline, std::pair<const char*, co
} }
} }
bool GCodeReader::parse_file(const std::string &file, callback_t callback) template<typename ParseLineCallback, typename LineEndCallback>
bool GCodeReader::parse_file_raw_internal(const std::string &filename, ParseLineCallback parse_line_callback, LineEndCallback line_end_callback)
{ {
boost::nowide::ifstream f(file); FilePtr in{ boost::nowide::fopen(filename.c_str(), "rb") };
f.sync_with_stdio(false);
// Read the input stream 64kB at a time, extract lines and process them.
std::vector<char> buffer(65536 * 10, 0); std::vector<char> buffer(65536 * 10, 0);
std::string line; // Line buffer.
std::string gcode_line;
size_t file_pos = 0;
m_parsing = true; m_parsing = true;
GCodeLine gline; for (;;) {
while (m_parsing && ! f.eof()) { size_t cnt_read = ::fread(buffer.data(), 1, buffer.size(), in.f);
f.read(buffer.data(), buffer.size()); if (::ferror(in.f))
if (! f.eof() && ! f.good())
// Reading the input file failed.
return false; return false;
bool eof = cnt_read == 0;
auto it = buffer.begin(); auto it = buffer.begin();
auto it_bufend = buffer.begin() + f.gcount(); auto it_bufend = buffer.begin() + cnt_read;
while (it != it_bufend) { while (it != it_bufend || (eof && ! gcode_line.empty())) {
bool eol = false; // Find end of line.
bool eol = false;
auto it_end = it; auto it_end = it;
for (; it_end != it_bufend && ! (eol = *it_end == '\r' || *it_end == '\n'); ++ it_end) ; for (; it_end != it_bufend && ! (eol = *it_end == '\r' || *it_end == '\n'); ++ it_end)
eol |= f.eof() && it_end == it_bufend; if (*it_end == '\n')
line_end_callback((it_end - buffer.begin()) + 1);
// End of line is indicated also if end of file was reached.
eol |= eof && it_end == it_bufend;
if (eol) { if (eol) {
gline.reset(); if (gcode_line.empty())
if (line.empty()) parse_line_callback(&(*it), &(*it_end));
this->parse_line(&(*it), &(*it_end), gline, callback);
else { else {
line.insert(line.end(), it, it_end); gcode_line.insert(gcode_line.end(), it, it_end);
this->parse_line(line.c_str(), line.c_str() + line.size(), gline, callback); parse_line_callback(gcode_line.c_str(), gcode_line.c_str() + gcode_line.size());
line.clear(); gcode_line.clear();
} }
if (! m_parsing)
// The callback wishes to exit.
return true;
} else } else
line.insert(line.end(), it, it_end); gcode_line.insert(gcode_line.end(), it, it_end);
// Skip all the empty lines. // Skip EOL.
for (it = it_end; it != it_bufend && (*it == '\r' || *it == '\n'); ++ it) ; it = it_end;
if (it != it_bufend && *it == '\r')
++ it;
if (it != it_bufend && *it == '\n') {
line_end_callback((it - buffer.begin()) + 1);
++ it;
}
} }
if (eof)
break;
file_pos += cnt_read;
} }
return true; return true;
} }
template<typename ParseLineCallback, typename LineEndCallback>
bool GCodeReader::parse_file_internal(const std::string &filename, ParseLineCallback parse_line_callback, LineEndCallback line_end_callback)
{
GCodeLine gline;
return this->parse_file_raw_internal(filename,
[this, &gline, parse_line_callback](const char *begin, const char *end) {
gline.reset();
this->parse_line(begin, end, gline, parse_line_callback);
},
line_end_callback);
}
bool GCodeReader::parse_file(const std::string &file, callback_t callback)
{
return this->parse_file_internal(file, callback, [](size_t){});
}
bool GCodeReader::parse_file(const std::string &file, callback_t callback, std::vector<size_t> &lines_ends)
{
lines_ends.clear();
return this->parse_file_internal(file, callback, [&lines_ends](size_t file_pos){ lines_ends.emplace_back(file_pos); });
}
bool GCodeReader::parse_file_raw(const std::string &filename, raw_line_callback_t line_callback)
{
return this->parse_file_raw_internal(filename,
[this, line_callback](const char *begin, const char *end) { line_callback(*this, begin, end); },
[](size_t){});
}
bool GCodeReader::GCodeLine::has(char axis) const bool GCodeReader::GCodeLine::has(char axis) const
{ {
const char *c = m_raw.c_str(); const char *c = m_raw.c_str();

View file

@ -76,6 +76,7 @@ public:
}; };
typedef std::function<void(GCodeReader&, const GCodeLine&)> callback_t; typedef std::function<void(GCodeReader&, const GCodeLine&)> callback_t;
typedef std::function<void(GCodeReader&, const char*, const char*)> raw_line_callback_t;
GCodeReader() : m_verbose(false), m_extrusion_axis('E') { this->reset(); } GCodeReader() : m_verbose(false), m_extrusion_axis('E') { this->reset(); }
void reset() { memset(m_position, 0, sizeof(m_position)); } void reset() { memset(m_position, 0, sizeof(m_position)); }
@ -114,6 +115,13 @@ public:
// Returns false if reading the file failed. // Returns false if reading the file failed.
bool parse_file(const std::string &file, callback_t callback); bool parse_file(const std::string &file, callback_t callback);
// Collect positions of line ends in the binary G-code to be used by the G-code viewer when memory mapping and displaying section of G-code
// as an overlay in the 3D scene.
bool parse_file(const std::string &file, callback_t callback, std::vector<size_t> &lines_ends);
// Just read the G-code file line by line, calls callback (const char *begin, const char *end). Returns false if reading the file failed.
bool parse_file_raw(const std::string &file, raw_line_callback_t callback);
// To be called by the callback to stop parsing.
void quit_parsing() { m_parsing = false; } void quit_parsing() { m_parsing = false; }
float& x() { return m_position[X]; } float& x() { return m_position[X]; }
@ -132,6 +140,11 @@ public:
// void set_extrusion_axis(char axis) { m_extrusion_axis = axis; } // void set_extrusion_axis(char axis) { m_extrusion_axis = axis; }
private: private:
template<typename ParseLineCallback, typename LineEndCallback>
bool parse_file_raw_internal(const std::string &filename, ParseLineCallback parse_line_callback, LineEndCallback line_end_callback);
template<typename ParseLineCallback, typename LineEndCallback>
bool parse_file_internal(const std::string &filename, ParseLineCallback parse_line_callback, LineEndCallback line_end_callback);
const char* parse_line_internal(const char *ptr, const char *end, GCodeLine &gline, std::pair<const char*, const char*> &command); const char* parse_line_internal(const char *ptr, const char *end, GCodeLine &gline, std::pair<const char*, const char*> &command);
void update_coordinates(GCodeLine &gline, std::pair<const char*, const char*> &command); void update_coordinates(GCodeLine &gline, std::pair<const char*, const char*> &command);
@ -154,6 +167,7 @@ private:
char m_extrusion_axis; char m_extrusion_axis;
float m_position[NUM_AXES]; float m_position[NUM_AXES];
bool m_verbose; bool m_verbose;
// To be set by the callback to stop parsing.
bool m_parsing{ false }; bool m_parsing{ false };
}; };

View file

@ -51,7 +51,7 @@ bool is_decimal_separator_point()
} }
double string_to_double_decimal_point(const std::string& str, size_t* pos /* = nullptr*/) double string_to_double_decimal_point(const std::string_view str, size_t* pos /* = nullptr*/)
{ {
double out; double out;
size_t p = fast_float::from_chars(str.data(), str.data() + str.size(), out).ptr - str.data(); size_t p = fast_float::from_chars(str.data(), str.data() + str.size(), out).ptr - str.data();

View file

@ -5,6 +5,7 @@
#include <clocale> #include <clocale>
#include <iomanip> #include <iomanip>
#include <cassert> #include <cassert>
#include <string_view>
#ifdef __APPLE__ #ifdef __APPLE__
#include <xlocale.h> #include <xlocale.h>
@ -40,7 +41,7 @@ bool is_decimal_separator_point();
// (We use user C locales and "C" C++ locales in most of the code.) // (We use user C locales and "C" C++ locales in most of the code.)
std::string float_to_string_decimal_point(double value, int precision = -1); std::string float_to_string_decimal_point(double value, int precision = -1);
//std::string float_to_string_decimal_point(float value, int precision = -1); //std::string float_to_string_decimal_point(float value, int precision = -1);
double string_to_double_decimal_point(const std::string& str, size_t* pos = nullptr); double string_to_double_decimal_point(const std::string_view str, size_t* pos = nullptr);
} // namespace Slic3r } // namespace Slic3r

View file

@ -255,6 +255,19 @@ template<typename T> struct IsTriviallyCopyable { static constexpr bool value =
template<typename T> struct IsTriviallyCopyable : public std::is_trivially_copyable<T> {}; template<typename T> struct IsTriviallyCopyable : public std::is_trivially_copyable<T> {};
#endif #endif
// A very lightweight ROII wrapper around C FILE.
// The old C file API is much faster than C++ streams, thus they are recommended for processing large / huge files.
struct FilePtr {
FilePtr(FILE *f) : f(f) {}
~FilePtr() { this->close(); }
void close() {
if (this->f) {
::fclose(this->f);
this->f = nullptr;
}
}
FILE* f = nullptr;
};
class ScopeGuard class ScopeGuard
{ {

View file

@ -889,7 +889,7 @@ std::string string_printf(const char *format, ...)
std::string header_slic3r_generated() std::string header_slic3r_generated()
{ {
return std::string("generated by " SLIC3R_APP_NAME " " SLIC3R_VERSION " on " ) + Utils::utc_timestamp(); return std::string("generated by PrusaSlicer " SLIC3R_VERSION " on " ) + Utils::utc_timestamp();
} }
std::string header_gcodeviewer_generated() std::string header_gcodeviewer_generated()