MachineSettingsAction.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, TYPE_CHECKING
  4. from PyQt5.QtCore import pyqtProperty
  5. import UM.i18n
  6. from UM.FlameProfiler import pyqtSlot
  7. from UM.Settings.ContainerRegistry import ContainerRegistry
  8. from UM.Settings.DefinitionContainer import DefinitionContainer
  9. from UM.Util import parseBool
  10. import cura.CuraApplication # Imported like this to prevent circular dependencies.
  11. from cura.MachineAction import MachineAction
  12. from cura.Machines.ContainerTree import ContainerTree # To re-build the machine node when hasMaterials changes.
  13. from cura.Settings.CuraStackBuilder import CuraStackBuilder
  14. from cura.Settings.cura_empty_instance_containers import isEmptyContainer
  15. if TYPE_CHECKING:
  16. from PyQt5.QtCore import QObject
  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: Optional["QObject"] = None) -> None:
  22. super().__init__("MachineSettingsAction", catalog.i18nc("@action", "Machine Settings"))
  23. self._qml_url = "MachineSettingsAction.qml"
  24. from cura.CuraApplication import CuraApplication
  25. self._application = CuraApplication.getInstance()
  26. from cura.Settings.CuraContainerStack import _ContainerIndexes
  27. self._store_container_index = _ContainerIndexes.DefinitionChanges
  28. self._container_registry = ContainerRegistry.getInstance()
  29. self._container_registry.containerAdded.connect(self._onContainerAdded)
  30. # The machine settings dialog blocks auto-slicing when it's shown, and re-enables it when it's finished.
  31. self._backend = self._application.getBackend()
  32. self.onFinished.connect(self._onFinished)
  33. # If the g-code flavour changes between UltiGCode and another flavour, we need to update the container tree.
  34. self._application.globalContainerStackChanged.connect(self._updateHasMaterialsInContainerTree)
  35. # Which container index in a stack to store machine setting changes.
  36. @pyqtProperty(int, constant = True)
  37. def storeContainerIndex(self) -> int:
  38. return self._store_container_index
  39. def _onContainerAdded(self, container):
  40. # Add this action as a supported action to all machine definitions
  41. if isinstance(container, DefinitionContainer) and container.getMetaDataEntry("type") == "machine":
  42. self._application.getMachineActionManager().addSupportedAction(container.getId(), self.getKey())
  43. ## Triggered when the global container stack changes or when the g-code
  44. # flavour setting is changed.
  45. def _updateHasMaterialsInContainerTree(self) -> None:
  46. global_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()
  47. if global_stack is None:
  48. return
  49. machine_node = ContainerTree.getInstance().machines[global_stack.definition.getId()]
  50. if machine_node.has_materials != parseBool(global_stack.getMetaDataEntry("has_materials")): # May have changed due to the g-code flavour.
  51. machine_node.has_materials = parseBool(global_stack.getMetaDataEntry("has_materials"))
  52. machine_node._loadAll()
  53. def _reset(self):
  54. global_stack = self._application.getMachineManager().activeMachine
  55. if not global_stack:
  56. return
  57. # Make sure there is a definition_changes container to store the machine settings
  58. definition_changes_id = global_stack.definitionChanges.getId()
  59. if isEmptyContainer(definition_changes_id):
  60. CuraStackBuilder.createDefinitionChangesContainer(global_stack,
  61. global_stack.getName() + "_settings")
  62. # Disable auto-slicing while the MachineAction is showing
  63. if self._backend: # This sometimes triggers before backend is loaded.
  64. self._backend.disableTimer()
  65. def _onFinished(self):
  66. # Restore auto-slicing when the machine action is dismissed
  67. if self._backend and self._backend.determineAutoSlicing():
  68. self._backend.enableTimer()
  69. self._backend.tickle()
  70. @pyqtSlot(int)
  71. def setMachineExtruderCount(self, extruder_count: int) -> None:
  72. # Note: this method was in this class before, but since it's quite generic and other plugins also need it
  73. # it was moved to the machine manager instead. Now this method just calls the machine manager.
  74. self._application.getMachineManager().setActiveMachineExtruderCount(extruder_count)
  75. @pyqtSlot()
  76. def forceUpdate(self) -> None:
  77. # Force rebuilding the build volume by reloading the global container stack.
  78. # This is a bit of a hack, but it seems quick enough.
  79. self._application.getMachineManager().globalContainerChanged.emit()
  80. @pyqtSlot()
  81. def updateHasMaterialsMetadata(self) -> None:
  82. global_stack = self._application.getMachineManager().activeMachine
  83. # Updates the has_materials metadata flag after switching gcode flavor
  84. if not global_stack:
  85. return
  86. definition = global_stack.getDefinition()
  87. if definition.getProperty("machine_gcode_flavor", "value") != "UltiGCode" or parseBool(definition.getMetaDataEntry("has_materials", False)):
  88. # In other words: only continue for the UM2 (extended), but not for the UM2+
  89. return
  90. machine_manager = self._application.getMachineManager()
  91. has_materials = global_stack.getProperty("machine_gcode_flavor", "value") != "UltiGCode"
  92. if has_materials:
  93. global_stack.setMetaDataEntry("has_materials", True)
  94. else:
  95. # The metadata entry is stored in an ini, and ini files are parsed as strings only.
  96. # Because any non-empty string evaluates to a boolean True, we have to remove the entry to make it False.
  97. if "has_materials" in global_stack.getMetaData():
  98. global_stack.removeMetaDataEntry("has_materials")
  99. self._updateHasMaterialsInContainerTree()
  100. # set materials
  101. machine_node = ContainerTree.getInstance().machines[global_stack.definition.getId()]
  102. for position, extruder in enumerate(global_stack.extruderList):
  103. #Find out what material we need to default to.
  104. approximate_diameter = round(extruder.getProperty("material_diameter", "value"))
  105. material_node = machine_node.variants[extruder.variant.getName()].preferredMaterial(approximate_diameter)
  106. machine_manager.setMaterial(str(position), material_node)
  107. self._application.globalContainerStackChanged.emit()
  108. @pyqtSlot(int)
  109. def updateMaterialForDiameter(self, extruder_position: int) -> None:
  110. # Updates the material container to a material that matches the material diameter set for the printer
  111. self._application.getMachineManager().updateMaterialWithVariant(str(extruder_position))