PrinterOutputDevice.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. from UM.OutputDevice.OutputDevice import OutputDevice
  2. from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject
  3. from enum import IntEnum # For the connection state tracking.
  4. from UM.Logger import Logger
  5. ## Printer output device adds extra interface options on top of output device.
  6. #
  7. # The assumption is made the printer is a FDM printer.
  8. #
  9. # Note that a number of settings are marked as "final". This is because decorators
  10. # are not inherited by children. To fix this we use the private counter part of those
  11. # functions to actually have the implementation.
  12. #
  13. # For all other uses it should be used in the same way as a "regular" OutputDevice.
  14. class PrinterOutputDevice(OutputDevice, QObject):
  15. def __init__(self, device_id, parent = None):
  16. super().__init__(device_id = device_id, parent = parent)
  17. self._target_bed_temperature = 0
  18. self._bed_temperature = 0
  19. self._num_extruders = 1
  20. self._hotend_temperatures = [0] * self._num_extruders
  21. self._target_hotend_temperatures = [0] * self._num_extruders
  22. self._progress = 0
  23. self._head_x = 0
  24. self._head_y = 0
  25. self._head_z = 0
  26. self._connection_state = ConnectionState.CLOSED
  27. def requestWrite(self, node, file_name = None, filter_by_machine = False):
  28. raise NotImplementedError("requestWrite needs to be implemented")
  29. ## Signals
  30. # Signal to be emitted when bed temp is changed
  31. bedTemperatureChanged = pyqtSignal()
  32. # Signal to be emitted when target bed temp is changed
  33. targetBedTemperatureChanged = pyqtSignal()
  34. # Signal when the progress is changed (usually when this output device is printing / sending lots of data)
  35. progressChanged = pyqtSignal()
  36. # Signal to be emitted when hotend temp is changed
  37. hotendTemperaturesChanged = pyqtSignal()
  38. # Signal to be emitted when target hotend temp is changed
  39. targetHotendTemperaturesChanged = pyqtSignal()
  40. # Signal to be emitted when head position is changed (x,y,z)
  41. headPositionChanged = pyqtSignal()
  42. # Signal that is emitted every time connection state is changed.
  43. # it also sends it's own device_id (for convenience sake)
  44. connectionStateChanged = pyqtSignal(str)
  45. ## Get the bed temperature of the bed (if any)
  46. # This function is "final" (do not re-implement)
  47. # /sa _getBedTemperature implementation function
  48. @pyqtProperty(float, notify = bedTemperatureChanged)
  49. def bedTemperature(self):
  50. return self._bed_temperature
  51. ## Set the (target) bed temperature
  52. # This function is "final" (do not re-implement)
  53. # /param temperature new target temperature of the bed (in deg C)
  54. # /sa _setTargetBedTemperature implementation function
  55. @pyqtSlot(int)
  56. def setTargetBedTemperature(self, temperature):
  57. self._setTargetBedTemperature(temperature)
  58. self._target_bed_temperature = temperature
  59. self.targetBedTemperatureChanged.emit()
  60. ## Home the head of the connected printer
  61. # This function is "final" (do not re-implement)
  62. # /sa _homeHead implementation function
  63. @pyqtSlot()
  64. def homeHead(self):
  65. self._homeHead()
  66. ## Home the head of the connected printer
  67. # This is an implementation function and should be overriden by children.
  68. def _homeHead(self):
  69. Logger.log("w", "_homeHead is not implemented by this output device")
  70. ## Home the bed of the connected printer
  71. # This function is "final" (do not re-implement)
  72. # /sa _homeBed implementation function
  73. @pyqtSlot()
  74. def homeBed(self):
  75. self._homeBed()
  76. ## Home the bed of the connected printer
  77. # This is an implementation function and should be overriden by children.
  78. # /sa homeBed
  79. def _homeBed(self):
  80. Logger.log("w", "_homeBed is not implemented by this output device")
  81. ## Protected setter for the bed temperature of the connected printer (if any).
  82. # /parameter temperature Temperature bed needs to go to (in deg celsius)
  83. # /sa setTargetBedTemperature
  84. def _setTargetBedTemperature(self, temperature):
  85. Logger.log("w", "_setTargetBedTemperature is not implemented by this output device")
  86. ## Protected setter for the current bed temperature.
  87. # This simply sets the bed temperature, but ensures that a signal is emitted.
  88. # /param temperature temperature of the bed.
  89. def _setBedTemperature(self, temperature):
  90. self._bed_temperature = temperature
  91. self.bedTemperatureChanged.emit()
  92. ## Get the target bed temperature if connected printer (if any)
  93. @pyqtProperty(int, notify = targetBedTemperatureChanged)
  94. def targetBedTemperature(self):
  95. return self._target_bed_temperature
  96. ## Set the (target) hotend temperature
  97. # This function is "final" (do not re-implement)
  98. # /param index the index of the hotend that needs to change temperature
  99. # /param temperature The temperature it needs to change to (in deg celsius).
  100. # /sa _setTargetHotendTemperature implementation function
  101. @pyqtSlot(int, int)
  102. def setTargetHotendTemperature(self, index, temperature):
  103. self._setTargetHotendTemperature(index, temperature)
  104. self._target_hotend_temperatures[index] = temperature
  105. self.targetHotendTemperaturesChanged.emit()
  106. ## Implementation function of setTargetHotendTemperature.
  107. # /param index Index of the hotend to set the temperature of
  108. # /param temperature Temperature to set the hotend to (in deg C)
  109. # /sa setTargetHotendTemperature
  110. def _setTargetHotendTemperature(self, index, temperature):
  111. Logger.log("w", "_setTargetHotendTemperature is not implemented by this output device")
  112. @pyqtProperty("QVariantList", notify = targetHotendTemperaturesChanged)
  113. def targetHotendTemperatures(self):
  114. return self._target_hotend_temperatures
  115. @pyqtProperty("QVariantList", notify = hotendTemperaturesChanged)
  116. def hotendTemperatures(self):
  117. return self._hotend_temperatures
  118. ## Protected setter for the current hotend temperature.
  119. # This simply sets the hotend temperature, but ensures that a signal is emitted.
  120. # /param index Index of the hotend
  121. # /param temperature temperature of the hotend (in deg C)
  122. def _setHotendTemperature(self, index, temperature):
  123. self._hotend_temperatures[index] = temperature
  124. self.hotendTemperaturesChanged.emit()
  125. ## Attempt to establish connection
  126. def connect(self):
  127. raise NotImplementedError("connect needs to be implemented")
  128. ## Attempt to close the connection
  129. def close(self):
  130. raise NotImplementedError("close needs to be implemented")
  131. @pyqtProperty(bool, notify = connectionStateChanged)
  132. def connectionState(self):
  133. return self._connection_state
  134. ## Set the connection state of this output device.
  135. # /param connection_state ConnectionState enum.
  136. def setConnectionState(self, connection_state):
  137. self._connection_state = connection_state
  138. self.connectionStateChanged.emit(self._id)
  139. ## Ensure that close gets called when object is destroyed
  140. def __del__(self):
  141. self.close()
  142. ## Get the x position of the head.
  143. # This function is "final" (do not re-implement)
  144. @pyqtProperty(float, notify = headPositionChanged)
  145. def headX(self):
  146. return self._head_x
  147. ## Get the y position of the head.
  148. # This function is "final" (do not re-implement)
  149. @pyqtProperty(float, notify = headPositionChanged)
  150. def headY(self):
  151. return self._head_y
  152. ## Get the z position of the head.
  153. # In some machines it's actually the bed that moves. For convenience sake we simply see it all as head movements.
  154. # This function is "final" (do not re-implement)
  155. @pyqtProperty(float, notify = headPositionChanged)
  156. def headZ(self):
  157. return self._head_z
  158. ## Set the position of the head.
  159. # In some machines it's actually the bed that moves. For convenience sake we simply see it all as head movements.
  160. # This function is "final" (do not re-implement)
  161. # /param x new x location of the head.
  162. # /param y new y location of the head.
  163. # /param z new z location of the head.
  164. # /param speed Speed by which it needs to move (in mm/minute)
  165. # /sa _setHeadPosition implementation function
  166. @pyqtSlot("long", "long", "long")
  167. @pyqtSlot("long", "long", "long", "long")
  168. def setHeadPosition(self, x, y, z, speed = 3000):
  169. self._setHeadPosition(x, y , z, speed)
  170. ## Set the X position of the head.
  171. # This function is "final" (do not re-implement)
  172. # /param x x position head needs to move to.
  173. # /param speed Speed by which it needs to move (in mm/minute)
  174. # /sa _setHeadx implementation function
  175. @pyqtSlot("long")
  176. @pyqtSlot("long", "long")
  177. def setHeadX(self, x, speed = 3000):
  178. self._setHeadX(x, speed)
  179. ## Set the Y position of the head.
  180. # This function is "final" (do not re-implement)
  181. # /param y y position head needs to move to.
  182. # /param speed Speed by which it needs to move (in mm/minute)
  183. # /sa _setHeadY implementation function
  184. @pyqtSlot("long")
  185. @pyqtSlot("long", "long")
  186. def setHeadY(self, y, speed = 3000):
  187. self._setHeadY(y, speed)
  188. ## Set the Z position of the head.
  189. # In some machines it's actually the bed that moves. For convenience sake we simply see it all as head movements.
  190. # This function is "final" (do not re-implement)
  191. # /param z z position head needs to move to.
  192. # /param speed Speed by which it needs to move (in mm/minute)
  193. # /sa _setHeadZ implementation function
  194. @pyqtSlot("long")
  195. @pyqtSlot("long", "long")
  196. def setHeadZ(self, z, speed = 3000):
  197. self._setHeadY(z, speed)
  198. ## Move the head of the printer.
  199. # Note that this is a relative move. If you want to move the head to a specific position you can use
  200. # setHeadPosition
  201. # This function is "final" (do not re-implement)
  202. # /param x distance in x to move
  203. # /param y distance in y to move
  204. # /param z distance in z to move
  205. # /param speed Speed by which it needs to move (in mm/minute)
  206. # /sa _moveHead implementation function
  207. @pyqtSlot("long", "long", "long")
  208. @pyqtSlot("long", "long", "long", "long")
  209. def moveHead(self, x = 0, y = 0, z = 0, speed = 3000):
  210. self._moveHead(x, y, z, speed)
  211. ## Implementation function of moveHead.
  212. # /param x distance in x to move
  213. # /param y distance in y to move
  214. # /param z distance in z to move
  215. # /param speed Speed by which it needs to move (in mm/minute)
  216. # /sa moveHead
  217. def _moveHead(self, x, y, z, speed):
  218. Logger.log("w", "_moveHead is not implemented by this output device")
  219. ## Implementation function of setHeadPosition.
  220. # /param x new x location of the head.
  221. # /param y new y location of the head.
  222. # /param z new z location of the head.
  223. # /param speed Speed by which it needs to move (in mm/minute)
  224. # /sa setHeadPosition
  225. def _setHeadPosition(self, x, y, z, speed):
  226. Logger.log("w", "_setHeadPosition is not implemented by this output device")
  227. ## Implementation function of setHeadX.
  228. # /param x new x location of the head.
  229. # /param speed Speed by which it needs to move (in mm/minute)
  230. # /sa setHeadX
  231. def _setHeadX(self, x, speed):
  232. Logger.log("w", "_setHeadX is not implemented by this output device")
  233. ## Implementation function of setHeadY.
  234. # /param y new y location of the head.
  235. # /param speed Speed by which it needs to move (in mm/minute)
  236. # /sa _setHeadY
  237. def _setHeadY(self, y, speed):
  238. Logger.log("w", "_setHeadY is not implemented by this output device")
  239. ## Implementation function of setHeadZ.
  240. # /param z new z location of the head.
  241. # /param speed Speed by which it needs to move (in mm/minute)
  242. # /sa _setHeadZ
  243. def _setHeadZ(self, z, speed):
  244. Logger.log("w", "_setHeadZ is not implemented by this output device")
  245. ## Get the progress of any currently active process.
  246. # This function is "final" (do not re-implement)
  247. # /sa _getProgress
  248. # /returns float progress of the process. -1 indicates that there is no process.
  249. @pyqtProperty(float, notify = progressChanged)
  250. def progress(self):
  251. return self._progress
  252. ## Set the progress of any currently active process
  253. # /param progress Progress of the process.
  254. def setProgress(self, progress):
  255. self._progress = progress
  256. self.progressChanged.emit()
  257. ## The current processing state of the backend.
  258. class ConnectionState(IntEnum):
  259. CLOSED = 0
  260. CONNECTING = 1
  261. CONNECTED = 2