PauseAtHeight.py 13 KB

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