PauseAtHeight.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from ..Script import Script
  4. from UM.Application import Application #To get the current printer's settings.
  5. class PauseAtHeight(Script):
  6. def __init__(self):
  7. super().__init__()
  8. def getSettingDataString(self):
  9. return """{
  10. "name": "Pause at height",
  11. "key": "PauseAtHeight",
  12. "metadata": {},
  13. "version": 2,
  14. "settings":
  15. {
  16. "pause_at":
  17. {
  18. "label": "Pause at",
  19. "description": "Whether to pause at a certain height or at a certain layer.",
  20. "type": "enum",
  21. "options": {"height": "Height", "layer_no": "Layer No."},
  22. "default_value": "height"
  23. },
  24. "pause_height":
  25. {
  26. "label": "Pause Height",
  27. "description": "At what height should the pause occur?",
  28. "unit": "mm",
  29. "type": "float",
  30. "default_value": 5.0,
  31. "minimum_value": "0",
  32. "minimum_value_warning": "0.27",
  33. "enabled": "pause_at == 'height'"
  34. },
  35. "pause_layer":
  36. {
  37. "label": "Pause Layer",
  38. "description": "At what layer should the pause occur?",
  39. "type": "int",
  40. "value": "math.floor((pause_height - 0.27) / 0.1) + 1",
  41. "minimum_value": "0",
  42. "minimum_value_warning": "1",
  43. "enabled": "pause_at == 'layer_no'"
  44. },
  45. "head_park_x":
  46. {
  47. "label": "Park Print Head X",
  48. "description": "What X location does the head move to when pausing.",
  49. "unit": "mm",
  50. "type": "float",
  51. "default_value": 190
  52. },
  53. "head_park_y":
  54. {
  55. "label": "Park Print Head Y",
  56. "description": "What Y location does the head move to when pausing.",
  57. "unit": "mm",
  58. "type": "float",
  59. "default_value": 190
  60. },
  61. "retraction_amount":
  62. {
  63. "label": "Retraction",
  64. "description": "How much filament must be retracted at pause.",
  65. "unit": "mm",
  66. "type": "float",
  67. "default_value": 0
  68. },
  69. "retraction_speed":
  70. {
  71. "label": "Retraction Speed",
  72. "description": "How fast to retract the filament.",
  73. "unit": "mm/s",
  74. "type": "float",
  75. "default_value": 25
  76. },
  77. "extrude_amount":
  78. {
  79. "label": "Extrude Amount",
  80. "description": "How much filament should be extruded after pause. This is needed when doing a material change on Ultimaker2's to compensate for the retraction after the change. In that case 128+ is recommended.",
  81. "unit": "mm",
  82. "type": "float",
  83. "default_value": 0
  84. },
  85. "extrude_speed":
  86. {
  87. "label": "Extrude Speed",
  88. "description": "How fast to extrude the material after pause.",
  89. "unit": "mm/s",
  90. "type": "float",
  91. "default_value": 3.3333
  92. },
  93. "redo_layers":
  94. {
  95. "label": "Redo Layers",
  96. "description": "Redo a number of previous layers after a pause to increases adhesion.",
  97. "unit": "layers",
  98. "type": "int",
  99. "default_value": 0
  100. },
  101. "standby_temperature":
  102. {
  103. "label": "Standby Temperature",
  104. "description": "Change the temperature during the pause",
  105. "unit": "°C",
  106. "type": "int",
  107. "default_value": 0
  108. }
  109. }
  110. }"""
  111. def getNextXY(self, layer: str):
  112. """
  113. Get the X and Y values for a layer (will be used to get X and Y of
  114. the layer after the pause
  115. """
  116. lines = layer.split("\n")
  117. for line in lines:
  118. if self.getValue(line, "X") is not None and self.getValue(line, "Y") is not None:
  119. x = self.getValue(line, "X")
  120. y = self.getValue(line, "Y")
  121. return x, y
  122. return 0, 0
  123. def execute(self, data: list):
  124. """data is a list. Each index contains a layer"""
  125. pause_at = self.getSettingValueByKey("pause_at")
  126. pause_height = self.getSettingValueByKey("pause_height")
  127. pause_layer = self.getSettingValueByKey("pause_layer")
  128. retraction_amount = self.getSettingValueByKey("retraction_amount")
  129. retraction_speed = self.getSettingValueByKey("retraction_speed")
  130. extrude_amount = self.getSettingValueByKey("extrude_amount")
  131. extrude_speed = self.getSettingValueByKey("extrude_speed")
  132. park_x = self.getSettingValueByKey("head_park_x")
  133. park_y = self.getSettingValueByKey("head_park_y")
  134. layers_started = False
  135. redo_layers = self.getSettingValueByKey("redo_layers")
  136. standby_temperature = self.getSettingValueByKey("standby_temperature")
  137. firmware_retract = Application.getInstance().getGlobalContainerStack().getProperty("machine_firmware_retract", "value")
  138. control_temperatures = Application.getInstance().getGlobalContainerStack().getProperty("machine_nozzle_temp_enabled", "value")
  139. initial_layer_height = Application.getInstance().getGlobalContainerStack().getProperty("layer_height_0", "value")
  140. is_griffin = False
  141. # T = ExtruderManager.getInstance().getActiveExtruderStack().getProperty("material_print_temperature", "value")
  142. # use offset to calculate the current height: <current_height> = <current_z> - <layer_0_z>
  143. layer_0_z = 0
  144. current_z = 0
  145. got_first_g_cmd_on_layer_0 = False
  146. current_t = 0 #Tracks the current extruder for tracking the target temperature.
  147. target_temperature = {} #Tracks the current target temperature for each extruder.
  148. nbr_negative_layers = 0
  149. for index, layer in enumerate(data):
  150. lines = layer.split("\n")
  151. # Scroll each line of instruction for each layer in the G-code
  152. for line in lines:
  153. if ";FLAVOR:Griffin" in line:
  154. is_griffin = True
  155. # Fist positive layer reached
  156. if ";LAYER:0" in line:
  157. layers_started = True
  158. # Count nbr of negative layers (raft)
  159. elif ";LAYER:-" in line:
  160. nbr_negative_layers += 1
  161. #Track the latest printing temperature in order to resume at the correct temperature.
  162. if line.startswith("T"):
  163. current_t = self.getValue(line, "T")
  164. m = self.getValue(line, "M")
  165. if m is not None and (m == 104 or m == 109) and self.getValue(line, "S") is not None:
  166. extruder = current_t
  167. if self.getValue(line, "T") is not None:
  168. extruder = self.getValue(line, "T")
  169. target_temperature[extruder] = self.getValue(line, "S")
  170. if not layers_started:
  171. continue
  172. # If a Z instruction is in the line, read the current Z
  173. if self.getValue(line, "Z") is not None:
  174. current_z = self.getValue(line, "Z")
  175. if pause_at == "height":
  176. # Ignore if the line is not G1 or G0
  177. if self.getValue(line, "G") != 1 and self.getValue(line, "G") != 0:
  178. continue
  179. # This block is executed once, the first time there is a G
  180. # command, to get the z offset (z for first positive layer)
  181. if not got_first_g_cmd_on_layer_0:
  182. layer_0_z = current_z - initial_layer_height
  183. got_first_g_cmd_on_layer_0 = True
  184. current_height = current_z - layer_0_z
  185. if current_height < pause_height:
  186. break # Try the next layer.
  187. # Pause at layer
  188. else:
  189. if not line.startswith(";LAYER:"):
  190. continue
  191. current_layer = line[len(";LAYER:"):]
  192. try:
  193. current_layer = int(current_layer)
  194. # Couldn't cast to int. Something is wrong with this
  195. # g-code data
  196. except ValueError:
  197. continue
  198. if current_layer < pause_layer - nbr_negative_layers:
  199. continue
  200. # Get X and Y from the next layer (better position for
  201. # the nozzle)
  202. next_layer = data[index + 1]
  203. x, y = self.getNextXY(next_layer)
  204. prev_layer = data[index - 1]
  205. prev_lines = prev_layer.split("\n")
  206. current_e = 0.
  207. # Access last layer, browse it backwards to find
  208. # last extruder absolute position
  209. for prevLine in reversed(prev_lines):
  210. current_e = self.getValue(prevLine, "E", -1)
  211. if current_e >= 0:
  212. break
  213. # include a number of previous layers
  214. for i in range(1, redo_layers + 1):
  215. prev_layer = data[index - i]
  216. layer = prev_layer + layer
  217. # Get extruder's absolute position at the
  218. # beginning of the first layer redone
  219. # see https://github.com/nallath/PostProcessingPlugin/issues/55
  220. if i == redo_layers:
  221. # Get X and Y from the next layer (better position for
  222. # the nozzle)
  223. x, y = self.getNextXY(layer)
  224. prev_lines = prev_layer.split("\n")
  225. for line in prev_lines:
  226. new_e = self.getValue(line, 'E', current_e)
  227. if new_e != current_e:
  228. current_e = new_e
  229. break
  230. prepend_gcode = ";TYPE:CUSTOM\n"
  231. prepend_gcode += ";added code by post processing\n"
  232. prepend_gcode += ";script: PauseAtHeight.py\n"
  233. if pause_at == "height":
  234. prepend_gcode += ";current z: {z}\n".format(z = current_z)
  235. prepend_gcode += ";current height: {height}\n".format(height = current_height)
  236. else:
  237. prepend_gcode += ";current layer: {layer}\n".format(layer = current_layer)
  238. if not is_griffin:
  239. # Retraction
  240. prepend_gcode += self.putValue(M = 83) + "\n"
  241. if retraction_amount != 0:
  242. if firmware_retract: #Can't set the distance directly to what the user wants. We have to choose ourselves.
  243. retraction_count = 1 if control_temperatures else 3 #Retract more if we don't control the temperature.
  244. for i in range(retraction_count):
  245. prepend_gcode += self.putValue(G = 10) + "\n"
  246. else:
  247. prepend_gcode += self.putValue(G = 1, E = -retraction_amount, F = retraction_speed * 60) + "\n"
  248. # Move the head away
  249. prepend_gcode += self.putValue(G = 1, Z = current_z + 1, F = 300) + "\n"
  250. # This line should be ok
  251. prepend_gcode += self.putValue(G = 1, X = park_x, Y = park_y, F = 9000) + "\n"
  252. if current_z < 15:
  253. prepend_gcode += self.putValue(G = 1, Z = 15, F = 300) + "\n"
  254. if control_temperatures:
  255. # Set extruder standby temperature
  256. prepend_gcode += self.putValue(M = 104, S = standby_temperature) + "; standby temperature\n"
  257. # Wait till the user continues printing
  258. prepend_gcode += self.putValue(M = 0) + ";Do the actual pause\n"
  259. if not is_griffin:
  260. if control_temperatures:
  261. # Set extruder resume temperature
  262. prepend_gcode += self.putValue(M = 109, S = int(target_temperature.get(current_t, 0))) + "; resume temperature\n"
  263. # Push the filament back,
  264. if retraction_amount != 0:
  265. prepend_gcode += self.putValue(G = 1, E = retraction_amount, F = retraction_speed * 60) + "\n"
  266. # Optionally extrude material
  267. if extrude_amount != 0:
  268. prepend_gcode += self.putValue(G = 1, E = extrude_amount, F = extrude_speed * 60) + "\n"
  269. # and retract again, the properly primes the nozzle
  270. # when changing filament.
  271. if retraction_amount != 0:
  272. prepend_gcode += self.putValue(G = 1, E = -retraction_amount, F = retraction_speed * 60) + "\n"
  273. # Move the head back
  274. prepend_gcode += self.putValue(G = 1, Z = current_z + 1, F = 300) + "\n"
  275. prepend_gcode += self.putValue(G = 1, X = x, Y = y, F = 9000) + "\n"
  276. if retraction_amount != 0:
  277. if firmware_retract: #Can't set the distance directly to what the user wants. We have to choose ourselves.
  278. retraction_count = 1 if control_temperatures else 3 #Retract more if we don't control the temperature.
  279. for i in range(retraction_count):
  280. prepend_gcode += self.putValue(G = 11) + "\n"
  281. else:
  282. prepend_gcode += self.putValue(G = 1, E = retraction_amount, F = retraction_speed * 60) + "\n"
  283. prepend_gcode += self.putValue(G = 1, F = 9000) + "\n"
  284. prepend_gcode += self.putValue(M = 82) + "\n"
  285. # reset extrude value to pre pause value
  286. prepend_gcode += self.putValue(G = 92, E = current_e) + "\n"
  287. layer = prepend_gcode + layer
  288. # Override the data of this layer with the
  289. # modified data
  290. data[index] = layer
  291. return data
  292. return data