FilamentChange.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. ## Inserts the filament change g-code at specific layer numbers.
  60. # \param data A list of layers of g-code.
  61. # \return A similar list, with filament change commands inserted.
  62. def execute(self, data: List[str]):
  63. layer_nums = self.getSettingValueByKey("layer_number")
  64. initial_retract = self.getSettingValueByKey("initial_retract")
  65. later_retract = self.getSettingValueByKey("later_retract")
  66. x_pos = self.getSettingValueByKey("x_position")
  67. y_pos = self.getSettingValueByKey("y_position")
  68. color_change = "M600"
  69. if initial_retract is not None and initial_retract > 0.:
  70. color_change = color_change + (" E%.2f" % initial_retract)
  71. if later_retract is not None and later_retract > 0.:
  72. color_change = color_change + (" L%.2f" % later_retract)
  73. if x_pos is not None:
  74. color_change = color_change + (" X%.2f" % x_pos)
  75. if y_pos is not None:
  76. color_change = color_change + (" Y%.2f" % y_pos)
  77. color_change = color_change + " ; Generated by FilamentChange plugin\n"
  78. layer_targets = layer_nums.split(",")
  79. if len(layer_targets) > 0:
  80. for layer_num in layer_targets:
  81. try:
  82. layer_num = int(layer_num.strip()) + 1 #Needs +1 because the 1st layer is reserved for start g-code.
  83. except ValueError: #Layer number is not an integer.
  84. continue
  85. if 0 < layer_num < len(data):
  86. data[layer_num] = color_change + data[layer_num]
  87. return data