Fix of gcode_substitutions will cause errors in prusaslicer_config dump at the end of gcode #7952

Suppress the G-code find / replace substitutions for the non-G-code
sections (comment blocks) at the start and at the end of the G-code file.
This commit is contained in:
Vojtech Bubnik 2022-02-21 11:04:43 +01:00
parent 6937b34fdc
commit 8aefe3fc90
2 changed files with 17 additions and 6 deletions

View File

@ -1101,7 +1101,7 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
if (! print.config().gcode_substitutions.values.empty()) { if (! print.config().gcode_substitutions.values.empty()) {
m_find_replace = make_unique<GCodeFindReplace>(print.config()); m_find_replace = make_unique<GCodeFindReplace>(print.config());
file.set_find_replace(m_find_replace.get()); file.set_find_replace(m_find_replace.get(), false);
} }
// resets analyzer's tracking data // resets analyzer's tracking data
@ -1202,6 +1202,9 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
} }
print.throw_if_canceled(); print.throw_if_canceled();
// Starting now, the G-code find / replace post-processor will be enabled.
file.find_replace_enable();
// adds tags for time estimators // adds tags for time estimators
if (print.config().remaining_times.value) if (print.config().remaining_times.value)
file.write_format(";%s\n", GCodeProcessor::reserved_tag(GCodeProcessor::ETags::First_Line_M73_Placeholder).c_str()); file.write_format(";%s\n", GCodeProcessor::reserved_tag(GCodeProcessor::ETags::First_Line_M73_Placeholder).c_str());
@ -1517,6 +1520,10 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
file.write_format("; total toolchanges = %i\n", print.m_print_statistics.total_toolchanges); file.write_format("; total toolchanges = %i\n", print.m_print_statistics.total_toolchanges);
file.write_format(";%s\n", GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Estimated_Printing_Time_Placeholder).c_str()); file.write_format(";%s\n", GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Estimated_Printing_Time_Placeholder).c_str());
// From now to the end of G-code, the G-code find / replace post-processor will be disabled.
// Thus the PrusaSlicer generated config will NOT be processed by the G-code post-processor, see GH issue #7952.
file.find_replace_supress();
// Append full config, delimited by two 'phony' configuration keys prusaslicer_config = begin and prusaslicer_config = end. // Append full config, delimited by two 'phony' configuration keys prusaslicer_config = begin and prusaslicer_config = end.
// The delimiters are structured as configuration key / value pairs to be parsable by older versions of PrusaSlicer G-code viewer. // The delimiters are structured as configuration key / value pairs to be parsable by older versions of PrusaSlicer G-code viewer.
{ {
@ -1574,7 +1581,7 @@ void GCode::process_layers(
); );
// The pipeline elements are joined using const references, thus no copying is performed. // The pipeline elements are joined using const references, thus no copying is performed.
output_stream.set_find_replace(nullptr); output_stream.find_replace_supress();
if (m_spiral_vase && m_find_replace) if (m_spiral_vase && m_find_replace)
tbb::parallel_pipeline(12, generator & spiral_vase & cooling & find_replace & output); tbb::parallel_pipeline(12, generator & spiral_vase & cooling & find_replace & output);
else if (m_spiral_vase) else if (m_spiral_vase)
@ -1583,7 +1590,7 @@ void GCode::process_layers(
tbb::parallel_pipeline(12, generator & cooling & find_replace & output); tbb::parallel_pipeline(12, generator & cooling & find_replace & output);
else else
tbb::parallel_pipeline(12, generator & cooling & output); tbb::parallel_pipeline(12, generator & cooling & output);
output_stream.set_find_replace(m_find_replace.get()); output_stream.find_replace_enable();
} }
// Process all layers of a single object instance (sequential mode) with a parallel pipeline: // Process all layers of a single object instance (sequential mode) with a parallel pipeline:
@ -1627,7 +1634,7 @@ void GCode::process_layers(
); );
// The pipeline elements are joined using const references, thus no copying is performed. // The pipeline elements are joined using const references, thus no copying is performed.
output_stream.set_find_replace(nullptr); output_stream.find_replace_supress();
if (m_spiral_vase && m_find_replace) if (m_spiral_vase && m_find_replace)
tbb::parallel_pipeline(12, generator & spiral_vase & cooling & find_replace & output); tbb::parallel_pipeline(12, generator & spiral_vase & cooling & find_replace & output);
else if (m_spiral_vase) else if (m_spiral_vase)
@ -1636,7 +1643,7 @@ void GCode::process_layers(
tbb::parallel_pipeline(12, generator & cooling & find_replace & output); tbb::parallel_pipeline(12, generator & cooling & find_replace & output);
else else
tbb::parallel_pipeline(12, generator & cooling & output); tbb::parallel_pipeline(12, generator & cooling & output);
output_stream.set_find_replace(m_find_replace.get()); output_stream.find_replace_enable();
} }
std::string GCode::placeholder_parser_process(const std::string &name, const std::string &templ, unsigned int current_extruder_id, const DynamicConfig *config_override) std::string GCode::placeholder_parser_process(const std::string &name, const std::string &templ, unsigned int current_extruder_id, const DynamicConfig *config_override)

View File

@ -193,7 +193,9 @@ private:
// Set a find-replace post-processor to modify the G-code before GCodePostProcessor. // 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 // It is being set to null inside process_layers(), because the find-replace process
// is being called on a secondary thread to improve performance. // is being called on a secondary thread to improve performance.
void set_find_replace(GCodeFindReplace *find_replace) { m_find_replace = find_replace; } void set_find_replace(GCodeFindReplace *find_replace, bool enabled) { m_find_replace_backup = find_replace; m_find_replace = enabled ? find_replace : nullptr; }
void find_replace_enable() { m_find_replace = m_find_replace_backup; }
void find_replace_supress() { m_find_replace = nullptr; }
bool is_open() const { return f; } bool is_open() const { return f; }
bool is_error() const; bool is_error() const;
@ -217,6 +219,8 @@ private:
FILE *f { nullptr }; FILE *f { nullptr };
// Find-replace post-processor to be called before GCodePostProcessor. // Find-replace post-processor to be called before GCodePostProcessor.
GCodeFindReplace *m_find_replace { nullptr }; GCodeFindReplace *m_find_replace { nullptr };
// If suppressed, the backoup holds m_find_replace.
GCodeFindReplace *m_find_replace_backup { nullptr };
GCodeProcessor &m_processor; GCodeProcessor &m_processor;
}; };
void _do_export(Print &print, GCodeOutputStream &file, ThumbnailsGeneratorCallback thumbnail_cb); void _do_export(Print &print, GCodeOutputStream &file, ThumbnailsGeneratorCallback thumbnail_cb);