PrinterOutputDevice.py 17 KB

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