Browse Source

Fix finding layer numbers

That seems to have been unreliable. Luckily the original g-code is already broken up in layers, so searching it is completely unnecessary.

Hopefully resolves issue #5630.
Ghostkeeper 5 years ago
parent
commit
c9e7f3a245
1 changed files with 9 additions and 29 deletions
  1. 9 29
      plugins/PostProcessingPlugin/scripts/FilamentChange.py

+ 9 - 29
plugins/PostProcessingPlugin/scripts/FilamentChange.py

@@ -1,9 +1,7 @@
 # Copyright (c) 2019 Ultimaker B.V.
 # The PostProcessingPlugin is released under the terms of the AGPLv3 or higher.
 
-from typing import Optional, Tuple
-
-from UM.Logger import Logger
+from typing import List
 from ..Script import Script
 
 class FilamentChange(Script):
@@ -65,9 +63,10 @@ class FilamentChange(Script):
             }
         }"""
 
-    def execute(self, data: list):
-
-        """data is a list. Each index contains a layer"""
+    ##  Inserts the filament change g-code at specific layer numbers.
+    #   \param data A list of layers of g-code.
+    #   \return A similar list, with filament change commands inserted.
+    def execute(self, data: List[str]):
         layer_nums = self.getSettingValueByKey("layer_number")
         initial_retract = self.getSettingValueByKey("initial_retract")
         later_retract = self.getSettingValueByKey("later_retract")
@@ -88,32 +87,13 @@ class FilamentChange(Script):
         if y_pos is not None:
             color_change = color_change + (" Y%.2f" % y_pos)
 
-        color_change = color_change + " ; Generated by FilamentChange plugin"
+        color_change = color_change + " ; Generated by FilamentChange plugin\n"
 
         layer_targets = layer_nums.split(",")
         if len(layer_targets) > 0:
             for layer_num in layer_targets:
-                layer_num = int(layer_num.strip())
+                layer_num = int(layer_num.strip()) + 1
                 if layer_num <= len(data):
-                    index, layer_data = self._searchLayerData(data, layer_num - 1)
-                    if layer_data is None:
-                        Logger.log("e", "Could not find the layer {layer_num}".format(layer_num = layer_num))
-                        continue
-                    lines = layer_data.split("\n")
-                    lines.insert(2, color_change)
-                    final_line = "\n".join(lines)
-                    data[index] = final_line
-
-        return data
+                    data[layer_num] = color_change + data[layer_num]
 
-    ##  This method returns the data corresponding with the indicated layer number, looking in the gcode for
-    #   the occurrence of this layer number.
-    def _searchLayerData(self, data: list, layer_num: int) -> Tuple[int, Optional[str]]:
-        for index, layer_data in enumerate(data):
-            first_line = layer_data.split("\n")[0]
-            # The first line should contain the layer number at the beginning.
-            if first_line[:len(self._layer_keyword)] == self._layer_keyword:
-                # If found the layer that we are looking for, then return the data
-                if first_line[len(self._layer_keyword):] == str(layer_num):
-                    return index, layer_data
-        return 0, None
+        return data