PrinterOutputDevice.py 18 KB

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