PauseAtHeight.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. # Copyright (c) 2020 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. "default_value": ""
  182. },
  183. "custom_gcode_before_pause":
  184. {
  185. "label": "G-code Before Pause",
  186. "description": "Any custom g-code to run before the pause, for example, M300 S440 P200 to beep.",
  187. "type": "str",
  188. "default_value": ""
  189. },
  190. "custom_gcode_after_pause":
  191. {
  192. "label": "G-code After Pause",
  193. "description": "Any custom g-code to run after the pause, for example, M300 S440 P200 to beep.",
  194. "type": "str",
  195. "default_value": ""
  196. }
  197. }
  198. }"""
  199. ## Copy machine name and gcode flavor from global stack so we can use their value in the script stack
  200. def initialize(self) -> None:
  201. super().initialize()
  202. global_container_stack = Application.getInstance().getGlobalContainerStack()
  203. if global_container_stack is None or self._instance is None:
  204. return
  205. for key in ["machine_name", "machine_gcode_flavor"]:
  206. self._instance.setProperty(key, "value", global_container_stack.getProperty(key, "value"))
  207. ## Get the X and Y values for a layer (will be used to get X and Y of the
  208. # layer after the pause).
  209. def getNextXY(self, layer: str) -> Tuple[float, float]:
  210. """Get the X and Y values for a layer (will be used to get X and Y of the layer after the pause)."""
  211. lines = layer.split("\n")
  212. for line in lines:
  213. if line.startswith(("G0", "G1", "G2", "G3")):
  214. if self.getValue(line, "X") is not None and self.getValue(line, "Y") is not None:
  215. x = self.getValue(line, "X")
  216. y = self.getValue(line, "Y")
  217. return x, y
  218. return 0, 0
  219. def execute(self, data: List[str]) -> List[str]:
  220. """Inserts the pause commands.
  221. :param data: List of layers.
  222. :return: New list of layers.
  223. """
  224. pause_at = self.getSettingValueByKey("pause_at")
  225. pause_height = self.getSettingValueByKey("pause_height")
  226. pause_layer = self.getSettingValueByKey("pause_layer")
  227. disarm_timeout = self.getSettingValueByKey("disarm_timeout")
  228. retraction_amount = self.getSettingValueByKey("retraction_amount")
  229. retraction_speed = self.getSettingValueByKey("retraction_speed")
  230. extrude_amount = self.getSettingValueByKey("extrude_amount")
  231. extrude_speed = self.getSettingValueByKey("extrude_speed")
  232. park_x = self.getSettingValueByKey("head_park_x")
  233. park_y = self.getSettingValueByKey("head_park_y")
  234. move_z = self.getSettingValueByKey("head_move_z")
  235. layers_started = False
  236. redo_layer = self.getSettingValueByKey("redo_layer")
  237. standby_temperature = self.getSettingValueByKey("standby_temperature")
  238. firmware_retract = Application.getInstance().getGlobalContainerStack().getProperty("machine_firmware_retract", "value")
  239. control_temperatures = Application.getInstance().getGlobalContainerStack().getProperty("machine_nozzle_temp_enabled", "value")
  240. initial_layer_height = Application.getInstance().getGlobalContainerStack().getProperty("layer_height_0", "value")
  241. display_text = self.getSettingValueByKey("display_text")
  242. gcode_before = self.getSettingValueByKey("custom_gcode_before_pause")
  243. gcode_after = self.getSettingValueByKey("custom_gcode_after_pause")
  244. pause_method = self.getSettingValueByKey("pause_method")
  245. pause_command = {
  246. "marlin": self.putValue(M = 0),
  247. "griffin": self.putValue(M = 0),
  248. "bq": self.putValue(M = 25),
  249. "reprap": self.putValue(M = 226),
  250. "repetier": self.putValue("@pause now change filament and press continue printing")
  251. }[pause_method]
  252. # T = ExtruderManager.getInstance().getActiveExtruderStack().getProperty("material_print_temperature", "value")
  253. # use offset to calculate the current height: <current_height> = <current_z> - <layer_0_z>
  254. layer_0_z = 0
  255. current_z = 0
  256. current_height = 0
  257. current_layer = 0
  258. current_extrusion_f = 0
  259. got_first_g_cmd_on_layer_0 = False
  260. current_t = 0 #Tracks the current extruder for tracking the target temperature.
  261. target_temperature = {} #Tracks the current target temperature for each extruder.
  262. nbr_negative_layers = 0
  263. for index, layer in enumerate(data):
  264. lines = layer.split("\n")
  265. # Scroll each line of instruction for each layer in the G-code
  266. for line in lines:
  267. # Fist positive layer reached
  268. if ";LAYER:0" in line:
  269. layers_started = True
  270. # Count nbr of negative layers (raft)
  271. elif ";LAYER:-" in line:
  272. nbr_negative_layers += 1
  273. #Track the latest printing temperature in order to resume at the correct temperature.
  274. if line.startswith("T"):
  275. current_t = self.getValue(line, "T")
  276. m = self.getValue(line, "M")
  277. if m is not None and (m == 104 or m == 109) and self.getValue(line, "S") is not None:
  278. extruder = current_t
  279. if self.getValue(line, "T") is not None:
  280. extruder = self.getValue(line, "T")
  281. target_temperature[extruder] = self.getValue(line, "S")
  282. if not layers_started:
  283. continue
  284. # Look for the feed rate of an extrusion instruction
  285. if self.getValue(line, "F") is not None and self.getValue(line, "E") is not None:
  286. current_extrusion_f = self.getValue(line, "F")
  287. # If a Z instruction is in the line, read the current Z
  288. if self.getValue(line, "Z") is not None:
  289. current_z = self.getValue(line, "Z")
  290. if pause_at == "height":
  291. # Ignore if the line is not G1 or G0
  292. if self.getValue(line, "G") != 1 and self.getValue(line, "G") != 0:
  293. continue
  294. # This block is executed once, the first time there is a G
  295. # command, to get the z offset (z for first positive layer)
  296. if not got_first_g_cmd_on_layer_0:
  297. layer_0_z = current_z - initial_layer_height
  298. got_first_g_cmd_on_layer_0 = True
  299. current_height = current_z - layer_0_z
  300. if current_height < pause_height:
  301. continue # Scan the enitre layer, z-changes are not always on the same/first line.
  302. # Pause at layer
  303. else:
  304. if not line.startswith(";LAYER:"):
  305. continue
  306. current_layer = line[len(";LAYER:"):]
  307. try:
  308. current_layer = int(current_layer)
  309. # Couldn't cast to int. Something is wrong with this
  310. # g-code data
  311. except ValueError:
  312. continue
  313. if current_layer < pause_layer - nbr_negative_layers:
  314. continue
  315. # Get X and Y from the next layer (better position for
  316. # the nozzle)
  317. next_layer = data[index + 1]
  318. x, y = self.getNextXY(next_layer)
  319. prev_layer = data[index - 1]
  320. prev_lines = prev_layer.split("\n")
  321. current_e = 0.
  322. # Access last layer, browse it backwards to find
  323. # last extruder absolute position
  324. for prevLine in reversed(prev_lines):
  325. current_e = self.getValue(prevLine, "E", -1)
  326. if current_e >= 0:
  327. break
  328. # Maybe redo the last layer.
  329. if redo_layer:
  330. prev_layer = data[index - 1]
  331. layer = prev_layer + layer
  332. # Get extruder's absolute position at the
  333. # beginning of the redone layer.
  334. # see https://github.com/nallath/PostProcessingPlugin/issues/55
  335. # Get X and Y from the next layer (better position for
  336. # the nozzle)
  337. x, y = self.getNextXY(layer)
  338. prev_lines = prev_layer.split("\n")
  339. for lin in prev_lines:
  340. new_e = self.getValue(lin, "E", current_e)
  341. if new_e != current_e:
  342. current_e = new_e
  343. break
  344. prepend_gcode = ";TYPE:CUSTOM\n"
  345. prepend_gcode += ";added code by post processing\n"
  346. prepend_gcode += ";script: PauseAtHeight.py\n"
  347. if pause_at == "height":
  348. prepend_gcode += ";current z: {z}\n".format(z = current_z)
  349. prepend_gcode += ";current height: {height}\n".format(height = current_height)
  350. else:
  351. prepend_gcode += ";current layer: {layer}\n".format(layer = current_layer)
  352. if pause_method == "repetier":
  353. #Retraction
  354. prepend_gcode += self.putValue(M = 83) + " ; switch to relative E values for any needed retraction\n"
  355. if retraction_amount != 0:
  356. prepend_gcode += self.putValue(G = 1, E = retraction_amount, F = 6000) + "\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. prepend_gcode += self.putValue(G = 1, X = park_x, Y = park_y, F = 9000) + "\n"
  360. if current_z < move_z:
  361. prepend_gcode += self.putValue(G = 1, Z = current_z + move_z, F = 300) + "\n"
  362. #Disable the E steppers
  363. prepend_gcode += self.putValue(M = 84, E = 0) + "\n"
  364. elif pause_method != "griffin":
  365. # Retraction
  366. prepend_gcode += self.putValue(M = 83) + " ; switch to relative E values for any needed retraction\n"
  367. if retraction_amount != 0:
  368. if firmware_retract: #Can't set the distance directly to what the user wants. We have to choose ourselves.
  369. retraction_count = 1 if control_temperatures else 3 #Retract more if we don't control the temperature.
  370. for i in range(retraction_count):
  371. prepend_gcode += self.putValue(G = 10) + "\n"
  372. else:
  373. prepend_gcode += self.putValue(G = 1, E = -retraction_amount, F = retraction_speed * 60) + "\n"
  374. # Move the head away
  375. prepend_gcode += self.putValue(G = 1, Z = current_z + 1, F = 300) + " ; move up a millimeter to get out of the way\n"
  376. # This line should be ok
  377. prepend_gcode += self.putValue(G = 1, X = park_x, Y = park_y, F = 9000) + "\n"
  378. if current_z < 15:
  379. prepend_gcode += self.putValue(G = 1, Z = 15, F = 300) + " ; too close to bed--move to at least 15mm\n"
  380. if control_temperatures:
  381. # Set extruder standby temperature
  382. prepend_gcode += self.putValue(M = 104, S = standby_temperature) + " ; standby temperature\n"
  383. if display_text:
  384. prepend_gcode += "M117 " + display_text + "\n"
  385. # Set the disarm timeout
  386. if disarm_timeout > 0:
  387. prepend_gcode += self.putValue(M = 18, S = disarm_timeout) + " ; Set the disarm timeout\n"
  388. # Set a custom GCODE section before pause
  389. if gcode_before:
  390. prepend_gcode += gcode_before + "\n"
  391. # Wait till the user continues printing
  392. prepend_gcode += pause_command + " ; Do the actual pause\n"
  393. # Set a custom GCODE section before pause
  394. if gcode_after:
  395. prepend_gcode += gcode_after + "\n"
  396. if pause_method == "repetier":
  397. #Push the filament back,
  398. if retraction_amount != 0:
  399. prepend_gcode += self.putValue(G = 1, E = retraction_amount, F = 6000) + "\n"
  400. # Optionally extrude material
  401. if extrude_amount != 0:
  402. prepend_gcode += self.putValue(G = 1, E = extrude_amount, F = 200) + "\n"
  403. prepend_gcode += self.putValue("@info wait for cleaning nozzle from previous filament") + "\n"
  404. prepend_gcode += self.putValue("@pause remove the waste filament from parking area and press continue printing") + "\n"
  405. # and retract again, the properly primes the nozzle when changing filament.
  406. if retraction_amount != 0:
  407. prepend_gcode += self.putValue(G = 1, E = -retraction_amount, F = 6000) + "\n"
  408. #Move the head back
  409. prepend_gcode += self.putValue(G = 1, Z = current_z + 1, F = 300) + "\n"
  410. prepend_gcode += self.putValue(G = 1, X = x, Y = y, F = 9000) + "\n"
  411. if retraction_amount != 0:
  412. prepend_gcode += self.putValue(G = 1, E = retraction_amount, F = 6000) + "\n"
  413. if current_extrusion_f != 0:
  414. prepend_gcode += self.putValue(G = 1, F = current_extrusion_f) + " ; restore extrusion feedrate\n"
  415. else:
  416. Logger.log("w", "No previous feedrate found in gcode, feedrate for next layer(s) might be incorrect")
  417. prepend_gcode += self.putValue(M = 82) + "\n"
  418. # reset extrude value to pre pause value
  419. prepend_gcode += self.putValue(G = 92, E = current_e) + "\n"
  420. elif pause_method != "griffin":
  421. if control_temperatures:
  422. # Set extruder resume temperature
  423. prepend_gcode += self.putValue(M = 109, S = int(target_temperature.get(current_t, 0))) + " ; resume temperature\n"
  424. # Push the filament back,
  425. if retraction_amount != 0:
  426. prepend_gcode += self.putValue(G = 1, E = retraction_amount, F = retraction_speed * 60) + "\n"
  427. # Optionally extrude material
  428. if extrude_amount != 0:
  429. prepend_gcode += self.putValue(G = 1, E = extrude_amount, F = extrude_speed * 60) + "\n"
  430. # and retract again, the properly primes the nozzle
  431. # when changing filament.
  432. if retraction_amount != 0:
  433. prepend_gcode += self.putValue(G = 1, E = -retraction_amount, F = retraction_speed * 60) + "\n"
  434. # Move the head back
  435. if current_z < 15:
  436. prepend_gcode += self.putValue(G = 1, Z = current_z + 1, F = 300) + "\n"
  437. prepend_gcode += self.putValue(G = 1, X = x, Y = y, F = 9000) + "\n"
  438. prepend_gcode += self.putValue(G = 1, Z = current_z, F = 300) + " ; move back down to resume height\n"
  439. if retraction_amount != 0:
  440. if firmware_retract: #Can't set the distance directly to what the user wants. We have to choose ourselves.
  441. retraction_count = 1 if control_temperatures else 3 #Retract more if we don't control the temperature.
  442. for i in range(retraction_count):
  443. prepend_gcode += self.putValue(G = 11) + "\n"
  444. else:
  445. prepend_gcode += self.putValue(G = 1, E = retraction_amount, F = retraction_speed * 60) + "\n"
  446. if current_extrusion_f != 0:
  447. prepend_gcode += self.putValue(G = 1, F = current_extrusion_f) + " ; restore extrusion feedrate\n"
  448. else:
  449. Logger.log("w", "No previous feedrate found in gcode, feedrate for next layer(s) might be incorrect")
  450. prepend_gcode += self.putValue(M = 82) + " ; switch back to absolute E values\n"
  451. # reset extrude value to pre pause value
  452. prepend_gcode += self.putValue(G = 92, E = current_e) + "\n"
  453. layer = prepend_gcode + layer
  454. # Override the data of this layer with the
  455. # modified data
  456. data[index] = layer
  457. return data
  458. return data