PauseAtHeight.py 15 KB

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