PrinterOutputDevice.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. from UM.Signal import signalemitter
  6. ## Printer output device adds extra interface options on top of output device.
  7. #
  8. # The assumption is made the printer is a FDM printer.
  9. #
  10. # Note that a number of settings are marked as "final". This is because decorators
  11. # are not inherited by children. To fix this we use the private counter part of those
  12. # functions to actually have the implementation.
  13. #
  14. # For all other uses it should be used in the same way as a "regular" OutputDevice.
  15. @signalemitter
  16. class PrinterOutputDevice(QObject, OutputDevice):
  17. def __init__(self, device_id, parent = None):
  18. super().__init__(device_id = device_id, parent = parent)
  19. self._target_bed_temperature = 0
  20. self._bed_temperature = 0
  21. self._num_extruders = 1
  22. self._hotend_temperatures = [0] * self._num_extruders
  23. self._target_hotend_temperatures = [0] * self._num_extruders
  24. self._material_ids = [""] * self._num_extruders
  25. self._hotend_ids = [""] * self._num_extruders
  26. self._progress = 0
  27. self._head_x = 0
  28. self._head_y = 0
  29. self._head_z = 0
  30. self._connection_state = ConnectionState.closed
  31. self._time_elapsed = 0
  32. self._time_total = 0
  33. self._job_state = ""
  34. self._job_name = ""
  35. def requestWrite(self, node, file_name = None, filter_by_machine = False):
  36. raise NotImplementedError("requestWrite needs to be implemented")
  37. ## Signals
  38. # Signal to be emitted when bed temp is changed
  39. bedTemperatureChanged = pyqtSignal()
  40. # Signal to be emitted when target bed temp is changed
  41. targetBedTemperatureChanged = pyqtSignal()
  42. # Signal when the progress is changed (usually when this output device is printing / sending lots of data)
  43. progressChanged = pyqtSignal()
  44. # Signal to be emitted when hotend temp is changed
  45. hotendTemperaturesChanged = pyqtSignal()
  46. # Signal to be emitted when target hotend temp is changed
  47. targetHotendTemperaturesChanged = pyqtSignal()
  48. # Signal to be emitted when head position is changed (x,y,z)
  49. headPositionChanged = pyqtSignal()
  50. # Signal to be emitted when either of the material ids is changed
  51. materialIdChanged = pyqtSignal(int, str, arguments = ["index", "id"])
  52. # Signal to be emitted when either of the hotend ids is changed
  53. hotendIdChanged = pyqtSignal(int, str, arguments = ["index", "id"])
  54. # Signal that is emitted every time connection state is changed.
  55. # it also sends it's own device_id (for convenience sake)
  56. connectionStateChanged = pyqtSignal(str)
  57. timeElapsedChanged = pyqtSignal()
  58. timeTotalChanged = pyqtSignal()
  59. jobStateChanged = pyqtSignal()
  60. jobNameChanged = pyqtSignal()
  61. @pyqtProperty(str, notify = jobStateChanged)
  62. def jobState(self):
  63. return self._job_state
  64. def _updateJobState(self, job_state):
  65. if self._job_state != job_state:
  66. self._job_state = job_state
  67. self.jobStateChanged.emit()
  68. @pyqtSlot(str)
  69. def setJobState(self, job_state):
  70. self._setJobState(job_state)
  71. def _setJobState(self, job_state):
  72. Logger.log("w", "_setJobState is not implemented by this output device")
  73. @pyqtProperty(str, notify = jobNameChanged)
  74. def jobName(self):
  75. return self._job_name
  76. def setJobName(self, name):
  77. if self._job_name != name:
  78. self._job_name = name
  79. self.jobNameChanged.emit()
  80. ## Get the bed temperature of the bed (if any)
  81. # This function is "final" (do not re-implement)
  82. # /sa _getBedTemperature implementation function
  83. @pyqtProperty(float, notify = bedTemperatureChanged)
  84. def bedTemperature(self):
  85. return self._bed_temperature
  86. ## Set the (target) bed temperature
  87. # This function is "final" (do not re-implement)
  88. # /param temperature new target temperature of the bed (in deg C)
  89. # /sa _setTargetBedTemperature implementation function
  90. @pyqtSlot(int)
  91. def setTargetBedTemperature(self, temperature):
  92. self._setTargetBedTemperature(temperature)
  93. self._target_bed_temperature = temperature
  94. self.targetBedTemperatureChanged.emit()
  95. ## Time the print has been printing.
  96. # Note that timeTotal - timeElapsed should give time remaining.
  97. @pyqtProperty(float, notify = timeElapsedChanged)
  98. def timeElapsed(self):
  99. return self._time_elapsed
  100. ## Total time of the print
  101. # Note that timeTotal - timeElapsed should give time remaining.
  102. @pyqtProperty(float, notify=timeTotalChanged)
  103. def timeTotal(self):
  104. return self._time_total
  105. @pyqtSlot(float)
  106. def setTimeTotal(self, new_total):
  107. if self._time_total != new_total:
  108. self._time_total = new_total
  109. self.timeTotalChanged.emit()
  110. @pyqtSlot(float)
  111. def setTimeElapsed(self, time_elapsed):
  112. if self._time_elapsed != time_elapsed:
  113. self._time_elapsed = time_elapsed
  114. self.timeElapsedChanged.emit()
  115. ## Home the head of the connected printer
  116. # This function is "final" (do not re-implement)
  117. # /sa _homeHead implementation function
  118. @pyqtSlot()
  119. def homeHead(self):
  120. self._homeHead()
  121. ## Home the head of the connected printer
  122. # This is an implementation function and should be overriden by children.
  123. def _homeHead(self):
  124. Logger.log("w", "_homeHead is not implemented by this output device")
  125. ## Home the bed of the connected printer
  126. # This function is "final" (do not re-implement)
  127. # /sa _homeBed implementation function
  128. @pyqtSlot()
  129. def homeBed(self):
  130. self._homeBed()
  131. ## Home the bed of the connected printer
  132. # This is an implementation function and should be overriden by children.
  133. # /sa homeBed
  134. def _homeBed(self):
  135. Logger.log("w", "_homeBed is not implemented by this output device")
  136. ## Protected setter for the bed temperature of the connected printer (if any).
  137. # /parameter temperature Temperature bed needs to go to (in deg celsius)
  138. # /sa setTargetBedTemperature
  139. def _setTargetBedTemperature(self, temperature):
  140. Logger.log("w", "_setTargetBedTemperature is not implemented by this output device")
  141. ## Protected setter for the current bed temperature.
  142. # This simply sets the bed temperature, but ensures that a signal is emitted.
  143. # /param temperature temperature of the bed.
  144. def _setBedTemperature(self, temperature):
  145. self._bed_temperature = temperature
  146. self.bedTemperatureChanged.emit()
  147. ## Get the target bed temperature if connected printer (if any)
  148. @pyqtProperty(int, notify = targetBedTemperatureChanged)
  149. def targetBedTemperature(self):
  150. return self._target_bed_temperature
  151. ## Set the (target) hotend temperature
  152. # This function is "final" (do not re-implement)
  153. # /param index the index of the hotend that needs to change temperature
  154. # /param temperature The temperature it needs to change to (in deg celsius).
  155. # /sa _setTargetHotendTemperature implementation function
  156. @pyqtSlot(int, int)
  157. def setTargetHotendTemperature(self, index, temperature):
  158. self._setTargetHotendTemperature(index, temperature)
  159. self._target_hotend_temperatures[index] = temperature
  160. self.targetHotendTemperaturesChanged.emit()
  161. ## Implementation function of setTargetHotendTemperature.
  162. # /param index Index of the hotend to set the temperature of
  163. # /param temperature Temperature to set the hotend to (in deg C)
  164. # /sa setTargetHotendTemperature
  165. def _setTargetHotendTemperature(self, index, temperature):
  166. Logger.log("w", "_setTargetHotendTemperature is not implemented by this output device")
  167. @pyqtProperty("QVariantList", notify = targetHotendTemperaturesChanged)
  168. def targetHotendTemperatures(self):
  169. return self._target_hotend_temperatures
  170. @pyqtProperty("QVariantList", notify = hotendTemperaturesChanged)
  171. def hotendTemperatures(self):
  172. return self._hotend_temperatures
  173. ## Protected setter for the current hotend temperature.
  174. # This simply sets the hotend temperature, but ensures that a signal is emitted.
  175. # /param index Index of the hotend
  176. # /param temperature temperature of the hotend (in deg C)
  177. def _setHotendTemperature(self, index, temperature):
  178. self._hotend_temperatures[index] = temperature
  179. self.hotendTemperaturesChanged.emit()
  180. @pyqtProperty("QVariantList", notify = materialIdChanged)
  181. def materialIds(self):
  182. return self._material_ids
  183. ## Protected setter for the current material id.
  184. # /param index Index of the extruder
  185. # /param material_id id of the material
  186. def _setMaterialId(self, index, material_id):
  187. if material_id and material_id != "" and material_id != self._material_ids[index]:
  188. Logger.log("d", "Setting material id of hotend %d to %s" % (index, material_id))
  189. self._material_ids[index] = material_id
  190. self.materialIdChanged.emit(index, material_id)
  191. @pyqtProperty("QVariantList", notify = hotendIdChanged)
  192. def hotendIds(self):
  193. return self._hotend_ids
  194. ## Protected setter for the current hotend id.
  195. # /param index Index of the extruder
  196. # /param hotend_id id of the hotend
  197. def _setHotendId(self, index, hotend_id):
  198. if hotend_id and hotend_id != "" and hotend_id != self._hotend_ids[index]:
  199. Logger.log("d", "Setting hotend id of hotend %d to %s" % (index, hotend_id))
  200. self._hotend_ids[index] = hotend_id
  201. self.hotendIdChanged.emit(index, hotend_id)
  202. ## Attempt to establish connection
  203. def connect(self):
  204. raise NotImplementedError("connect needs to be implemented")
  205. ## Attempt to close the connection
  206. def close(self):
  207. raise NotImplementedError("close needs to be implemented")
  208. @pyqtProperty(bool, notify = connectionStateChanged)
  209. def connectionState(self):
  210. return self._connection_state
  211. ## Set the connection state of this output device.
  212. # /param connection_state ConnectionState enum.
  213. def setConnectionState(self, connection_state):
  214. self._connection_state = connection_state
  215. self.connectionStateChanged.emit(self._id)
  216. ## Ensure that close gets called when object is destroyed
  217. def __del__(self):
  218. self.close()
  219. ## Get the x position of the head.
  220. # This function is "final" (do not re-implement)
  221. @pyqtProperty(float, notify = headPositionChanged)
  222. def headX(self):
  223. return self._head_x
  224. ## Get the y position of the head.
  225. # This function is "final" (do not re-implement)
  226. @pyqtProperty(float, notify = headPositionChanged)
  227. def headY(self):
  228. return self._head_y
  229. ## Get the z position of the head.
  230. # In some machines it's actually the bed that moves. For convenience sake we simply see it all as head movements.
  231. # This function is "final" (do not re-implement)
  232. @pyqtProperty(float, notify = headPositionChanged)
  233. def headZ(self):
  234. return self._head_z
  235. ## Update the saved position of the head
  236. # This function should be called when a new position for the head is recieved.
  237. def _updateHeadPosition(self, x, y ,z):
  238. position_changed = False
  239. if self._head_x != x:
  240. self._head_x = x
  241. position_changed = True
  242. if self._head_y != y:
  243. self._head_y = y
  244. position_changed = True
  245. if self._head_z != z:
  246. self._head_z = z
  247. position_changed = True
  248. if position_changed:
  249. self.headPositionChanged.emit()
  250. ## Set the position of the head.
  251. # In some machines it's actually the bed that moves. For convenience sake we simply see it all as head movements.
  252. # This function is "final" (do not re-implement)
  253. # /param x new x location of the head.
  254. # /param y new y location of the head.
  255. # /param z new z location of the head.
  256. # /param speed Speed by which it needs to move (in mm/minute)
  257. # /sa _setHeadPosition implementation function
  258. @pyqtSlot("long", "long", "long")
  259. @pyqtSlot("long", "long", "long", "long")
  260. def setHeadPosition(self, x, y, z, speed = 3000):
  261. self._setHeadPosition(x, y , z, speed)
  262. ## Set the X position of the head.
  263. # This function is "final" (do not re-implement)
  264. # /param x x position head needs to move to.
  265. # /param speed Speed by which it needs to move (in mm/minute)
  266. # /sa _setHeadx implementation function
  267. @pyqtSlot("long")
  268. @pyqtSlot("long", "long")
  269. def setHeadX(self, x, speed = 3000):
  270. self._setHeadX(x, speed)
  271. ## Set the Y position of the head.
  272. # This function is "final" (do not re-implement)
  273. # /param y y position head needs to move to.
  274. # /param speed Speed by which it needs to move (in mm/minute)
  275. # /sa _setHeadY implementation function
  276. @pyqtSlot("long")
  277. @pyqtSlot("long", "long")
  278. def setHeadY(self, y, speed = 3000):
  279. self._setHeadY(y, speed)
  280. ## Set the Z position of the head.
  281. # In some machines it's actually the bed that moves. For convenience sake we simply see it all as head movements.
  282. # This function is "final" (do not re-implement)
  283. # /param z z position head needs to move to.
  284. # /param speed Speed by which it needs to move (in mm/minute)
  285. # /sa _setHeadZ implementation function
  286. @pyqtSlot("long")
  287. @pyqtSlot("long", "long")
  288. def setHeadZ(self, z, speed = 3000):
  289. self._setHeadY(z, speed)
  290. ## Move the head of the printer.
  291. # Note that this is a relative move. If you want to move the head to a specific position you can use
  292. # setHeadPosition
  293. # This function is "final" (do not re-implement)
  294. # /param x distance in x to move
  295. # /param y distance in y to move
  296. # /param z distance in z to move
  297. # /param speed Speed by which it needs to move (in mm/minute)
  298. # /sa _moveHead implementation function
  299. @pyqtSlot("long", "long", "long")
  300. @pyqtSlot("long", "long", "long", "long")
  301. def moveHead(self, x = 0, y = 0, z = 0, speed = 3000):
  302. self._moveHead(x, y, z, speed)
  303. ## Implementation function of moveHead.
  304. # /param x distance in x to move
  305. # /param y distance in y to move
  306. # /param z distance in z to move
  307. # /param speed Speed by which it needs to move (in mm/minute)
  308. # /sa moveHead
  309. def _moveHead(self, x, y, z, speed):
  310. Logger.log("w", "_moveHead is not implemented by this output device")
  311. ## Implementation function of setHeadPosition.
  312. # /param x new x location of the head.
  313. # /param y new y location of the head.
  314. # /param z new z location of the head.
  315. # /param speed Speed by which it needs to move (in mm/minute)
  316. # /sa setHeadPosition
  317. def _setHeadPosition(self, x, y, z, speed):
  318. Logger.log("w", "_setHeadPosition is not implemented by this output device")
  319. ## Implementation function of setHeadX.
  320. # /param x new x location of the head.
  321. # /param speed Speed by which it needs to move (in mm/minute)
  322. # /sa setHeadX
  323. def _setHeadX(self, x, speed):
  324. Logger.log("w", "_setHeadX is not implemented by this output device")
  325. ## Implementation function of setHeadY.
  326. # /param y new y location of the head.
  327. # /param speed Speed by which it needs to move (in mm/minute)
  328. # /sa _setHeadY
  329. def _setHeadY(self, y, speed):
  330. Logger.log("w", "_setHeadY is not implemented by this output device")
  331. ## Implementation function of setHeadZ.
  332. # /param z new z location of the head.
  333. # /param speed Speed by which it needs to move (in mm/minute)
  334. # /sa _setHeadZ
  335. def _setHeadZ(self, z, speed):
  336. Logger.log("w", "_setHeadZ is not implemented by this output device")
  337. ## Get the progress of any currently active process.
  338. # This function is "final" (do not re-implement)
  339. # /sa _getProgress
  340. # /returns float progress of the process. -1 indicates that there is no process.
  341. @pyqtProperty(float, notify = progressChanged)
  342. def progress(self):
  343. return self._progress
  344. ## Set the progress of any currently active process
  345. # /param progress Progress of the process.
  346. def setProgress(self, progress):
  347. if self._progress != progress:
  348. self._progress = progress
  349. self.progressChanged.emit()
  350. ## The current processing state of the backend.
  351. class ConnectionState(IntEnum):
  352. closed = 0
  353. connecting = 1
  354. connected = 2
  355. busy = 3
  356. error = 4