PrinterOutputDevice.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.i18n import i18nCatalog
  4. from UM.OutputDevice.OutputDevice import OutputDevice
  5. from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject
  6. from PyQt5.QtWidgets import QMessageBox
  7. import UM.Settings.ContainerRegistry
  8. from enum import IntEnum # For the connection state tracking.
  9. from UM.Logger import Logger
  10. from UM.Application import Application
  11. from UM.Signal import signalemitter
  12. i18n_catalog = i18nCatalog("cura")
  13. ## Printer output device adds extra interface options on top of output device.
  14. #
  15. # The assumption is made the printer is a FDM printer.
  16. #
  17. # Note that a number of settings are marked as "final". This is because decorators
  18. # are not inherited by children. To fix this we use the private counter part of those
  19. # functions to actually have the implementation.
  20. #
  21. # For all other uses it should be used in the same way as a "regular" OutputDevice.
  22. @signalemitter
  23. class PrinterOutputDevice(QObject, OutputDevice):
  24. def __init__(self, device_id, parent = None):
  25. super().__init__(device_id = device_id, parent = parent)
  26. self._container_registry = UM.Settings.ContainerRegistry.getInstance()
  27. self._target_bed_temperature = 0
  28. self._bed_temperature = 0
  29. self._num_extruders = 1
  30. self._hotend_temperatures = [0] * self._num_extruders
  31. self._target_hotend_temperatures = [0] * self._num_extruders
  32. self._material_ids = [""] * self._num_extruders
  33. self._hotend_ids = [""] * self._num_extruders
  34. self._progress = 0
  35. self._head_x = 0
  36. self._head_y = 0
  37. self._head_z = 0
  38. self._connection_state = ConnectionState.closed
  39. self._connection_text = ""
  40. self._time_elapsed = 0
  41. self._time_total = 0
  42. self._job_state = ""
  43. self._job_name = ""
  44. self._error_text = ""
  45. self._accepts_commands = True
  46. self._preheat_bed_timeout = 900 #Default time-out for pre-heating the bed, in seconds.
  47. self._printer_state = ""
  48. self._printer_type = "unknown"
  49. self._camera_active = False
  50. def requestWrite(self, nodes, file_name = None, filter_by_machine = False, file_handler = None):
  51. raise NotImplementedError("requestWrite needs to be implemented")
  52. ## Signals
  53. # Signal to be emitted when bed temp is changed
  54. bedTemperatureChanged = pyqtSignal()
  55. # Signal to be emitted when target bed temp is changed
  56. targetBedTemperatureChanged = pyqtSignal()
  57. # Signal when the progress is changed (usually when this output device is printing / sending lots of data)
  58. progressChanged = pyqtSignal()
  59. # Signal to be emitted when hotend temp is changed
  60. hotendTemperaturesChanged = pyqtSignal()
  61. # Signal to be emitted when target hotend temp is changed
  62. targetHotendTemperaturesChanged = pyqtSignal()
  63. # Signal to be emitted when head position is changed (x,y,z)
  64. headPositionChanged = pyqtSignal()
  65. # Signal to be emitted when either of the material ids is changed
  66. materialIdChanged = pyqtSignal(int, str, arguments = ["index", "id"])
  67. # Signal to be emitted when either of the hotend ids is changed
  68. hotendIdChanged = pyqtSignal(int, str, arguments = ["index", "id"])
  69. # Signal that is emitted every time connection state is changed.
  70. # it also sends it's own device_id (for convenience sake)
  71. connectionStateChanged = pyqtSignal(str)
  72. connectionTextChanged = pyqtSignal()
  73. timeElapsedChanged = pyqtSignal()
  74. timeTotalChanged = pyqtSignal()
  75. jobStateChanged = pyqtSignal()
  76. jobNameChanged = pyqtSignal()
  77. errorTextChanged = pyqtSignal()
  78. acceptsCommandsChanged = pyqtSignal()
  79. printerStateChanged = pyqtSignal()
  80. printerTypeChanged = pyqtSignal()
  81. @pyqtProperty(str, notify=printerTypeChanged)
  82. def printerType(self):
  83. return self._printer_type
  84. @pyqtProperty(str, notify=printerStateChanged)
  85. def printerState(self):
  86. return self._printer_state
  87. @pyqtProperty(str, notify = jobStateChanged)
  88. def jobState(self):
  89. return self._job_state
  90. def _updatePrinterType(self, printer_type):
  91. if self._printer_type != printer_type:
  92. self._printer_type = printer_type
  93. self.printerTypeChanged.emit()
  94. def _updatePrinterState(self, printer_state):
  95. if self._printer_state != printer_state:
  96. self._printer_state = printer_state
  97. self.printerStateChanged.emit()
  98. def _updateJobState(self, job_state):
  99. if self._job_state != job_state:
  100. self._job_state = job_state
  101. self.jobStateChanged.emit()
  102. @pyqtSlot(str)
  103. def setJobState(self, job_state):
  104. self._setJobState(job_state)
  105. def _setJobState(self, job_state):
  106. Logger.log("w", "_setJobState is not implemented by this output device")
  107. @pyqtSlot()
  108. def startCamera(self):
  109. self._camera_active = True
  110. self._startCamera()
  111. def _startCamera(self):
  112. Logger.log("w", "_startCamera is not implemented by this output device")
  113. @pyqtSlot()
  114. def stopCamera(self):
  115. self._camera_active = False
  116. self._stopCamera()
  117. def _stopCamera(self):
  118. Logger.log("w", "_stopCamera is not implemented by this output device")
  119. @pyqtProperty(str, notify = jobNameChanged)
  120. def jobName(self):
  121. return self._job_name
  122. def setJobName(self, name):
  123. if self._job_name != name:
  124. self._job_name = name
  125. self.jobNameChanged.emit()
  126. ## Gives a human-readable address where the device can be found.
  127. @pyqtProperty(str, constant = True)
  128. def address(self):
  129. Logger.log("w", "address is not implemented by this output device.")
  130. @pyqtProperty(str, notify = errorTextChanged)
  131. def errorText(self):
  132. return self._error_text
  133. ## Set the error-text that is shown in the print monitor in case of an error
  134. def setErrorText(self, error_text):
  135. if self._error_text != error_text:
  136. self._error_text = error_text
  137. self.errorTextChanged.emit()
  138. @pyqtProperty(bool, notify = acceptsCommandsChanged)
  139. def acceptsCommands(self):
  140. return self._accepts_commands
  141. ## Set a flag to signal the UI that the printer is not (yet) ready to receive commands
  142. def setAcceptsCommands(self, accepts_commands):
  143. if self._accepts_commands != accepts_commands:
  144. self._accepts_commands = accepts_commands
  145. self.acceptsCommandsChanged.emit()
  146. ## Get the bed temperature of the bed (if any)
  147. # This function is "final" (do not re-implement)
  148. # /sa _getBedTemperature implementation function
  149. @pyqtProperty(float, notify = bedTemperatureChanged)
  150. def bedTemperature(self):
  151. return self._bed_temperature
  152. ## Set the (target) bed temperature
  153. # This function is "final" (do not re-implement)
  154. # /param temperature new target temperature of the bed (in deg C)
  155. # /sa _setTargetBedTemperature implementation function
  156. @pyqtSlot(int)
  157. def setTargetBedTemperature(self, temperature):
  158. self._setTargetBedTemperature(temperature)
  159. if self._target_bed_temperature != temperature:
  160. self._target_bed_temperature = temperature
  161. self.targetBedTemperatureChanged.emit()
  162. ## The duration of the time-out to pre-heat the bed, in seconds.
  163. #
  164. # \return The duration of the time-out to pre-heat the bed, in seconds.
  165. @pyqtProperty(int)
  166. def preheatBedTimeout(self):
  167. return self._preheat_bed_timeout
  168. ## Time the print has been printing.
  169. # Note that timeTotal - timeElapsed should give time remaining.
  170. @pyqtProperty(float, notify = timeElapsedChanged)
  171. def timeElapsed(self):
  172. return self._time_elapsed
  173. ## Total time of the print
  174. # Note that timeTotal - timeElapsed should give time remaining.
  175. @pyqtProperty(float, notify=timeTotalChanged)
  176. def timeTotal(self):
  177. return self._time_total
  178. @pyqtSlot(float)
  179. def setTimeTotal(self, new_total):
  180. if self._time_total != new_total:
  181. self._time_total = new_total
  182. self.timeTotalChanged.emit()
  183. @pyqtSlot(float)
  184. def setTimeElapsed(self, time_elapsed):
  185. if self._time_elapsed != time_elapsed:
  186. self._time_elapsed = time_elapsed
  187. self.timeElapsedChanged.emit()
  188. ## Home the head of the connected printer
  189. # This function is "final" (do not re-implement)
  190. # /sa _homeHead implementation function
  191. @pyqtSlot()
  192. def homeHead(self):
  193. self._homeHead()
  194. ## Home the head of the connected printer
  195. # This is an implementation function and should be overriden by children.
  196. def _homeHead(self):
  197. Logger.log("w", "_homeHead is not implemented by this output device")
  198. ## Home the bed of the connected printer
  199. # This function is "final" (do not re-implement)
  200. # /sa _homeBed implementation function
  201. @pyqtSlot()
  202. def homeBed(self):
  203. self._homeBed()
  204. ## Home the bed of the connected printer
  205. # This is an implementation function and should be overriden by children.
  206. # /sa homeBed
  207. def _homeBed(self):
  208. Logger.log("w", "_homeBed is not implemented by this output device")
  209. ## Protected setter for the bed temperature of the connected printer (if any).
  210. # /parameter temperature Temperature bed needs to go to (in deg celsius)
  211. # /sa setTargetBedTemperature
  212. def _setTargetBedTemperature(self, temperature):
  213. Logger.log("w", "_setTargetBedTemperature is not implemented by this output device")
  214. ## Pre-heats the heated bed of the printer.
  215. #
  216. # \param temperature The temperature to heat the bed to, in degrees
  217. # Celsius.
  218. # \param duration How long the bed should stay warm, in seconds.
  219. @pyqtSlot(float, float)
  220. def preheatBed(self, temperature, duration):
  221. Logger.log("w", "preheatBed is not implemented by this output device.")
  222. ## Cancels pre-heating the heated bed of the printer.
  223. #
  224. # If the bed is not pre-heated, nothing happens.
  225. @pyqtSlot()
  226. def cancelPreheatBed(self):
  227. Logger.log("w", "cancelPreheatBed is not implemented by this output device.")
  228. ## Protected setter for the current bed temperature.
  229. # This simply sets the bed temperature, but ensures that a signal is emitted.
  230. # /param temperature temperature of the bed.
  231. def _setBedTemperature(self, temperature):
  232. if self._bed_temperature != temperature:
  233. self._bed_temperature = temperature
  234. self.bedTemperatureChanged.emit()
  235. ## Get the target bed temperature if connected printer (if any)
  236. @pyqtProperty(int, notify = targetBedTemperatureChanged)
  237. def targetBedTemperature(self):
  238. return self._target_bed_temperature
  239. ## Set the (target) hotend temperature
  240. # This function is "final" (do not re-implement)
  241. # /param index the index of the hotend that needs to change temperature
  242. # /param temperature The temperature it needs to change to (in deg celsius).
  243. # /sa _setTargetHotendTemperature implementation function
  244. @pyqtSlot(int, int)
  245. def setTargetHotendTemperature(self, index, temperature):
  246. self._setTargetHotendTemperature(index, temperature)
  247. if self._target_hotend_temperatures[index] != temperature:
  248. self._target_hotend_temperatures[index] = temperature
  249. self.targetHotendTemperaturesChanged.emit()
  250. ## Implementation function of setTargetHotendTemperature.
  251. # /param index Index of the hotend to set the temperature of
  252. # /param temperature Temperature to set the hotend to (in deg C)
  253. # /sa setTargetHotendTemperature
  254. def _setTargetHotendTemperature(self, index, temperature):
  255. Logger.log("w", "_setTargetHotendTemperature is not implemented by this output device")
  256. @pyqtProperty("QVariantList", notify = targetHotendTemperaturesChanged)
  257. def targetHotendTemperatures(self):
  258. return self._target_hotend_temperatures
  259. @pyqtProperty("QVariantList", notify = hotendTemperaturesChanged)
  260. def hotendTemperatures(self):
  261. return self._hotend_temperatures
  262. ## Protected setter for the current hotend temperature.
  263. # This simply sets the hotend temperature, but ensures that a signal is emitted.
  264. # /param index Index of the hotend
  265. # /param temperature temperature of the hotend (in deg C)
  266. def _setHotendTemperature(self, index, temperature):
  267. if self._hotend_temperatures[index] != temperature:
  268. self._hotend_temperatures[index] = temperature
  269. self.hotendTemperaturesChanged.emit()
  270. @pyqtProperty("QVariantList", notify = materialIdChanged)
  271. def materialIds(self):
  272. return self._material_ids
  273. @pyqtProperty("QVariantList", notify = materialIdChanged)
  274. def materialNames(self):
  275. result = []
  276. for material_id in self._material_ids:
  277. if material_id is None:
  278. result.append(i18n_catalog.i18nc("@item:material", "No material loaded"))
  279. continue
  280. containers = self._container_registry.findInstanceContainers(type = "material", GUID = material_id)
  281. if containers:
  282. result.append(containers[0].getName())
  283. else:
  284. result.append(i18n_catalog.i18nc("@item:material", "Unknown material"))
  285. return result
  286. ## List of the colours of the currently loaded materials.
  287. #
  288. # The list is in order of extruders. If there is no material in an
  289. # extruder, the colour is shown as transparent.
  290. #
  291. # The colours are returned in hex-format AARRGGBB or RRGGBB
  292. # (e.g. #800000ff for transparent blue or #00ff00 for pure green).
  293. @pyqtProperty("QVariantList", notify = materialIdChanged)
  294. def materialColors(self):
  295. result = []
  296. for material_id in self._material_ids:
  297. if material_id is None:
  298. result.append("#00000000") #No material.
  299. continue
  300. containers = self._container_registry.findInstanceContainers(type = "material", GUID = material_id)
  301. if containers:
  302. result.append(containers[0].getMetaDataEntry("color_code"))
  303. else:
  304. result.append("#00000000") #Unknown material.
  305. return result
  306. ## Protected setter for the current material id.
  307. # /param index Index of the extruder
  308. # /param material_id id of the material
  309. def _setMaterialId(self, index, material_id):
  310. if material_id and material_id != "" and material_id != self._material_ids[index]:
  311. Logger.log("d", "Setting material id of hotend %d to %s" % (index, material_id))
  312. self._material_ids[index] = material_id
  313. self.materialIdChanged.emit(index, material_id)
  314. @pyqtProperty("QVariantList", notify = hotendIdChanged)
  315. def hotendIds(self):
  316. return self._hotend_ids
  317. ## Protected setter for the current hotend id.
  318. # /param index Index of the extruder
  319. # /param hotend_id id of the hotend
  320. def _setHotendId(self, index, hotend_id):
  321. if hotend_id and hotend_id != "" and hotend_id != self._hotend_ids[index]:
  322. Logger.log("d", "Setting hotend id of hotend %d to %s" % (index, hotend_id))
  323. self._hotend_ids[index] = hotend_id
  324. self.hotendIdChanged.emit(index, hotend_id)
  325. ## Let the user decide if the hotends and/or material should be synced with the printer
  326. # NB: the UX needs to be implemented by the plugin
  327. def materialHotendChangedMessage(self, callback):
  328. Logger.log("w", "materialHotendChangedMessage needs to be implemented, returning 'Yes'")
  329. callback(QMessageBox.Yes)
  330. ## Attempt to establish connection
  331. def connect(self):
  332. raise NotImplementedError("connect needs to be implemented")
  333. ## Attempt to close the connection
  334. def close(self):
  335. raise NotImplementedError("close needs to be implemented")
  336. @pyqtProperty(bool, notify = connectionStateChanged)
  337. def connectionState(self):
  338. return self._connection_state
  339. ## Set the connection state of this output device.
  340. # /param connection_state ConnectionState enum.
  341. def setConnectionState(self, connection_state):
  342. if self._connection_state != connection_state:
  343. self._connection_state = connection_state
  344. self.connectionStateChanged.emit(self._id)
  345. @pyqtProperty(str, notify = connectionTextChanged)
  346. def connectionText(self):
  347. return self._connection_text
  348. ## Set a text that is shown on top of the print monitor tab
  349. def setConnectionText(self, connection_text):
  350. if self._connection_text != connection_text:
  351. self._connection_text = connection_text
  352. self.connectionTextChanged.emit()
  353. ## Ensure that close gets called when object is destroyed
  354. def __del__(self):
  355. self.close()
  356. ## Get the x position of the head.
  357. # This function is "final" (do not re-implement)
  358. @pyqtProperty(float, notify = headPositionChanged)
  359. def headX(self):
  360. return self._head_x
  361. ## Get the y position of the head.
  362. # This function is "final" (do not re-implement)
  363. @pyqtProperty(float, notify = headPositionChanged)
  364. def headY(self):
  365. return self._head_y
  366. ## Get the z position of the head.
  367. # In some machines it's actually the bed that moves. For convenience sake we simply see it all as head movements.
  368. # This function is "final" (do not re-implement)
  369. @pyqtProperty(float, notify = headPositionChanged)
  370. def headZ(self):
  371. return self._head_z
  372. ## Update the saved position of the head
  373. # This function should be called when a new position for the head is received.
  374. def _updateHeadPosition(self, x, y ,z):
  375. position_changed = False
  376. if self._head_x != x:
  377. self._head_x = x
  378. position_changed = True
  379. if self._head_y != y:
  380. self._head_y = y
  381. position_changed = True
  382. if self._head_z != z:
  383. self._head_z = z
  384. position_changed = True
  385. if position_changed:
  386. self.headPositionChanged.emit()
  387. ## Set the position of the head.
  388. # In some machines it's actually the bed that moves. For convenience sake we simply see it all as head movements.
  389. # This function is "final" (do not re-implement)
  390. # /param x new x location of the head.
  391. # /param y new y location of the head.
  392. # /param z new z location of the head.
  393. # /param speed Speed by which it needs to move (in mm/minute)
  394. # /sa _setHeadPosition implementation function
  395. @pyqtSlot("long", "long", "long")
  396. @pyqtSlot("long", "long", "long", "long")
  397. def setHeadPosition(self, x, y, z, speed = 3000):
  398. self._setHeadPosition(x, y , z, speed)
  399. ## Set the X position of the head.
  400. # This function is "final" (do not re-implement)
  401. # /param x x position head needs to move to.
  402. # /param speed Speed by which it needs to move (in mm/minute)
  403. # /sa _setHeadx implementation function
  404. @pyqtSlot("long")
  405. @pyqtSlot("long", "long")
  406. def setHeadX(self, x, speed = 3000):
  407. self._setHeadX(x, speed)
  408. ## Set the Y position of the head.
  409. # This function is "final" (do not re-implement)
  410. # /param y y position head needs to move to.
  411. # /param speed Speed by which it needs to move (in mm/minute)
  412. # /sa _setHeadY implementation function
  413. @pyqtSlot("long")
  414. @pyqtSlot("long", "long")
  415. def setHeadY(self, y, speed = 3000):
  416. self._setHeadY(y, speed)
  417. ## Set the Z position of the head.
  418. # In some machines it's actually the bed that moves. For convenience sake we simply see it all as head movements.
  419. # This function is "final" (do not re-implement)
  420. # /param z z position head needs to move to.
  421. # /param speed Speed by which it needs to move (in mm/minute)
  422. # /sa _setHeadZ implementation function
  423. @pyqtSlot("long")
  424. @pyqtSlot("long", "long")
  425. def setHeadZ(self, z, speed = 3000):
  426. self._setHeadY(z, speed)
  427. ## Move the head of the printer.
  428. # Note that this is a relative move. If you want to move the head to a specific position you can use
  429. # setHeadPosition
  430. # This function is "final" (do not re-implement)
  431. # /param x distance in x to move
  432. # /param y distance in y to move
  433. # /param z distance in z to move
  434. # /param speed Speed by which it needs to move (in mm/minute)
  435. # /sa _moveHead implementation function
  436. @pyqtSlot("long", "long", "long")
  437. @pyqtSlot("long", "long", "long", "long")
  438. def moveHead(self, x = 0, y = 0, z = 0, speed = 3000):
  439. self._moveHead(x, y, z, speed)
  440. ## Implementation function of moveHead.
  441. # /param x distance in x to move
  442. # /param y distance in y to move
  443. # /param z distance in z to move
  444. # /param speed Speed by which it needs to move (in mm/minute)
  445. # /sa moveHead
  446. def _moveHead(self, x, y, z, speed):
  447. Logger.log("w", "_moveHead is not implemented by this output device")
  448. ## Implementation function of setHeadPosition.
  449. # /param x new x location of the head.
  450. # /param y new y location of the head.
  451. # /param z new z location of the head.
  452. # /param speed Speed by which it needs to move (in mm/minute)
  453. # /sa setHeadPosition
  454. def _setHeadPosition(self, x, y, z, speed):
  455. Logger.log("w", "_setHeadPosition is not implemented by this output device")
  456. ## Implementation function of setHeadX.
  457. # /param x new x location of the head.
  458. # /param speed Speed by which it needs to move (in mm/minute)
  459. # /sa setHeadX
  460. def _setHeadX(self, x, speed):
  461. Logger.log("w", "_setHeadX is not implemented by this output device")
  462. ## Implementation function of setHeadY.
  463. # /param y new y location of the head.
  464. # /param speed Speed by which it needs to move (in mm/minute)
  465. # /sa _setHeadY
  466. def _setHeadY(self, y, speed):
  467. Logger.log("w", "_setHeadY is not implemented by this output device")
  468. ## Implementation function of setHeadZ.
  469. # /param z new z location of the head.
  470. # /param speed Speed by which it needs to move (in mm/minute)
  471. # /sa _setHeadZ
  472. def _setHeadZ(self, z, speed):
  473. Logger.log("w", "_setHeadZ is not implemented by this output device")
  474. ## Get the progress of any currently active process.
  475. # This function is "final" (do not re-implement)
  476. # /sa _getProgress
  477. # /returns float progress of the process. -1 indicates that there is no process.
  478. @pyqtProperty(float, notify = progressChanged)
  479. def progress(self):
  480. return self._progress
  481. ## Set the progress of any currently active process
  482. # /param progress Progress of the process.
  483. def setProgress(self, progress):
  484. if self._progress != progress:
  485. self._progress = progress
  486. self.progressChanged.emit()
  487. ## The current processing state of the backend.
  488. class ConnectionState(IntEnum):
  489. closed = 0
  490. connecting = 1
  491. connected = 2
  492. busy = 3
  493. error = 4