SimulationViewProxy.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import TYPE_CHECKING
  4. import numpy
  5. from PyQt6.QtCore import QObject, pyqtSignal, pyqtProperty
  6. from UM.FlameProfiler import pyqtSlot
  7. from UM.Application import Application
  8. if TYPE_CHECKING:
  9. from .SimulationView import SimulationView
  10. class SimulationViewProxy(QObject):
  11. S_TO_MS = 1000
  12. SPEED_OF_SIMULATION = 10
  13. FACTOR = S_TO_MS/SPEED_OF_SIMULATION
  14. def __init__(self, simulation_view: "SimulationView", parent=None) -> None:
  15. super().__init__(parent)
  16. self._simulation_view = simulation_view
  17. self._current_layer = 0
  18. self._controller = Application.getInstance().getController()
  19. self._controller.activeViewChanged.connect(self._onActiveViewChanged)
  20. self.is_simulationView_selected = False
  21. self._onActiveViewChanged()
  22. currentLayerChanged = pyqtSignal()
  23. currentPathChanged = pyqtSignal()
  24. maxLayersChanged = pyqtSignal()
  25. maxPathsChanged = pyqtSignal()
  26. activityChanged = pyqtSignal()
  27. globalStackChanged = pyqtSignal()
  28. preferencesChanged = pyqtSignal()
  29. busyChanged = pyqtSignal()
  30. colorSchemeLimitsChanged = pyqtSignal()
  31. @pyqtProperty(bool, notify=activityChanged)
  32. def layerActivity(self):
  33. return self._simulation_view.getActivity()
  34. @pyqtProperty(int, notify=maxLayersChanged)
  35. def numLayers(self):
  36. return self._simulation_view.getMaxLayers()
  37. @pyqtProperty(int, notify=currentLayerChanged)
  38. def currentLayer(self):
  39. return self._simulation_view.getCurrentLayer()
  40. @pyqtProperty(int, notify=currentLayerChanged)
  41. def minimumLayer(self):
  42. return self._simulation_view.getMinimumLayer()
  43. @pyqtProperty(int, notify=maxPathsChanged)
  44. def numPaths(self):
  45. return self._simulation_view.getMaxPaths()
  46. @pyqtProperty(int, notify=currentPathChanged)
  47. def currentPath(self):
  48. return self._simulation_view.getCurrentPath()
  49. @pyqtProperty(int, notify=currentPathChanged)
  50. def simulationTime(self):
  51. # Extracts the currents paths simulation time (in seconds) for the current path from the dict of simulation time of the current layer.
  52. # We multiply the time with 100 to make it to ms from s.(Should be 1000 in real time). This scaling makes the simulation time 10x faster than the real time.
  53. simulationTimeOfpath = self._simulation_view.getSimulationTime(self._simulation_view.getCurrentPath()) * SimulationViewProxy.FACTOR
  54. # Since the timer cannot process time less than 1 ms, we put a lower limit here
  55. return int(max(1, simulationTimeOfpath))
  56. @pyqtProperty(int, notify=currentPathChanged)
  57. def minimumPath(self):
  58. return self._simulation_view.getMinimumPath()
  59. @pyqtProperty(bool, notify=busyChanged)
  60. def busy(self):
  61. return self._simulation_view.isBusy()
  62. @pyqtProperty(bool, notify=preferencesChanged)
  63. def compatibilityMode(self):
  64. return self._simulation_view.getCompatibilityMode()
  65. @pyqtProperty(int, notify=globalStackChanged)
  66. def extruderCount(self):
  67. return self._simulation_view.getExtruderCount()
  68. @pyqtSlot(int)
  69. def setCurrentLayer(self, layer_num):
  70. self._simulation_view.setLayer(layer_num)
  71. @pyqtSlot(int)
  72. def setMinimumLayer(self, layer_num):
  73. self._simulation_view.setMinimumLayer(layer_num)
  74. @pyqtSlot(int)
  75. def setCurrentPath(self, path_num):
  76. self._simulation_view.setPath(path_num)
  77. @pyqtSlot(int)
  78. def setMinimumPath(self, path_num):
  79. self._simulation_view.setMinimumPath(path_num)
  80. @pyqtSlot(int)
  81. def setSimulationViewType(self, layer_view_type):
  82. self._simulation_view.setSimulationViewType(layer_view_type)
  83. @pyqtSlot(result=int)
  84. def getSimulationViewType(self):
  85. return self._simulation_view.getSimulationViewType()
  86. @pyqtSlot(bool)
  87. def setSimulationRunning(self, running):
  88. self._simulation_view.setSimulationRunning(running)
  89. @pyqtSlot(result=bool)
  90. def getSimulationRunning(self):
  91. return self._simulation_view.isSimulationRunning()
  92. @pyqtProperty(float, notify = colorSchemeLimitsChanged)
  93. def minFeedrate(self):
  94. return self._simulation_view.getMinFeedrate()
  95. @pyqtProperty(float, notify = colorSchemeLimitsChanged)
  96. def maxFeedrate(self):
  97. return self._simulation_view.getMaxFeedrate()
  98. @pyqtProperty(float, notify = colorSchemeLimitsChanged)
  99. def minThickness(self):
  100. return self._simulation_view.getMinThickness()
  101. @pyqtProperty(float, notify = colorSchemeLimitsChanged)
  102. def maxThickness(self):
  103. return self._simulation_view.getMaxThickness()
  104. @pyqtProperty(float, notify = colorSchemeLimitsChanged)
  105. def maxLineWidth(self):
  106. return self._simulation_view.getMaxLineWidth()
  107. @pyqtProperty(float, notify = colorSchemeLimitsChanged)
  108. def minLineWidth(self):
  109. return self._simulation_view.getMinLineWidth()
  110. @pyqtProperty(float, notify=colorSchemeLimitsChanged)
  111. def maxFlowRate(self):
  112. return self._simulation_view.getMaxFlowRate()
  113. @pyqtProperty(float, notify=colorSchemeLimitsChanged)
  114. def minFlowRate(self):
  115. return self._simulation_view.getMinFlowRate()
  116. # Opacity 0..1
  117. @pyqtSlot(int, float)
  118. def setExtruderOpacity(self, extruder_nr, opacity):
  119. self._simulation_view.setExtruderOpacity(extruder_nr, opacity)
  120. @pyqtSlot(int)
  121. def setShowTravelMoves(self, show):
  122. self._simulation_view.setShowTravelMoves(show)
  123. @pyqtSlot(int)
  124. def setShowHelpers(self, show):
  125. self._simulation_view.setShowHelpers(show)
  126. @pyqtSlot(int)
  127. def setShowSkin(self, show):
  128. self._simulation_view.setShowSkin(show)
  129. @pyqtSlot(int)
  130. def setShowInfill(self, show):
  131. self._simulation_view.setShowInfill(show)
  132. def _layerActivityChanged(self):
  133. self.activityChanged.emit()
  134. def _onLayerChanged(self):
  135. self.currentLayerChanged.emit()
  136. self._layerActivityChanged()
  137. def _onColorSchemeLimitsChanged(self):
  138. self.colorSchemeLimitsChanged.emit()
  139. def _onPathChanged(self):
  140. self.currentPathChanged.emit()
  141. self._layerActivityChanged()
  142. scene = Application.getInstance().getController().getScene()
  143. scene.sceneChanged.emit(scene.getRoot())
  144. def _onMaxLayersChanged(self):
  145. self.maxLayersChanged.emit()
  146. def _onMaxPathsChanged(self):
  147. self.maxPathsChanged.emit()
  148. def _onBusyChanged(self):
  149. self.busyChanged.emit()
  150. def _onActivityChanged(self):
  151. self.activityChanged.emit()
  152. def _onGlobalStackChanged(self):
  153. self.globalStackChanged.emit()
  154. def _onPreferencesChanged(self):
  155. self.preferencesChanged.emit()
  156. def _onActiveViewChanged(self):
  157. active_view = self._controller.getActiveView()
  158. if active_view == self._simulation_view:
  159. self._simulation_view.currentLayerNumChanged.connect(self._onLayerChanged)
  160. self._simulation_view.colorSchemeLimitsChanged.connect(self._onColorSchemeLimitsChanged)
  161. self._simulation_view.currentPathNumChanged.connect(self._onPathChanged)
  162. self._simulation_view.maxLayersChanged.connect(self._onMaxLayersChanged)
  163. self._simulation_view.maxPathsChanged.connect(self._onMaxPathsChanged)
  164. self._simulation_view.busyChanged.connect(self._onBusyChanged)
  165. self._simulation_view.activityChanged.connect(self._onActivityChanged)
  166. self._simulation_view.globalStackChanged.connect(self._onGlobalStackChanged)
  167. self._simulation_view.preferencesChanged.connect(self._onPreferencesChanged)
  168. self.is_simulationView_selected = True
  169. elif self.is_simulationView_selected:
  170. # Disconnect all of em again.
  171. self.is_simulationView_selected = False
  172. self._simulation_view.currentLayerNumChanged.disconnect(self._onLayerChanged)
  173. self._simulation_view.colorSchemeLimitsChanged.connect(self._onColorSchemeLimitsChanged)
  174. self._simulation_view.currentPathNumChanged.disconnect(self._onPathChanged)
  175. self._simulation_view.maxLayersChanged.disconnect(self._onMaxLayersChanged)
  176. self._simulation_view.maxPathsChanged.disconnect(self._onMaxPathsChanged)
  177. self._simulation_view.busyChanged.disconnect(self._onBusyChanged)
  178. self._simulation_view.activityChanged.disconnect(self._onActivityChanged)
  179. self._simulation_view.globalStackChanged.disconnect(self._onGlobalStackChanged)
  180. self._simulation_view.preferencesChanged.disconnect(self._onPreferencesChanged)