PauseAtHeight.py 29 KB

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