MachineManager.py 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import collections
  4. import time
  5. #Type hinting.
  6. from typing import Union, List, Dict, TYPE_CHECKING, Optional
  7. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  8. from UM.Signal import Signal
  9. from PyQt5.QtCore import QObject, pyqtProperty, pyqtSignal, QTimer
  10. import UM.FlameProfiler
  11. from UM.FlameProfiler import pyqtSlot
  12. from UM import Util
  13. from UM.Application import Application
  14. from UM.Preferences import Preferences
  15. from UM.Logger import Logger
  16. from UM.Message import Message
  17. from UM.Settings.ContainerRegistry import ContainerRegistry
  18. from UM.Settings.SettingFunction import SettingFunction
  19. from UM.Signal import postponeSignals, CompressTechnique
  20. from cura.Machines.QualityManager import getMachineDefinitionIDForQualitySearch
  21. from cura.Machines.VariantManager import VariantType
  22. from cura.PrinterOutputDevice import PrinterOutputDevice
  23. from cura.PrinterOutput.ConfigurationModel import ConfigurationModel
  24. from cura.PrinterOutput.ExtruderConfigurationModel import ExtruderConfigurationModel
  25. from cura.PrinterOutput.MaterialOutputModel import MaterialOutputModel
  26. from cura.Settings.ExtruderManager import ExtruderManager
  27. from .CuraStackBuilder import CuraStackBuilder
  28. from UM.i18n import i18nCatalog
  29. catalog = i18nCatalog("cura")
  30. if TYPE_CHECKING:
  31. from cura.Settings.CuraContainerStack import CuraContainerStack
  32. from cura.Settings.GlobalStack import GlobalStack
  33. class MachineManager(QObject):
  34. def __init__(self, parent = None):
  35. super().__init__(parent)
  36. self._active_container_stack = None # type: CuraContainerStack
  37. self._global_container_stack = None # type: GlobalStack
  38. self._current_root_material_id = {}
  39. self._current_root_material_name = {}
  40. self._current_quality_group = None
  41. self._current_quality_changes_group = None
  42. self.machine_extruder_material_update_dict = collections.defaultdict(list)
  43. self._error_check_timer = QTimer()
  44. self._error_check_timer.setInterval(250)
  45. self._error_check_timer.setSingleShot(True)
  46. self._error_check_timer.timeout.connect(self._updateStacksHaveErrors)
  47. self._instance_container_timer = QTimer()
  48. self._instance_container_timer.setInterval(250)
  49. self._instance_container_timer.setSingleShot(True)
  50. self._instance_container_timer.timeout.connect(self.__emitChangedSignals)
  51. self._application = Application.getInstance()
  52. self._application.globalContainerStackChanged.connect(self._onGlobalContainerChanged)
  53. self._application.getContainerRegistry().containerLoadComplete.connect(self._onInstanceContainersChanged)
  54. ## When the global container is changed, active material probably needs to be updated.
  55. self.globalContainerChanged.connect(self.activeMaterialChanged)
  56. self.globalContainerChanged.connect(self.activeVariantChanged)
  57. self.globalContainerChanged.connect(self.activeQualityChanged)
  58. self.globalContainerChanged.connect(self.activeQualityChangesGroupChanged)
  59. self.globalContainerChanged.connect(self.activeQualityGroupChanged)
  60. self._stacks_have_errors = None # type:Optional[bool]
  61. self._empty_definition_changes_container = ContainerRegistry.getInstance().findContainers(id = "empty_definition_changes")[0]
  62. self._empty_variant_container = ContainerRegistry.getInstance().findContainers(id = "empty_variant")[0]
  63. self._empty_material_container = ContainerRegistry.getInstance().findContainers(id = "empty_material")[0]
  64. self._empty_quality_container = ContainerRegistry.getInstance().findContainers(id = "empty_quality")[0]
  65. self._empty_quality_changes_container = ContainerRegistry.getInstance().findContainers(id = "empty_quality_changes")[0]
  66. self._onGlobalContainerChanged()
  67. ExtruderManager.getInstance().activeExtruderChanged.connect(self._onActiveExtruderStackChanged)
  68. self._onActiveExtruderStackChanged()
  69. ExtruderManager.getInstance().activeExtruderChanged.connect(self.activeMaterialChanged)
  70. ExtruderManager.getInstance().activeExtruderChanged.connect(self.activeVariantChanged)
  71. ExtruderManager.getInstance().activeExtruderChanged.connect(self.activeQualityChanged)
  72. self.globalContainerChanged.connect(self.activeStackChanged)
  73. self.globalValueChanged.connect(self.activeStackValueChanged)
  74. ExtruderManager.getInstance().activeExtruderChanged.connect(self.activeStackChanged)
  75. self.activeStackChanged.connect(self.activeStackValueChanged)
  76. Preferences.getInstance().addPreference("cura/active_machine", "")
  77. self._global_event_keys = set()
  78. self._printer_output_devices = []
  79. Application.getInstance().getOutputDeviceManager().outputDevicesChanged.connect(self._onOutputDevicesChanged)
  80. # There might already be some output devices by the time the signal is connected
  81. self._onOutputDevicesChanged()
  82. self._current_printer_configuration = ConfigurationModel() # Indicates the current configuration setup in this printer
  83. self.activeMaterialChanged.connect(self._onCurrentConfigurationChanged)
  84. self.activeVariantChanged.connect(self._onCurrentConfigurationChanged)
  85. # Force to compute the current configuration
  86. self._onCurrentConfigurationChanged()
  87. self._application.callLater(self.setInitialActiveMachine)
  88. self._material_incompatible_message = Message(catalog.i18nc("@info:status",
  89. "The selected material is incompatible with the selected machine or configuration."),
  90. title = catalog.i18nc("@info:title", "Incompatible Material"))
  91. containers = ContainerRegistry.getInstance().findInstanceContainers(id = self.activeMaterialId)
  92. if containers:
  93. containers[0].nameChanged.connect(self._onMaterialNameChanged)
  94. self._material_manager = self._application.getMaterialManager()
  95. self._variant_manager = self._application.getVariantManager()
  96. self._quality_manager = self._application.getQualityManager()
  97. # When the materials lookup table gets updated, it can mean that a material has its name changed, which should
  98. # be reflected on the GUI. This signal emission makes sure that it happens.
  99. self._material_manager.materialsUpdated.connect(self.rootMaterialChanged)
  100. activeQualityGroupChanged = pyqtSignal()
  101. activeQualityChangesGroupChanged = pyqtSignal()
  102. globalContainerChanged = pyqtSignal() # Emitted whenever the global stack is changed (ie: when changing between printers, changing a global profile, but not when changing a value)
  103. activeMaterialChanged = pyqtSignal()
  104. activeVariantChanged = pyqtSignal()
  105. activeQualityChanged = pyqtSignal()
  106. activeStackChanged = pyqtSignal() # Emitted whenever the active stack is changed (ie: when changing between extruders, changing a profile, but not when changing a value)
  107. globalValueChanged = pyqtSignal() # Emitted whenever a value inside global container is changed.
  108. activeStackValueChanged = pyqtSignal() # Emitted whenever a value inside the active stack is changed.
  109. activeStackValidationChanged = pyqtSignal() # Emitted whenever a validation inside active container is changed
  110. stacksValidationChanged = pyqtSignal() # Emitted whenever a validation is changed
  111. blurSettings = pyqtSignal() # Emitted to force fields in the advanced sidebar to un-focus, so they update properly
  112. outputDevicesChanged = pyqtSignal()
  113. currentConfigurationChanged = pyqtSignal() # Emitted every time the current configurations of the machine changes
  114. rootMaterialChanged = pyqtSignal()
  115. def setInitialActiveMachine(self):
  116. active_machine_id = Preferences.getInstance().getValue("cura/active_machine")
  117. if active_machine_id != "" and ContainerRegistry.getInstance().findContainerStacksMetadata(id = active_machine_id):
  118. # An active machine was saved, so restore it.
  119. self.setActiveMachine(active_machine_id)
  120. # Make sure _active_container_stack is properly initiated
  121. ExtruderManager.getInstance().setActiveExtruderIndex(0)
  122. def _onOutputDevicesChanged(self) -> None:
  123. self._printer_output_devices = []
  124. for printer_output_device in Application.getInstance().getOutputDeviceManager().getOutputDevices():
  125. if isinstance(printer_output_device, PrinterOutputDevice):
  126. self._printer_output_devices.append(printer_output_device)
  127. self.outputDevicesChanged.emit()
  128. @pyqtProperty(QObject, notify = currentConfigurationChanged)
  129. def currentConfiguration(self):
  130. return self._current_printer_configuration
  131. def _onCurrentConfigurationChanged(self) -> None:
  132. if not self._global_container_stack:
  133. return
  134. # Create the configuration model with the current data in Cura
  135. self._current_printer_configuration.printerType = self._global_container_stack.definition.getName()
  136. self._current_printer_configuration.extruderConfigurations = []
  137. for extruder in self._global_container_stack.extruders.values():
  138. extruder_configuration = ExtruderConfigurationModel()
  139. # For compare just the GUID is needed at this moment
  140. mat_type = extruder.material.getMetaDataEntry("material") if extruder.material != self._empty_material_container else None
  141. mat_guid = extruder.material.getMetaDataEntry("GUID") if extruder.material != self._empty_material_container else None
  142. mat_color = extruder.material.getMetaDataEntry("color_name") if extruder.material != self._empty_material_container else None
  143. mat_brand = extruder.material.getMetaDataEntry("brand") if extruder.material != self._empty_material_container else None
  144. mat_name = extruder.material.getMetaDataEntry("name") if extruder.material != self._empty_material_container else None
  145. material_model = MaterialOutputModel(mat_guid, mat_type, mat_color, mat_brand, mat_name)
  146. extruder_configuration.position = int(extruder.getMetaDataEntry("position"))
  147. extruder_configuration.material = material_model
  148. extruder_configuration.hotendID = extruder.variant.getName() if extruder.variant != self._empty_variant_container else None
  149. self._current_printer_configuration.extruderConfigurations.append(extruder_configuration)
  150. self._current_printer_configuration.buildplateConfiguration = self._global_container_stack.getProperty("machine_buildplate_type", "value") if self._global_container_stack.variant != self._empty_variant_container else None
  151. self.currentConfigurationChanged.emit()
  152. @pyqtSlot(QObject, result = bool)
  153. def matchesConfiguration(self, configuration: ConfigurationModel) -> bool:
  154. # print(configuration)
  155. # print(self._current_printer_configuration)
  156. # print("%%%%%%%%", configuration == self._current_printer_configuration)
  157. return self._current_printer_configuration == configuration
  158. @pyqtProperty("QVariantList", notify = outputDevicesChanged)
  159. def printerOutputDevices(self):
  160. return self._printer_output_devices
  161. @pyqtProperty(int, constant=True)
  162. def totalNumberOfSettings(self) -> int:
  163. return len(ContainerRegistry.getInstance().findDefinitionContainers(id = "fdmprinter")[0].getAllKeys())
  164. def _onGlobalContainerChanged(self) -> None:
  165. if self._global_container_stack:
  166. try:
  167. self._global_container_stack.nameChanged.disconnect(self._onMachineNameChanged)
  168. except TypeError: # pyQtSignal gives a TypeError when disconnecting from something that was already disconnected.
  169. pass
  170. try:
  171. self._global_container_stack.containersChanged.disconnect(self._onInstanceContainersChanged)
  172. except TypeError:
  173. pass
  174. try:
  175. self._global_container_stack.propertyChanged.disconnect(self._onPropertyChanged)
  176. except TypeError:
  177. pass
  178. for extruder_stack in ExtruderManager.getInstance().getActiveExtruderStacks():
  179. extruder_stack.propertyChanged.disconnect(self._onPropertyChanged)
  180. extruder_stack.containersChanged.disconnect(self._onInstanceContainersChanged)
  181. # Update the local global container stack reference
  182. self._global_container_stack = Application.getInstance().getGlobalContainerStack()
  183. self.globalContainerChanged.emit()
  184. # after switching the global stack we reconnect all the signals and set the variant and material references
  185. if self._global_container_stack:
  186. Preferences.getInstance().setValue("cura/active_machine", self._global_container_stack.getId())
  187. self._global_container_stack.nameChanged.connect(self._onMachineNameChanged)
  188. self._global_container_stack.containersChanged.connect(self._onInstanceContainersChanged)
  189. self._global_container_stack.propertyChanged.connect(self._onPropertyChanged)
  190. # Global stack can have only a variant if it is a buildplate
  191. global_variant = self._global_container_stack.variant
  192. if global_variant != self._empty_variant_container:
  193. if global_variant.getMetaDataEntry("hardware_type") != "buildplate":
  194. self._global_container_stack.setVariant(self._empty_variant_container)
  195. # set the global material to empty as we now use the extruder stack at all times - CURA-4482
  196. global_material = self._global_container_stack.material
  197. if global_material != self._empty_material_container:
  198. self._global_container_stack.setMaterial(self._empty_material_container)
  199. # Listen for changes on all extruder stacks
  200. for extruder_stack in ExtruderManager.getInstance().getActiveExtruderStacks():
  201. extruder_stack.propertyChanged.connect(self._onPropertyChanged)
  202. extruder_stack.containersChanged.connect(self._onInstanceContainersChanged)
  203. if self._global_container_stack.getId() in self.machine_extruder_material_update_dict:
  204. for func in self.machine_extruder_material_update_dict[self._global_container_stack.getId()]:
  205. Application.getInstance().callLater(func)
  206. del self.machine_extruder_material_update_dict[self._global_container_stack.getId()]
  207. self.activeQualityGroupChanged.emit()
  208. self._error_check_timer.start()
  209. ## Update self._stacks_valid according to _checkStacksForErrors and emit if change.
  210. def _updateStacksHaveErrors(self) -> None:
  211. old_stacks_have_errors = self._stacks_have_errors
  212. self._stacks_have_errors = self._checkStacksHaveErrors()
  213. if old_stacks_have_errors != self._stacks_have_errors:
  214. self.stacksValidationChanged.emit()
  215. Application.getInstance().stacksValidationFinished.emit()
  216. def _onActiveExtruderStackChanged(self) -> None:
  217. self.blurSettings.emit() # Ensure no-one has focus.
  218. old_active_container_stack = self._active_container_stack
  219. self._active_container_stack = ExtruderManager.getInstance().getActiveExtruderStack()
  220. if old_active_container_stack != self._active_container_stack:
  221. # Many methods and properties related to the active quality actually depend
  222. # on _active_container_stack. If it changes, then the properties change.
  223. self.activeQualityChanged.emit()
  224. def __emitChangedSignals(self) -> None:
  225. self.activeQualityChanged.emit()
  226. self.activeVariantChanged.emit()
  227. self.activeMaterialChanged.emit()
  228. self.rootMaterialChanged.emit()
  229. self._error_check_timer.start()
  230. def _onInstanceContainersChanged(self, container) -> None:
  231. self._instance_container_timer.start()
  232. def _onPropertyChanged(self, key: str, property_name: str) -> None:
  233. if property_name == "value":
  234. # Notify UI items, such as the "changed" star in profile pull down menu.
  235. self.activeStackValueChanged.emit()
  236. elif property_name == "validationState":
  237. self._error_check_timer.start()
  238. ## Given a global_stack, make sure that it's all valid by searching for this quality group and applying it again
  239. def _initMachineState(self, global_stack):
  240. material_dict = {}
  241. for position, extruder in global_stack.extruders.items():
  242. material_dict[position] = extruder.material.getMetaDataEntry("base_file")
  243. self._current_root_material_id = material_dict
  244. global_quality = global_stack.quality
  245. quality_type = global_quality.getMetaDataEntry("quality_type")
  246. global_quality_changes = global_stack.qualityChanges
  247. global_quality_changes_name = global_quality_changes.getName()
  248. if global_quality_changes.getId() != "empty_quality_changes":
  249. quality_changes_groups = self._application._quality_manager.getQualityChangesGroups(global_stack)
  250. if global_quality_changes_name in quality_changes_groups:
  251. new_quality_changes_group = quality_changes_groups[global_quality_changes_name]
  252. self._setQualityChangesGroup(new_quality_changes_group)
  253. else:
  254. quality_groups = self._application._quality_manager.getQualityGroups(global_stack)
  255. if quality_type not in quality_groups:
  256. Logger.log("w", "Quality type [%s] not found in available qualities [%s]", quality_type, str(quality_groups.values()))
  257. self._setEmptyQuality()
  258. return
  259. new_quality_group = quality_groups[quality_type]
  260. self._setQualityGroup(new_quality_group, empty_quality_changes = True)
  261. @pyqtSlot(str)
  262. def setActiveMachine(self, stack_id: str) -> None:
  263. self.blurSettings.emit() # Ensure no-one has focus.
  264. container_registry = ContainerRegistry.getInstance()
  265. containers = container_registry.findContainerStacks(id = stack_id)
  266. if containers:
  267. global_stack = containers[0]
  268. ExtruderManager.getInstance().setActiveExtruderIndex(0) # Switch to first extruder
  269. self._global_container_stack = global_stack
  270. Application.getInstance().setGlobalContainerStack(global_stack)
  271. ExtruderManager.getInstance()._globalContainerStackChanged()
  272. self._initMachineState(containers[0])
  273. self._onGlobalContainerChanged()
  274. self.__emitChangedSignals()
  275. @pyqtSlot(str, str)
  276. def addMachine(self, name: str, definition_id: str) -> None:
  277. new_stack = CuraStackBuilder.createMachine(name, definition_id)
  278. if new_stack:
  279. # Instead of setting the global container stack here, we set the active machine and so the signals are emitted
  280. self.setActiveMachine(new_stack.getId())
  281. else:
  282. Logger.log("w", "Failed creating a new machine!")
  283. def _checkStacksHaveErrors(self) -> bool:
  284. time_start = time.time()
  285. if self._global_container_stack is None: #No active machine.
  286. return False
  287. if self._global_container_stack.hasErrors():
  288. Logger.log("d", "Checking global stack for errors took %0.2f s and we found and error" % (time.time() - time_start))
  289. return True
  290. # Not a very pretty solution, but the extruder manager doesn't really know how many extruders there are
  291. machine_extruder_count = self._global_container_stack.getProperty("machine_extruder_count", "value")
  292. extruder_stacks = ExtruderManager.getInstance().getMachineExtruders(self._global_container_stack.getId())
  293. count = 1 # we start with the global stack
  294. for stack in extruder_stacks:
  295. md = stack.getMetaData()
  296. if "position" in md and int(md["position"]) >= machine_extruder_count:
  297. continue
  298. count += 1
  299. if stack.hasErrors():
  300. Logger.log("d", "Checking %s stacks for errors took %.2f s and we found an error in stack [%s]" % (count, time.time() - time_start, str(stack)))
  301. return True
  302. Logger.log("d", "Checking %s stacks for errors took %.2f s" % (count, time.time() - time_start))
  303. return False
  304. ## Check if the global_container has instances in the user container
  305. @pyqtProperty(bool, notify = activeStackValueChanged)
  306. def hasUserSettings(self) -> bool:
  307. if not self._global_container_stack:
  308. return False
  309. if self._global_container_stack.getTop().findInstances():
  310. return True
  311. stacks = list(ExtruderManager.getInstance().getMachineExtruders(self._global_container_stack.getId()))
  312. for stack in stacks:
  313. if stack.getTop().findInstances():
  314. return True
  315. return False
  316. @pyqtProperty(int, notify = activeStackValueChanged)
  317. def numUserSettings(self) -> int:
  318. if not self._global_container_stack:
  319. return 0
  320. num_user_settings = 0
  321. num_user_settings += len(self._global_container_stack.getTop().findInstances())
  322. stacks = list(ExtruderManager.getInstance().getMachineExtruders(self._global_container_stack.getId()))
  323. for stack in stacks:
  324. num_user_settings += len(stack.getTop().findInstances())
  325. return num_user_settings
  326. ## Delete a user setting from the global stack and all extruder stacks.
  327. # \param key \type{str} the name of the key to delete
  328. @pyqtSlot(str)
  329. def clearUserSettingAllCurrentStacks(self, key: str) -> None:
  330. if not self._global_container_stack:
  331. return
  332. send_emits_containers = []
  333. top_container = self._global_container_stack.getTop()
  334. top_container.removeInstance(key, postpone_emit=True)
  335. send_emits_containers.append(top_container)
  336. linked = not self._global_container_stack.getProperty(key, "settable_per_extruder") or \
  337. self._global_container_stack.getProperty(key, "limit_to_extruder") != "-1"
  338. if not linked:
  339. stack = ExtruderManager.getInstance().getActiveExtruderStack()
  340. stacks = [stack]
  341. else:
  342. stacks = ExtruderManager.getInstance().getMachineExtruders(self._global_container_stack.getId())
  343. for stack in stacks:
  344. if stack is not None:
  345. container = stack.getTop()
  346. container.removeInstance(key, postpone_emit=True)
  347. send_emits_containers.append(container)
  348. for container in send_emits_containers:
  349. container.sendPostponedEmits()
  350. ## Check if none of the stacks contain error states
  351. # Note that the _stacks_have_errors is cached due to performance issues
  352. # Calling _checkStack(s)ForErrors on every change is simply too expensive
  353. @pyqtProperty(bool, notify = stacksValidationChanged)
  354. def stacksHaveErrors(self) -> bool:
  355. return bool(self._stacks_have_errors)
  356. @pyqtProperty(str, notify = globalContainerChanged)
  357. def activeMachineDefinitionName(self) -> str:
  358. if self._global_container_stack:
  359. return self._global_container_stack.definition.getName()
  360. return ""
  361. @pyqtProperty(str, notify = globalContainerChanged)
  362. def activeMachineName(self) -> str:
  363. if self._global_container_stack:
  364. return self._global_container_stack.getName()
  365. return ""
  366. @pyqtProperty(str, notify = globalContainerChanged)
  367. def activeMachineId(self) -> str:
  368. if self._global_container_stack:
  369. return self._global_container_stack.getId()
  370. return ""
  371. @pyqtProperty(QObject, notify = globalContainerChanged)
  372. def activeMachine(self) -> Optional["GlobalStack"]:
  373. return self._global_container_stack
  374. @pyqtProperty(str, notify = activeStackChanged)
  375. def activeStackId(self) -> str:
  376. if self._active_container_stack:
  377. return self._active_container_stack.getId()
  378. return ""
  379. @pyqtProperty(QObject, notify = activeStackChanged)
  380. def activeStack(self) -> Optional["ExtruderStack"]:
  381. return self._active_container_stack
  382. @pyqtProperty(str, notify=activeMaterialChanged)
  383. def activeMaterialId(self) -> str:
  384. if self._active_container_stack:
  385. material = self._active_container_stack.material
  386. if material:
  387. return material.getId()
  388. return ""
  389. ## Gets a dict with the active materials ids set in all extruder stacks and the global stack
  390. # (when there is one extruder, the material is set in the global stack)
  391. #
  392. # \return The material ids in all stacks
  393. @pyqtProperty("QVariantMap", notify = activeMaterialChanged)
  394. def allActiveMaterialIds(self) -> Dict[str, str]:
  395. result = {}
  396. active_stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  397. if active_stacks is not None: # If we have extruder stacks
  398. for stack in active_stacks:
  399. material_container = stack.material
  400. if not material_container:
  401. continue
  402. result[stack.getId()] = material_container.getId()
  403. return result
  404. ## Gets the layer height of the currently active quality profile.
  405. #
  406. # This is indicated together with the name of the active quality profile.
  407. #
  408. # \return The layer height of the currently active quality profile. If
  409. # there is no quality profile, this returns 0.
  410. @pyqtProperty(float, notify = activeQualityGroupChanged)
  411. def activeQualityLayerHeight(self) -> float:
  412. if not self._global_container_stack:
  413. return 0
  414. if self._current_quality_changes_group:
  415. value = self._global_container_stack.getRawProperty("layer_height", "value", skip_until_container = self._global_container_stack.qualityChanges.getId())
  416. if isinstance(value, SettingFunction):
  417. value = value(self._global_container_stack)
  418. return value
  419. elif self._current_quality_group:
  420. value = self._global_container_stack.getRawProperty("layer_height", "value", skip_until_container = self._global_container_stack.quality.getId())
  421. if isinstance(value, SettingFunction):
  422. value = value(self._global_container_stack)
  423. return value
  424. return 0
  425. @pyqtProperty(str, notify = activeVariantChanged)
  426. def globalVariantName(self) -> str:
  427. if self._global_container_stack:
  428. variant = self._global_container_stack.variant
  429. if variant and not isinstance(variant, type(self._empty_variant_container)):
  430. return variant.getName()
  431. return ""
  432. @pyqtProperty(str, notify = activeQualityGroupChanged)
  433. def activeQualityType(self) -> str:
  434. quality_type = ""
  435. if self._active_container_stack:
  436. if self._current_quality_group:
  437. quality_type = self._current_quality_group.quality_type
  438. return quality_type
  439. @pyqtProperty(bool, notify = activeQualityGroupChanged)
  440. def isActiveQualitySupported(self) -> bool:
  441. is_supported = False
  442. if self._global_container_stack:
  443. if self._current_quality_group:
  444. is_supported = self._current_quality_group.is_available
  445. return is_supported
  446. ## Returns whether there is anything unsupported in the current set-up.
  447. #
  448. # The current set-up signifies the global stack and all extruder stacks,
  449. # so this indicates whether there is any container in any of the container
  450. # stacks that is not marked as supported.
  451. @pyqtProperty(bool, notify = activeQualityChanged)
  452. def isCurrentSetupSupported(self) -> bool:
  453. if not self._global_container_stack:
  454. return False
  455. for stack in [self._global_container_stack] + list(self._global_container_stack.extruders.values()):
  456. for container in stack.getContainers():
  457. if not container:
  458. return False
  459. if not Util.parseBool(container.getMetaDataEntry("supported", True)):
  460. return False
  461. return True
  462. ## Check if a container is read_only
  463. @pyqtSlot(str, result = bool)
  464. def isReadOnly(self, container_id: str) -> bool:
  465. return ContainerRegistry.getInstance().isReadOnly(container_id)
  466. ## Copy the value of the setting of the current extruder to all other extruders as well as the global container.
  467. @pyqtSlot(str)
  468. def copyValueToExtruders(self, key: str):
  469. new_value = self._active_container_stack.getProperty(key, "value")
  470. extruder_stacks = [stack for stack in ExtruderManager.getInstance().getMachineExtruders(self._global_container_stack.getId())]
  471. # check in which stack the value has to be replaced
  472. for extruder_stack in extruder_stacks:
  473. if extruder_stack != self._active_container_stack and extruder_stack.getProperty(key, "value") != new_value:
  474. extruder_stack.userChanges.setProperty(key, "value", new_value) # TODO: nested property access, should be improved
  475. @pyqtProperty(str, notify = activeVariantChanged)
  476. def activeVariantName(self) -> str:
  477. if self._active_container_stack:
  478. variant = self._active_container_stack.variant
  479. if variant:
  480. return variant.getName()
  481. return ""
  482. @pyqtProperty(str, notify = activeVariantChanged)
  483. def activeVariantBuildplateName(self) -> str:
  484. if self._global_container_stack:
  485. variant = self._global_container_stack.variant
  486. if variant:
  487. return variant.getName()
  488. return ""
  489. @pyqtProperty(str, notify = globalContainerChanged)
  490. def activeDefinitionId(self) -> str:
  491. if self._global_container_stack:
  492. return self._global_container_stack.definition.id
  493. return ""
  494. ## Get the Definition ID to use to select quality profiles for the currently active machine
  495. # \returns DefinitionID (string) if found, empty string otherwise
  496. @pyqtProperty(str, notify = globalContainerChanged)
  497. def activeQualityDefinitionId(self) -> str:
  498. if self._global_container_stack:
  499. return getMachineDefinitionIDForQualitySearch(self._global_container_stack)
  500. return ""
  501. ## Gets how the active definition calls variants
  502. # Caveat: per-definition-variant-title is currently not translated (though the fallback is)
  503. @pyqtProperty(str, notify = globalContainerChanged)
  504. def activeDefinitionVariantsName(self) -> str:
  505. fallback_title = catalog.i18nc("@label", "Nozzle")
  506. if self._global_container_stack:
  507. return self._global_container_stack.definition.getMetaDataEntry("variants_name", fallback_title)
  508. return fallback_title
  509. @pyqtSlot(str, str)
  510. def renameMachine(self, machine_id: str, new_name: str):
  511. container_registry = ContainerRegistry.getInstance()
  512. machine_stack = container_registry.findContainerStacks(id = machine_id)
  513. if machine_stack:
  514. new_name = container_registry.createUniqueName("machine", machine_stack[0].getName(), new_name, machine_stack[0].definition.getName())
  515. machine_stack[0].setName(new_name)
  516. self.globalContainerChanged.emit()
  517. @pyqtSlot(str)
  518. def removeMachine(self, machine_id: str):
  519. # If the machine that is being removed is the currently active machine, set another machine as the active machine.
  520. activate_new_machine = (self._global_container_stack and self._global_container_stack.getId() == machine_id)
  521. # activate a new machine before removing a machine because this is safer
  522. if activate_new_machine:
  523. machine_stacks = ContainerRegistry.getInstance().findContainerStacksMetadata(type = "machine")
  524. other_machine_stacks = [s for s in machine_stacks if s["id"] != machine_id]
  525. if other_machine_stacks:
  526. self.setActiveMachine(other_machine_stacks[0]["id"])
  527. ExtruderManager.getInstance().removeMachineExtruders(machine_id)
  528. containers = ContainerRegistry.getInstance().findInstanceContainersMetadata(type = "user", machine = machine_id)
  529. for container in containers:
  530. ContainerRegistry.getInstance().removeContainer(container["id"])
  531. ContainerRegistry.getInstance().removeContainer(machine_id)
  532. @pyqtProperty(bool, notify = globalContainerChanged)
  533. def hasMaterials(self) -> bool:
  534. if self._global_container_stack:
  535. return Util.parseBool(self._global_container_stack.getMetaDataEntry("has_materials", False))
  536. return False
  537. @pyqtProperty(bool, notify = globalContainerChanged)
  538. def hasVariants(self) -> bool:
  539. if self._global_container_stack:
  540. return Util.parseBool(self._global_container_stack.getMetaDataEntry("has_variants", False))
  541. return False
  542. @pyqtProperty(bool, notify = globalContainerChanged)
  543. def hasVariantBuildplates(self) -> bool:
  544. if self._global_container_stack:
  545. return Util.parseBool(self._global_container_stack.getMetaDataEntry("has_variant_buildplates", False))
  546. return False
  547. ## The selected buildplate is compatible if it is compatible with all the materials in all the extruders
  548. @pyqtProperty(bool, notify = activeMaterialChanged)
  549. def variantBuildplateCompatible(self) -> bool:
  550. if not self._global_container_stack:
  551. return True
  552. buildplate_compatible = True # It is compatible by default
  553. extruder_stacks = self._global_container_stack.extruders.values()
  554. for stack in extruder_stacks:
  555. material_container = stack.material
  556. if material_container == self._empty_material_container:
  557. continue
  558. if material_container.getMetaDataEntry("buildplate_compatible"):
  559. buildplate_compatible = buildplate_compatible and material_container.getMetaDataEntry("buildplate_compatible")[self.activeVariantBuildplateName]
  560. return buildplate_compatible
  561. ## The selected buildplate is usable if it is usable for all materials OR it is compatible for one but not compatible
  562. # for the other material but the buildplate is still usable
  563. @pyqtProperty(bool, notify = activeMaterialChanged)
  564. def variantBuildplateUsable(self) -> bool:
  565. if not self._global_container_stack:
  566. return True
  567. # Here the next formula is being calculated:
  568. # result = (not (material_left_compatible and material_right_compatible)) and
  569. # (material_left_compatible or material_left_usable) and
  570. # (material_right_compatible or material_right_usable)
  571. result = not self.variantBuildplateCompatible
  572. extruder_stacks = self._global_container_stack.extruders.values()
  573. for stack in extruder_stacks:
  574. material_container = stack.material
  575. if material_container == self._empty_material_container:
  576. continue
  577. buildplate_compatible = material_container.getMetaDataEntry("buildplate_compatible")[self.activeVariantBuildplateName] if material_container.getMetaDataEntry("buildplate_compatible") else True
  578. buildplate_usable = material_container.getMetaDataEntry("buildplate_recommended")[self.activeVariantBuildplateName] if material_container.getMetaDataEntry("buildplate_recommended") else True
  579. result = result and (buildplate_compatible or buildplate_usable)
  580. return result
  581. ## Property to indicate if a machine has "specialized" material profiles.
  582. # Some machines have their own material profiles that "override" the default catch all profiles.
  583. @pyqtProperty(bool, notify = globalContainerChanged)
  584. def filterMaterialsByMachine(self) -> bool:
  585. if self._global_container_stack:
  586. return Util.parseBool(self._global_container_stack.getMetaDataEntry("has_machine_materials", False))
  587. return False
  588. ## Property to indicate if a machine has "specialized" quality profiles.
  589. # Some machines have their own quality profiles that "override" the default catch all profiles.
  590. @pyqtProperty(bool, notify = globalContainerChanged)
  591. def filterQualityByMachine(self) -> bool:
  592. if self._global_container_stack:
  593. return Util.parseBool(self._global_container_stack.getMetaDataEntry("has_machine_quality", False))
  594. return False
  595. ## Get the Definition ID of a machine (specified by ID)
  596. # \param machine_id string machine id to get the definition ID of
  597. # \returns DefinitionID (string) if found, None otherwise
  598. @pyqtSlot(str, result = str)
  599. def getDefinitionByMachineId(self, machine_id: str) -> str:
  600. containers = ContainerRegistry.getInstance().findContainerStacks(id = machine_id)
  601. if containers:
  602. return containers[0].definition.getId()
  603. ## Set the amount of extruders on the active machine (global stack)
  604. # \param extruder_count int the number of extruders to set
  605. def setActiveMachineExtruderCount(self, extruder_count):
  606. extruder_manager = Application.getInstance().getExtruderManager()
  607. definition_changes_container = self._global_container_stack.definitionChanges
  608. if not self._global_container_stack or definition_changes_container == self._empty_definition_changes_container:
  609. return
  610. previous_extruder_count = self._global_container_stack.getProperty("machine_extruder_count", "value")
  611. if extruder_count == previous_extruder_count:
  612. return
  613. # reset all extruder number settings whose value is no longer valid
  614. for setting_instance in self._global_container_stack.userChanges.findInstances():
  615. setting_key = setting_instance.definition.key
  616. if not self._global_container_stack.getProperty(setting_key, "type") in ("extruder", "optional_extruder"):
  617. continue
  618. old_value = int(self._global_container_stack.userChanges.getProperty(setting_key, "value"))
  619. if old_value >= extruder_count:
  620. self._global_container_stack.userChanges.removeInstance(setting_key)
  621. Logger.log("d", "Reset [%s] because its old value [%s] is no longer valid ", setting_key, old_value)
  622. # Check to see if any objects are set to print with an extruder that will no longer exist
  623. root_node = Application.getInstance().getController().getScene().getRoot()
  624. for node in DepthFirstIterator(root_node):
  625. if node.getMeshData():
  626. extruder_nr = node.callDecoration("getActiveExtruderPosition")
  627. if extruder_nr is not None and int(extruder_nr) > extruder_count - 1:
  628. node.callDecoration("setActiveExtruder", extruder_manager.getExtruderStack(extruder_count - 1).getId())
  629. definition_changes_container.setProperty("machine_extruder_count", "value", extruder_count)
  630. # Make sure one of the extruder stacks is active
  631. extruder_manager.setActiveExtruderIndex(0)
  632. # Move settable_per_extruder values out of the global container
  633. # After CURA-4482 this should not be the case anymore, but we still want to support older project files.
  634. global_user_container = self._global_container_stack.getTop()
  635. # Make sure extruder_stacks exists
  636. extruder_stacks = []
  637. if previous_extruder_count == 1:
  638. extruder_stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  639. global_user_container = self._global_container_stack.getTop()
  640. for setting_instance in global_user_container.findInstances():
  641. setting_key = setting_instance.definition.key
  642. settable_per_extruder = self._global_container_stack.getProperty(setting_key, "settable_per_extruder")
  643. if settable_per_extruder:
  644. limit_to_extruder = int(self._global_container_stack.getProperty(setting_key, "limit_to_extruder"))
  645. extruder_stack = extruder_stacks[max(0, limit_to_extruder)]
  646. extruder_stack.getTop().setProperty(setting_key, "value", global_user_container.getProperty(setting_key, "value"))
  647. global_user_container.removeInstance(setting_key)
  648. # Signal that the global stack has changed
  649. Application.getInstance().globalContainerStackChanged.emit()
  650. @pyqtSlot(int, result = QObject)
  651. def getExtruder(self, position: int):
  652. extruder = None
  653. if self._global_container_stack:
  654. extruder = self._global_container_stack.extruders.get(str(position))
  655. return extruder
  656. def _onMachineNameChanged(self):
  657. self.globalContainerChanged.emit()
  658. def _onMaterialNameChanged(self):
  659. self.activeMaterialChanged.emit()
  660. def _onQualityNameChanged(self):
  661. self.activeQualityChanged.emit()
  662. def _getContainerChangedSignals(self) -> List[Signal]:
  663. stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  664. stacks.append(self._global_container_stack)
  665. return [ s.containersChanged for s in stacks ]
  666. @pyqtSlot(str, str, str)
  667. def setSettingForAllExtruders(self, setting_name: str, property_name: str, property_value: str):
  668. for key, extruder in self._global_container_stack.extruders.items():
  669. container = extruder.userChanges
  670. container.setProperty(setting_name, property_name, property_value)
  671. @pyqtProperty("QVariantList", notify = rootMaterialChanged)
  672. def currentExtruderPositions(self):
  673. return sorted(list(self._current_root_material_id.keys()))
  674. @pyqtProperty("QVariant", notify = rootMaterialChanged)
  675. def currentRootMaterialId(self):
  676. # initial filling the current_root_material_id
  677. self._current_root_material_id = {}
  678. for position in self._global_container_stack.extruders:
  679. self._current_root_material_id[position] = self._global_container_stack.extruders[position].material.getMetaDataEntry("base_file")
  680. return self._current_root_material_id
  681. @pyqtProperty("QVariant", notify = rootMaterialChanged)
  682. def currentRootMaterialName(self):
  683. # initial filling the current_root_material_name
  684. if self._global_container_stack:
  685. self._current_root_material_name = {}
  686. for position in self._global_container_stack.extruders:
  687. if position not in self._current_root_material_name:
  688. material = self._global_container_stack.extruders[position].material
  689. self._current_root_material_name[position] = material.getName()
  690. return self._current_root_material_name
  691. ## Return the variant names in the extruder stack(s).
  692. ## For the variant in the global stack, use activeVariantBuildplateName
  693. @pyqtProperty("QVariant", notify = activeVariantChanged)
  694. def activeVariantNames(self):
  695. result = {}
  696. active_stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  697. if active_stacks is not None:
  698. for stack in active_stacks:
  699. variant_container = stack.variant
  700. position = stack.getMetaDataEntry("position")
  701. if variant_container and variant_container != self._empty_variant_container:
  702. result[position] = variant_container.getName()
  703. return result
  704. #
  705. # Sets all quality and quality_changes containers to empty_quality and empty_quality_changes containers
  706. # for all stacks in the currently active machine.
  707. #
  708. def _setEmptyQuality(self):
  709. self._current_quality_group = None
  710. self._current_quality_changes_group = None
  711. self._global_container_stack.quality = self._empty_quality_container
  712. self._global_container_stack.qualityChanges = self._empty_quality_changes_container
  713. for extruder in self._global_container_stack.extruders.values():
  714. extruder.quality = self._empty_quality_container
  715. extruder.qualityChanges = self._empty_quality_changes_container
  716. self.activeQualityGroupChanged.emit()
  717. self.activeQualityChangesGroupChanged.emit()
  718. def _setQualityGroup(self, quality_group, empty_quality_changes = True):
  719. self._current_quality_group = quality_group
  720. if empty_quality_changes:
  721. self._current_quality_changes_group = None
  722. # Set quality and quality_changes for the GlobalStack
  723. self._global_container_stack.quality = quality_group.node_for_global.getContainer()
  724. if empty_quality_changes:
  725. self._global_container_stack.qualityChanges = self._empty_quality_changes_container
  726. # Set quality and quality_changes for each ExtruderStack
  727. for position, node in quality_group.nodes_for_extruders.items():
  728. self._global_container_stack.extruders[position].quality = node.getContainer()
  729. if empty_quality_changes:
  730. self._global_container_stack.extruders[position].qualityChanges = self._empty_quality_changes_container
  731. self.activeQualityGroupChanged.emit()
  732. self.activeQualityChangesGroupChanged.emit()
  733. def _setQualityChangesGroup(self, quality_changes_group):
  734. quality_type = quality_changes_group.quality_type
  735. quality_group_dict = self._quality_manager.getQualityGroups(self._global_container_stack)
  736. quality_group = quality_group_dict[quality_type]
  737. quality_changes_container = self._empty_quality_changes_container
  738. quality_container = self._empty_quality_changes_container
  739. if quality_changes_group.node_for_global:
  740. quality_changes_container = quality_changes_group.node_for_global.getContainer()
  741. if quality_group.node_for_global:
  742. quality_container = quality_group.node_for_global.getContainer()
  743. self._global_container_stack.quality = quality_container
  744. self._global_container_stack.qualityChanges = quality_changes_container
  745. for position, extruder in self._global_container_stack.extruders.items():
  746. quality_changes_node = quality_changes_group.nodes_for_extruders.get(position)
  747. quality_node = quality_group.nodes_for_extruders.get(position)
  748. quality_changes_container = self._empty_quality_changes_container
  749. quality_container = self._empty_quality_changes_container
  750. if quality_changes_node:
  751. quality_changes_container = quality_changes_node.getContainer()
  752. if quality_node:
  753. quality_container = quality_node.getContainer()
  754. extruder.quality = quality_container
  755. extruder.qualityChanges = quality_changes_container
  756. self._current_quality_group = quality_group
  757. self._current_quality_changes_group = quality_changes_group
  758. self.activeQualityGroupChanged.emit()
  759. self.activeQualityChangesGroupChanged.emit()
  760. def _setVariantNode(self, position, container_node):
  761. self._global_container_stack.extruders[position].variant = container_node.getContainer()
  762. self.activeVariantChanged.emit()
  763. def _setGlobalVariant(self, container_node):
  764. self._global_container_stack.variant = container_node.getContainer()
  765. def _setMaterial(self, position, container_node = None):
  766. if container_node:
  767. self._global_container_stack.extruders[position].material = container_node.getContainer()
  768. else:
  769. self._global_container_stack.extruders[position].material = self._empty_material_container
  770. # The _current_root_material_id is used in the MaterialMenu to see which material is selected
  771. root_material_id = container_node.metadata["base_file"]
  772. root_material_name = container_node.getContainer().getName()
  773. if root_material_id != self._current_root_material_id[position]:
  774. self._current_root_material_id[position] = root_material_id
  775. self._current_root_material_name[position] = root_material_name
  776. self.rootMaterialChanged.emit()
  777. def activeMaterialsCompatible(self):
  778. # check material - variant compatibility
  779. if Util.parseBool(self._global_container_stack.getMetaDataEntry("has_materials", False)):
  780. for position, extruder in self._global_container_stack.extruders.items():
  781. if not extruder.material.getMetaDataEntry("compatible"):
  782. return False
  783. return True
  784. ## Update current quality type and machine after setting material
  785. def _updateQualityWithMaterial(self):
  786. Logger.log("i", "Updating quality/quality_changes due to material change")
  787. current_quality_type = None
  788. if self._current_quality_group:
  789. current_quality_type = self._current_quality_group.quality_type
  790. candidate_quality_groups = self._quality_manager.getQualityGroups(self._global_container_stack)
  791. available_quality_types = {qt for qt, g in candidate_quality_groups.items() if g.is_available}
  792. Logger.log("d", "Current quality type = [%s]", current_quality_type)
  793. if not self.activeMaterialsCompatible():
  794. Logger.log("i", "Active materials are not compatible, setting all qualities to empty (Not Supported).")
  795. self._setEmptyQuality()
  796. return
  797. if not available_quality_types:
  798. Logger.log("i", "No available quality types found, setting all qualities to empty (Not Supported).")
  799. self._setEmptyQuality()
  800. return
  801. if current_quality_type in available_quality_types:
  802. Logger.log("i", "Current available quality type [%s] is available, applying changes.", current_quality_type)
  803. self._setQualityGroup(candidate_quality_groups[current_quality_type], empty_quality_changes = False)
  804. return
  805. # The current quality type is not available so we use the preferred quality type if it's available,
  806. # otherwise use one of the available quality types.
  807. quality_type = sorted(list(available_quality_types))[0]
  808. preferred_quality_type = self._global_container_stack.getMetaDataEntry("preferred_quality_type")
  809. if preferred_quality_type in available_quality_types:
  810. quality_type = preferred_quality_type
  811. Logger.log("i", "The current quality type [%s] is not available, switching to [%s] instead",
  812. current_quality_type, quality_type)
  813. self._setQualityGroup(candidate_quality_groups[quality_type], empty_quality_changes = True)
  814. def _updateMaterialWithVariant(self, position: Optional[str]):
  815. if position is None:
  816. position_list = list(self._global_container_stack.extruders.keys())
  817. else:
  818. position_list = [position]
  819. for position in position_list:
  820. extruder = self._global_container_stack.extruders[position]
  821. current_material_base_name = extruder.material.getMetaDataEntry("base_file")
  822. current_variant_name = extruder.variant.getMetaDataEntry("name")
  823. material_diameter = self._global_container_stack.getProperty("material_diameter", "value")
  824. candidate_materials = self._material_manager.getAvailableMaterials(
  825. self._global_container_stack.definition.getId(),
  826. current_variant_name,
  827. material_diameter)
  828. if not candidate_materials:
  829. self._setMaterial(position, container_node = None)
  830. continue
  831. if current_material_base_name in candidate_materials:
  832. new_material = candidate_materials[current_material_base_name]
  833. self._setMaterial(position, new_material)
  834. continue
  835. def switchPrinterType(self, machine_type):
  836. container_registry = ContainerRegistry.getInstance()
  837. machine_definition = container_registry.findDefinitionContainers(name = machine_type)[0]
  838. self._global_container_stack.definition = machine_definition
  839. self.globalContainerChanged.emit()
  840. # machine_stack = CuraStackBuilder.createMachine("ultimaker_s5" + "_instance", "ultimaker_s5")
  841. # # if not machine_stack:
  842. # # raise Exception("No machine found for ID {}".format(machine_id))
  843. # Logger.log("d", "Setting active machine to %s", machine_stack.getId())
  844. # self.setActiveMachine(machine_stack.getId())
  845. @pyqtSlot(QObject)
  846. def applyRemoteConfiguration(self, configuration: ConfigurationModel):
  847. self.blurSettings.emit()
  848. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  849. for extruder_configuration in configuration.extruderConfigurations:
  850. position = str(extruder_configuration.position)
  851. variant_container_node = self._variant_manager.getVariantNode(self._global_container_stack.definition.getId(), extruder_configuration.hotendID)
  852. material_container_node = self._material_manager.getMaterialNodeByType(self._global_container_stack, extruder_configuration.hotendID,extruder_configuration.material.guid)
  853. if variant_container_node:
  854. self._setVariantNode(position, variant_container_node)
  855. else:
  856. self._global_container_stack.extruders[position].variant = self._empty_variant_container
  857. if material_container_node:
  858. self._setMaterial(position, material_container_node)
  859. else:
  860. self._global_container_stack.extruders[position].material = self._empty_material_container
  861. self._updateMaterialWithVariant(position)
  862. if configuration.buildplateConfiguration is not None:
  863. global_variant_container_node = self._variant_manager.getBuildplateVariantNode(self._global_container_stack.definition.getId(), configuration.buildplateConfiguration)
  864. self._setGlobalVariant(global_variant_container_node)
  865. else:
  866. self._global_container_stack.variant = self._empty_variant_container
  867. self._updateQualityWithMaterial()
  868. @pyqtSlot("QVariant")
  869. def setGlobalVariant(self, container_node):
  870. self.blurSettings.emit()
  871. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  872. self._setGlobalVariant(container_node)
  873. self._updateMaterialWithVariant(None) # Update all materials
  874. self._updateQualityWithMaterial()
  875. @pyqtSlot(str, "QVariant")
  876. def setMaterial(self, position, container_node):
  877. position = str(position)
  878. self.blurSettings.emit()
  879. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  880. self._setMaterial(position, container_node)
  881. self._updateQualityWithMaterial()
  882. @pyqtSlot(str, "QVariant")
  883. def setVariantGroup(self, position, container_node):
  884. position = str(position)
  885. self.blurSettings.emit()
  886. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  887. self._setVariantNode(position, container_node)
  888. self._updateMaterialWithVariant(position)
  889. self._updateQualityWithMaterial()
  890. @pyqtSlot(QObject)
  891. def setQualityGroup(self, quality_group):
  892. self.blurSettings.emit()
  893. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  894. self._setQualityGroup(quality_group)
  895. # See if we need to show the Discard or Keep changes screen
  896. if self.hasUserSettings and Preferences.getInstance().getValue("cura/active_mode") == 1:
  897. self._application.discardOrKeepProfileChanges()
  898. @pyqtProperty(QObject, fset = setQualityGroup, notify = activeQualityGroupChanged)
  899. def activeQualityGroup(self):
  900. return self._current_quality_group
  901. @pyqtSlot(QObject)
  902. def setQualityChangesGroup(self, quality_changes_group):
  903. self.blurSettings.emit()
  904. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  905. self._setQualityChangesGroup(quality_changes_group)
  906. # See if we need to show the Discard or Keep changes screen
  907. if self.hasUserSettings and Preferences.getInstance().getValue("cura/active_mode") == 1:
  908. self._application.discardOrKeepProfileChanges()
  909. @pyqtProperty(QObject, fset = setQualityChangesGroup, notify = activeQualityChangesGroupChanged)
  910. def activeQualityChangesGroup(self):
  911. return self._current_quality_changes_group
  912. @pyqtProperty(str, notify = activeQualityGroupChanged)
  913. def activeQualityOrQualityChangesName(self):
  914. name = self._empty_quality_container.getName()
  915. if self._current_quality_changes_group:
  916. name = self._current_quality_changes_group.name
  917. elif self._current_quality_group:
  918. name = self._current_quality_group.name
  919. return name