Skip to content

Commit

Permalink
Allow specifying the extruder stack to use for each replacement pattern
Browse files Browse the repository at this point in the history
The replacement pattern is (optionally) extended with an extruder_nr: {setting_key,extruder_nr}. The extruder_nr can either be a number (-1 for the global stack, 0 or higher for an extruder), or a setting keyword such as support_extruder_nr etc.

Contributes to #1296
  • Loading branch information
fieldOfView committed Dec 31, 2017
1 parent 80ba5fc commit 3cefe67
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions plugins/CuraEngineBackend/StartSliceJob.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,27 @@ class GcodeStartEndFormatter(Formatter):
def get_value(self, key, args, kwargs): # [CodeStyle: get_value is an overridden function from the Formatter class]
if isinstance(key, str):
try:
return kwargs[key]
extruder_nr = kwargs["default_extruder_nr"]
except ValueError:
extruder_nr = -1

key_fragments = [fragment.strip() for fragment in key.split(',')]
if len(key_fragments) == 2:
try:
extruder_nr = int(key_fragments[1])
except ValueError:
try:
extruder_nr = int(kwargs["-1"][key_fragments[1]])
except (KeyError, ValueError):
# either the key does not exist, or the value is not an int
Logger.log("w", "Unable to determine stack nr '%s' for key '%s' in start/end gcode, using global stack", key_fragments[1], key_fragments[0])
elif len(key_fragments) != 1:
Logger.log("w", "Incorrectly formatted placeholder '%s' in start/end gcode", key)
return "{" + str(key) + "}"

key = key_fragments[0]
try:
return kwargs[str(extruder_nr)][key]
except KeyError:
Logger.log("w", "Unable to replace '%s' placeholder in start/end gcode", key)
return "{" + key + "}"
Expand All @@ -56,6 +76,8 @@ def __init__(self, slice_message):
self._slice_message = slice_message
self._is_cancelled = False

self._all_extruders_settings = None

def getSliceMessage(self):
return self._slice_message

Expand Down Expand Up @@ -237,12 +259,22 @@ def _buildReplacementTokens(self, stack) -> dict:

## Replace setting tokens in a piece of g-code.
# \param value A piece of g-code to replace tokens in.
# \param settings A dictionary of tokens to replace and their respective
# replacement strings.
def _expandGcodeTokens(self, value: str, settings: dict):
# \param default_extruder_nr Stack nr to use when no stack nr is specified, defaults to the
# global stack
def _expandGcodeTokens(self, value: str, default_extruder_nr: int = -1):
if not self._all_extruders_settings:
global_stack = Application.getInstance().getGlobalContainerStack()
self._all_extruders_settings = {}
self._all_extruders_settings["-1"] = self._buildReplacementTokens(global_stack)
for extruder_stack in ExtruderManager.getInstance().getMachineExtruders(global_stack.getId()):
extruder_nr = extruder_stack.getProperty("extruder_nr", "value")
self._all_extruders_settings[str(extruder_nr)] = self._buildReplacementTokens(extruder_stack)
print("!!!!!", self._all_extruders_settings)
try:
# any setting can be used as a token
fmt = GcodeStartEndFormatter()
settings = self._all_extruders_settings.copy()
settings["default_extruder_nr"] = default_extruder_nr
return str(fmt.format(value, **settings))
except:
Logger.logException("w", "Unable to do token replacement on start/end gcode")
Expand All @@ -259,8 +291,9 @@ def _buildExtruderMessage(self, stack):
settings["material_guid"] = stack.material.getMetaDataEntry("GUID", "")

# Replace the setting tokens in start and end g-code.
settings["machine_extruder_start_code"] = self._expandGcodeTokens(settings["machine_extruder_start_code"], settings)
settings["machine_extruder_end_code"] = self._expandGcodeTokens(settings["machine_extruder_end_code"], settings)
extruder_nr = stack.getProperty("extruder_nr", "value")
settings["machine_extruder_start_code"] = self._expandGcodeTokens(settings["machine_extruder_start_code"], extruder_nr)
settings["machine_extruder_end_code"] = self._expandGcodeTokens(settings["machine_extruder_end_code"], extruder_nr)

for key, value in settings.items():
# Do not send settings that are not settable_per_extruder.
Expand All @@ -286,12 +319,12 @@ def _buildGlobalSettingsMessage(self, stack):
settings["material_print_temp_prepend"] = all(("{" + setting + "}" not in start_gcode for setting in print_temperature_settings))

# Find the correct temperatures from the first used extruder
extruder_stack = Application.getInstance().getExtruderManager().getUsedExtruderStacks()[0]
extruder_0_settings = self._buildReplacementTokens(extruder_stack)
initial_extruder_stack = Application.getInstance().getExtruderManager().getUsedExtruderStacks()[0]
initial_extruder_nr = initial_extruder_stack.getProperty("extruder_nr", "value")

# Replace the setting tokens in start and end g-code.
settings["machine_start_gcode"] = self._expandGcodeTokens(settings["machine_start_gcode"], extruder_0_settings)
settings["machine_end_gcode"] = self._expandGcodeTokens(settings["machine_end_gcode"], extruder_0_settings)
settings["machine_start_gcode"] = self._expandGcodeTokens(settings["machine_start_gcode"], initial_extruder_nr)
settings["machine_end_gcode"] = self._expandGcodeTokens(settings["machine_end_gcode"], initial_extruder_nr)

# Add all sub-messages for each individual setting.
for key, value in settings.items():
Expand Down

0 comments on commit 3cefe67

Please sign in to comment.