GCodeProcessor -> Load config data from gcode files generated by PrusaSlicer

This commit is contained in:
enricoturri1966 2020-07-29 11:13:48 +02:00
parent 11cf9a87f1
commit 16e282110d
4 changed files with 52 additions and 5 deletions

View File

@ -346,6 +346,18 @@ void GCodeProcessor::apply_config(const PrintConfig& config)
}
}
void GCodeProcessor::apply_config(const DynamicPrintConfig& config)
{
m_parser.apply_config(config);
const ConfigOptionFloats* filament_diameters = config.option<ConfigOptionFloats>("filament_diameter");
if (filament_diameters != nullptr) {
for (double diam : filament_diameters->values) {
m_filament_diameters.push_back(static_cast<float>(diam));
}
}
}
void GCodeProcessor::enable_stealth_time_estimator(bool enabled)
{
m_time_processor.machines[static_cast<size_t>(ETimeMode::Stealth)].enabled = enabled;
@ -391,6 +403,28 @@ void GCodeProcessor::process_file(const std::string& filename)
auto start_time = std::chrono::high_resolution_clock::now();
#endif // ENABLE_GCODE_VIEWER_STATISTICS
// pre-processing
// parse the gcode file to detect its producer
if (m_producers_enabled) {
m_parser.parse_file(filename, [this](GCodeReader& reader, const GCodeReader::GCodeLine& line) {
std::string cmd = line.cmd();
if (cmd.length() == 0) {
std::string comment = line.comment();
if (comment.length() > 1 && detect_producer(comment))
m_parser.quit_parsing_file();
}
});
// if the gcode was produced by PrusaSlicer,
// extract the config from it
if (m_producer == EProducer::PrusaSlicer) {
DynamicPrintConfig config;
config.apply(FullPrintConfig::defaults());
config.load_from_gcode_file(filename);
apply_config(config);
}
}
m_result.id = ++s_result_id;
m_result.moves.emplace_back(MoveVertex());
m_parser.parse_file(filename, [this](GCodeReader& reader, const GCodeReader::GCodeLine& line) { process_gcode_line(line); });
@ -554,11 +588,12 @@ void GCodeProcessor::process_gcode_line(const GCodeReader::GCodeLine& line)
void GCodeProcessor::process_tags(const std::string& comment)
{
if (m_producers_enabled && m_producer == EProducer::Unknown && detect_producer(comment))
return;
else if (m_producers_enabled && m_producer != EProducer::Unknown) {
if (process_producers_tags(comment))
return;
// producers tags
if (m_producers_enabled) {
if (m_producer != EProducer::Unknown) {
if (process_producers_tags(comment))
return;
}
}
// extrusion role tag

View File

@ -266,6 +266,7 @@ namespace Slic3r {
GCodeProcessor() { reset(); }
void apply_config(const PrintConfig& config);
void apply_config(const DynamicPrintConfig& config);
void enable_stealth_time_estimator(bool enabled);
void enable_producers(bool enabled) { m_producers_enabled = enabled; }
void reset();

View File

@ -115,7 +115,12 @@ void GCodeReader::parse_file(const std::string &file, callback_t callback)
{
std::ifstream f(file);
std::string line;
#if ENABLE_GCODE_VIEWER
m_parsing_file = true;
while (m_parsing_file && std::getline(f, line))
#else
while (std::getline(f, line))
#endif // ENABLE_GCODE_VIEWER
this->parse_line(line, callback);
}

View File

@ -107,6 +107,9 @@ public:
{ GCodeLine gline; this->parse_line(line.c_str(), gline, callback); }
void parse_file(const std::string &file, callback_t callback);
#if ENABLE_GCODE_VIEWER
void quit_parsing_file() { m_parsing_file = false; }
#endif // ENABLE_GCODE_VIEWER
float& x() { return m_position[X]; }
float x() const { return m_position[X]; }
@ -145,6 +148,9 @@ private:
char m_extrusion_axis;
float m_position[NUM_AXES];
bool m_verbose;
#if ENABLE_GCODE_VIEWER
bool m_parsing_file{ false };
#endif // ENABLE_GCODE_VIEWER
};
} /* namespace Slic3r */