PrinterOutputDevice.py 22 KB

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