From 8aefe3fc90687aaa96a0f0fce59d073ad334eb75 Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Mon, 21 Feb 2022 11:04:43 +0100 Subject: [PATCH] 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. --- src/libslic3r/GCode.cpp | 17 ++++++++++++----- src/libslic3r/GCode.hpp | 6 +++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 5fac4b822..28d28f71d 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -1101,7 +1101,7 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato if (! print.config().gcode_substitutions.values.empty()) { m_find_replace = make_unique(print.config()); - file.set_find_replace(m_find_replace.get()); + file.set_find_replace(m_find_replace.get(), false); } // resets analyzer's tracking data @@ -1202,6 +1202,9 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato } 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 if (print.config().remaining_times.value) 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(";%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. // 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. - output_stream.set_find_replace(nullptr); + output_stream.find_replace_supress(); if (m_spiral_vase && m_find_replace) tbb::parallel_pipeline(12, generator & spiral_vase & cooling & find_replace & output); else if (m_spiral_vase) @@ -1583,7 +1590,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()); + output_stream.find_replace_enable(); } // 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. - output_stream.set_find_replace(nullptr); + output_stream.find_replace_supress(); if (m_spiral_vase && m_find_replace) tbb::parallel_pipeline(12, generator & spiral_vase & cooling & find_replace & output); else if (m_spiral_vase) @@ -1636,7 +1643,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()); + 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) diff --git a/src/libslic3r/GCode.hpp b/src/libslic3r/GCode.hpp index f46558c35..e37b47d81 100644 --- a/src/libslic3r/GCode.hpp +++ b/src/libslic3r/GCode.hpp @@ -193,7 +193,9 @@ private: // 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; } + 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_error() const; @@ -217,6 +219,8 @@ private: FILE *f { nullptr }; // Find-replace post-processor to be called before GCodePostProcessor. GCodeFindReplace *m_find_replace { nullptr }; + // If suppressed, the backoup holds m_find_replace. + GCodeFindReplace *m_find_replace_backup { nullptr }; GCodeProcessor &m_processor; }; void _do_export(Print &print, GCodeOutputStream &file, ThumbnailsGeneratorCallback thumbnail_cb);