PrintInformation.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from PyQt5.QtCore import QObject, QDateTime, QTimer, pyqtSignal, pyqtSlot, pyqtProperty
  4. from UM.Application import Application
  5. from UM.Settings.MachineSettings import MachineSettings
  6. from UM.Resources import Resources
  7. from UM.Scene.SceneNode import SceneNode
  8. from UM.Qt.Duration import Duration
  9. ## A class for processing and calculating minimum, currrent and maximum print time.
  10. #
  11. # This class contains all the logic relating to calculation and slicing for the
  12. # time/quality slider concept. It is a rather tricky combination of event handling
  13. # and state management. The logic behind this is as follows:
  14. #
  15. # - A scene change or settting change event happens.
  16. # We track what the source was of the change, either a scene change, a setting change, an active machine change or something else.
  17. # - This triggers a new slice with the current settings - this is the "current settings pass".
  18. # - When the slice is done, we update the current print time and material amount.
  19. # - If the source of the slice was not a Setting change, we start the second slice pass, the "low quality settings pass". Otherwise we stop here.
  20. # - When that is done, we update the minimum print time and start the final slcice pass, the "high quality settings pass".
  21. # - When the high quality pass is done, we update the maximum print time.
  22. #
  23. class PrintInformation(QObject):
  24. class SlicePass:
  25. CurrentSettings = 1
  26. LowQualitySettings = 2
  27. HighQualitySettings = 3
  28. class SliceReason:
  29. SceneChanged = 1
  30. SettingChanged = 2
  31. ActiveMachineChanged = 3
  32. Other = 4
  33. def __init__(self, parent = None):
  34. super().__init__(parent)
  35. self._minimum_print_time = Duration(None, self)
  36. self._current_print_time = Duration(None, self)
  37. self._maximum_print_time = Duration(None, self)
  38. self._material_amount = -1
  39. self._time_quality_value = 50
  40. self._time_quality_changed_timer = QTimer()
  41. self._time_quality_changed_timer.setInterval(500)
  42. self._time_quality_changed_timer.setSingleShot(True)
  43. self._time_quality_changed_timer.timeout.connect(self._updateTimeQualitySettings)
  44. self._interpolation_settings = {
  45. "layer_height": { "minimum": "low", "maximum": "high", "curve": "linear" },
  46. "fill_sparse_density": { "minimum": "low", "maximum": "high", "curve": "linear" }
  47. }
  48. self._low_quality_settings = None
  49. self._current_settings = None
  50. self._high_quality_settings = None
  51. self._slice_pass = None
  52. self._slice_reason = None
  53. Application.getInstance().activeMachineChanged.connect(self._onActiveMachineChanged)
  54. self._onActiveMachineChanged()
  55. Application.getInstance().getController().getScene().sceneChanged.connect(self._onSceneChanged)
  56. self._backend = Application.getInstance().getBackend()
  57. if self._backend:
  58. self._backend.printDurationMessage.connect(self._onPrintDurationMessage)
  59. self._backend.slicingStarted.connect(self._onSlicingStarted)
  60. self._backend.slicingCancelled.connect(self._onSlicingCancelled)
  61. minimumPrintTimeChanged = pyqtSignal()
  62. @pyqtProperty(Duration, notify = minimumPrintTimeChanged)
  63. def minimumPrintTime(self):
  64. return self._minimum_print_time
  65. currentPrintTimeChanged = pyqtSignal()
  66. @pyqtProperty(Duration, notify = currentPrintTimeChanged)
  67. def currentPrintTime(self):
  68. return self._current_print_time
  69. maximumPrintTimeChanged = pyqtSignal()
  70. @pyqtProperty(Duration, notify = maximumPrintTimeChanged)
  71. def maximumPrintTime(self):
  72. return self._maximum_print_time
  73. materialAmountChanged = pyqtSignal()
  74. @pyqtProperty(float, notify = materialAmountChanged)
  75. def materialAmount(self):
  76. return self._material_amount
  77. timeQualityValueChanged = pyqtSignal()
  78. @pyqtProperty(int, notify = timeQualityValueChanged)
  79. def timeQualityValue(self):
  80. return self._time_quality_value
  81. @pyqtSlot(int)
  82. def setTimeQualityValue(self, value):
  83. if value != self._time_quality_value:
  84. self._time_quality_value = value
  85. self.timeQualityValueChanged.emit()
  86. self._time_quality_changed_timer.start()
  87. def _onSlicingStarted(self):
  88. if self._slice_pass is None:
  89. self._slice_pass = self.SlicePass.CurrentSettings
  90. if self._slice_reason is None:
  91. self._slice_reason = self.SliceReason.Other
  92. if self._slice_pass == self.SlicePass.CurrentSettings and self._slice_reason != self.SliceReason.SettingChanged:
  93. self._minimum_print_time.setDuration(-1)
  94. self.minimumPrintTimeChanged.emit()
  95. self._maximum_print_time.setDuration(-1)
  96. self.maximumPrintTimeChanged.emit()
  97. def _onPrintDurationMessage(self, time, amount):
  98. if self._slice_pass == self.SlicePass.CurrentSettings:
  99. self._current_print_time.setDuration(time)
  100. self.currentPrintTimeChanged.emit()
  101. self._material_amount = round(amount / 10) / 100
  102. self.materialAmountChanged.emit()
  103. if self._slice_reason != self.SliceReason.SettingChanged:
  104. self._slice_pass = self.SlicePass.LowQualitySettings
  105. self._backend.slice(settings = self._low_quality_settings, save_gcode = False, save_polygons = False, force_restart = False, report_progress = False)
  106. else:
  107. self._slice_pass = None
  108. self._slice_reason = None
  109. elif self._slice_pass == self.SlicePass.LowQualitySettings:
  110. self._minimum_print_time.setDuration(time)
  111. self.minimumPrintTimeChanged.emit()
  112. self._slice_pass = self.SlicePass.HighQualitySettings
  113. self._backend.slice(settings = self._high_quality_settings, save_gcode = False, save_polygons = False, force_restart = False, report_progress = False)
  114. elif self._slice_pass == self.SlicePass.HighQualitySettings:
  115. self._maximum_print_time.setDuration(time)
  116. self.maximumPrintTimeChanged.emit()
  117. self._slice_pass = None
  118. self._slice_reason = None
  119. def _onActiveMachineChanged(self):
  120. if self._current_settings:
  121. self._current_settings.settingChanged.disconnect(self._onSettingChanged)
  122. self._current_settings = Application.getInstance().getActiveMachine()
  123. if self._current_settings:
  124. self._current_settings.settingChanged.connect(self._onSettingChanged)
  125. self._low_quality_settings = None
  126. self._high_quality_settings = None
  127. self._updateTimeQualitySettings()
  128. self._slice_reason = self.SliceReason.ActiveMachineChanged
  129. def _updateTimeQualitySettings(self):
  130. if not self._current_settings:
  131. return
  132. if not self._low_quality_settings:
  133. self._low_quality_settings = MachineSettings()
  134. self._low_quality_settings.loadSettingsFromFile(Resources.getPath(Resources.SettingsLocation, self._current_settings.getTypeID() + ".json"))
  135. self._low_quality_settings.loadValuesFromFile(Resources.getPath(Resources.SettingsLocation, "profiles", "low_quality.conf"))
  136. if not self._high_quality_settings:
  137. self._high_quality_settings = MachineSettings()
  138. self._high_quality_settings.loadSettingsFromFile(Resources.getPath(Resources.SettingsLocation, self._current_settings.getTypeID() + ".json"))
  139. self._high_quality_settings.loadValuesFromFile(Resources.getPath(Resources.SettingsLocation, "profiles", "high_quality.conf"))
  140. for key, options in self._interpolation_settings.items():
  141. minimum_value = None
  142. if options["minimum"] == "low":
  143. minimum_value = self._low_quality_settings.getSettingValueByKey(key)
  144. elif options["minimum"] == "high":
  145. minimum_value = self._high_quality_settings.getSettingValueByKey(key)
  146. else:
  147. continue
  148. maximum_value = None
  149. if options["maximum"] == "low":
  150. maximum_value = self._low_quality_settings.getSettingValueByKey(key)
  151. elif options["maximum"] == "high":
  152. maximum_value = self._high_quality_settings.getSettingValueByKey(key)
  153. else:
  154. continue
  155. setting_value = minimum_value + (maximum_value - minimum_value) * (self._time_quality_value / 100)
  156. self._current_settings.setSettingValueByKey(key, setting_value)
  157. def _onSceneChanged(self, source):
  158. self._slice_reason = self.SliceReason.SceneChanged
  159. def _onSettingChanged(self, source):
  160. self._slice_reason = self.SliceReason.SettingChanged
  161. def _onSlicingCancelled(self):
  162. self._slice_pass = None