FilamentChange.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # The PostProcessingPlugin is released under the terms of the AGPLv3 or higher.
  3. # Modification 06.09.2020
  4. # add checkbox, now you can choose and use configuration from the firmware itself.
  5. from typing import List
  6. from ..Script import Script
  7. class FilamentChange(Script):
  8. _layer_keyword = ";LAYER:"
  9. def __init__(self):
  10. super().__init__()
  11. def getSettingDataString(self):
  12. return """{
  13. "name": "Filament Change",
  14. "key": "FilamentChange",
  15. "metadata": {},
  16. "version": 2,
  17. "settings":
  18. {
  19. "layer_number":
  20. {
  21. "label": "Layer",
  22. "description": "At what layer should color change occur. This will be before the layer starts printing. Specify multiple color changes with a comma.",
  23. "unit": "",
  24. "type": "str",
  25. "default_value": "1"
  26. },
  27. "firmware_config":
  28. {
  29. "label": "Use Firmware Configuration",
  30. "description": "Use the settings in your firmware, or customise the parameters of the filament change here.",
  31. "type": "bool",
  32. "default_value": false
  33. },
  34. "initial_retract":
  35. {
  36. "label": "Initial Retraction",
  37. "description": "Initial filament retraction distance. The filament will be retracted with this amount before moving the nozzle away from the ongoing print.",
  38. "unit": "mm",
  39. "type": "float",
  40. "default_value": 30.0,
  41. "enabled": "not firmware_config"
  42. },
  43. "later_retract":
  44. {
  45. "label": "Later Retraction Distance",
  46. "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.",
  47. "unit": "mm",
  48. "type": "float",
  49. "default_value": 300.0,
  50. "enabled": "not firmware_config"
  51. },
  52. "x_position":
  53. {
  54. "label": "X Position",
  55. "description": "Extruder X position. The print head will move here for filament change.",
  56. "unit": "mm",
  57. "type": "float",
  58. "default_value": 0,
  59. "enabled": "not firmware_config"
  60. },
  61. "y_position":
  62. {
  63. "label": "Y Position",
  64. "description": "Extruder Y position. The print head will move here for filament change.",
  65. "unit": "mm",
  66. "type": "float",
  67. "default_value": 0,
  68. "enabled": "not firmware_config"
  69. },
  70. "z_position":
  71. {
  72. "label": "Z Position (relative)",
  73. "description": "Extruder relative Z position. Move the print head up for filament change.",
  74. "unit": "mm",
  75. "type": "float",
  76. "default_value": 0,
  77. "minimum_value": 0
  78. }
  79. }
  80. }"""
  81. def execute(self, data: List[str]):
  82. """Inserts the filament change g-code at specific layer numbers.
  83. :param data: A list of layers of g-code.
  84. :return: A similar list, with filament change commands inserted.
  85. """
  86. layer_nums = self.getSettingValueByKey("layer_number")
  87. initial_retract = self.getSettingValueByKey("initial_retract")
  88. later_retract = self.getSettingValueByKey("later_retract")
  89. x_pos = self.getSettingValueByKey("x_position")
  90. y_pos = self.getSettingValueByKey("y_position")
  91. z_pos = self.getSettingValueByKey("z_position")
  92. firmware_config = self.getSettingValueByKey("firmware_config")
  93. color_change = "M600"
  94. if not firmware_config:
  95. if initial_retract is not None and initial_retract > 0.:
  96. color_change = color_change + (" E%.2f" % initial_retract)
  97. if later_retract is not None and later_retract > 0.:
  98. color_change = color_change + (" L%.2f" % later_retract)
  99. if x_pos is not None:
  100. color_change = color_change + (" X%.2f" % x_pos)
  101. if y_pos is not None:
  102. color_change = color_change + (" Y%.2f" % y_pos)
  103. if z_pos is not None and z_pos > 0.:
  104. color_change = color_change + (" Z%.2f" % z_pos)
  105. color_change = color_change + " ; Generated by FilamentChange plugin\n"
  106. layer_targets = layer_nums.split(",")
  107. if len(layer_targets) > 0:
  108. for layer_num in layer_targets:
  109. try:
  110. layer_num = int(layer_num.strip()) + 1 #Needs +1 because the 1st layer is reserved for start g-code.
  111. except ValueError: #Layer number is not an integer.
  112. continue
  113. if 0 < layer_num < len(data):
  114. data[layer_num] = color_change + data[layer_num]
  115. return data