ExtruderManager.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from PyQt5.QtCore import pyqtSignal, pyqtProperty, pyqtSlot, QObject, QVariant #For communicating data and events to Qt.
  4. import UM.Application #To get the global container stack to find the current machine.
  5. import UM.Logger
  6. import UM.Settings.ContainerRegistry #Finding containers by ID.
  7. ## Manages all existing extruder stacks.
  8. #
  9. # This keeps a list of extruder stacks for each machine.
  10. class ExtruderManager(QObject):
  11. ## Signal to notify other components when the list of extruders changes.
  12. extrudersChanged = pyqtSignal(QVariant)
  13. ## Notify when the user switches the currently active extruder.
  14. activeExtruderChanged = pyqtSignal()
  15. ## Registers listeners and such to listen to changes to the extruders.
  16. def __init__(self, parent = None):
  17. super().__init__(parent)
  18. self._extruder_trains = { } #Per machine, a dictionary of extruder container stack IDs.
  19. self._active_extruder_index = 0
  20. UM.Application.getInstance().globalContainerStackChanged.connect(self._addCurrentMachineExtruders)
  21. ## Gets the unique identifier of the currently active extruder stack.
  22. #
  23. # The currently active extruder stack is the stack that is currently being
  24. # edited.
  25. #
  26. # \return The unique ID of the currently active extruder stack.
  27. @pyqtProperty(str, notify = activeExtruderChanged)
  28. def activeExtruderStackId(self):
  29. if not UM.Application.getInstance().getGlobalContainerStack():
  30. return None #No active machine, so no active extruder.
  31. try:
  32. return self._extruder_trains[UM.Application.getInstance().getGlobalContainerStack().getBottom().getId()][str(self._active_extruder_index)].getId()
  33. except KeyError: #Extruder index could be -1 if the global tab is selected, or the entry doesn't exist if the machine definition is wrong.
  34. return None
  35. ## The instance of the singleton pattern.
  36. #
  37. # It's None if the extruder manager hasn't been created yet.
  38. __instance = None
  39. ## Gets an instance of the extruder manager, or creates one if no instance
  40. # exists yet.
  41. #
  42. # This is an implementation of singleton. If an extruder manager already
  43. # exists, it is re-used.
  44. #
  45. # \return The extruder manager.
  46. @classmethod
  47. def getInstance(cls):
  48. if not cls.__instance:
  49. cls.__instance = ExtruderManager()
  50. return cls.__instance
  51. ## Changes the active extruder by index.
  52. #
  53. # \param index The index of the new active extruder.
  54. @pyqtSlot(int)
  55. def setActiveExtruderIndex(self, index):
  56. self._active_extruder_index = index
  57. self.activeExtruderChanged.emit()
  58. def getActiveExtruderStack(self):
  59. global_container_stack = UM.Application.getInstance().getGlobalContainerStack()
  60. if global_container_stack:
  61. global_definition_container = UM.Application.getInstance().getGlobalContainerStack().getBottom()
  62. if global_definition_container:
  63. if global_definition_container.getId() in self._extruder_trains:
  64. if str(self._active_extruder_index) in self._extruder_trains[global_definition_container.getId()]:
  65. return self._extruder_trains[global_definition_container.getId()][str(self._active_extruder_index)]
  66. ## Adds all extruders of a specific machine definition to the extruder
  67. # manager.
  68. #
  69. # \param machine_definition The machine to add the extruders for.
  70. def addMachineExtruders(self, machine_definition):
  71. machine_id = machine_definition.getId()
  72. if machine_id not in self._extruder_trains:
  73. self._extruder_trains[machine_id] = { }
  74. container_registry = UM.Settings.ContainerRegistry.getInstance()
  75. if not container_registry: #Then we shouldn't have any machine definition either. In any case, there are no extruder trains then so bye bye.
  76. return
  77. #Add the extruder trains that don't exist yet.
  78. for extruder_definition in container_registry.findDefinitionContainers(machine = machine_definition.getId()):
  79. position = extruder_definition.getMetaDataEntry("position", None)
  80. if not position:
  81. UM.Logger.log("w", "Extruder definition %s specifies no position metadata entry.", extruder_definition.getId())
  82. if not container_registry.findContainerStacks(machine = machine_id, position = position): #Doesn't exist yet.
  83. self.createExtruderTrain(extruder_definition, machine_definition, position)
  84. #Gets the extruder trains that we just created as well as any that still existed.
  85. extruder_trains = container_registry.findContainerStacks(type = "extruder_train", machine = machine_definition.getId())
  86. for extruder_train in extruder_trains:
  87. self._extruder_trains[machine_id][extruder_train.getMetaDataEntry("position")] = extruder_train
  88. #Ensure that the extruder train stacks are linked to global stack.
  89. extruder_train.setNextStack(UM.Application.getInstance().getGlobalContainerStack())
  90. if extruder_trains:
  91. self.extrudersChanged.emit(machine_definition)
  92. ## Creates a container stack for an extruder train.
  93. #
  94. # The container stack has an extruder definition at the bottom, which is
  95. # linked to a machine definition. Then it has a variant profile, a material
  96. # profile, a quality profile and a user profile, in that order.
  97. #
  98. # The resulting container stack is added to the registry.
  99. #
  100. # \param extruder_definition The extruder to create the extruder train
  101. # for.
  102. # \param machine_definition The machine that the extruder train belongs
  103. # to.
  104. # \param position The position of this extruder train in the extruder
  105. # slots of the machine.
  106. def createExtruderTrain(self, extruder_definition, machine_definition, position):
  107. #Cache some things.
  108. container_registry = UM.Settings.ContainerRegistry.getInstance()
  109. machine_id = machine_definition.getId()
  110. #Create a container stack for this extruder.
  111. extruder_stack_id = container_registry.uniqueName(extruder_definition.getId())
  112. container_stack = UM.Settings.ContainerStack(extruder_stack_id)
  113. container_stack.setName(extruder_definition.getName()) #Take over the display name to display the stack with.
  114. container_stack.addMetaDataEntry("type", "extruder_train")
  115. container_stack.addMetaDataEntry("machine", machine_definition.getId())
  116. container_stack.addMetaDataEntry("position", position)
  117. container_stack.addContainer(extruder_definition)
  118. #Find the variant to use for this extruder.
  119. variant = container_registry.getEmptyInstanceContainer()
  120. if machine_definition.getMetaDataEntry("has_variants"):
  121. #First add any variant. Later, overwrite with preference if the preference is valid.
  122. variants = container_registry.findInstanceContainers(definition = machine_id, type = "variant")
  123. if len(variants) >= 1:
  124. variant = variants[0]
  125. preferred_variant_id = machine_definition.getMetaDataEntry("preferred_variant")
  126. if preferred_variant_id:
  127. preferred_variants = container_registry.findInstanceContainers(id = preferred_variant_id, type = "variant")
  128. if len(preferred_variants) >= 1:
  129. variant = preferred_variants[0]
  130. else:
  131. UM.Logger.log("w", "The preferred variant \"%s\" of machine %s doesn't exist or is not a variant profile.", preferred_variant_id, machine_id)
  132. #And leave it at the default variant.
  133. container_stack.addContainer(variant)
  134. #Find a material to use for this variant.
  135. material = container_registry.getEmptyInstanceContainer()
  136. if machine_definition.getMetaDataEntry("has_materials"):
  137. #First add any material. Later, overwrite with preference if the preference is valid.
  138. if machine_definition.getMetaDataEntry("has_variant_materials", default = "False") == "True":
  139. materials = container_registry.findInstanceContainers(type = "material", definition = machine_id, variant = variant.getId())
  140. else:
  141. materials = container_registry.findInstanceContainers(type = "material", definition = machine_id)
  142. if len(materials) >= 1:
  143. material = materials[0]
  144. preferred_material_id = machine_definition.getMetaDataEntry("preferred_material")
  145. if preferred_material_id:
  146. preferred_materials = container_registry.findInstanceContainers(id = preferred_material_id, type = "material")
  147. if len(preferred_materials) >= 1:
  148. material = preferred_materials[0]
  149. else:
  150. UM.Logger.log("w", "The preferred material \"%s\" of machine %s doesn't exist or is not a material profile.", preferred_material_id, machine_id)
  151. #And leave it at the default material.
  152. container_stack.addContainer(material)
  153. #Find a quality to use for this extruder.
  154. quality = container_registry.getEmptyInstanceContainer()
  155. #First add any quality. Later, overwrite with preference if the preference is valid.
  156. qualities = container_registry.findInstanceContainers(type = "quality")
  157. if len(qualities) >= 1:
  158. quality = qualities[0]
  159. preferred_quality_id = machine_definition.getMetaDataEntry("preferred_quality")
  160. if preferred_quality_id:
  161. preferred_quality = container_registry.findInstanceContainers(id = preferred_quality_id, type = "quality")
  162. if len(preferred_quality) >= 1:
  163. quality = preferred_quality[0]
  164. else:
  165. UM.Logger.log("w", "The preferred quality \"%s\" of machine %s doesn't exist or is not a quality profile.", preferred_quality_id, machine_id)
  166. #And leave it at the default quality.
  167. container_stack.addContainer(quality)
  168. user_profile = container_registry.findInstanceContainers(id = extruder_stack_id + "_current_settings")
  169. if user_profile: #There was already a user profile, loaded from settings.
  170. user_profile = user_profile[0]
  171. else:
  172. user_profile = UM.Settings.InstanceContainer(extruder_stack_id + "_current_settings") #Add an empty user profile.
  173. user_profile.addMetaDataEntry("type", "user")
  174. user_profile.setDefinition(machine_definition)
  175. container_registry.addContainer(user_profile)
  176. container_stack.addContainer(user_profile)
  177. container_stack.setNextStack(UM.Application.getInstance().getGlobalContainerStack())
  178. container_registry.addContainer(container_stack)
  179. ## Generates extruders for a specific machine.
  180. #
  181. # \param machine_id The machine to get the extruders of.
  182. def getMachineExtruders(self, machine_id):
  183. if machine_id not in self._extruder_trains:
  184. UM.Logger.log("w", "Tried to get the extruder trains for machine %s, which doesn't exist.", machine_id)
  185. return
  186. for name in self._extruder_trains[machine_id]:
  187. yield self._extruder_trains[machine_id][name]
  188. ## Adds the extruders of the currently active machine.
  189. def _addCurrentMachineExtruders(self):
  190. global_stack = UM.Application.getInstance().getGlobalContainerStack()
  191. if global_stack and global_stack.getBottom():
  192. self.addMachineExtruders(global_stack.getBottom())