Welcome! Log In Create A New Profile


Adding a "custom preset" and accessing it via a "custom GCode placeholder"

Posted by ShaharB 
Adding a "custom preset" and accessing it via a "custom GCode placeholder"
June 23, 2019 12:16PM

Is there a way to:
1. Add a new parameter (aka "preset") - (Maybe via the PresetEditor.pm? i.e. I prefer using Perl if possible to avoid rebuild any Slic3r binaries)
2. Access the value of this new preset from a Custom GCode placeholder?

For example,
Let's say it I want a completely new 'int' value (e.g.: "special_delay") to be set and configurable by the user, like any other Slic3r setting.
And I also want this "special_delay" to be accessible via the Custom GCode module.

I should be able to write a Custom GCode line like this:
G4 P{[special_delay] + 20} ; do special delay

To hopefully make my example a little more realistic:

I am using multiple extruders, and I need my Hardware to make a small delay on every "Tool Change".
Tool Change events are supported by Custom G-code hook, so I decided it would make sense
to implement my delay via the Custom G-code mechanism, like that:

To here:
Printer Settings -> Custom G-code -> Tool change G-code

Add this line:
G4 P1000 ; wait 1000 ms here

Well, so far so good: The G4 P1000 line will be injected into the generated GCode whenever a tool-change occurs.

But now I wish to make this delay a parameter (aka "preset", I'll name it 'special_delay' as before)
Allowing the user to normally change its value via the Print Settings section, and save this value.
(Just like he/she would change and save the Print's "Layer Height" in mm, for instance)
And, this new 'special_delay' parameter should also be visible to the Custom G-code parser.

And I'd expect the "Custom Tool change" line I'll write should become something like this:
G4 P{[special_delay]} ; do special delay according to user 'special_delay'

The Custom G-code parser is expected to replace the placeholder [special_delay] with the actual value set by the user.
So, assuming the user changed the value to 1500, the injected G-code line would be:
G4 P1500 ; do special delay according to user 'special_delay'

Hope this edit makes my question clearer... (-:

Or, maybe, this is the wrong approach, and I should prefer the GCode post-processing for this task??

Many Thanks!!

Edited 2 time(s). Last edit at 06/25/2019 02:15AM by ShaharB.
Re: Adding a "custom preset" and accessing it via a "custom GCode placeholder"
July 28, 2019 08:44PM
I'm using a post-processing script to do something similar, but the user has to set the value at the top of the custom g-code. For example...

;define placeholder values here

;use a placeholder anywhere after its definition
G4 P[custom_special_delay]

Post-processing (JavaScript using the Windows Script Host):
@if (@CodeSection == @Batch) @then
  @cscript //Nologo //E:jscript "%~f0" %* & goto :eof



//get the contents of the file
var filename = WScript.Arguments(0),
	fso = new ActiveXObject("Scripting.FileSystemObject"),
	file = fso.GetFile(filename),
	ts = file.OpenAsTextStream(FOR_READING),
	input = ts.ReadAll();

ts = file.OpenAsTextStream(FOR_WRITING);

var rxp = /;\s*>>[(custom_\w+)\]\s*=\s*([a-z0-9]+(?:\s+[a-z0-9]+)*)\s*(?=;|\r?\n|$)/i,

//overwrite the file as placeholders are replaced
while( (match = rxp.exec(input)) ){
	//WScript.Echo("Definition found:", match[1], match[2]);
	//write/append the text before-and-including the definition
	ts.Write(input.slice(0, match.index + match[0].length));
	//truncate the input, and replace the placeholder throughout the remainder
	input = input.slice(match.index + match[0].length).replace(new RegExp("\["+match[1]+"\\]", "ig"), match[2]);

//write/append anything left from the input
Re: Adding a "custom preset" and accessing it via a "custom GCode placeholder"
August 05, 2019 06:49AM
Many thanks Wizard04 for your detailed answer.

I guess doing it via Post Processing is the fallback I am finally going to find myself implementing.

I am building a customized printer for a customer and preferred to make it as user friendly as possible and as less error-prone as possible.
The most straightforward way I could think of was adding a parameter ("preset") to Slic3r.
But apparently, as implied from the lack of replies, it is not such a straightforward way after all...

I'll probably end up creating some simple GUI (just for entering and validating parameter values) behind post processing.

I'll update.
Sorry, only registered users may post in this forum.

Click here to login