WIP G-code Find-Replace: Optimize regexps, process complete G-code,

not only G-code of layers.
This commit is contained in:
Vojtech Bubnik 2022-01-06 14:13:36 +01:00 committed by YuSanka
parent 658f01b64b
commit add1e994fa
3 changed files with 24 additions and 8 deletions

View file

@ -1099,6 +1099,11 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
// modifies m_silent_time_estimator_enabled
DoExport::init_gcode_processor(print.config(), m_processor, m_silent_time_estimator_enabled);
if (! print.config().gcode_substitutions.values.empty()) {
m_find_replace = make_unique<GCodeFindReplace>(print.config());
file.set_find_replace(m_find_replace.get());
}
// resets analyzer's tracking data
m_last_height = 0.f;
m_last_layer_z = 0.f;
@ -1155,9 +1160,6 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
m_enable_extrusion_role_markers = false;
#endif /* HAS_PRESSURE_EQUALIZER */
if (! print.config().gcode_substitutions.values.empty())
m_find_replace = make_unique<GCodeFindReplace>(print.config());
// Write information on the generator.
file.write_format("; %s\n\n", Slic3r::header_slic3r_generated().c_str());
@ -1572,6 +1574,7 @@ void GCode::process_layers(
);
// The pipeline elements are joined using const references, thus no copying is performed.
output_stream.set_find_replace(nullptr);
if (m_spiral_vase && m_find_replace)
tbb::parallel_pipeline(12, generator & spiral_vase & cooling & find_replace & output);
else if (m_spiral_vase)
@ -1580,6 +1583,7 @@ void GCode::process_layers(
tbb::parallel_pipeline(12, generator & cooling & find_replace & output);
else
tbb::parallel_pipeline(12, generator & cooling & output);
output_stream.set_find_replace(m_find_replace.get());
}
// Process all layers of a single object instance (sequential mode) with a parallel pipeline:
@ -1623,6 +1627,7 @@ void GCode::process_layers(
);
// The pipeline elements are joined using const references, thus no copying is performed.
output_stream.set_find_replace(nullptr);
if (m_spiral_vase && m_find_replace)
tbb::parallel_pipeline(12, generator & spiral_vase & cooling & find_replace & output);
else if (m_spiral_vase)
@ -1631,6 +1636,7 @@ void GCode::process_layers(
tbb::parallel_pipeline(12, generator & cooling & find_replace & output);
else
tbb::parallel_pipeline(12, generator & cooling & output);
output_stream.set_find_replace(m_find_replace.get());
}
std::string GCode::placeholder_parser_process(const std::string &name, const std::string &templ, unsigned int current_extruder_id, const DynamicConfig *config_override)
@ -2847,7 +2853,10 @@ void GCode::GCodeOutputStream::write(const char *what)
// writes string to file
fwrite(gcode, 1, ::strlen(gcode), this->f);
//FIXME don't allocate a string, maybe process a batch of lines?
m_processor.process_buffer(std::string(gcode));
if (m_find_replace)
m_processor.process_buffer(m_find_replace->process_layer(std::string(gcode)));
else
m_processor.process_buffer(std::string(gcode));
}
}

View file

@ -190,6 +190,11 @@ private:
GCodeOutputStream(FILE *f, GCodeProcessor &processor) : f(f), m_processor(processor) {}
~GCodeOutputStream() { this->close(); }
// Set a find-replace post-processor to modify the G-code before GCodePostProcessor.
// It is being set to null inside process_layers(), because the find-replace process
// is being called on a secondary thread to improve performance.
void set_find_replace(GCodeFindReplace *find_replace) { m_find_replace = find_replace; }
bool is_open() const { return f; }
bool is_error() const;
@ -209,8 +214,10 @@ private:
void write_format(const char* format, ...);
private:
FILE *f = nullptr;
GCodeProcessor &m_processor;
FILE *f { nullptr };
// Find-replace post-processor to be called before GCodePostProcessor.
GCodeFindReplace *m_find_replace { nullptr };
GCodeProcessor &m_processor;
};
void _do_export(Print &print, GCodeOutputStream &file, ThumbnailsGeneratorCallback thumbnail_cb);

View file

@ -14,7 +14,7 @@ GCodeFindReplace::GCodeFindReplace(const PrintConfig &print_config)
for (size_t i = 0; i < subst.size(); i += 3) {
boost::regex pattern;
try {
pattern.assign(subst[i]);
pattern.assign(subst[i], boost::regex::optimize); // boost::regex::icase
} catch (const std::exception &ex) {
throw RuntimeError(std::string("Invalid gcode_substitutions parameter, failed to compile regular expression: ") + ex.what());
}
@ -60,7 +60,7 @@ std::string GCodeFindReplace::process_layer(const std::string &ain)
temp.clear();
temp.reserve(in->size());
boost::regex_replace(ToStringIterator(temp), in->begin(), in->end(),
substitution.pattern, substitution.format, boost::match_default | boost::format_all);
substitution.pattern, substitution.format, boost::match_default | boost::match_not_dot_newline | boost::match_not_dot_null | boost::format_all);
std::swap(out, temp);
in = &out;
}