MachineManager.py 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import collections
  4. import time
  5. from typing import Any, Callable, List, Dict, TYPE_CHECKING, Optional, cast
  6. from UM.ConfigurationErrorMessage import ConfigurationErrorMessage
  7. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  8. from UM.Settings.InstanceContainer import InstanceContainer
  9. from UM.Settings.Interfaces import ContainerInterface
  10. from UM.Signal import Signal
  11. from PyQt5.QtCore import QObject, pyqtProperty, pyqtSignal, QTimer
  12. from UM.FlameProfiler import pyqtSlot
  13. from UM import Util
  14. from UM.Logger import Logger
  15. from UM.Message import Message
  16. from UM.Settings.SettingFunction import SettingFunction
  17. from UM.Signal import postponeSignals, CompressTechnique
  18. import cura.CuraApplication
  19. from cura.Machines.QualityManager import getMachineDefinitionIDForQualitySearch
  20. from cura.PrinterOutputDevice import PrinterOutputDevice
  21. from cura.PrinterOutput.ConfigurationModel import ConfigurationModel
  22. from cura.PrinterOutput.ExtruderConfigurationModel import ExtruderConfigurationModel
  23. from cura.PrinterOutput.MaterialOutputModel import MaterialOutputModel
  24. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry
  25. from cura.Settings.ExtruderManager import ExtruderManager
  26. from cura.Settings.ExtruderStack import ExtruderStack
  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. from cura.Machines.MaterialManager import MaterialManager
  34. from cura.Machines.QualityManager import QualityManager
  35. from cura.Machines.VariantManager import VariantManager
  36. from cura.Machines.ContainerNode import ContainerNode
  37. from cura.Machines.QualityChangesGroup import QualityChangesGroup
  38. from cura.Machines.QualityGroup import QualityGroup
  39. class MachineManager(QObject):
  40. def __init__(self, parent: QObject = None) -> None:
  41. super().__init__(parent)
  42. self._active_container_stack = None # type: Optional[ExtruderStack]
  43. self._global_container_stack = None # type: Optional[GlobalStack]
  44. self._current_root_material_id = {} # type: Dict[str, str]
  45. self._current_quality_group = None # type: Optional[QualityGroup]
  46. self._current_quality_changes_group = None # type: Optional[QualityChangesGroup]
  47. self._default_extruder_position = "0" # to be updated when extruders are switched on and off
  48. self.machine_extruder_material_update_dict = collections.defaultdict(list) #type: Dict[str, List[Callable[[], None]]]
  49. self._instance_container_timer = QTimer() #type: QTimer
  50. self._instance_container_timer.setInterval(250)
  51. self._instance_container_timer.setSingleShot(True)
  52. self._instance_container_timer.timeout.connect(self.__emitChangedSignals)
  53. self._application = cura.CuraApplication.CuraApplication.getInstance() #type: cura.CuraApplication.CuraApplication
  54. self._application.globalContainerStackChanged.connect(self._onGlobalContainerChanged)
  55. self._application.getContainerRegistry().containerLoadComplete.connect(self._onContainersChanged)
  56. ## When the global container is changed, active material probably needs to be updated.
  57. self.globalContainerChanged.connect(self.activeMaterialChanged)
  58. self.globalContainerChanged.connect(self.activeVariantChanged)
  59. self.globalContainerChanged.connect(self.activeQualityChanged)
  60. self.globalContainerChanged.connect(self.activeQualityChangesGroupChanged)
  61. self.globalContainerChanged.connect(self.activeQualityGroupChanged)
  62. self._stacks_have_errors = None # type: Optional[bool]
  63. self._empty_container = CuraContainerRegistry.getInstance().getEmptyInstanceContainer() #type: InstanceContainer
  64. self._empty_definition_changes_container = CuraContainerRegistry.getInstance().findContainers(id = "empty_definition_changes")[0] #type: InstanceContainer
  65. self._empty_variant_container = CuraContainerRegistry.getInstance().findContainers(id = "empty_variant")[0] #type: InstanceContainer
  66. self._empty_material_container = CuraContainerRegistry.getInstance().findContainers(id = "empty_material")[0] #type: InstanceContainer
  67. self._empty_quality_container = CuraContainerRegistry.getInstance().findContainers(id = "empty_quality")[0] #type: InstanceContainer
  68. self._empty_quality_changes_container = CuraContainerRegistry.getInstance().findContainers(id = "empty_quality_changes")[0] #type: InstanceContainer
  69. self._onGlobalContainerChanged()
  70. ExtruderManager.getInstance().activeExtruderChanged.connect(self._onActiveExtruderStackChanged)
  71. self._onActiveExtruderStackChanged()
  72. ExtruderManager.getInstance().activeExtruderChanged.connect(self.activeMaterialChanged)
  73. ExtruderManager.getInstance().activeExtruderChanged.connect(self.activeVariantChanged)
  74. ExtruderManager.getInstance().activeExtruderChanged.connect(self.activeQualityChanged)
  75. self.globalContainerChanged.connect(self.activeStackChanged)
  76. self.globalValueChanged.connect(self.activeStackValueChanged)
  77. ExtruderManager.getInstance().activeExtruderChanged.connect(self.activeStackChanged)
  78. self.activeStackChanged.connect(self.activeStackValueChanged)
  79. self._application.getPreferences().addPreference("cura/active_machine", "")
  80. self._printer_output_devices = [] # type: List[PrinterOutputDevice]
  81. self._application.getOutputDeviceManager().outputDevicesChanged.connect(self._onOutputDevicesChanged)
  82. # There might already be some output devices by the time the signal is connected
  83. self._onOutputDevicesChanged()
  84. self._current_printer_configuration = ConfigurationModel() # Indicates the current configuration setup in this printer
  85. self.activeMaterialChanged.connect(self._onCurrentConfigurationChanged)
  86. self.activeVariantChanged.connect(self._onCurrentConfigurationChanged)
  87. # Force to compute the current configuration
  88. self._onCurrentConfigurationChanged()
  89. self._application.callLater(self.setInitialActiveMachine)
  90. self._material_incompatible_message = Message(catalog.i18nc("@info:status",
  91. "The selected material is incompatible with the selected machine or configuration."),
  92. title = catalog.i18nc("@info:title", "Incompatible Material")) #type: Message
  93. containers = CuraContainerRegistry.getInstance().findInstanceContainers(id = self.activeMaterialId) #type: List[InstanceContainer]
  94. if containers:
  95. containers[0].nameChanged.connect(self._onMaterialNameChanged)
  96. self._material_manager = self._application.getMaterialManager() #type: MaterialManager
  97. self._variant_manager = self._application.getVariantManager() #type: VariantManager
  98. self._quality_manager = self._application.getQualityManager() #type: QualityManager
  99. # When the materials lookup table gets updated, it can mean that a material has its name changed, which should
  100. # be reflected on the GUI. This signal emission makes sure that it happens.
  101. self._material_manager.materialsUpdated.connect(self.rootMaterialChanged)
  102. # When the materials get updated, it can be that an activated material's diameter gets changed. In that case,
  103. # a material update should be triggered to make sure that the machine still has compatible materials activated.
  104. self._material_manager.materialsUpdated.connect(self._updateUponMaterialMetadataChange)
  105. self.rootMaterialChanged.connect(self._onRootMaterialChanged)
  106. # Emit the printerConnectedStatusChanged when either globalContainerChanged or outputDevicesChanged are emitted
  107. self.globalContainerChanged.connect(self.printerConnectedStatusChanged)
  108. self.outputDevicesChanged.connect(self.printerConnectedStatusChanged)
  109. activeQualityGroupChanged = pyqtSignal()
  110. activeQualityChangesGroupChanged = pyqtSignal()
  111. globalContainerChanged = pyqtSignal() # Emitted whenever the global stack is changed (ie: when changing between printers, changing a global profile, but not when changing a value)
  112. activeMaterialChanged = pyqtSignal()
  113. activeVariantChanged = pyqtSignal()
  114. activeQualityChanged = pyqtSignal()
  115. activeStackChanged = pyqtSignal() # Emitted whenever the active stack is changed (ie: when changing between extruders, changing a profile, but not when changing a value)
  116. extruderChanged = pyqtSignal()
  117. globalValueChanged = pyqtSignal() # Emitted whenever a value inside global container is changed.
  118. activeStackValueChanged = pyqtSignal() # Emitted whenever a value inside the active stack is changed.
  119. activeStackValidationChanged = pyqtSignal() # Emitted whenever a validation inside active container is changed
  120. stacksValidationChanged = pyqtSignal() # Emitted whenever a validation is changed
  121. numberExtrudersEnabledChanged = pyqtSignal() # Emitted when the number of extruders that are enabled changed
  122. blurSettings = pyqtSignal() # Emitted to force fields in the advanced sidebar to un-focus, so they update properly
  123. outputDevicesChanged = pyqtSignal()
  124. currentConfigurationChanged = pyqtSignal() # Emitted every time the current configurations of the machine changes
  125. printerConnectedStatusChanged = pyqtSignal() # Emitted every time the active machine change or the outputdevices change
  126. rootMaterialChanged = pyqtSignal()
  127. def setInitialActiveMachine(self) -> None:
  128. active_machine_id = self._application.getPreferences().getValue("cura/active_machine")
  129. if active_machine_id != "" and CuraContainerRegistry.getInstance().findContainerStacksMetadata(id = active_machine_id):
  130. # An active machine was saved, so restore it.
  131. self.setActiveMachine(active_machine_id)
  132. def _onOutputDevicesChanged(self) -> None:
  133. self._printer_output_devices = []
  134. for printer_output_device in self._application.getOutputDeviceManager().getOutputDevices():
  135. if isinstance(printer_output_device, PrinterOutputDevice):
  136. self._printer_output_devices.append(printer_output_device)
  137. self.outputDevicesChanged.emit()
  138. @pyqtProperty(QObject, notify = currentConfigurationChanged)
  139. def currentConfiguration(self) -> ConfigurationModel:
  140. return self._current_printer_configuration
  141. def _onCurrentConfigurationChanged(self) -> None:
  142. if not self._global_container_stack:
  143. return
  144. # Create the configuration model with the current data in Cura
  145. self._current_printer_configuration.printerType = self._global_container_stack.definition.getName()
  146. self._current_printer_configuration.extruderConfigurations = []
  147. for extruder in self._global_container_stack.extruders.values():
  148. extruder_configuration = ExtruderConfigurationModel()
  149. # For compare just the GUID is needed at this moment
  150. mat_type = extruder.material.getMetaDataEntry("material") if extruder.material != self._empty_material_container else None
  151. mat_guid = extruder.material.getMetaDataEntry("GUID") if extruder.material != self._empty_material_container else None
  152. mat_color = extruder.material.getMetaDataEntry("color_name") if extruder.material != self._empty_material_container else None
  153. mat_brand = extruder.material.getMetaDataEntry("brand") if extruder.material != self._empty_material_container else None
  154. mat_name = extruder.material.getMetaDataEntry("name") if extruder.material != self._empty_material_container else None
  155. material_model = MaterialOutputModel(mat_guid, mat_type, mat_color, mat_brand, mat_name)
  156. extruder_configuration.position = int(extruder.getMetaDataEntry("position"))
  157. extruder_configuration.material = material_model
  158. extruder_configuration.hotendID = extruder.variant.getName() if extruder.variant != self._empty_variant_container else None
  159. self._current_printer_configuration.extruderConfigurations.append(extruder_configuration)
  160. 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
  161. self.currentConfigurationChanged.emit()
  162. @pyqtSlot(QObject, result = bool)
  163. def matchesConfiguration(self, configuration: ConfigurationModel) -> bool:
  164. return self._current_printer_configuration == configuration
  165. @pyqtProperty("QVariantList", notify = outputDevicesChanged)
  166. def printerOutputDevices(self) -> List[PrinterOutputDevice]:
  167. return self._printer_output_devices
  168. @pyqtProperty(int, constant=True)
  169. def totalNumberOfSettings(self) -> int:
  170. general_definition_containers = CuraContainerRegistry.getInstance().findDefinitionContainers(id = "fdmprinter")
  171. if not general_definition_containers:
  172. return 0
  173. return len(general_definition_containers[0].getAllKeys())
  174. def _onGlobalContainerChanged(self) -> None:
  175. if self._global_container_stack:
  176. try:
  177. self._global_container_stack.nameChanged.disconnect(self._onMachineNameChanged)
  178. except TypeError: # pyQtSignal gives a TypeError when disconnecting from something that was already disconnected.
  179. pass
  180. try:
  181. self._global_container_stack.containersChanged.disconnect(self._onContainersChanged)
  182. except TypeError:
  183. pass
  184. try:
  185. self._global_container_stack.propertyChanged.disconnect(self._onPropertyChanged)
  186. except TypeError:
  187. pass
  188. for extruder_stack in ExtruderManager.getInstance().getActiveExtruderStacks():
  189. extruder_stack.propertyChanged.disconnect(self._onPropertyChanged)
  190. extruder_stack.containersChanged.disconnect(self._onContainersChanged)
  191. # Update the local global container stack reference
  192. self._global_container_stack = self._application.getGlobalContainerStack()
  193. if self._global_container_stack:
  194. self.updateDefaultExtruder()
  195. self.updateNumberExtrudersEnabled()
  196. self.globalContainerChanged.emit()
  197. # after switching the global stack we reconnect all the signals and set the variant and material references
  198. if self._global_container_stack:
  199. self._application.getPreferences().setValue("cura/active_machine", self._global_container_stack.getId())
  200. self._global_container_stack.nameChanged.connect(self._onMachineNameChanged)
  201. self._global_container_stack.containersChanged.connect(self._onContainersChanged)
  202. self._global_container_stack.propertyChanged.connect(self._onPropertyChanged)
  203. # Global stack can have only a variant if it is a buildplate
  204. global_variant = self._global_container_stack.variant
  205. if global_variant != self._empty_variant_container:
  206. if global_variant.getMetaDataEntry("hardware_type") != "buildplate":
  207. self._global_container_stack.setVariant(self._empty_variant_container)
  208. # set the global material to empty as we now use the extruder stack at all times - CURA-4482
  209. global_material = self._global_container_stack.material
  210. if global_material != self._empty_material_container:
  211. self._global_container_stack.setMaterial(self._empty_material_container)
  212. # Listen for changes on all extruder stacks
  213. for extruder_stack in ExtruderManager.getInstance().getActiveExtruderStacks():
  214. extruder_stack.propertyChanged.connect(self._onPropertyChanged)
  215. extruder_stack.containersChanged.connect(self._onContainersChanged)
  216. if self._global_container_stack.getId() in self.machine_extruder_material_update_dict:
  217. for func in self.machine_extruder_material_update_dict[self._global_container_stack.getId()]:
  218. self._application.callLater(func)
  219. del self.machine_extruder_material_update_dict[self._global_container_stack.getId()]
  220. self.activeQualityGroupChanged.emit()
  221. def _onActiveExtruderStackChanged(self) -> None:
  222. self.blurSettings.emit() # Ensure no-one has focus.
  223. old_active_container_stack = self._active_container_stack
  224. self._active_container_stack = ExtruderManager.getInstance().getActiveExtruderStack()
  225. if old_active_container_stack != self._active_container_stack:
  226. # Many methods and properties related to the active quality actually depend
  227. # on _active_container_stack. If it changes, then the properties change.
  228. self.activeQualityChanged.emit()
  229. def __emitChangedSignals(self) -> None:
  230. self.activeQualityChanged.emit()
  231. self.activeVariantChanged.emit()
  232. self.activeMaterialChanged.emit()
  233. self.rootMaterialChanged.emit()
  234. def _onContainersChanged(self, container: ContainerInterface) -> None:
  235. self._instance_container_timer.start()
  236. def _onPropertyChanged(self, key: str, property_name: str) -> None:
  237. if property_name == "value":
  238. # Notify UI items, such as the "changed" star in profile pull down menu.
  239. self.activeStackValueChanged.emit()
  240. ## Given a global_stack, make sure that it's all valid by searching for this quality group and applying it again
  241. def _initMachineState(self, global_stack: "CuraContainerStack") -> None:
  242. material_dict = {}
  243. for position, extruder in global_stack.extruders.items():
  244. material_dict[position] = extruder.material.getMetaDataEntry("base_file")
  245. self._current_root_material_id = material_dict
  246. # Update materials to make sure that the diameters match with the machine's
  247. for position in global_stack.extruders:
  248. self.updateMaterialWithVariant(position)
  249. global_quality = global_stack.quality
  250. quality_type = global_quality.getMetaDataEntry("quality_type")
  251. global_quality_changes = global_stack.qualityChanges
  252. global_quality_changes_name = global_quality_changes.getName()
  253. # Try to set the same quality/quality_changes as the machine specified.
  254. # If the quality/quality_changes is not available, switch to the default or the first quality that's available.
  255. same_quality_found = False
  256. quality_groups = self._application.getQualityManager().getQualityGroups(global_stack)
  257. if global_quality_changes.getId() != "empty_quality_changes":
  258. quality_changes_groups = self._application.getQualityManager().getQualityChangesGroups(global_stack)
  259. new_quality_changes_group = quality_changes_groups.get(global_quality_changes_name)
  260. if new_quality_changes_group is not None:
  261. self._setQualityChangesGroup(new_quality_changes_group)
  262. same_quality_found = True
  263. Logger.log("i", "Machine '%s' quality changes set to '%s'",
  264. global_stack.getName(), new_quality_changes_group.name)
  265. else:
  266. new_quality_group = quality_groups.get(quality_type)
  267. if new_quality_group is not None:
  268. self._setQualityGroup(new_quality_group, empty_quality_changes = True)
  269. same_quality_found = True
  270. Logger.log("i", "Machine '%s' quality set to '%s'",
  271. global_stack.getName(), new_quality_group.quality_type)
  272. # Could not find the specified quality/quality_changes, switch to the preferred quality if available,
  273. # otherwise the first quality that's available, otherwise empty (not supported).
  274. if not same_quality_found:
  275. Logger.log("i", "Machine '%s' could not find quality_type '%s' and quality_changes '%s'. "
  276. "Available quality types are [%s]. Switching to default quality.",
  277. global_stack.getName(), quality_type, global_quality_changes_name,
  278. ", ".join(quality_groups.keys()))
  279. preferred_quality_type = global_stack.getMetaDataEntry("preferred_quality_type")
  280. quality_group = quality_groups.get(preferred_quality_type)
  281. if quality_group is None:
  282. if quality_groups:
  283. quality_group = list(quality_groups.values())[0]
  284. self._setQualityGroup(quality_group, empty_quality_changes = True)
  285. @pyqtSlot(str)
  286. def setActiveMachine(self, stack_id: str) -> None:
  287. self.blurSettings.emit() # Ensure no-one has focus.
  288. container_registry = CuraContainerRegistry.getInstance()
  289. containers = container_registry.findContainerStacks(id = stack_id)
  290. if not containers:
  291. return
  292. global_stack = containers[0]
  293. ExtruderManager.getInstance()._fixSingleExtrusionMachineExtruderDefinition(global_stack)
  294. if not global_stack.isValid():
  295. # Mark global stack as invalid
  296. ConfigurationErrorMessage.getInstance().addFaultyContainers(global_stack.getId())
  297. return # We're done here
  298. ExtruderManager.getInstance().setActiveExtruderIndex(0) # Switch to first extruder
  299. self._global_container_stack = global_stack
  300. self._application.setGlobalContainerStack(global_stack)
  301. ExtruderManager.getInstance()._globalContainerStackChanged()
  302. self._initMachineState(global_stack)
  303. self._onGlobalContainerChanged()
  304. self.__emitChangedSignals()
  305. ## Given a definition id, return the machine with this id.
  306. # Optional: add a list of keys and values to filter the list of machines with the given definition id
  307. # \param definition_id \type{str} definition id that needs to look for
  308. # \param metadata_filter \type{dict} list of metadata keys and values used for filtering
  309. @staticmethod
  310. def getMachine(definition_id: str, metadata_filter: Optional[Dict[str, str]] = None) -> Optional["GlobalStack"]:
  311. if metadata_filter is None:
  312. metadata_filter = {}
  313. machines = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine", **metadata_filter)
  314. for machine in machines:
  315. if machine.definition.getId() == definition_id:
  316. return machine
  317. return None
  318. @pyqtSlot(str, str)
  319. def addMachine(self, name: str, definition_id: str) -> None:
  320. new_stack = CuraStackBuilder.createMachine(name, definition_id)
  321. if new_stack:
  322. # Instead of setting the global container stack here, we set the active machine and so the signals are emitted
  323. self.setActiveMachine(new_stack.getId())
  324. else:
  325. Logger.log("w", "Failed creating a new machine!")
  326. def _checkStacksHaveErrors(self) -> bool:
  327. time_start = time.time()
  328. if self._global_container_stack is None: #No active machine.
  329. return False
  330. if self._global_container_stack.hasErrors():
  331. Logger.log("d", "Checking global stack for errors took %0.2f s and we found an error" % (time.time() - time_start))
  332. return True
  333. # Not a very pretty solution, but the extruder manager doesn't really know how many extruders there are
  334. machine_extruder_count = self._global_container_stack.getProperty("machine_extruder_count", "value")
  335. extruder_stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  336. count = 1 # we start with the global stack
  337. for stack in extruder_stacks:
  338. md = stack.getMetaData()
  339. if "position" in md and int(md["position"]) >= machine_extruder_count:
  340. continue
  341. count += 1
  342. if stack.hasErrors():
  343. 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)))
  344. return True
  345. Logger.log("d", "Checking %s stacks for errors took %.2f s" % (count, time.time() - time_start))
  346. return False
  347. ## Check if the global_container has instances in the user container
  348. @pyqtProperty(bool, notify = activeStackValueChanged)
  349. def hasUserSettings(self) -> bool:
  350. if not self._global_container_stack:
  351. return False
  352. if self._global_container_stack.getTop().findInstances():
  353. return True
  354. stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  355. for stack in stacks:
  356. if stack.getTop().findInstances():
  357. return True
  358. return False
  359. @pyqtProperty(int, notify = activeStackValueChanged)
  360. def numUserSettings(self) -> int:
  361. if not self._global_container_stack:
  362. return 0
  363. num_user_settings = 0
  364. num_user_settings += len(self._global_container_stack.getTop().findInstances())
  365. stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  366. for stack in stacks:
  367. num_user_settings += len(stack.getTop().findInstances())
  368. return num_user_settings
  369. ## Delete a user setting from the global stack and all extruder stacks.
  370. # \param key \type{str} the name of the key to delete
  371. @pyqtSlot(str)
  372. def clearUserSettingAllCurrentStacks(self, key: str) -> None:
  373. if not self._global_container_stack:
  374. return
  375. send_emits_containers = []
  376. top_container = self._global_container_stack.getTop()
  377. top_container.removeInstance(key, postpone_emit=True)
  378. send_emits_containers.append(top_container)
  379. linked = not self._global_container_stack.getProperty(key, "settable_per_extruder") or \
  380. self._global_container_stack.getProperty(key, "limit_to_extruder") != "-1"
  381. if not linked:
  382. stack = ExtruderManager.getInstance().getActiveExtruderStack()
  383. stacks = [stack]
  384. else:
  385. stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  386. for stack in stacks:
  387. if stack is not None:
  388. container = stack.getTop()
  389. container.removeInstance(key, postpone_emit=True)
  390. send_emits_containers.append(container)
  391. for container in send_emits_containers:
  392. container.sendPostponedEmits()
  393. ## Check if none of the stacks contain error states
  394. # Note that the _stacks_have_errors is cached due to performance issues
  395. # Calling _checkStack(s)ForErrors on every change is simply too expensive
  396. @pyqtProperty(bool, notify = stacksValidationChanged)
  397. def stacksHaveErrors(self) -> bool:
  398. return bool(self._stacks_have_errors)
  399. @pyqtProperty(str, notify = globalContainerChanged)
  400. def activeMachineDefinitionName(self) -> str:
  401. if self._global_container_stack:
  402. return self._global_container_stack.definition.getName()
  403. return ""
  404. @pyqtProperty(str, notify = globalContainerChanged)
  405. def activeMachineName(self) -> str:
  406. if self._global_container_stack:
  407. return self._global_container_stack.getName()
  408. return ""
  409. @pyqtProperty(str, notify = globalContainerChanged)
  410. def activeMachineId(self) -> str:
  411. if self._global_container_stack:
  412. return self._global_container_stack.getId()
  413. return ""
  414. @pyqtProperty(bool, notify = printerConnectedStatusChanged)
  415. def printerConnected(self):
  416. return bool(self._printer_output_devices)
  417. @pyqtProperty(str, notify = printerConnectedStatusChanged)
  418. def activeMachineNetworkKey(self) -> str:
  419. if self._global_container_stack:
  420. return self._global_container_stack.getMetaDataEntry("um_network_key", "")
  421. return ""
  422. @pyqtProperty(str, notify = printerConnectedStatusChanged)
  423. def activeMachineNetworkGroupName(self) -> str:
  424. if self._global_container_stack:
  425. return self._global_container_stack.getMetaDataEntry("connect_group_name", "")
  426. return ""
  427. @pyqtProperty(QObject, notify = globalContainerChanged)
  428. def activeMachine(self) -> Optional["GlobalStack"]:
  429. return self._global_container_stack
  430. @pyqtProperty(str, notify = activeStackChanged)
  431. def activeStackId(self) -> str:
  432. if self._active_container_stack:
  433. return self._active_container_stack.getId()
  434. return ""
  435. @pyqtProperty(QObject, notify = activeStackChanged)
  436. def activeStack(self) -> Optional["ExtruderStack"]:
  437. return self._active_container_stack
  438. @pyqtProperty(str, notify=activeMaterialChanged)
  439. def activeMaterialId(self) -> str:
  440. if self._active_container_stack:
  441. material = self._active_container_stack.material
  442. if material:
  443. return material.getId()
  444. return ""
  445. ## Gets a dict with the active materials ids set in all extruder stacks and the global stack
  446. # (when there is one extruder, the material is set in the global stack)
  447. #
  448. # \return The material ids in all stacks
  449. @pyqtProperty("QVariantMap", notify = activeMaterialChanged)
  450. def allActiveMaterialIds(self) -> Dict[str, str]:
  451. result = {}
  452. active_stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  453. for stack in active_stacks:
  454. material_container = stack.material
  455. if not material_container:
  456. continue
  457. result[stack.getId()] = material_container.getId()
  458. return result
  459. ## Gets the layer height of the currently active quality profile.
  460. #
  461. # This is indicated together with the name of the active quality profile.
  462. #
  463. # \return The layer height of the currently active quality profile. If
  464. # there is no quality profile, this returns 0.
  465. @pyqtProperty(float, notify = activeQualityGroupChanged)
  466. def activeQualityLayerHeight(self) -> float:
  467. if not self._global_container_stack:
  468. return 0
  469. if self._current_quality_changes_group:
  470. value = self._global_container_stack.getRawProperty("layer_height", "value", skip_until_container = self._global_container_stack.qualityChanges.getId())
  471. if isinstance(value, SettingFunction):
  472. value = value(self._global_container_stack)
  473. return value
  474. elif self._current_quality_group:
  475. value = self._global_container_stack.getRawProperty("layer_height", "value", skip_until_container = self._global_container_stack.quality.getId())
  476. if isinstance(value, SettingFunction):
  477. value = value(self._global_container_stack)
  478. return value
  479. return 0
  480. @pyqtProperty(str, notify = activeVariantChanged)
  481. def globalVariantName(self) -> str:
  482. if self._global_container_stack:
  483. variant = self._global_container_stack.variant
  484. if variant and not isinstance(variant, type(self._empty_variant_container)):
  485. return variant.getName()
  486. return ""
  487. @pyqtProperty(str, notify = activeQualityGroupChanged)
  488. def activeQualityType(self) -> str:
  489. quality_type = ""
  490. if self._active_container_stack:
  491. if self._current_quality_group:
  492. quality_type = self._current_quality_group.quality_type
  493. return quality_type
  494. @pyqtProperty(bool, notify = activeQualityGroupChanged)
  495. def isActiveQualitySupported(self) -> bool:
  496. is_supported = False
  497. if self._global_container_stack:
  498. if self._current_quality_group:
  499. is_supported = self._current_quality_group.is_available
  500. return is_supported
  501. ## Returns whether there is anything unsupported in the current set-up.
  502. #
  503. # The current set-up signifies the global stack and all extruder stacks,
  504. # so this indicates whether there is any container in any of the container
  505. # stacks that is not marked as supported.
  506. @pyqtProperty(bool, notify = activeQualityChanged)
  507. def isCurrentSetupSupported(self) -> bool:
  508. if not self._global_container_stack:
  509. return False
  510. for stack in [self._global_container_stack] + list(self._global_container_stack.extruders.values()):
  511. for container in stack.getContainers():
  512. if not container:
  513. return False
  514. if not Util.parseBool(container.getMetaDataEntry("supported", True)):
  515. return False
  516. return True
  517. ## Check if a container is read_only
  518. @pyqtSlot(str, result = bool)
  519. def isReadOnly(self, container_id: str) -> bool:
  520. return CuraContainerRegistry.getInstance().isReadOnly(container_id)
  521. ## Copy the value of the setting of the current extruder to all other extruders as well as the global container.
  522. @pyqtSlot(str)
  523. def copyValueToExtruders(self, key: str) -> None:
  524. if self._active_container_stack is None or self._global_container_stack is None:
  525. return
  526. new_value = self._active_container_stack.getProperty(key, "value")
  527. extruder_stacks = [stack for stack in ExtruderManager.getInstance().getActiveExtruderStacks()]
  528. # check in which stack the value has to be replaced
  529. for extruder_stack in extruder_stacks:
  530. if extruder_stack != self._active_container_stack and extruder_stack.getProperty(key, "value") != new_value:
  531. extruder_stack.userChanges.setProperty(key, "value", new_value) # TODO: nested property access, should be improved
  532. ## Copy the value of all manually changed settings of the current extruder to all other extruders.
  533. @pyqtSlot()
  534. def copyAllValuesToExtruders(self) -> None:
  535. if self._active_container_stack is None or self._global_container_stack is None:
  536. return
  537. extruder_stacks = list(self._global_container_stack.extruders.values())
  538. for extruder_stack in extruder_stacks:
  539. if extruder_stack != self._active_container_stack:
  540. for key in self._active_container_stack.userChanges.getAllKeys():
  541. new_value = self._active_container_stack.getProperty(key, "value")
  542. # check if the value has to be replaced
  543. extruder_stack.userChanges.setProperty(key, "value", new_value)
  544. @pyqtProperty(str, notify = activeVariantChanged)
  545. def activeVariantName(self) -> str:
  546. if self._active_container_stack:
  547. variant = self._active_container_stack.variant
  548. if variant:
  549. return variant.getName()
  550. return ""
  551. @pyqtProperty(str, notify = activeVariantChanged)
  552. def activeVariantId(self) -> str:
  553. if self._active_container_stack:
  554. variant = self._active_container_stack.variant
  555. if variant:
  556. return variant.getId()
  557. return ""
  558. @pyqtProperty(str, notify = activeVariantChanged)
  559. def activeVariantBuildplateName(self) -> str:
  560. if self._global_container_stack:
  561. variant = self._global_container_stack.variant
  562. if variant:
  563. return variant.getName()
  564. return ""
  565. @pyqtProperty(str, notify = globalContainerChanged)
  566. def activeDefinitionId(self) -> str:
  567. if self._global_container_stack:
  568. return self._global_container_stack.definition.id
  569. return ""
  570. ## Get the Definition ID to use to select quality profiles for the currently active machine
  571. # \returns DefinitionID (string) if found, empty string otherwise
  572. @pyqtProperty(str, notify = globalContainerChanged)
  573. def activeQualityDefinitionId(self) -> str:
  574. if self._global_container_stack:
  575. return getMachineDefinitionIDForQualitySearch(self._global_container_stack.definition)
  576. return ""
  577. ## Gets how the active definition calls variants
  578. # Caveat: per-definition-variant-title is currently not translated (though the fallback is)
  579. @pyqtProperty(str, notify = globalContainerChanged)
  580. def activeDefinitionVariantsName(self) -> str:
  581. fallback_title = catalog.i18nc("@label", "Nozzle")
  582. if self._global_container_stack:
  583. return self._global_container_stack.definition.getMetaDataEntry("variants_name", fallback_title)
  584. return fallback_title
  585. @pyqtSlot(str, str)
  586. def renameMachine(self, machine_id: str, new_name: str) -> None:
  587. container_registry = CuraContainerRegistry.getInstance()
  588. machine_stack = container_registry.findContainerStacks(id = machine_id)
  589. if machine_stack:
  590. new_name = container_registry.createUniqueName("machine", machine_stack[0].getName(), new_name, machine_stack[0].definition.getName())
  591. machine_stack[0].setName(new_name)
  592. self.globalContainerChanged.emit()
  593. @pyqtSlot(str)
  594. def removeMachine(self, machine_id: str) -> None:
  595. # If the machine that is being removed is the currently active machine, set another machine as the active machine.
  596. activate_new_machine = (self._global_container_stack and self._global_container_stack.getId() == machine_id)
  597. # activate a new machine before removing a machine because this is safer
  598. if activate_new_machine:
  599. machine_stacks = CuraContainerRegistry.getInstance().findContainerStacksMetadata(type = "machine")
  600. other_machine_stacks = [s for s in machine_stacks if s["id"] != machine_id]
  601. if other_machine_stacks:
  602. self.setActiveMachine(other_machine_stacks[0]["id"])
  603. metadata = CuraContainerRegistry.getInstance().findContainerStacksMetadata(id = machine_id)[0]
  604. network_key = metadata["um_network_key"] if "um_network_key" in metadata else None
  605. ExtruderManager.getInstance().removeMachineExtruders(machine_id)
  606. containers = CuraContainerRegistry.getInstance().findInstanceContainersMetadata(type = "user", machine = machine_id)
  607. for container in containers:
  608. CuraContainerRegistry.getInstance().removeContainer(container["id"])
  609. CuraContainerRegistry.getInstance().removeContainer(machine_id)
  610. # If the printer that is being removed is a network printer, the hidden printers have to be also removed
  611. if network_key:
  612. metadata_filter = {"um_network_key": network_key}
  613. hidden_containers = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine", **metadata_filter)
  614. if hidden_containers:
  615. # This reuses the method and remove all printers recursively
  616. self.removeMachine(hidden_containers[0].getId())
  617. @pyqtProperty(bool, notify = globalContainerChanged)
  618. def hasMaterials(self) -> bool:
  619. if self._global_container_stack:
  620. return Util.parseBool(self._global_container_stack.getMetaDataEntry("has_materials", False))
  621. return False
  622. @pyqtProperty(bool, notify = globalContainerChanged)
  623. def hasVariants(self) -> bool:
  624. if self._global_container_stack:
  625. return Util.parseBool(self._global_container_stack.getMetaDataEntry("has_variants", False))
  626. return False
  627. @pyqtProperty(bool, notify = globalContainerChanged)
  628. def hasVariantBuildplates(self) -> bool:
  629. if self._global_container_stack:
  630. return Util.parseBool(self._global_container_stack.getMetaDataEntry("has_variant_buildplates", False))
  631. return False
  632. ## The selected buildplate is compatible if it is compatible with all the materials in all the extruders
  633. @pyqtProperty(bool, notify = activeMaterialChanged)
  634. def variantBuildplateCompatible(self) -> bool:
  635. if not self._global_container_stack:
  636. return True
  637. buildplate_compatible = True # It is compatible by default
  638. extruder_stacks = self._global_container_stack.extruders.values()
  639. for stack in extruder_stacks:
  640. if not stack.isEnabled:
  641. continue
  642. material_container = stack.material
  643. if material_container == self._empty_material_container:
  644. continue
  645. if material_container.getMetaDataEntry("buildplate_compatible"):
  646. buildplate_compatible = buildplate_compatible and material_container.getMetaDataEntry("buildplate_compatible")[self.activeVariantBuildplateName]
  647. return buildplate_compatible
  648. ## The selected buildplate is usable if it is usable for all materials OR it is compatible for one but not compatible
  649. # for the other material but the buildplate is still usable
  650. @pyqtProperty(bool, notify = activeMaterialChanged)
  651. def variantBuildplateUsable(self) -> bool:
  652. if not self._global_container_stack:
  653. return True
  654. # Here the next formula is being calculated:
  655. # result = (not (material_left_compatible and material_right_compatible)) and
  656. # (material_left_compatible or material_left_usable) and
  657. # (material_right_compatible or material_right_usable)
  658. result = not self.variantBuildplateCompatible
  659. extruder_stacks = self._global_container_stack.extruders.values()
  660. for stack in extruder_stacks:
  661. material_container = stack.material
  662. if material_container == self._empty_material_container:
  663. continue
  664. buildplate_compatible = material_container.getMetaDataEntry("buildplate_compatible")[self.activeVariantBuildplateName] if material_container.getMetaDataEntry("buildplate_compatible") else True
  665. buildplate_usable = material_container.getMetaDataEntry("buildplate_recommended")[self.activeVariantBuildplateName] if material_container.getMetaDataEntry("buildplate_recommended") else True
  666. result = result and (buildplate_compatible or buildplate_usable)
  667. return result
  668. ## Get the Definition ID of a machine (specified by ID)
  669. # \param machine_id string machine id to get the definition ID of
  670. # \returns DefinitionID if found, None otherwise
  671. @pyqtSlot(str, result = str)
  672. def getDefinitionByMachineId(self, machine_id: str) -> Optional[str]:
  673. containers = CuraContainerRegistry.getInstance().findContainerStacks(id = machine_id)
  674. if containers:
  675. return containers[0].definition.getId()
  676. return None
  677. def getIncompatibleSettingsOnEnabledExtruders(self, container: InstanceContainer) -> List[str]:
  678. if self._global_container_stack is None:
  679. return []
  680. extruder_count = self._global_container_stack.getProperty("machine_extruder_count", "value")
  681. result = [] # type: List[str]
  682. for setting_instance in container.findInstances():
  683. setting_key = setting_instance.definition.key
  684. setting_enabled = self._global_container_stack.getProperty(setting_key, "enabled")
  685. if not setting_enabled:
  686. # A setting is not visible anymore
  687. result.append(setting_key)
  688. Logger.log("d", "Reset setting [%s] from [%s] because the setting is no longer enabled", setting_key, container)
  689. continue
  690. if not self._global_container_stack.getProperty(setting_key, "type") in ("extruder", "optional_extruder"):
  691. continue
  692. old_value = container.getProperty(setting_key, "value")
  693. if int(old_value) < 0:
  694. continue
  695. if int(old_value) >= extruder_count or not self._global_container_stack.extruders[str(old_value)].isEnabled:
  696. result.append(setting_key)
  697. Logger.log("d", "Reset setting [%s] in [%s] because its old value [%s] is no longer valid", setting_key, container, old_value)
  698. return result
  699. ## Update extruder number to a valid value when the number of extruders are changed, or when an extruder is changed
  700. def correctExtruderSettings(self) -> None:
  701. if self._global_container_stack is None:
  702. return
  703. for setting_key in self.getIncompatibleSettingsOnEnabledExtruders(self._global_container_stack.userChanges):
  704. self._global_container_stack.userChanges.removeInstance(setting_key)
  705. add_user_changes = self.getIncompatibleSettingsOnEnabledExtruders(self._global_container_stack.qualityChanges)
  706. for setting_key in add_user_changes:
  707. # Apply quality changes that are incompatible to user changes, so we do not change the quality changes itself.
  708. self._global_container_stack.userChanges.setProperty(setting_key, "value", self._default_extruder_position)
  709. if add_user_changes:
  710. caution_message = Message(catalog.i18nc(
  711. "@info:generic",
  712. "Settings have been changed to match the current availability of extruders: [%s]" % ", ".join(add_user_changes)),
  713. lifetime=0,
  714. title = catalog.i18nc("@info:title", "Settings updated"))
  715. caution_message.show()
  716. ## Set the amount of extruders on the active machine (global stack)
  717. # \param extruder_count int the number of extruders to set
  718. def setActiveMachineExtruderCount(self, extruder_count: int) -> None:
  719. if self._global_container_stack is None:
  720. return
  721. extruder_manager = self._application.getExtruderManager()
  722. definition_changes_container = self._global_container_stack.definitionChanges
  723. if not self._global_container_stack or definition_changes_container == self._empty_definition_changes_container:
  724. return
  725. previous_extruder_count = self._global_container_stack.getProperty("machine_extruder_count", "value")
  726. if extruder_count == previous_extruder_count:
  727. return
  728. definition_changes_container.setProperty("machine_extruder_count", "value", extruder_count)
  729. self.updateDefaultExtruder()
  730. self.updateNumberExtrudersEnabled()
  731. self.correctExtruderSettings()
  732. # Check to see if any objects are set to print with an extruder that will no longer exist
  733. root_node = self._application.getController().getScene().getRoot()
  734. for node in DepthFirstIterator(root_node): #type: ignore #Ignore type error because iter() should get called automatically by Python syntax.
  735. if node.getMeshData():
  736. extruder_nr = node.callDecoration("getActiveExtruderPosition")
  737. if extruder_nr is not None and int(extruder_nr) > extruder_count - 1:
  738. node.callDecoration("setActiveExtruder", extruder_manager.getExtruderStack(extruder_count - 1).getId())
  739. # Make sure one of the extruder stacks is active
  740. extruder_manager.setActiveExtruderIndex(0)
  741. # Move settable_per_extruder values out of the global container
  742. # After CURA-4482 this should not be the case anymore, but we still want to support older project files.
  743. global_user_container = self._global_container_stack.userChanges
  744. # Make sure extruder_stacks exists
  745. extruder_stacks = [] #type: List[ExtruderStack]
  746. if previous_extruder_count == 1:
  747. extruder_stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  748. global_user_container = self._global_container_stack.userChanges
  749. for setting_instance in global_user_container.findInstances():
  750. setting_key = setting_instance.definition.key
  751. settable_per_extruder = self._global_container_stack.getProperty(setting_key, "settable_per_extruder")
  752. if settable_per_extruder:
  753. limit_to_extruder = int(self._global_container_stack.getProperty(setting_key, "limit_to_extruder"))
  754. extruder_stack = extruder_stacks[max(0, limit_to_extruder)]
  755. extruder_stack.userChanges.setProperty(setting_key, "value", global_user_container.getProperty(setting_key, "value"))
  756. global_user_container.removeInstance(setting_key)
  757. # Signal that the global stack has changed
  758. self._application.globalContainerStackChanged.emit()
  759. self.forceUpdateAllSettings()
  760. @pyqtSlot(int, result = QObject)
  761. def getExtruder(self, position: int) -> Optional[ExtruderStack]:
  762. extruder = None
  763. if self._global_container_stack:
  764. extruder = self._global_container_stack.extruders.get(str(position))
  765. return extruder
  766. def updateDefaultExtruder(self) -> None:
  767. if self._global_container_stack is None:
  768. return
  769. extruder_items = sorted(self._global_container_stack.extruders.items())
  770. old_position = self._default_extruder_position
  771. new_default_position = "0"
  772. for position, extruder in extruder_items:
  773. if extruder.isEnabled:
  774. new_default_position = position
  775. break
  776. if new_default_position != old_position:
  777. self._default_extruder_position = new_default_position
  778. self.extruderChanged.emit()
  779. def updateNumberExtrudersEnabled(self) -> None:
  780. if self._global_container_stack is None:
  781. return
  782. definition_changes_container = self._global_container_stack.definitionChanges
  783. machine_extruder_count = self._global_container_stack.getProperty("machine_extruder_count", "value")
  784. extruder_count = 0
  785. for position, extruder in self._global_container_stack.extruders.items():
  786. if extruder.isEnabled and int(position) < machine_extruder_count:
  787. extruder_count += 1
  788. if self.numberExtrudersEnabled != extruder_count:
  789. definition_changes_container.setProperty("extruders_enabled_count", "value", extruder_count)
  790. self.numberExtrudersEnabledChanged.emit()
  791. @pyqtProperty(int, notify = numberExtrudersEnabledChanged)
  792. def numberExtrudersEnabled(self) -> int:
  793. if self._global_container_stack is None:
  794. return 1
  795. return self._global_container_stack.definitionChanges.getProperty("extruders_enabled_count", "value")
  796. @pyqtProperty(str, notify = extruderChanged)
  797. def defaultExtruderPosition(self) -> str:
  798. return self._default_extruder_position
  799. ## This will fire the propertiesChanged for all settings so they will be updated in the front-end
  800. @pyqtSlot()
  801. def forceUpdateAllSettings(self) -> None:
  802. if self._global_container_stack is None:
  803. return
  804. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  805. property_names = ["value", "resolve", "validationState"]
  806. for container in [self._global_container_stack] + list(self._global_container_stack.extruders.values()):
  807. for setting_key in container.getAllKeys():
  808. container.propertiesChanged.emit(setting_key, property_names)
  809. @pyqtSlot(int, bool)
  810. def setExtruderEnabled(self, position: int, enabled: bool) -> None:
  811. extruder = self.getExtruder(position)
  812. if not extruder or self._global_container_stack is None:
  813. Logger.log("w", "Could not find extruder on position %s", position)
  814. return
  815. extruder.setEnabled(enabled)
  816. self.updateDefaultExtruder()
  817. self.updateNumberExtrudersEnabled()
  818. self.correctExtruderSettings()
  819. # In case this extruder is being disabled and it's the currently selected one, switch to the default extruder
  820. if not enabled and position == ExtruderManager.getInstance().activeExtruderIndex:
  821. ExtruderManager.getInstance().setActiveExtruderIndex(int(self._default_extruder_position))
  822. # ensure that the quality profile is compatible with current combination, or choose a compatible one if available
  823. self._updateQualityWithMaterial()
  824. self.extruderChanged.emit()
  825. # update material compatibility color
  826. self.activeQualityGroupChanged.emit()
  827. # update items in SettingExtruder
  828. ExtruderManager.getInstance().extrudersChanged.emit(self._global_container_stack.getId())
  829. # Make sure the front end reflects changes
  830. self.forceUpdateAllSettings()
  831. # Also trigger the build plate compatibility to update
  832. self.activeMaterialChanged.emit()
  833. def _onMachineNameChanged(self) -> None:
  834. self.globalContainerChanged.emit()
  835. def _onMaterialNameChanged(self) -> None:
  836. self.activeMaterialChanged.emit()
  837. def _onQualityNameChanged(self) -> None:
  838. self.activeQualityChanged.emit()
  839. def _getContainerChangedSignals(self) -> List[Signal]:
  840. if self._global_container_stack is None:
  841. return []
  842. stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  843. stacks.append(self._global_container_stack)
  844. return [ s.containersChanged for s in stacks ]
  845. @pyqtSlot(str, str, str)
  846. def setSettingForAllExtruders(self, setting_name: str, property_name: str, property_value: str) -> None:
  847. if self._global_container_stack is None:
  848. return
  849. for key, extruder in self._global_container_stack.extruders.items():
  850. container = extruder.userChanges
  851. container.setProperty(setting_name, property_name, property_value)
  852. ## Reset all setting properties of a setting for all extruders.
  853. # \param setting_name The ID of the setting to reset.
  854. @pyqtSlot(str)
  855. def resetSettingForAllExtruders(self, setting_name: str) -> None:
  856. if self._global_container_stack is None:
  857. return
  858. for key, extruder in self._global_container_stack.extruders.items():
  859. container = extruder.userChanges
  860. container.removeInstance(setting_name)
  861. @pyqtProperty("QVariantList", notify = globalContainerChanged)
  862. def currentExtruderPositions(self) -> List[str]:
  863. if self._global_container_stack is None:
  864. return []
  865. return sorted(list(self._global_container_stack.extruders.keys()))
  866. ## Update _current_root_material_id when the current root material was changed.
  867. def _onRootMaterialChanged(self) -> None:
  868. self._current_root_material_id = {}
  869. if self._global_container_stack:
  870. for position in self._global_container_stack.extruders:
  871. self._current_root_material_id[position] = self._global_container_stack.extruders[position].material.getMetaDataEntry("base_file")
  872. @pyqtProperty("QVariant", notify = rootMaterialChanged)
  873. def currentRootMaterialId(self) -> Dict[str, str]:
  874. return self._current_root_material_id
  875. ## Return the variant names in the extruder stack(s).
  876. ## For the variant in the global stack, use activeVariantBuildplateName
  877. @pyqtProperty("QVariant", notify = activeVariantChanged)
  878. def activeVariantNames(self) -> Dict[str, str]:
  879. result = {}
  880. active_stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  881. for stack in active_stacks:
  882. variant_container = stack.variant
  883. position = stack.getMetaDataEntry("position")
  884. if variant_container and variant_container != self._empty_variant_container:
  885. result[position] = variant_container.getName()
  886. return result
  887. #
  888. # Sets all quality and quality_changes containers to empty_quality and empty_quality_changes containers
  889. # for all stacks in the currently active machine.
  890. #
  891. def _setEmptyQuality(self) -> None:
  892. if self._global_container_stack is None:
  893. return
  894. self._current_quality_group = None
  895. self._current_quality_changes_group = None
  896. self._global_container_stack.quality = self._empty_quality_container
  897. self._global_container_stack.qualityChanges = self._empty_quality_changes_container
  898. for extruder in self._global_container_stack.extruders.values():
  899. extruder.quality = self._empty_quality_container
  900. extruder.qualityChanges = self._empty_quality_changes_container
  901. self.activeQualityGroupChanged.emit()
  902. self.activeQualityChangesGroupChanged.emit()
  903. def _setQualityGroup(self, quality_group: Optional["QualityGroup"], empty_quality_changes: bool = True) -> None:
  904. if self._global_container_stack is None:
  905. return
  906. if quality_group is None:
  907. self._setEmptyQuality()
  908. return
  909. if quality_group.node_for_global is None or quality_group.node_for_global.getContainer() is None:
  910. return
  911. for node in quality_group.nodes_for_extruders.values():
  912. if node.getContainer() is None:
  913. return
  914. self._current_quality_group = quality_group
  915. if empty_quality_changes:
  916. self._current_quality_changes_group = None
  917. # Set quality and quality_changes for the GlobalStack
  918. self._global_container_stack.quality = quality_group.node_for_global.getContainer()
  919. if empty_quality_changes:
  920. self._global_container_stack.qualityChanges = self._empty_quality_changes_container
  921. # Set quality and quality_changes for each ExtruderStack
  922. for position, node in quality_group.nodes_for_extruders.items():
  923. self._global_container_stack.extruders[str(position)].quality = node.getContainer()
  924. if empty_quality_changes:
  925. self._global_container_stack.extruders[str(position)].qualityChanges = self._empty_quality_changes_container
  926. self.activeQualityGroupChanged.emit()
  927. self.activeQualityChangesGroupChanged.emit()
  928. def _fixQualityChangesGroupToNotSupported(self, quality_changes_group: "QualityChangesGroup") -> None:
  929. nodes = [quality_changes_group.node_for_global] + list(quality_changes_group.nodes_for_extruders.values())
  930. containers = [n.getContainer() for n in nodes if n is not None]
  931. for container in containers:
  932. if container:
  933. container.setMetaDataEntry("quality_type", "not_supported")
  934. quality_changes_group.quality_type = "not_supported"
  935. def _setQualityChangesGroup(self, quality_changes_group: "QualityChangesGroup") -> None:
  936. if self._global_container_stack is None:
  937. return #Can't change that.
  938. quality_type = quality_changes_group.quality_type
  939. # A custom quality can be created based on "not supported".
  940. # In that case, do not set quality containers to empty.
  941. quality_group = None
  942. if quality_type != "not_supported":
  943. quality_group_dict = self._quality_manager.getQualityGroups(self._global_container_stack)
  944. quality_group = quality_group_dict.get(quality_type)
  945. if quality_group is None:
  946. self._fixQualityChangesGroupToNotSupported(quality_changes_group)
  947. quality_changes_container = self._empty_quality_changes_container
  948. quality_container = self._empty_quality_container
  949. if quality_changes_group.node_for_global and quality_changes_group.node_for_global.getContainer():
  950. quality_changes_container = cast(InstanceContainer, quality_changes_group.node_for_global.getContainer())
  951. if quality_group is not None and quality_group.node_for_global and quality_group.node_for_global.getContainer():
  952. quality_container = quality_group.node_for_global.getContainer()
  953. self._global_container_stack.quality = quality_container
  954. self._global_container_stack.qualityChanges = quality_changes_container
  955. for position, extruder in self._global_container_stack.extruders.items():
  956. quality_changes_node = quality_changes_group.nodes_for_extruders.get(position)
  957. quality_node = None
  958. if quality_group is not None:
  959. quality_node = quality_group.nodes_for_extruders.get(position)
  960. quality_changes_container = self._empty_quality_changes_container
  961. quality_container = self._empty_quality_container
  962. if quality_changes_node and quality_changes_node.getContainer():
  963. quality_changes_container = cast(InstanceContainer, quality_changes_node.getContainer())
  964. if quality_node and quality_node.getContainer():
  965. quality_container = quality_node.getContainer()
  966. extruder.quality = quality_container
  967. extruder.qualityChanges = quality_changes_container
  968. self._current_quality_group = quality_group
  969. self._current_quality_changes_group = quality_changes_group
  970. self.activeQualityGroupChanged.emit()
  971. self.activeQualityChangesGroupChanged.emit()
  972. def _setVariantNode(self, position: str, container_node: "ContainerNode") -> None:
  973. if container_node.getContainer() is None or self._global_container_stack is None:
  974. return
  975. self._global_container_stack.extruders[position].variant = container_node.getContainer()
  976. self.activeVariantChanged.emit()
  977. def _setGlobalVariant(self, container_node: "ContainerNode") -> None:
  978. if self._global_container_stack is None:
  979. return
  980. self._global_container_stack.variant = container_node.getContainer()
  981. if not self._global_container_stack.variant:
  982. self._global_container_stack.variant = self._application.empty_variant_container
  983. def _setMaterial(self, position: str, container_node: Optional["ContainerNode"] = None) -> None:
  984. if self._global_container_stack is None:
  985. return
  986. if container_node and container_node.getContainer():
  987. self._global_container_stack.extruders[position].material = container_node.getContainer()
  988. root_material_id = container_node.getMetaDataEntry("base_file", None)
  989. else:
  990. self._global_container_stack.extruders[position].material = self._empty_material_container
  991. root_material_id = None
  992. # The _current_root_material_id is used in the MaterialMenu to see which material is selected
  993. if root_material_id != self._current_root_material_id[position]:
  994. self._current_root_material_id[position] = root_material_id
  995. self.rootMaterialChanged.emit()
  996. def activeMaterialsCompatible(self) -> bool:
  997. # check material - variant compatibility
  998. if self._global_container_stack is not None:
  999. if Util.parseBool(self._global_container_stack.getMetaDataEntry("has_materials", False)):
  1000. for position, extruder in self._global_container_stack.extruders.items():
  1001. if extruder.isEnabled and not extruder.material.getMetaDataEntry("compatible"):
  1002. return False
  1003. if not extruder.material.getMetaDataEntry("compatible"):
  1004. return False
  1005. return True
  1006. ## Update current quality type and machine after setting material
  1007. def _updateQualityWithMaterial(self, *args: Any) -> None:
  1008. if self._global_container_stack is None:
  1009. return
  1010. Logger.log("i", "Updating quality/quality_changes due to material change")
  1011. current_quality_type = None
  1012. if self._current_quality_group:
  1013. current_quality_type = self._current_quality_group.quality_type
  1014. candidate_quality_groups = self._quality_manager.getQualityGroups(self._global_container_stack)
  1015. available_quality_types = {qt for qt, g in candidate_quality_groups.items() if g.is_available}
  1016. Logger.log("d", "Current quality type = [%s]", current_quality_type)
  1017. if not self.activeMaterialsCompatible():
  1018. if current_quality_type is not None:
  1019. Logger.log("i", "Active materials are not compatible, setting all qualities to empty (Not Supported).")
  1020. self._setEmptyQuality()
  1021. return
  1022. if not available_quality_types:
  1023. if self._current_quality_changes_group is None:
  1024. Logger.log("i", "No available quality types found, setting all qualities to empty (Not Supported).")
  1025. self._setEmptyQuality()
  1026. return
  1027. if current_quality_type in available_quality_types:
  1028. Logger.log("i", "Current available quality type [%s] is available, applying changes.", current_quality_type)
  1029. self._setQualityGroup(candidate_quality_groups[current_quality_type], empty_quality_changes = False)
  1030. return
  1031. # The current quality type is not available so we use the preferred quality type if it's available,
  1032. # otherwise use one of the available quality types.
  1033. quality_type = sorted(list(available_quality_types))[0]
  1034. preferred_quality_type = self._global_container_stack.getMetaDataEntry("preferred_quality_type")
  1035. if preferred_quality_type in available_quality_types:
  1036. quality_type = preferred_quality_type
  1037. Logger.log("i", "The current quality type [%s] is not available, switching to [%s] instead",
  1038. current_quality_type, quality_type)
  1039. self._setQualityGroup(candidate_quality_groups[quality_type], empty_quality_changes = True)
  1040. def updateMaterialWithVariant(self, position: Optional[str]) -> None:
  1041. if self._global_container_stack is None:
  1042. return
  1043. if position is None:
  1044. position_list = list(self._global_container_stack.extruders.keys())
  1045. else:
  1046. position_list = [position]
  1047. buildplate_name = None
  1048. if self._global_container_stack.variant.getId() != "empty_variant":
  1049. buildplate_name = self._global_container_stack.variant.getName()
  1050. for position_item in position_list:
  1051. extruder = self._global_container_stack.extruders[position_item]
  1052. current_material_base_name = extruder.material.getMetaDataEntry("base_file")
  1053. current_nozzle_name = None
  1054. if extruder.variant.getId() != self._empty_variant_container.getId():
  1055. current_nozzle_name = extruder.variant.getMetaDataEntry("name")
  1056. from UM.Settings.Interfaces import PropertyEvaluationContext
  1057. from cura.Settings.CuraContainerStack import _ContainerIndexes
  1058. context = PropertyEvaluationContext(extruder)
  1059. context.context["evaluate_from_container_index"] = _ContainerIndexes.DefinitionChanges
  1060. material_diameter = extruder.getProperty("material_diameter", "value", context)
  1061. candidate_materials = self._material_manager.getAvailableMaterials(
  1062. self._global_container_stack.definition,
  1063. current_nozzle_name,
  1064. buildplate_name,
  1065. material_diameter)
  1066. if not candidate_materials:
  1067. self._setMaterial(position_item, container_node = None)
  1068. continue
  1069. if current_material_base_name in candidate_materials:
  1070. new_material = candidate_materials[current_material_base_name]
  1071. self._setMaterial(position_item, new_material)
  1072. continue
  1073. # The current material is not available, find the preferred one
  1074. material_node = self._material_manager.getDefaultMaterial(self._global_container_stack, position_item, current_nozzle_name)
  1075. if material_node is not None:
  1076. self._setMaterial(position_item, material_node)
  1077. ## Given a printer definition name, select the right machine instance. In case it doesn't exist, create a new
  1078. # instance with the same network key.
  1079. @pyqtSlot(str)
  1080. def switchPrinterType(self, machine_name: str) -> None:
  1081. # Don't switch if the user tries to change to the same type of printer
  1082. if self._global_container_stack is None or self.activeMachineDefinitionName == machine_name:
  1083. return
  1084. # Get the definition id corresponding to this machine name
  1085. machine_definition_id = CuraContainerRegistry.getInstance().findDefinitionContainers(name = machine_name)[0].getId()
  1086. # Try to find a machine with the same network key
  1087. new_machine = self.getMachine(machine_definition_id, metadata_filter = {"um_network_key": self.activeMachineNetworkKey})
  1088. # If there is no machine, then create a new one and set it to the non-hidden instance
  1089. if not new_machine:
  1090. new_machine = CuraStackBuilder.createMachine(machine_definition_id + "_sync", machine_definition_id)
  1091. if not new_machine:
  1092. return
  1093. new_machine.setMetaDataEntry("um_network_key", self.activeMachineNetworkKey)
  1094. new_machine.setMetaDataEntry("connect_group_name", self.activeMachineNetworkGroupName)
  1095. new_machine.setMetaDataEntry("hidden", False)
  1096. else:
  1097. Logger.log("i", "Found a %s with the key %s. Let's use it!", machine_name, self.activeMachineNetworkKey)
  1098. new_machine.setMetaDataEntry("hidden", False)
  1099. # Set the current printer instance to hidden (the metadata entry must exist)
  1100. self._global_container_stack.setMetaDataEntry("hidden", True)
  1101. self.setActiveMachine(new_machine.getId())
  1102. @pyqtSlot(QObject)
  1103. def applyRemoteConfiguration(self, configuration: ConfigurationModel) -> None:
  1104. if self._global_container_stack is None:
  1105. return
  1106. self.blurSettings.emit()
  1107. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1108. self.switchPrinterType(configuration.printerType)
  1109. for extruder_configuration in configuration.extruderConfigurations:
  1110. position = str(extruder_configuration.position)
  1111. variant_container_node = self._variant_manager.getVariantNode(self._global_container_stack.definition.getId(), extruder_configuration.hotendID)
  1112. material_container_node = self._material_manager.getMaterialNodeByType(self._global_container_stack,
  1113. position,
  1114. extruder_configuration.hotendID,
  1115. configuration.buildplateConfiguration,
  1116. extruder_configuration.material.guid)
  1117. if variant_container_node:
  1118. self._setVariantNode(position, variant_container_node)
  1119. else:
  1120. self._global_container_stack.extruders[position].variant = self._empty_variant_container
  1121. if material_container_node:
  1122. self._setMaterial(position, material_container_node)
  1123. else:
  1124. self._global_container_stack.extruders[position].material = self._empty_material_container
  1125. self.updateMaterialWithVariant(position)
  1126. if configuration.buildplateConfiguration is not None:
  1127. global_variant_container_node = self._variant_manager.getBuildplateVariantNode(self._global_container_stack.definition.getId(), configuration.buildplateConfiguration)
  1128. if global_variant_container_node:
  1129. self._setGlobalVariant(global_variant_container_node)
  1130. else:
  1131. self._global_container_stack.variant = self._empty_variant_container
  1132. else:
  1133. self._global_container_stack.variant = self._empty_variant_container
  1134. self._updateQualityWithMaterial()
  1135. # See if we need to show the Discard or Keep changes screen
  1136. if self.hasUserSettings and self._application.getPreferences().getValue("cura/active_mode") == 1:
  1137. self._application.discardOrKeepProfileChanges()
  1138. ## Find all container stacks that has the pair 'key = value' in its metadata and replaces the value with 'new_value'
  1139. def replaceContainersMetadata(self, key: str, value: str, new_value: str) -> None:
  1140. machines = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine")
  1141. for machine in machines:
  1142. if machine.getMetaDataEntry(key) == value:
  1143. machine.setMetaDataEntry(key, new_value)
  1144. ## This method checks if the name of the group stored in the definition container is correct.
  1145. # After updating from 3.2 to 3.3 some group names may be temporary. If there is a mismatch in the name of the group
  1146. # then all the container stacks are updated, both the current and the hidden ones.
  1147. def checkCorrectGroupName(self, device_id: str, group_name: str) -> None:
  1148. if self._global_container_stack and device_id == self.activeMachineNetworkKey:
  1149. # Check if the connect_group_name is correct. If not, update all the containers connected to the same printer
  1150. if self.activeMachineNetworkGroupName != group_name:
  1151. metadata_filter = {"um_network_key": self.activeMachineNetworkKey}
  1152. containers = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine", **metadata_filter)
  1153. for container in containers:
  1154. container.setMetaDataEntry("connect_group_name", group_name)
  1155. ## This method checks if there is an instance connected to the given network_key
  1156. def existNetworkInstances(self, network_key: str) -> bool:
  1157. metadata_filter = {"um_network_key": network_key}
  1158. containers = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine", **metadata_filter)
  1159. return bool(containers)
  1160. @pyqtSlot("QVariant")
  1161. def setGlobalVariant(self, container_node: "ContainerNode") -> None:
  1162. self.blurSettings.emit()
  1163. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1164. self._setGlobalVariant(container_node)
  1165. self.updateMaterialWithVariant(None) # Update all materials
  1166. self._updateQualityWithMaterial()
  1167. @pyqtSlot(str, str)
  1168. def setMaterialById(self, position: str, root_material_id: str) -> None:
  1169. if self._global_container_stack is None:
  1170. return
  1171. buildplate_name = None
  1172. if self._global_container_stack.variant.getId() != "empty_variant":
  1173. buildplate_name = self._global_container_stack.variant.getName()
  1174. machine_definition_id = self._global_container_stack.definition.id
  1175. position = str(position)
  1176. extruder_stack = self._global_container_stack.extruders[position]
  1177. nozzle_name = extruder_stack.variant.getName()
  1178. material_diameter = extruder_stack.approximateMaterialDiameter
  1179. material_node = self._material_manager.getMaterialNode(machine_definition_id, nozzle_name, buildplate_name,
  1180. material_diameter, root_material_id)
  1181. self.setMaterial(position, material_node)
  1182. ## global_stack: if you want to provide your own global_stack instead of the current active one
  1183. # if you update an active machine, special measures have to be taken.
  1184. @pyqtSlot(str, "QVariant")
  1185. def setMaterial(self, position: str, container_node, global_stack: Optional["GlobalStack"] = None) -> None:
  1186. if global_stack is not None and global_stack != self._global_container_stack:
  1187. global_stack.extruders[position].material = container_node.getContainer()
  1188. return
  1189. position = str(position)
  1190. self.blurSettings.emit()
  1191. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1192. self._setMaterial(position, container_node)
  1193. self._updateQualityWithMaterial()
  1194. # See if we need to show the Discard or Keep changes screen
  1195. if self.hasUserSettings and self._application.getPreferences().getValue("cura/active_mode") == 1:
  1196. self._application.discardOrKeepProfileChanges()
  1197. @pyqtSlot(str, str)
  1198. def setVariantByName(self, position: str, variant_name: str) -> None:
  1199. if self._global_container_stack is None:
  1200. return
  1201. machine_definition_id = self._global_container_stack.definition.id
  1202. variant_node = self._variant_manager.getVariantNode(machine_definition_id, variant_name)
  1203. self.setVariant(position, variant_node)
  1204. @pyqtSlot(str, "QVariant")
  1205. def setVariant(self, position: str, container_node: "ContainerNode") -> None:
  1206. position = str(position)
  1207. self.blurSettings.emit()
  1208. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1209. self._setVariantNode(position, container_node)
  1210. self.updateMaterialWithVariant(position)
  1211. self._updateQualityWithMaterial()
  1212. # See if we need to show the Discard or Keep changes screen
  1213. if self.hasUserSettings and self._application.getPreferences().getValue("cura/active_mode") == 1:
  1214. self._application.discardOrKeepProfileChanges()
  1215. @pyqtSlot(str)
  1216. def setQualityGroupByQualityType(self, quality_type: str) -> None:
  1217. if self._global_container_stack is None:
  1218. return
  1219. # Get all the quality groups for this global stack and filter out by quality_type
  1220. quality_group_dict = self._quality_manager.getQualityGroups(self._global_container_stack)
  1221. quality_group = quality_group_dict[quality_type]
  1222. self.setQualityGroup(quality_group)
  1223. ## Optionally provide global_stack if you want to use your own
  1224. # The active global_stack is treated differently.
  1225. @pyqtSlot(QObject)
  1226. def setQualityGroup(self, quality_group: "QualityGroup", no_dialog: bool = False, global_stack: Optional["GlobalStack"] = None) -> None:
  1227. if global_stack is not None and global_stack != self._global_container_stack:
  1228. if quality_group is None:
  1229. Logger.log("e", "Could not set quality group because quality group is None")
  1230. return
  1231. if quality_group.node_for_global is None:
  1232. Logger.log("e", "Could not set quality group [%s] because it has no node_for_global", str(quality_group))
  1233. return
  1234. # This is not changing the quality for the active machine !!!!!!!!
  1235. global_stack.quality = quality_group.node_for_global.getContainer()
  1236. for extruder_nr, extruder_stack in global_stack.extruders.items():
  1237. quality_container = self._empty_quality_container
  1238. if extruder_nr in quality_group.nodes_for_extruders:
  1239. container = quality_group.nodes_for_extruders[extruder_nr].getContainer()
  1240. quality_container = container if container is not None else quality_container
  1241. extruder_stack.quality = quality_container
  1242. return
  1243. self.blurSettings.emit()
  1244. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1245. self._setQualityGroup(quality_group)
  1246. # See if we need to show the Discard or Keep changes screen
  1247. if not no_dialog and self.hasUserSettings and self._application.getPreferences().getValue("cura/active_mode") == 1:
  1248. self._application.discardOrKeepProfileChanges()
  1249. @pyqtProperty(QObject, fset = setQualityGroup, notify = activeQualityGroupChanged)
  1250. def activeQualityGroup(self) -> Optional["QualityGroup"]:
  1251. return self._current_quality_group
  1252. @pyqtSlot(QObject)
  1253. def setQualityChangesGroup(self, quality_changes_group: "QualityChangesGroup", no_dialog: bool = False) -> None:
  1254. self.blurSettings.emit()
  1255. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1256. self._setQualityChangesGroup(quality_changes_group)
  1257. # See if we need to show the Discard or Keep changes screen
  1258. if not no_dialog and self.hasUserSettings and self._application.getPreferences().getValue("cura/active_mode") == 1:
  1259. self._application.discardOrKeepProfileChanges()
  1260. @pyqtSlot()
  1261. def resetToUseDefaultQuality(self) -> None:
  1262. if self._global_container_stack is None:
  1263. return
  1264. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1265. self._setQualityGroup(self._current_quality_group)
  1266. for stack in [self._global_container_stack] + list(self._global_container_stack.extruders.values()):
  1267. stack.userChanges.clear()
  1268. @pyqtProperty(QObject, fset = setQualityChangesGroup, notify = activeQualityChangesGroupChanged)
  1269. def activeQualityChangesGroup(self) -> Optional["QualityChangesGroup"]:
  1270. return self._current_quality_changes_group
  1271. @pyqtProperty(str, notify = activeQualityGroupChanged)
  1272. def activeQualityOrQualityChangesName(self) -> str:
  1273. name = self._empty_quality_container.getName()
  1274. if self._current_quality_changes_group:
  1275. name = self._current_quality_changes_group.name
  1276. elif self._current_quality_group:
  1277. name = self._current_quality_group.name
  1278. return name
  1279. def _updateUponMaterialMetadataChange(self) -> None:
  1280. if self._global_container_stack is None:
  1281. return
  1282. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1283. self.updateMaterialWithVariant(None)
  1284. self._updateQualityWithMaterial()