PauseAtHeight.py 17 KB

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