MachineSettingsAction.py 7.1 KB

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