English
Login

G-code substitutions

Relevant for

MK4 family
XL family
MINI family
MK3 family
MK3.5 family
+
5 comments
Article is also available in following languages
English
Čeština
Polski
Deutsch
Français
Español
Italiano
日本語
This feature will be available in PrusaSlicer 2.4.1

PrusaSlicer offers simple G-Code post-processing by specifying find and replace pairs, with optional regex matching, case sensitivity and whole word matching, similar to what common text editors offer. Regular expressions are very powerful and versatile tool that allows to set up even quite complicated substitutions. If you need more complicated post-processing, it is possible to use an external post-processing script instead.

The feature accessible from Print Settings -> Output options.

The G-Code is processed before it is previewed, so your changes will be visible in the G-Code preview. It may be helpful to turn on 'Verbose G-Code' to make PrusaSlicer generate extra annotations, which you can then match with a regex.

Regular expression syntax

Regular expression syntax of Perl is supported as described in boost Perl regular expression syntax, where a "single line" modifier mimics the Perl's s/ modifier. With regular expressions active, the "Replace with" pattern supports the "extended" syntax as described in boost extended format syntax. Lookahead and lookbehind are supported by the regular expression engine, but only for fixed length patterns. With regular expression disabled, n, r, t, and placeholders are supported in both "Find" and "Replace with" patterns similarly to Notepad++'s "extended" syntax.

The G-code find/replace post-processor processes G-code in blocks, where a single G-code block is generated for each layer starting with the Z up to move to the next layer. Multiple find/replace patterns are applied one by one to one G-code block before processing the next G-code block. Matching multi-line patterns is possible, however only inside one G-code block, most likely inside a single layer. Regular expressions are powerful but tricky, thus we recommend the regular expressions 101 online playground to get familiar with regular expressions and to test and fine tune the regular expressions modifying G-code before entering them into PrusaSlicer. Don't forget to switch the regex flavor to ECMAScript in the regular expressions 101 online playground.

 

Example

An example of a quite powerful G-code modification by a reasonably complex regular expression: Increase the extrusion rate of top solid infill from the default 95 percent to 98 percent.
Find: (;TYPE:Top solid infilln)(.*?)(;TYPE:|$)(?!Top solid infill)
Replace with: ${1}M221 S98n${2}M221 S95n${3}
Modifiers: regexp, single line

 

5 comments

Log in  to post a comment
Infinitymodular
I find this works with the example above to change the extrusion_multiplier for the top layer infill... I am trying to use the [extrusion_multiplier] placeholder value in the <replace> section, in order to offset the flow rate from the configured extrusion multiplier. Is this possible? It just seems to take the exact text in the replace field and replace with that. No placeholder injection. 
Scott Gartner
In PrusaSlicer 2.7.1 I can't get this feature to work at all.  I tried with and without regular expressions, I tried with and without comments, I get the following line output in the gcode, but no actual replacements are made (I tried valid substitutions as well):
; gcode_substitutions = M73;xxxxxx;;
This was the setting I was hoping to make work:
; gcode_substitutions = "(M73\\s+P[0-9]+\\s+R[0-9]+)\\n";"${1} ;LAYER_CHANGE_TRIGGER_CAMERA\\nG91\\nG1 E-1 F1800\\nG1 Z0.3\\nG90\\nG1 X180 Y180 F3600\\nG91\\nG1 Z-0.3\\nG1 E1 F1800\\nG90\\n";r;"Move head to trigger keeping frame"
 
xA59rhR8FF

fe60 has it correct, the Find command should be:
 
(;TYPE:Top solid infill\n)(.*?)(;TYPE:|$)(?!Top solid infill) 
 
And the replace command should be:
 
${1}M221 S98\n${2}M221 S95\n${3}
 
In my case I wanted top solid infill to be 95% and then subsequently reset to 100% to match my settings in SuperSlicer, so my replace command was as follows (leaving M221 without parameter resets it.)
 
${1}M221 S95\n${2}M221\n${3}
fe60
There are more backslashes missing in the last sentence of the first paragraph about the "Regular expression syntax".
fe60
Regarding Example: The backslashes of the newlines are not visible (1x in "Find" and 2x in "Replace with"). Hence the given example doesn't work.