From 036516b8f9d18a94d20c0e366a9bf7b4d049f023 Mon Sep 17 00:00:00 2001 From: "c.lamboo" Date: Thu, 16 Nov 2023 13:53:43 +0100 Subject: [PATCH 1/3] Re-add support for post slice data variables in start/end gcode With previous implementation we lost support for these variables in start/end gcode "filament_cost", "print_time", "filament_amount", "filament_weight", "jobname" CURA-11347 --- plugins/CuraEngineBackend/StartSliceJob.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/plugins/CuraEngineBackend/StartSliceJob.py b/plugins/CuraEngineBackend/StartSliceJob.py index 2887743b4f..37a2116f5d 100644 --- a/plugins/CuraEngineBackend/StartSliceJob.py +++ b/plugins/CuraEngineBackend/StartSliceJob.py @@ -69,6 +69,14 @@ class GcodeStartEndFormatter(Formatter): self._additional_per_extruder_settings: Optional[Dict[str, Dict[str, any]]] = additional_per_extruder_settings def get_value(self, expression: str, args: [str], kwargs: dict) -> str: + + # The following variables are not settings, but only become available after slicing. + # when these variables are encountered, we return them as-is. They are replaced later + # when the actual values are known. + post_slice_data_variables = ["filament_cost", "print_time", "filament_amount", "filament_weight", "jobname"] + if expression in post_slice_data_variables: + return f"{{{expression}}}" + extruder_nr = self._default_extruder_nr # The settings may specify a specific extruder to use. This is done by @@ -102,6 +110,9 @@ class GcodeStartEndFormatter(Formatter): setting_function = SettingFunction(expression) value = setting_function(container_stack, additional_variables=additional_variables) + print("value", value) + print("expression", expression) + return value From 36f2deea1dacb8f68b2685a98b2e2e5277010814 Mon Sep 17 00:00:00 2001 From: "c.lamboo" Date: Thu, 16 Nov 2023 14:45:46 +0100 Subject: [PATCH 2/3] Fix some more parsing issues Example of issues we had is that parsing floating point numbers would parse the dot as an attribute syntax and trying to retrieve both sides of the attributes to the get_value resolve. E.g `3.14` would be interpreted as getting the `14` property of the object `3`, which ofcourse throws an error. CURA-11347 --- plugins/CuraEngineBackend/StartSliceJob.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/plugins/CuraEngineBackend/StartSliceJob.py b/plugins/CuraEngineBackend/StartSliceJob.py index 37a2116f5d..a0a16e7d26 100644 --- a/plugins/CuraEngineBackend/StartSliceJob.py +++ b/plugins/CuraEngineBackend/StartSliceJob.py @@ -6,7 +6,7 @@ import numpy from string import Formatter from enum import IntEnum import time -from typing import Any, cast, Dict, List, Optional, Set +from typing import Any, cast, Dict, List, Optional, Set, Tuple import re import pyArcus as Arcus # For typing. from PyQt6.QtCore import QCoreApplication @@ -68,6 +68,14 @@ class GcodeStartEndFormatter(Formatter): self._default_extruder_nr: int = default_extruder_nr self._additional_per_extruder_settings: Optional[Dict[str, Dict[str, any]]] = additional_per_extruder_settings + def get_field(self, field_name, args: [str], kwargs: dict) -> Tuple[str, str]: + # get_field method parses all fields in the format-string and parses them individually to the get_value method. + # e.g. for a string "Hello {foo.bar}" would the complete field "foo.bar" would be passed to get_field, and then + # the individual parts "foo" and "bar" would be passed to get_value. This poses a problem for us, because want + # to parse the entire field as a single expression. To solve this, we override the get_field method and return + # the entire field as the expression. + return self.get_value(field_name, args, kwargs), field_name + def get_value(self, expression: str, args: [str], kwargs: dict) -> str: # The following variables are not settings, but only become available after slicing. From afe3f54167aa856cb96eef5d8739580c3f96ce03 Mon Sep 17 00:00:00 2001 From: Casper Lamboo Date: Fri, 17 Nov 2023 11:00:50 +0100 Subject: [PATCH 3/3] Remove debug statements Co-authored-by: Jaime van Kessel --- plugins/CuraEngineBackend/StartSliceJob.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/CuraEngineBackend/StartSliceJob.py b/plugins/CuraEngineBackend/StartSliceJob.py index a0a16e7d26..a12e9e655d 100644 --- a/plugins/CuraEngineBackend/StartSliceJob.py +++ b/plugins/CuraEngineBackend/StartSliceJob.py @@ -118,8 +118,6 @@ class GcodeStartEndFormatter(Formatter): setting_function = SettingFunction(expression) value = setting_function(container_stack, additional_variables=additional_variables) - print("value", value) - print("expression", expression) return value