PauseAtHeight.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 Number"},
  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. "pause_method":
  48. {
  49. "label": "Method",
  50. "description": "The method or gcode command to use for pausing.",
  51. "type": "enum",
  52. "options": {"marlin": "Marlin (M0)", "griffin": "Griffin (M0, firmware retract)", "bq": "BQ (M25)", "reprap": "RepRap (M226)", "repetier": "Repetier (@pause)"},
  53. "default_value": "marlin",
  54. "value": "\\\"griffin\\\" if machine_gcode_flavor==\\\"Griffin\\\" else \\\"reprap\\\" if machine_gcode_flavor==\\\"RepRap (RepRap)\\\" else \\\"repetier\\\" if machine_gcode_flavor==\\\"Repetier\\\" else \\\"bq\\\" if \\\"BQ\\\" in machine_name or \\\"Flying Bear Ghost 4S\\\" in machine_name else \\\"marlin\\\""
  55. },
  56. "disarm_timeout":
  57. {
  58. "label": "Disarm timeout",
  59. "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.",
  60. "type": "int",
  61. "value": "0",
  62. "minimum_value": "0",
  63. "minimum_value_warning": "0",
  64. "maximum_value_warning": "1800",
  65. "unit": "s"
  66. },
  67. "head_park_x":
  68. {
  69. "label": "Park Print Head X",
  70. "description": "What X location does the head move to when pausing.",
  71. "unit": "mm",
  72. "type": "float",
  73. "default_value": 190,
  74. "enabled": "pause_method != \\\"griffin\\\""
  75. },
  76. "head_park_y":
  77. {
  78. "label": "Park Print Head Y",
  79. "description": "What Y location does the head move to when pausing.",
  80. "unit": "mm",
  81. "type": "float",
  82. "default_value": 190,
  83. "enabled": "pause_method != \\\"griffin\\\""
  84. },
  85. "head_move_z":
  86. {
  87. "label": "Head move Z",
  88. "description": "The Height of Z-axis retraction before parking.",
  89. "unit": "mm",
  90. "type": "float",
  91. "default_value": 15.0,
  92. "enabled": "pause_method == \\\"repetier\\\""
  93. },
  94. "retraction_amount":
  95. {
  96. "label": "Retraction",
  97. "description": "How much filament must be retracted at pause.",
  98. "unit": "mm",
  99. "type": "float",
  100. "default_value": 0,
  101. "enabled": "pause_method != \\\"griffin\\\""
  102. },
  103. "retraction_speed":
  104. {
  105. "label": "Retraction Speed",
  106. "description": "How fast to retract the filament.",
  107. "unit": "mm/s",
  108. "type": "float",
  109. "default_value": 25,
  110. "enabled": "pause_method not in [\\\"griffin\\\", \\\"repetier\\\"]"
  111. },
  112. "extrude_amount":
  113. {
  114. "label": "Extrude Amount",
  115. "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.",
  116. "unit": "mm",
  117. "type": "float",
  118. "default_value": 0,
  119. "enabled": "pause_method != \\\"griffin\\\""
  120. },
  121. "extrude_speed":
  122. {
  123. "label": "Extrude Speed",
  124. "description": "How fast to extrude the material after pause.",
  125. "unit": "mm/s",
  126. "type": "float",
  127. "default_value": 3.3333,
  128. "enabled": "pause_method not in [\\\"griffin\\\", \\\"repetier\\\"]"
  129. },
  130. "redo_layer":
  131. {
  132. "label": "Redo Layer",
  133. "description": "Redo the last layer before the pause, to get the filament flowing again after having oozed a bit during the pause.",
  134. "type": "bool",
  135. "default_value": false
  136. },
  137. "standby_temperature":
  138. {
  139. "label": "Standby Temperature",
  140. "description": "Change the temperature during the pause.",
  141. "unit": "°C",
  142. "type": "int",
  143. "default_value": 0,
  144. "enabled": "pause_method not in [\\\"griffin\\\", \\\"repetier\\\"]"
  145. },
  146. "display_text":
  147. {
  148. "label": "Display Text",
  149. "description": "Text that should appear on the display while paused. If left empty, there will not be any message.",
  150. "type": "str",
  151. "default_value": "",
  152. "enabled": "pause_method != \\\"repetier\\\""
  153. },
  154. "machine_name":
  155. {
  156. "label": "Machine Type",
  157. "description": "The name of your 3D printer model. This setting is controlled by the script and will not be visible.",
  158. "default_value": "Unknown",
  159. "type": "str",
  160. "enabled": false
  161. },
  162. "machine_gcode_flavor":
  163. {
  164. "label": "G-code flavor",
  165. "description": "The type of g-code to be generated. This setting is controlled by the script and will not be visible.",
  166. "type": "enum",
  167. "options":
  168. {
  169. "RepRap (Marlin/Sprinter)": "Marlin",
  170. "RepRap (Volumetric)": "Marlin (Volumetric)",
  171. "RepRap (RepRap)": "RepRap",
  172. "UltiGCode": "Ultimaker 2",
  173. "Griffin": "Griffin",
  174. "Makerbot": "Makerbot",
  175. "BFB": "Bits from Bytes",
  176. "MACH3": "Mach3",
  177. "Repetier": "Repetier"
  178. },
  179. "default_value": "RepRap (Marlin/Sprinter)",
  180. "enabled": false
  181. }
  182. }
  183. }"""
  184. ## Copy machine name and gcode flavor from global stack so we can use their value in the script stack
  185. def initialize(self) -> None:
  186. super().initialize()
  187. global_container_stack = Application.getInstance().getGlobalContainerStack()
  188. if global_container_stack is None or self._instance is None:
  189. return
  190. for key in ["machine_name", "machine_gcode_flavor"]:
  191. self._instance.setProperty(key, "value", global_container_stack.getProperty(key, "value"))
  192. ## Get the X and Y values for a layer (will be used to get X and Y of the
  193. # layer after the pause).
  194. def getNextXY(self, layer: str) -> Tuple[float, float]:
  195. """Get the X and Y values for a layer (will be used to get X and Y of the layer after the pause)."""
  196. lines = layer.split("\n")
  197. for line in lines:
  198. if line.startswith(("G0", "G1", "G2", "G3")):
  199. if self.getValue(line, "X") is not None and self.getValue(line, "Y") is not None:
  200. x = self.getValue(line, "X")
  201. y = self.getValue(line, "Y")
  202. return x, y
  203. return 0, 0
  204. def execute(self, data: List[str]) -> List[str]:
  205. """Inserts the pause commands.
  206. :param data: List of layers.
  207. :return: New list of layers.
  208. """
  209. pause_at = self.getSettingValueByKey("pause_at")
  210. pause_height = self.getSettingValueByKey("pause_height")
  211. pause_layer = self.getSettingValueByKey("pause_layer")
  212. disarm_timeout = self.getSettingValueByKey("disarm_timeout")
  213. retraction_amount = self.getSettingValueByKey("retraction_amount")
  214. retraction_speed = self.getSettingValueByKey("retraction_speed")
  215. extrude_amount = self.getSettingValueByKey("extrude_amount")
  216. extrude_speed = self.getSettingValueByKey("extrude_speed")
  217. park_x = self.getSettingValueByKey("head_park_x")
  218. park_y = self.getSettingValueByKey("head_park_y")
  219. move_z = self.getSettingValueByKey("head_move_z")
  220. layers_started = False
  221. redo_layer = self.getSettingValueByKey("redo_layer")
  222. standby_temperature = self.getSettingValueByKey("standby_temperature")
  223. firmware_retract = Application.getInstance().getGlobalContainerStack().getProperty("machine_firmware_retract", "value")
  224. control_temperatures = Application.getInstance().getGlobalContainerStack().getProperty("machine_nozzle_temp_enabled", "value")
  225. initial_layer_height = Application.getInstance().getGlobalContainerStack().getProperty("layer_height_0", "value")
  226. display_text = self.getSettingValueByKey("display_text")
  227. pause_method = self.getSettingValueByKey("pause_method")
  228. pause_command = {
  229. "marlin": self.putValue(M = 0),
  230. "griffin": self.putValue(M = 0),
  231. "bq": self.putValue(M = 25),
  232. "reprap": self.putValue(M = 226),
  233. "repetier": self.putValue("@pause now change filament and press continue printing")
  234. }[pause_method]
  235. # T = ExtruderManager.getInstance().getActiveExtruderStack().getProperty("material_print_temperature", "value")
  236. # use offset to calculate the current height: <current_height> = <current_z> - <layer_0_z>
  237. layer_0_z = 0
  238. current_z = 0
  239. current_height = 0
  240. current_layer = 0
  241. current_extrusion_f = 0
  242. got_first_g_cmd_on_layer_0 = False
  243. current_t = 0 #Tracks the current extruder for tracking the target temperature.
  244. target_temperature = {} #Tracks the current target temperature for each extruder.
  245. nbr_negative_layers = 0
  246. for index, layer in enumerate(data):
  247. lines = layer.split("\n")
  248. # Scroll each line of instruction for each layer in the G-code
  249. for line in lines:
  250. # Fist positive layer reached
  251. if ";LAYER:0" in line:
  252. layers_started = True
  253. # Count nbr of negative layers (raft)
  254. elif ";LAYER:-" in line:
  255. nbr_negative_layers += 1
  256. #Track the latest printing temperature in order to resume at the correct temperature.
  257. if line.startswith("T"):
  258. current_t = self.getValue(line, "T")
  259. m = self.getValue(line, "M")
  260. if m is not None and (m == 104 or m == 109) and self.getValue(line, "S") is not None:
  261. extruder = current_t
  262. if self.getValue(line, "T") is not None:
  263. extruder = self.getValue(line, "T")
  264. target_temperature[extruder] = self.getValue(line, "S")
  265. if not layers_started:
  266. continue
  267. # Look for the feed rate of an extrusion instruction
  268. if self.getValue(line, "F") is not None and self.getValue(line, "E") is not None:
  269. current_extrusion_f = self.getValue(line, "F")
  270. # If a Z instruction is in the line, read the current Z
  271. if self.getValue(line, "Z") is not None:
  272. current_z = self.getValue(line, "Z")
  273. if pause_at == "height":
  274. # Ignore if the line is not G1 or G0
  275. if self.getValue(line, "G") != 1 and self.getValue(line, "G") != 0:
  276. continue
  277. # This block is executed once, the first time there is a G
  278. # command, to get the z offset (z for first positive layer)
  279. if not got_first_g_cmd_on_layer_0:
  280. layer_0_z = current_z - initial_layer_height
  281. got_first_g_cmd_on_layer_0 = True
  282. current_height = current_z - layer_0_z
  283. if current_height < pause_height:
  284. continue # Scan the enitre layer, z-changes are not always on the same/first line.
  285. # Pause at layer
  286. else:
  287. if not line.startswith(";LAYER:"):
  288. continue
  289. current_layer = line[len(";LAYER:"):]
  290. try:
  291. current_layer = int(current_layer)
  292. # Couldn't cast to int. Something is wrong with this
  293. # g-code data
  294. except ValueError:
  295. continue
  296. if current_layer < pause_layer - nbr_negative_layers:
  297. continue
  298. # Get X and Y from the next layer (better position for
  299. # the nozzle)
  300. next_layer = data[index + 1]
  301. x, y = self.getNextXY(next_layer)
  302. prev_layer = data[index - 1]
  303. prev_lines = prev_layer.split("\n")
  304. current_e = 0.
  305. # Access last layer, browse it backwards to find
  306. # last extruder absolute position
  307. for prevLine in reversed(prev_lines):
  308. current_e = self.getValue(prevLine, "E", -1)
  309. if current_e >= 0:
  310. break
  311. # Maybe redo the last layer.
  312. if redo_layer:
  313. prev_layer = data[index - 1]
  314. layer = prev_layer + layer
  315. # Get extruder's absolute position at the
  316. # beginning of the redone layer.
  317. # see https://github.com/nallath/PostProcessingPlugin/issues/55
  318. # Get X and Y from the next layer (better position for
  319. # the nozzle)
  320. x, y = self.getNextXY(layer)
  321. prev_lines = prev_layer.split("\n")
  322. for lin in prev_lines:
  323. new_e = self.getValue(lin, "E", current_e)
  324. if new_e != current_e:
  325. current_e = new_e
  326. break
  327. prepend_gcode = ";TYPE:CUSTOM\n"
  328. prepend_gcode += ";added code by post processing\n"
  329. prepend_gcode += ";script: PauseAtHeight.py\n"
  330. if pause_at == "height":
  331. prepend_gcode += ";current z: {z}\n".format(z = current_z)
  332. prepend_gcode += ";current height: {height}\n".format(height = current_height)
  333. else:
  334. prepend_gcode += ";current layer: {layer}\n".format(layer = current_layer)
  335. if pause_method == "repetier":
  336. #Retraction
  337. prepend_gcode += self.putValue(M = 83) + " ; switch to relative E values for any needed retraction\n"
  338. if retraction_amount != 0:
  339. prepend_gcode += self.putValue(G = 1, E = retraction_amount, F = 6000) + "\n"
  340. #Move the head away
  341. prepend_gcode += self.putValue(G = 1, Z = current_z + 1, F = 300) + " ; move up a millimeter to get out of the way\n"
  342. prepend_gcode += self.putValue(G = 1, X = park_x, Y = park_y, F = 9000) + "\n"
  343. if current_z < move_z:
  344. prepend_gcode += self.putValue(G = 1, Z = current_z + move_z, F = 300) + "\n"
  345. #Disable the E steppers
  346. prepend_gcode += self.putValue(M = 84, E = 0) + "\n"
  347. elif pause_method != "griffin":
  348. # Retraction
  349. prepend_gcode += self.putValue(M = 83) + " ; switch to relative E values for any needed retraction\n"
  350. if retraction_amount != 0:
  351. if firmware_retract: #Can't set the distance directly to what the user wants. We have to choose ourselves.
  352. retraction_count = 1 if control_temperatures else 3 #Retract more if we don't control the temperature.
  353. for i in range(retraction_count):
  354. prepend_gcode += self.putValue(G = 10) + "\n"
  355. else:
  356. prepend_gcode += self.putValue(G = 1, E = -retraction_amount, F = retraction_speed * 60) + "\n"
  357. # Move the head away
  358. prepend_gcode += self.putValue(G = 1, Z = current_z + 1, F = 300) + " ; move up a millimeter to get out of the way\n"
  359. # This line should be ok
  360. prepend_gcode += self.putValue(G = 1, X = park_x, Y = park_y, F = 9000) + "\n"
  361. if current_z < 15:
  362. prepend_gcode += self.putValue(G = 1, Z = 15, F = 300) + " ; too close to bed--move to at least 15mm\n"
  363. if control_temperatures:
  364. # Set extruder standby temperature
  365. prepend_gcode += self.putValue(M = 104, S = standby_temperature) + " ; standby temperature\n"
  366. if display_text:
  367. prepend_gcode += "M117 " + display_text + "\n"
  368. # Set the disarm timeout
  369. if disarm_timeout > 0:
  370. prepend_gcode += self.putValue(M = 18, S = disarm_timeout) + " ; Set the disarm timeout\n"
  371. # Wait till the user continues printing
  372. prepend_gcode += pause_command + " ; Do the actual pause\n"
  373. if pause_method == "repetier":
  374. #Push the filament back,
  375. if retraction_amount != 0:
  376. prepend_gcode += self.putValue(G = 1, E = retraction_amount, F = 6000) + "\n"
  377. # Optionally extrude material
  378. if extrude_amount != 0:
  379. prepend_gcode += self.putValue(G = 1, E = extrude_amount, F = 200) + "\n"
  380. prepend_gcode += self.putValue("@info wait for cleaning nozzle from previous filament") + "\n"
  381. prepend_gcode += self.putValue("@pause remove the waste filament from parking area and press continue printing") + "\n"
  382. # and retract again, the properly primes the nozzle when changing filament.
  383. if retraction_amount != 0:
  384. prepend_gcode += self.putValue(G = 1, E = -retraction_amount, F = 6000) + "\n"
  385. #Move the head back
  386. prepend_gcode += self.putValue(G = 1, Z = current_z + 1, F = 300) + "\n"
  387. prepend_gcode += self.putValue(G = 1, X = x, Y = y, F = 9000) + "\n"
  388. if retraction_amount != 0:
  389. prepend_gcode += self.putValue(G = 1, E = retraction_amount, F = 6000) + "\n"
  390. if current_extrusion_f != 0:
  391. prepend_gcode += self.putValue(G = 1, F = current_extrusion_f) + " ; restore extrusion feedrate\n"
  392. else:
  393. Logger.log("w", "No previous feedrate found in gcode, feedrate for next layer(s) might be incorrect")
  394. prepend_gcode += self.putValue(M = 82) + "\n"
  395. # reset extrude value to pre pause value
  396. prepend_gcode += self.putValue(G = 92, E = current_e) + "\n"
  397. elif pause_method != "griffin":
  398. if control_temperatures:
  399. # Set extruder resume temperature
  400. prepend_gcode += self.putValue(M = 109, S = int(target_temperature.get(current_t, 0))) + " ; resume temperature\n"
  401. # Push the filament back,
  402. if retraction_amount != 0:
  403. prepend_gcode += self.putValue(G = 1, E = retraction_amount, F = retraction_speed * 60) + "\n"
  404. # Optionally extrude material
  405. if extrude_amount != 0:
  406. prepend_gcode += self.putValue(G = 1, E = extrude_amount, F = extrude_speed * 60) + "\n"
  407. # and retract again, the properly primes the nozzle
  408. # when changing filament.
  409. if retraction_amount != 0:
  410. prepend_gcode += self.putValue(G = 1, E = -retraction_amount, F = retraction_speed * 60) + "\n"
  411. # Move the head back
  412. if current_z < 15:
  413. prepend_gcode += self.putValue(G = 1, Z = current_z + 1, F = 300) + "\n"
  414. prepend_gcode += self.putValue(G = 1, X = x, Y = y, F = 9000) + "\n"
  415. prepend_gcode += self.putValue(G = 1, Z = current_z, F = 300) + " ; move back down to resume height\n"
  416. if retraction_amount != 0:
  417. if firmware_retract: #Can't set the distance directly to what the user wants. We have to choose ourselves.
  418. retraction_count = 1 if control_temperatures else 3 #Retract more if we don't control the temperature.
  419. for i in range(retraction_count):
  420. prepend_gcode += self.putValue(G = 11) + "\n"
  421. else:
  422. prepend_gcode += self.putValue(G = 1, E = retraction_amount, F = retraction_speed * 60) + "\n"
  423. if current_extrusion_f != 0:
  424. prepend_gcode += self.putValue(G = 1, F = current_extrusion_f) + " ; restore extrusion feedrate\n"
  425. else:
  426. Logger.log("w", "No previous feedrate found in gcode, feedrate for next layer(s) might be incorrect")
  427. prepend_gcode += self.putValue(M = 82) + " ; switch back to absolute E values\n"
  428. # reset extrude value to pre pause value
  429. prepend_gcode += self.putValue(G = 92, E = current_e) + "\n"
  430. layer = prepend_gcode + layer
  431. # Override the data of this layer with the
  432. # modified data
  433. data[index] = layer
  434. return data
  435. return data