FilamentChange.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # The PostProcessingPlugin is released under the terms of the AGPLv3 or higher.
  3. from typing import List
  4. from ..Script import Script
  5. class FilamentChange(Script):
  6. _layer_keyword = ";LAYER:"
  7. def __init__(self):
  8. super().__init__()
  9. def getSettingDataString(self):
  10. return """{
  11. "name":"Filament Change",
  12. "key": "FilamentChange",
  13. "metadata": {},
  14. "version": 2,
  15. "settings":
  16. {
  17. "layer_number":
  18. {
  19. "label": "Layer",
  20. "description": "At what layer should color change occur. This will be before the layer starts printing. Specify multiple color changes with a comma.",
  21. "unit": "",
  22. "type": "str",
  23. "default_value": "1"
  24. },
  25. "initial_retract":
  26. {
  27. "label": "Initial Retraction",
  28. "description": "Initial filament retraction distance. The filament will be retracted with this amount before moving the nozzle away from the ongoing print.",
  29. "unit": "mm",
  30. "type": "float",
  31. "default_value": 30.0
  32. },
  33. "later_retract":
  34. {
  35. "label": "Later Retraction Distance",
  36. "description": "Later filament retraction distance for removal. The filament will be retracted all the way out of the printer so that you can change the filament.",
  37. "unit": "mm",
  38. "type": "float",
  39. "default_value": 300.0
  40. },
  41. "x_position":
  42. {
  43. "label": "X Position",
  44. "description": "Extruder X position. The print head will move here for filament change.",
  45. "unit": "mm",
  46. "type": "float",
  47. "default_value": 0
  48. },
  49. "y_position":
  50. {
  51. "label": "Y Position",
  52. "description": "Extruder Y position. The print head will move here for filament change.",
  53. "unit": "mm",
  54. "type": "float",
  55. "default_value": 0
  56. }
  57. }
  58. }"""
  59. def execute(self, data: List[str]):
  60. """Inserts the filament change g-code at specific layer numbers.
  61. :param data: A list of layers of g-code.
  62. :return: A similar list, with filament change commands inserted.
  63. """
  64. layer_nums = self.getSettingValueByKey("layer_number")
  65. initial_retract = self.getSettingValueByKey("initial_retract")
  66. later_retract = self.getSettingValueByKey("later_retract")
  67. x_pos = self.getSettingValueByKey("x_position")
  68. y_pos = self.getSettingValueByKey("y_position")
  69. color_change = "M600"
  70. if initial_retract is not None and initial_retract > 0.:
  71. color_change = color_change + (" E%.2f" % initial_retract)
  72. if later_retract is not None and later_retract > 0.:
  73. color_change = color_change + (" L%.2f" % later_retract)
  74. if x_pos is not None:
  75. color_change = color_change + (" X%.2f" % x_pos)
  76. if y_pos is not None:
  77. color_change = color_change + (" Y%.2f" % y_pos)
  78. color_change = color_change + " ; Generated by FilamentChange plugin\n"
  79. layer_targets = layer_nums.split(",")
  80. if len(layer_targets) > 0:
  81. for layer_num in layer_targets:
  82. try:
  83. layer_num = int(layer_num.strip()) + 1 #Needs +1 because the 1st layer is reserved for start g-code.
  84. except ValueError: #Layer number is not an integer.
  85. continue
  86. if 0 < layer_num < len(data):
  87. data[layer_num] = color_change + data[layer_num]
  88. return data