PauseAtHeight.py 16 KB

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