MachineSettingsAction.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. import UM.i18n
  5. from UM.FlameProfiler import pyqtSlot
  6. from UM.Application import Application
  7. from UM.Settings.ContainerRegistry import ContainerRegistry
  8. from UM.Settings.DefinitionContainer import DefinitionContainer
  9. from cura.MachineAction import MachineAction
  10. from cura.Settings.CuraStackBuilder import CuraStackBuilder
  11. catalog = UM.i18n.i18nCatalog("cura")
  12. ## This action allows for certain settings that are "machine only") to be modified.
  13. # It automatically detects machine definitions that it knows how to change and attaches itself to those.
  14. class MachineSettingsAction(MachineAction):
  15. def __init__(self, parent = None):
  16. super().__init__("MachineSettingsAction", catalog.i18nc("@action", "Machine Settings"))
  17. self._qml_url = "MachineSettingsAction.qml"
  18. self._application = Application.getInstance()
  19. self._global_container_stack = None
  20. from cura.Settings.CuraContainerStack import _ContainerIndexes
  21. self._container_index = _ContainerIndexes.DefinitionChanges
  22. self._container_registry = ContainerRegistry.getInstance()
  23. self._container_registry.containerAdded.connect(self._onContainerAdded)
  24. self._container_registry.containerRemoved.connect(self._onContainerRemoved)
  25. self._application.globalContainerStackChanged.connect(self._onGlobalContainerChanged)
  26. self._backend = self._application.getBackend()
  27. self._empty_definition_container_id_list = []
  28. def _isEmptyDefinitionChanges(self, container_id: str):
  29. if not self._empty_definition_container_id_list:
  30. self._empty_definition_container_id_list = [self._application.empty_container.getId(),
  31. self._application.empty_definition_changes_container.getId()]
  32. return container_id in self._empty_definition_container_id_list
  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. self._application.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_id = container.definitionChanges.getId()
  41. if self._isEmptyDefinitionChanges(definition_changes_id):
  42. return
  43. self._container_registry.removeContainer(definition_changes_id)
  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_id = self._global_container_stack.definitionChanges.getId()
  49. if self._isEmptyDefinitionChanges(definition_changes_id):
  50. CuraStackBuilder.createDefinitionChangesContainer(self._global_container_stack,
  51. self._global_container_stack.getName() + "_settings")
  52. # Notify the UI in which container to store the machine settings data
  53. from cura.Settings.CuraContainerStack import _ContainerIndexes
  54. container_index = _ContainerIndexes.DefinitionChanges
  55. if container_index != self._container_index:
  56. self._container_index = container_index
  57. self.containerIndexChanged.emit()
  58. # Disable auto-slicing while the MachineAction is showing
  59. if self._backend: # This sometimes triggers before backend is loaded.
  60. self._backend.disableTimer()
  61. @pyqtSlot()
  62. def onFinishAction(self):
  63. # Restore autoslicing when the machineaction is dismissed
  64. if self._backend and self._backend.determineAutoSlicing():
  65. self._backend.tickle()
  66. containerIndexChanged = pyqtSignal()
  67. @pyqtProperty(int, notify = containerIndexChanged)
  68. def containerIndex(self):
  69. return self._container_index
  70. def _onGlobalContainerChanged(self):
  71. self._global_container_stack = Application.getInstance().getGlobalContainerStack()
  72. # This additional emit is needed because we cannot connect a UM.Signal directly to a pyqtSignal
  73. self.globalContainerChanged.emit()
  74. globalContainerChanged = pyqtSignal()
  75. @pyqtProperty(int, notify = globalContainerChanged)
  76. def definedExtruderCount(self):
  77. if not self._global_container_stack:
  78. return 0
  79. return len(self._global_container_stack.getMetaDataEntry("machine_extruder_trains"))
  80. @pyqtSlot(int)
  81. def setMachineExtruderCount(self, extruder_count):
  82. # Note: this method was in this class before, but since it's quite generic and other plugins also need it
  83. # it was moved to the machine manager instead. Now this method just calls the machine manager.
  84. self._application.getMachineManager().setActiveMachineExtruderCount(extruder_count)
  85. @pyqtSlot()
  86. def forceUpdate(self):
  87. # Force rebuilding the build volume by reloading the global container stack.
  88. # This is a bit of a hack, but it seems quick enough.
  89. self._application.globalContainerStackChanged.emit()
  90. @pyqtSlot()
  91. def updateHasMaterialsMetadata(self):
  92. # Updates the has_materials metadata flag after switching gcode flavor
  93. if not self._global_container_stack:
  94. return
  95. definition = self._global_container_stack.getBottom()
  96. if definition.getProperty("machine_gcode_flavor", "value") != "UltiGCode" or definition.getMetaDataEntry("has_materials", False):
  97. # In other words: only continue for the UM2 (extended), but not for the UM2+
  98. return
  99. machine_manager = self._application.getMachineManager()
  100. extruder_positions = list(self._global_container_stack.extruders.keys())
  101. has_materials = self._global_container_stack.getProperty("machine_gcode_flavor", "value") != "UltiGCode"
  102. material_node = None
  103. if has_materials:
  104. if "has_materials" in self._global_container_stack.getMetaData():
  105. self._global_container_stack.setMetaDataEntry("has_materials", True)
  106. else:
  107. self._global_container_stack.addMetaDataEntry("has_materials", True)
  108. # Set the material container for each extruder to a sane default
  109. material_manager = self._application.getMaterialManager()
  110. material_node = material_manager.getDefaultMaterial(self._global_container_stack, None)
  111. else:
  112. # The metadata entry is stored in an ini, and ini files are parsed as strings only.
  113. # Because any non-empty string evaluates to a boolean True, we have to remove the entry to make it False.
  114. if "has_materials" in self._global_container_stack.getMetaData():
  115. self._global_container_stack.removeMetaDataEntry("has_materials")
  116. # set materials
  117. for position in extruder_positions:
  118. machine_manager.setMaterial(position, material_node)
  119. self._application.globalContainerStackChanged.emit()
  120. @pyqtSlot(int)
  121. def updateMaterialForDiameter(self, extruder_position: int):
  122. # Updates the material container to a material that matches the material diameter set for the printer
  123. self._application.getExtruderManager().updateMaterialForDiameter(extruder_position)