MachineSettingsAction.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import pyqtProperty, pyqtSignal
  4. from UM.FlameProfiler import pyqtSlot
  5. from cura.MachineAction import MachineAction
  6. from UM.Application import Application
  7. from UM.Preferences import Preferences
  8. from UM.Settings.InstanceContainer import InstanceContainer
  9. from UM.Settings.ContainerRegistry import ContainerRegistry
  10. from UM.Settings.DefinitionContainer import DefinitionContainer
  11. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  12. from UM.Logger import Logger
  13. from cura.CuraApplication import CuraApplication
  14. from cura.Settings.ExtruderManager import ExtruderManager
  15. from cura.Settings.CuraStackBuilder import CuraStackBuilder
  16. import UM.i18n
  17. catalog = UM.i18n.i18nCatalog("cura")
  18. ## This action allows for certain settings that are "machine only") to be modified.
  19. # It automatically detects machine definitions that it knows how to change and attaches itself to those.
  20. class MachineSettingsAction(MachineAction):
  21. def __init__(self, parent = None):
  22. super().__init__("MachineSettingsAction", catalog.i18nc("@action", "Machine Settings"))
  23. self._qml_url = "MachineSettingsAction.qml"
  24. self._global_container_stack = None
  25. self._container_index = 0
  26. self._container_registry = ContainerRegistry.getInstance()
  27. self._container_registry.containerAdded.connect(self._onContainerAdded)
  28. self._container_registry.containerRemoved.connect(self._onContainerRemoved)
  29. Application.getInstance().globalContainerStackChanged.connect(self._onGlobalContainerChanged)
  30. ExtruderManager.getInstance().activeExtruderChanged.connect(self._onActiveExtruderStackChanged)
  31. self._empty_container = self._container_registry.getEmptyInstanceContainer()
  32. self._backend = Application.getInstance().getBackend()
  33. def _onContainerAdded(self, container):
  34. # Add this action as a supported action to all machine definitions
  35. if isinstance(container, DefinitionContainer) and container.getMetaDataEntry("type") == "machine":
  36. Application.getInstance().getMachineActionManager().addSupportedAction(container.getId(), self.getKey())
  37. def _onContainerRemoved(self, container):
  38. # Remove definition_changes containers when a stack is removed
  39. if container.getMetaDataEntry("type") in ["machine", "extruder_train"]:
  40. definition_changes_container = container.definitionChanges
  41. if definition_changes_container == self._empty_container:
  42. return
  43. self._container_registry.removeContainer(definition_changes_container.getId())
  44. def _reset(self):
  45. if not self._global_container_stack:
  46. return
  47. # Make sure there is a definition_changes container to store the machine settings
  48. definition_changes_container = self._global_container_stack.definitionChanges
  49. if definition_changes_container == self._empty_container:
  50. definition_changes_container = CuraStackBuilder.createDefinitionChangesContainer(
  51. self._global_container_stack, self._global_container_stack.getName() + "_settings")
  52. # Notify the UI in which container to store the machine settings data
  53. container_index = self._global_container_stack.getContainerIndex(definition_changes_container)
  54. if container_index != self._container_index:
  55. self._container_index = container_index
  56. self.containerIndexChanged.emit()
  57. # Disable auto-slicing while the MachineAction is showing
  58. if self._backend: # This sometimes triggers before backend is loaded.
  59. self._backend.disableTimer()
  60. @pyqtSlot()
  61. def onFinishAction(self):
  62. # Restore autoslicing when the machineaction is dismissed
  63. if self._backend.determineAutoSlicing():
  64. self._backend.tickle()
  65. def _onActiveExtruderStackChanged(self):
  66. extruder_container_stack = ExtruderManager.getInstance().getActiveExtruderStack()
  67. if not self._global_container_stack or not extruder_container_stack:
  68. return
  69. # Make sure there is a definition_changes container to store the machine settings
  70. definition_changes_container = extruder_container_stack.definitionChanges
  71. if definition_changes_container == self._empty_container:
  72. definition_changes_container = CuraStackBuilder.createDefinitionChangesContainer(
  73. extruder_container_stack, extruder_container_stack.getId() + "_settings")
  74. containerIndexChanged = pyqtSignal()
  75. @pyqtProperty(int, notify = containerIndexChanged)
  76. def containerIndex(self):
  77. return self._container_index
  78. def _onGlobalContainerChanged(self):
  79. self._global_container_stack = Application.getInstance().getGlobalContainerStack()
  80. # This additional emit is needed because we cannot connect a UM.Signal directly to a pyqtSignal
  81. self.globalContainerChanged.emit()
  82. globalContainerChanged = pyqtSignal()
  83. @pyqtProperty(int, notify = globalContainerChanged)
  84. def definedExtruderCount(self):
  85. if not self._global_container_stack:
  86. return 0
  87. return len(self._global_container_stack.getMetaDataEntry("machine_extruder_trains"))
  88. @pyqtSlot(int)
  89. def setMachineExtruderCount(self, extruder_count):
  90. machine_manager = Application.getInstance().getMachineManager()
  91. extruder_manager = ExtruderManager.getInstance()
  92. definition_changes_container = self._global_container_stack.definitionChanges
  93. if not self._global_container_stack or definition_changes_container == self._empty_container:
  94. return
  95. previous_extruder_count = self._global_container_stack.getProperty("machine_extruder_count", "value")
  96. if extruder_count == previous_extruder_count:
  97. return
  98. extruder_material_id = None
  99. extruder_variant_id = None
  100. if extruder_count == 1:
  101. # Get the material and variant of the first extruder before setting the number extruders to 1
  102. if machine_manager.hasMaterials:
  103. extruder_material_id = machine_manager.allActiveMaterialIds[extruder_manager.extruderIds["0"]]
  104. if machine_manager.hasVariants:
  105. extruder_variant_id = machine_manager.allActiveVariantIds[extruder_manager.extruderIds["0"]]
  106. # Copy any settable_per_extruder setting value from the extruders to the global stack
  107. extruder_stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  108. extruder_stacks.reverse() # make sure the first extruder is done last, so its settings override any higher extruder settings
  109. global_user_container = self._global_container_stack.getTop()
  110. for extruder_stack in extruder_stacks:
  111. extruder_index = extruder_stack.getMetaDataEntry("position")
  112. extruder_user_container = extruder_stack.getTop()
  113. for setting_instance in extruder_user_container.findInstances():
  114. setting_key = setting_instance.definition.key
  115. settable_per_extruder = self._global_container_stack.getProperty(setting_key, "settable_per_extruder")
  116. if settable_per_extruder:
  117. limit_to_extruder = self._global_container_stack.getProperty(setting_key, "limit_to_extruder")
  118. if limit_to_extruder == "-1" or limit_to_extruder == extruder_index:
  119. global_user_container.setProperty(setting_key, "value", extruder_user_container.getProperty(setting_key, "value"))
  120. extruder_user_container.removeInstance(setting_key)
  121. # Check to see if any features are set to print with an extruder that will no longer exist
  122. for setting_key in ["adhesion_extruder_nr", "support_extruder_nr", "support_extruder_nr_layer_0", "support_infill_extruder_nr", "support_interface_extruder_nr"]:
  123. if int(self._global_container_stack.getProperty(setting_key, "value")) > extruder_count - 1:
  124. Logger.log("i", "Lowering %s setting to match number of extruders", setting_key)
  125. self._global_container_stack.getTop().setProperty(setting_key, "value", extruder_count - 1)
  126. # Check to see if any objects are set to print with an extruder that will no longer exist
  127. root_node = Application.getInstance().getController().getScene().getRoot()
  128. for node in DepthFirstIterator(root_node):
  129. if node.getMeshData():
  130. extruder_nr = node.callDecoration("getActiveExtruderPosition")
  131. if extruder_nr is not None and int(extruder_nr) > extruder_count - 1:
  132. node.callDecoration("setActiveExtruder", extruder_manager.getExtruderStack(extruder_count - 1).getId())
  133. definition_changes_container.setProperty("machine_extruder_count", "value", extruder_count)
  134. if extruder_count > 1:
  135. # Multiextrusion
  136. # Make sure one of the extruder stacks is active
  137. if extruder_manager.activeExtruderIndex == -1:
  138. extruder_manager.setActiveExtruderIndex(0)
  139. # Move settable_per_extruder values out of the global container
  140. if previous_extruder_count == 1:
  141. extruder_stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  142. global_user_container = self._global_container_stack.getTop()
  143. for setting_instance in global_user_container.findInstances():
  144. setting_key = setting_instance.definition.key
  145. settable_per_extruder = self._global_container_stack.getProperty(setting_key, "settable_per_extruder")
  146. if settable_per_extruder:
  147. limit_to_extruder = int(self._global_container_stack.getProperty(setting_key, "limit_to_extruder"))
  148. extruder_stack = extruder_stacks[max(0, limit_to_extruder)]
  149. extruder_stack.getTop().setProperty(setting_key, "value", global_user_container.getProperty(setting_key, "value"))
  150. global_user_container.removeInstance(setting_key)
  151. else:
  152. # Single extrusion
  153. # Make sure the machine stack is active
  154. if extruder_manager.activeExtruderIndex > -1:
  155. extruder_manager.setActiveExtruderIndex(-1)
  156. # Restore material and variant on global stack
  157. # MachineManager._onGlobalContainerChanged removes the global material and variant of multiextruder machines
  158. if extruder_material_id or extruder_variant_id:
  159. # Prevent the DiscardOrKeepProfileChangesDialog from popping up (twice) if there are user changes
  160. # The dialog is not relevant here, since we're restoring the previous situation as good as possible
  161. preferences = Preferences.getInstance()
  162. choice_on_profile_override = preferences.getValue("cura/choice_on_profile_override")
  163. preferences.setValue("cura/choice_on_profile_override", "always_keep")
  164. if extruder_material_id:
  165. machine_manager.setActiveMaterial(extruder_material_id)
  166. if extruder_variant_id:
  167. machine_manager.setActiveVariant(extruder_variant_id)
  168. preferences.setValue("cura/choice_on_profile_override", choice_on_profile_override)
  169. self.forceUpdate()
  170. @pyqtSlot()
  171. def forceUpdate(self):
  172. # Force rebuilding the build volume by reloading the global container stack.
  173. # This is a bit of a hack, but it seems quick enough.
  174. Application.getInstance().globalContainerStackChanged.emit()
  175. @pyqtSlot()
  176. def updateHasMaterialsMetadata(self):
  177. # Updates the has_materials metadata flag after switching gcode flavor
  178. if not self._global_container_stack:
  179. return
  180. definition = self._global_container_stack.getBottom()
  181. if definition.getProperty("machine_gcode_flavor", "value") != "UltiGCode" or definition.getMetaDataEntry("has_materials", False):
  182. # In other words: only continue for the UM2 (extended), but not for the UM2+
  183. return
  184. has_materials = self._global_container_stack.getProperty("machine_gcode_flavor", "value") != "UltiGCode"
  185. material_container = self._global_container_stack.material
  186. if has_materials:
  187. if "has_materials" in self._global_container_stack.getMetaData():
  188. self._global_container_stack.setMetaDataEntry("has_materials", True)
  189. else:
  190. self._global_container_stack.addMetaDataEntry("has_materials", True)
  191. # Set the material container to a sane default
  192. if material_container == self._empty_container:
  193. search_criteria = { "type": "material", "definition": "fdmprinter", "id": self._global_container_stack.getMetaDataEntry("preferred_material")}
  194. materials = self._container_registry.findInstanceContainers(**search_criteria)
  195. if materials:
  196. self._global_container_stack.material = materials[0]
  197. else:
  198. # The metadata entry is stored in an ini, and ini files are parsed as strings only.
  199. # Because any non-empty string evaluates to a boolean True, we have to remove the entry to make it False.
  200. if "has_materials" in self._global_container_stack.getMetaData():
  201. self._global_container_stack.removeMetaDataEntry("has_materials")
  202. self._global_container_stack.material = ContainerRegistry.getInstance().getEmptyInstanceContainer()
  203. Application.getInstance().globalContainerStackChanged.emit()
  204. @pyqtSlot()
  205. def updateMaterialForDiameter(self):
  206. # Updates the material container to a material that matches the material diameter set for the printer
  207. if not self._global_container_stack:
  208. return
  209. if not self._global_container_stack.getMetaDataEntry("has_materials", False):
  210. return
  211. machine_extruder_count = self._global_container_stack.getProperty("machine_extruder_count", "value")
  212. if machine_extruder_count > 1:
  213. material = ExtruderManager.getInstance().getActiveExtruderStack().material
  214. else:
  215. material = self._global_container_stack.material
  216. material_diameter = material.getProperty("material_diameter", "value")
  217. if not material_diameter: # in case of "empty" material
  218. material_diameter = 0
  219. material_approximate_diameter = str(round(material_diameter))
  220. definition_changes = self._global_container_stack.definitionChanges
  221. machine_diameter = definition_changes.getProperty("material_diameter", "value")
  222. if not machine_diameter:
  223. machine_diameter = self._global_container_stack.definition.getProperty("material_diameter", "value")
  224. machine_approximate_diameter = str(round(machine_diameter))
  225. if material_approximate_diameter != machine_approximate_diameter:
  226. Logger.log("i", "The the currently active material(s) do not match the diameter set for the printer. Finding alternatives.")
  227. if machine_extruder_count > 1:
  228. stacks = ExtruderManager.getInstance().getExtruderStacks()
  229. else:
  230. stacks = [self._global_container_stack]
  231. if self._global_container_stack.getMetaDataEntry("has_machine_materials", False):
  232. materials_definition = self._global_container_stack.definition.getId()
  233. has_material_variants = self._global_container_stack.getMetaDataEntry("has_variants", False)
  234. else:
  235. materials_definition = "fdmprinter"
  236. has_material_variants = False
  237. for stack in stacks:
  238. old_material = stack.material
  239. search_criteria = {
  240. "type": "material",
  241. "approximate_diameter": machine_approximate_diameter,
  242. "material": old_material.getMetaDataEntry("material", "value"),
  243. "supplier": old_material.getMetaDataEntry("supplier", "value"),
  244. "color_name": old_material.getMetaDataEntry("color_name", "value"),
  245. "definition": materials_definition
  246. }
  247. if has_material_variants:
  248. search_criteria["variant"] = stack.variant.getId()
  249. if old_material == self._empty_container:
  250. search_criteria.pop("material", None)
  251. search_criteria.pop("supplier", None)
  252. search_criteria.pop("definition", None)
  253. search_criteria["id"] = stack.getMetaDataEntry("preferred_material")
  254. materials = self._container_registry.findInstanceContainers(**search_criteria)
  255. if not materials:
  256. # Same material with new diameter is not found, search for generic version of the same material type
  257. search_criteria.pop("supplier", None)
  258. search_criteria["color_name"] = "Generic"
  259. materials = self._container_registry.findInstanceContainers(**search_criteria)
  260. if not materials:
  261. # Generic material with new diameter is not found, search for preferred material
  262. search_criteria.pop("color_name", None)
  263. search_criteria.pop("material", None)
  264. search_criteria["id"] = stack.getMetaDataEntry("preferred_material")
  265. materials = self._container_registry.findInstanceContainers(**search_criteria)
  266. if not materials:
  267. # Preferrd material with new diameter is not found, search for any material
  268. search_criteria.pop("id", None)
  269. materials = self._container_registry.findInstanceContainers(**search_criteria)
  270. if not materials:
  271. # Just use empty material as a final fallback
  272. materials = [self._empty_container]
  273. Logger.log("i", "Selecting new material: %s" % materials[0].getId())
  274. stack.material = materials[0]