From 4a3db419ec4545aa587985a1096bf80b3f0765f7 Mon Sep 17 00:00:00 2001 From: Mark Walker Date: Mon, 16 Nov 2015 01:12:53 -0800 Subject: [PATCH] Add back placeholder expansion for start/end gcode See issue Ultimaker/CuraEngine#240 The intent of this change it to allow token replacement in start/end gcode similar to the old Cura. Tokens are of the form {material_bed_temperature} where the name is any setting name that is passed to the engine. Control the backend's generation of temperature commands via new settings material_bed_temp_prepend and material_print_temp_prepend based on whether there are temperature tokens in the printer profile's start gcode. This change will generate the same temperature gcode as FffGcodeWriter at the head, if none of the temperature tokens are present in the machine profile. Otherwise, they're suppressed via the prepend settings. Also, set fdmprinter.json so that all that inherit from it default to having the gcode temperature prepend also wait for temperature. Profiles can override these settings: material_bed_temp_wait and material_print_temp_wait This PR intends to change both Cura and CuraEngine. However, it is safe to sequence with the Cura change first which will do token replacement (if present), but will fail to supress the temperature prepends. --- .../CuraEngineBackend/CuraEngineBackend.py | 2 +- plugins/CuraEngineBackend/StartSliceJob.py | 35 +++++++++++++++++-- resources/machines/fdmprinter.json | 6 ++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/plugins/CuraEngineBackend/CuraEngineBackend.py b/plugins/CuraEngineBackend/CuraEngineBackend.py index 91d3b627ca..604d2a9ad6 100644 --- a/plugins/CuraEngineBackend/CuraEngineBackend.py +++ b/plugins/CuraEngineBackend/CuraEngineBackend.py @@ -273,4 +273,4 @@ class CuraEngineBackend(Backend): self._process.terminate() except: # terminating a process that is already terminating causes an exception, silently ignore this. pass - self.slicingCancelled.emit() \ No newline at end of file + self.slicingCancelled.emit() diff --git a/plugins/CuraEngineBackend/StartSliceJob.py b/plugins/CuraEngineBackend/StartSliceJob.py index 50c3fadb0e..059ede8a8f 100644 --- a/plugins/CuraEngineBackend/StartSliceJob.py +++ b/plugins/CuraEngineBackend/StartSliceJob.py @@ -2,6 +2,8 @@ # Cura is released under the terms of the AGPLv3 or higher. import numpy +from string import Formatter +import traceback from UM.Job import Job from UM.Application import Application @@ -14,6 +16,19 @@ from cura.OneAtATimeIterator import OneAtATimeIterator from . import Cura_pb2 +## Formatter class that handles token expansion in start/end gcod +class GcodeStartEndFormatter(Formatter): + def get_value(self, key, args, kwargs): + if isinstance(key, str): + try: + return kwargs[key] + except KeyError: + Logger.log("w", "Unable to replace '%s' placeholder in start/end gcode", key) + return "{" + key + "}" + else: + Logger.log("w", "Incorrectly formatted placeholder '%s' in start/end gcode", key) + return "{" + str(key) + "}" + ## Job class that handles sending the current scene data to CuraEngine class StartSliceJob(Job): def __init__(self, profile, socket): @@ -90,12 +105,28 @@ class StartSliceJob(Job): self.setResult(True) + def _expandGcodeTokens(self, key, value, settings): + try: + # any setting can be used as a token + fmt = GcodeStartEndFormatter() + return str(fmt.format(value, **settings)).encode("utf-8") + except: + Logger.log("w", "Unabled to do token replacement on start/end gcode %s", traceback.format_exc()) + return str(value).encode("utf-8") + def _sendSettings(self, profile): msg = Cura_pb2.SettingList() - for key, value in profile.getAllSettingValues(include_machine = True).items(): + settings = profile.getAllSettingValues(include_machine = True) + start_gcode = settings["machine_start_gcode"] + settings["material_bed_temp_prepend"] = not "{material_bed_temperature}" in start_gcode + settings["material_print_temp_prepend"] = not "{material_print_temperature" in start_gcode + for key, value in settings.items(): s = msg.settings.add() s.name = key - s.value = str(value).encode("utf-8") + if key == "machine_start_gcode" or key == "machine_end_gcode": + s.value = self._expandGcodeTokens(key, value, settings) + else: + s.value = str(value).encode("utf-8") self._socket.sendMessage(msg) diff --git a/resources/machines/fdmprinter.json b/resources/machines/fdmprinter.json index a1ed7c4ce6..d466b1d2dc 100644 --- a/resources/machines/fdmprinter.json +++ b/resources/machines/fdmprinter.json @@ -17,6 +17,12 @@ "description": "Gcode commands to be executed at the very end - separated by \\n.", "default": "M104 S0\nM140 S0\n;Retract the filament\nG92 E1\nG1 E-1 F300\nG28 X0 Y0\nM84" }, + "material_bed_temp_wait": { + "default": true + }, + "material_print_temp_wait": { + "default": true + }, "machine_width": { "description": "The width (X-direction) of the printable area.", "default": 100