PrinterOutputDevice.py 18 KB

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