Fixed visualization of G-code lines in G-code viewer (3D view).
Improved speed of parsing external G-code.
This commit is contained in:
parent
116fd0526b
commit
ac7674b85a
7 changed files with 163 additions and 73 deletions
src/libslic3r/GCode
|
@ -343,18 +343,6 @@ void GCodeProcessor::TimeProcessor::reset()
|
|||
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)
|
||||
{
|
||||
FilePtr in{ boost::nowide::fopen(filename.c_str(), "rb") };
|
||||
|
@ -755,11 +743,11 @@ void GCodeProcessor::Result::reset() {
|
|||
#endif // ENABLE_GCODE_VIEWER_STATISTICS
|
||||
|
||||
const std::vector<std::pair<GCodeProcessor::EProducer, std::string>> GCodeProcessor::Producers = {
|
||||
{ EProducer::PrusaSlicer, "PrusaSlicer" },
|
||||
{ EProducer::Slic3rPE, "Slic3r Prusa Edition" },
|
||||
{ EProducer::Slic3r, "Slic3r" },
|
||||
{ EProducer::PrusaSlicer, "generated by PrusaSlicer" },
|
||||
{ EProducer::Slic3rPE, "generated by Slic3r Prusa Edition" },
|
||||
{ EProducer::Slic3r, "generated by Slic3r" },
|
||||
{ EProducer::Cura, "Cura_SteamEngine" },
|
||||
{ EProducer::Simplify3D, "Simplify3D" },
|
||||
{ EProducer::Simplify3D, "G-Code generated by Simplify3D(R)" },
|
||||
{ EProducer::CraftWare, "CraftWare" },
|
||||
{ EProducer::ideaMaker, "ideaMaker" },
|
||||
{ EProducer::KissSlicer, "KISSlicer" }
|
||||
|
@ -1191,6 +1179,16 @@ void GCodeProcessor::reset()
|
|||
#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)
|
||||
{
|
||||
CNumericLocalesSetter locales_setter;
|
||||
|
@ -1202,14 +1200,16 @@ void GCodeProcessor::process_file(const std::string& filename, std::function<voi
|
|||
// pre-processing
|
||||
// parse the gcode file to detect its producer
|
||||
{
|
||||
m_parser.parse_file(filename, [this](GCodeReader& reader, const GCodeReader::GCodeLine& line) {
|
||||
const std::string_view cmd = line.cmd();
|
||||
if (cmd.empty()) {
|
||||
const std::string_view comment = line.comment();
|
||||
if (comment.length() > 1 && detect_producer(comment))
|
||||
m_parser.parse_file_raw(filename, [this](GCodeReader& reader, const char *begin, const char *end) {
|
||||
begin = skip_whitespaces(begin, end);
|
||||
if (begin != end && *begin == ';') {
|
||||
// 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.reset();
|
||||
|
||||
// if the gcode was produced by PrusaSlicer,
|
||||
|
@ -1374,9 +1374,11 @@ void GCodeProcessor::apply_config_simplify3d(const std::string& filename)
|
|||
};
|
||||
|
||||
BedSize bed_size;
|
||||
bool producer_detected = false;
|
||||
|
||||
m_parser.parse_file(filename, [this, &bed_size](GCodeReader& reader, const GCodeReader::GCodeLine& line) {
|
||||
auto extract_double = [](const std::string& cmt, const std::string& key, double& out) {
|
||||
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_view cmt, const std::string& key, double& out) {
|
||||
size_t pos = cmt.find(key);
|
||||
if (pos != cmt.npos) {
|
||||
pos = cmt.find(',', pos);
|
||||
|
@ -1388,12 +1390,12 @@ void GCodeProcessor::apply_config_simplify3d(const std::string& filename)
|
|||
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);
|
||||
if (pos != cmt.npos) {
|
||||
pos = cmt.find(',', pos);
|
||||
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;
|
||||
boost::split(values_str, data_str, boost::is_any_of("|,"), boost::token_compress_on);
|
||||
for (const std::string& s : values_str) {
|
||||
|
@ -1404,28 +1406,39 @@ void GCodeProcessor::apply_config_simplify3d(const std::string& filename)
|
|||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const std::string& comment = line.raw();
|
||||
if (comment.length() > 2 && comment.front() == ';') {
|
||||
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);
|
||||
|
||||
begin = skip_whitespaces(begin, end);
|
||||
end = remove_eols(begin, end);
|
||||
if (begin != end)
|
||||
if (*begin == ';') {
|
||||
// Comment.
|
||||
begin = skip_whitespaces(++ begin, end);
|
||||
if (begin != end) {
|
||||
std::string_view comment(begin, end - begin);
|
||||
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)
|
||||
m_result.extruders_count = std::max<size_t>(1, std::min(m_result.filament_diameters.size(), m_result.filament_densities.size()));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue