PrinterOutputDevice.py 23 KB

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