ExtruderManager.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 = -1
  20. UM.Application.getInstance().globalContainerStackChanged.connect(self.__globalContainerStackChanged)
  21. self._addCurrentMachineExtruders()
  22. ## Gets the unique identifier of the currently active extruder stack.
  23. #
  24. # The currently active extruder stack is the stack that is currently being
  25. # edited.
  26. #
  27. # \return The unique ID of the currently active extruder stack.
  28. @pyqtProperty(str, notify = activeExtruderChanged)
  29. def activeExtruderStackId(self):
  30. if not UM.Application.getInstance().getGlobalContainerStack():
  31. return None #No active machine, so no active extruder.
  32. try:
  33. return self._extruder_trains[UM.Application.getInstance().getGlobalContainerStack().getBottom().getId()][str(self._active_extruder_index)].getId()
  34. 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.
  35. return None
  36. ## The instance of the singleton pattern.
  37. #
  38. # It's None if the extruder manager hasn't been created yet.
  39. __instance = None
  40. ## Gets an instance of the extruder manager, or creates one if no instance
  41. # exists yet.
  42. #
  43. # This is an implementation of singleton. If an extruder manager already
  44. # exists, it is re-used.
  45. #
  46. # \return The extruder manager.
  47. @classmethod
  48. def getInstance(cls):
  49. if not cls.__instance:
  50. cls.__instance = ExtruderManager()
  51. return cls.__instance
  52. ## Changes the active extruder by index.
  53. #
  54. # \param index The index of the new active extruder.
  55. @pyqtSlot(int)
  56. def setActiveExtruderIndex(self, index):
  57. self._active_extruder_index = index
  58. self.activeExtruderChanged.emit()
  59. @pyqtProperty(int, notify = activeExtruderChanged)
  60. def activeExtruderIndex(self):
  61. return self._active_extruder_index
  62. def getActiveExtruderStack(self):
  63. global_container_stack = UM.Application.getInstance().getGlobalContainerStack()
  64. if global_container_stack:
  65. global_definition_container = UM.Application.getInstance().getGlobalContainerStack().getBottom()
  66. if global_definition_container:
  67. if global_definition_container.getId() in self._extruder_trains:
  68. if str(self._active_extruder_index) in self._extruder_trains[global_definition_container.getId()]:
  69. return self._extruder_trains[global_definition_container.getId()][str(self._active_extruder_index)]
  70. return None
  71. ## Adds all extruders of a specific machine definition to the extruder
  72. # manager.
  73. #
  74. # \param machine_definition The machine to add the extruders for.
  75. def addMachineExtruders(self, machine_definition):
  76. changed = False
  77. machine_id = machine_definition.getId()
  78. if machine_id not in self._extruder_trains:
  79. self._extruder_trains[machine_id] = { }
  80. changed = True
  81. container_registry = UM.Settings.ContainerRegistry.getInstance()
  82. if container_registry:
  83. #Add the extruder trains that don't exist yet.
  84. for extruder_definition in container_registry.findDefinitionContainers(machine = machine_definition.getId()):
  85. position = extruder_definition.getMetaDataEntry("position", None)
  86. if not position:
  87. UM.Logger.log("w", "Extruder definition %s specifies no position metadata entry.", extruder_definition.getId())
  88. if not container_registry.findContainerStacks(machine = machine_id, position = position): #Doesn't exist yet.
  89. self.createExtruderTrain(extruder_definition, machine_definition, position)
  90. changed = True
  91. #Gets the extruder trains that we just created as well as any that still existed.
  92. extruder_trains = container_registry.findContainerStacks(type = "extruder_train", machine = machine_definition.getId())
  93. for extruder_train in extruder_trains:
  94. self._extruder_trains[machine_id][extruder_train.getMetaDataEntry("position")] = extruder_train
  95. #Ensure that the extruder train stacks are linked to global stack.
  96. extruder_train.setNextStack(UM.Application.getInstance().getGlobalContainerStack())
  97. changed = True
  98. if changed:
  99. self.extrudersChanged.emit(machine_definition)
  100. ## Creates a container stack for an extruder train.
  101. #
  102. # The container stack has an extruder definition at the bottom, which is
  103. # linked to a machine definition. Then it has a variant profile, a material
  104. # profile, a quality profile and a user profile, in that order.
  105. #
  106. # The resulting container stack is added to the registry.
  107. #
  108. # \param extruder_definition The extruder to create the extruder train
  109. # for.
  110. # \param machine_definition The machine that the extruder train belongs
  111. # to.
  112. # \param position The position of this extruder train in the extruder
  113. # slots of the machine.
  114. def createExtruderTrain(self, extruder_definition, machine_definition, position):
  115. #Cache some things.
  116. container_registry = UM.Settings.ContainerRegistry.getInstance()
  117. machine_id = machine_definition.getId()
  118. #Create a container stack for this extruder.
  119. extruder_stack_id = container_registry.uniqueName(extruder_definition.getId())
  120. container_stack = UM.Settings.ContainerStack(extruder_stack_id)
  121. container_stack.setName(extruder_definition.getName()) #Take over the display name to display the stack with.
  122. container_stack.addMetaDataEntry("type", "extruder_train")
  123. container_stack.addMetaDataEntry("machine", machine_definition.getId())
  124. container_stack.addMetaDataEntry("position", position)
  125. container_stack.addContainer(extruder_definition)
  126. #Find the variant to use for this extruder.
  127. variant = container_registry.getEmptyInstanceContainer()
  128. if machine_definition.getMetaDataEntry("has_variants"):
  129. #First add any variant. Later, overwrite with preference if the preference is valid.
  130. variants = container_registry.findInstanceContainers(definition = machine_id, type = "variant")
  131. if len(variants) >= 1:
  132. variant = variants[0]
  133. preferred_variant_id = machine_definition.getMetaDataEntry("preferred_variant")
  134. if preferred_variant_id:
  135. preferred_variants = container_registry.findInstanceContainers(id = preferred_variant_id, type = "variant")
  136. if len(preferred_variants) >= 1:
  137. variant = preferred_variants[0]
  138. else:
  139. 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)
  140. #And leave it at the default variant.
  141. container_stack.addContainer(variant)
  142. #Find a material to use for this variant.
  143. material = container_registry.getEmptyInstanceContainer()
  144. if machine_definition.getMetaDataEntry("has_materials"):
  145. #First add any material. Later, overwrite with preference if the preference is valid.
  146. if machine_definition.getMetaDataEntry("has_variant_materials", default = "False") == "True":
  147. materials = container_registry.findInstanceContainers(type = "material", definition = machine_id, variant = variant.getId())
  148. else:
  149. materials = container_registry.findInstanceContainers(type = "material", definition = machine_id)
  150. if len(materials) >= 1:
  151. material = materials[0]
  152. preferred_material_id = machine_definition.getMetaDataEntry("preferred_material")
  153. if preferred_material_id:
  154. search_criteria = { "type": "material", "id": preferred_material_id}
  155. if machine_definition.getMetaDataEntry("has_machine_materials"):
  156. search_criteria["definition"] = machine_definition.id
  157. if machine_definition.getMetaDataEntry("has_variants") and variant:
  158. search_criteria["variant"] = variant.id
  159. else:
  160. search_criteria["definition"] = "fdmprinter"
  161. preferred_materials = container_registry.findInstanceContainers(**search_criteria)
  162. if len(preferred_materials) >= 1:
  163. material = preferred_materials[0]
  164. else:
  165. 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)
  166. #And leave it at the default material.
  167. container_stack.addContainer(material)
  168. #Find a quality to use for this extruder.
  169. quality = container_registry.getEmptyInstanceContainer()
  170. #First add any quality. Later, overwrite with preference if the preference is valid.
  171. qualities = container_registry.findInstanceContainers(type = "quality")
  172. if len(qualities) >= 1:
  173. quality = qualities[0]
  174. preferred_quality_id = machine_definition.getMetaDataEntry("preferred_quality")
  175. if preferred_quality_id:
  176. preferred_quality = container_registry.findInstanceContainers(id = preferred_quality_id, type = "quality")
  177. if len(preferred_quality) >= 1:
  178. quality = preferred_quality[0]
  179. else:
  180. 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)
  181. #And leave it at the default quality.
  182. container_stack.addContainer(quality)
  183. user_profile = container_registry.findInstanceContainers(id = extruder_stack_id + "_current_settings")
  184. if user_profile: #There was already a user profile, loaded from settings.
  185. user_profile = user_profile[0]
  186. else:
  187. user_profile = UM.Settings.InstanceContainer(extruder_stack_id + "_current_settings") #Add an empty user profile.
  188. user_profile.addMetaDataEntry("type", "user")
  189. user_profile.setDefinition(machine_definition)
  190. container_registry.addContainer(user_profile)
  191. container_stack.addContainer(user_profile)
  192. container_stack.setNextStack(UM.Application.getInstance().getGlobalContainerStack())
  193. container_registry.addContainer(container_stack)
  194. ## Generates extruders for a specific machine.
  195. #
  196. # \param machine_id The machine to get the extruders of.
  197. def getMachineExtruders(self, machine_id):
  198. if machine_id not in self._extruder_trains:
  199. UM.Logger.log("w", "Tried to get the extruder trains for machine %s, which doesn't exist.", machine_id)
  200. return
  201. for name in self._extruder_trains[machine_id]:
  202. yield self._extruder_trains[machine_id][name]
  203. def __globalContainerStackChanged(self):
  204. self._addCurrentMachineExtruders()
  205. self.activeExtruderChanged.emit()
  206. ## Adds the extruders of the currently active machine.
  207. def _addCurrentMachineExtruders(self):
  208. global_stack = UM.Application.getInstance().getGlobalContainerStack()
  209. if global_stack and global_stack.getBottom():
  210. self.addMachineExtruders(global_stack.getBottom())