MachineSettingsAction.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 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.Settings.InstanceContainer import InstanceContainer
  8. from UM.Settings.ContainerRegistry import ContainerRegistry
  9. from UM.Settings.DefinitionContainer import DefinitionContainer
  10. from UM.Logger import Logger
  11. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry
  12. import UM.i18n
  13. catalog = UM.i18n.i18nCatalog("cura")
  14. ## This action allows for certain settings that are "machine only") to be modified.
  15. # It automatically detects machine definitions that it knows how to change and attaches itself to those.
  16. class MachineSettingsAction(MachineAction):
  17. def __init__(self, parent = None):
  18. super().__init__("MachineSettingsAction", catalog.i18nc("@action", "Machine Settings"))
  19. self._qml_url = "MachineSettingsAction.qml"
  20. self._container_index = 0
  21. self._container_registry = ContainerRegistry.getInstance()
  22. self._container_registry.containerAdded.connect(self._onContainerAdded)
  23. def _reset(self):
  24. global_container_stack = Application.getInstance().getGlobalContainerStack()
  25. if not global_container_stack:
  26. return
  27. # Make sure there is a definition_changes container to store the machine settings
  28. definition_changes_container = global_container_stack.findContainer({"type": "definition_changes"})
  29. if not definition_changes_container:
  30. definition_changes_container = self._createDefinitionChangesContainer(global_container_stack)
  31. # Notify the UI in which container to store the machine settings data
  32. container_index = global_container_stack.getContainerIndex(definition_changes_container)
  33. if container_index != self._container_index:
  34. self._container_index = container_index
  35. self.containerIndexChanged.emit()
  36. def _createDefinitionChangesContainer(self, global_container_stack, container_index = None):
  37. definition_changes_container = InstanceContainer(global_container_stack.getName() + "_settings")
  38. definition = global_container_stack.getBottom()
  39. definition_changes_container.setDefinition(definition)
  40. definition_changes_container.addMetaDataEntry("type", "definition_changes")
  41. self._container_registry.addContainer(definition_changes_container)
  42. # Insert definition_changes between the definition and the variant
  43. global_container_stack.insertContainer(-1, definition_changes_container)
  44. return definition_changes_container
  45. containerIndexChanged = pyqtSignal()
  46. @pyqtProperty(int, notify = containerIndexChanged)
  47. def containerIndex(self):
  48. return self._container_index
  49. def _onContainerAdded(self, container):
  50. # Add this action as a supported action to all machine definitions
  51. if isinstance(container, DefinitionContainer) and container.getMetaDataEntry("type") == "machine":
  52. if container.getProperty("machine_extruder_count", "value") > 1:
  53. # Multiextruder printers are not currently supported
  54. Logger.log("d", "Not attaching MachineSettingsAction to %s; Multi-extrusion printers are not supported", container.getId())
  55. return
  56. Application.getInstance().getMachineActionManager().addSupportedAction(container.getId(), self.getKey())
  57. @pyqtSlot()
  58. def forceUpdate(self):
  59. # Force rebuilding the build volume by reloading the global container stack.
  60. # This is a bit of a hack, but it seems quick enough.
  61. Application.getInstance().globalContainerStackChanged.emit()
  62. @pyqtSlot()
  63. def updateHasMaterialsMetadata(self):
  64. # Updates the has_materials metadata flag after switching gcode flavor
  65. global_container_stack = Application.getInstance().getGlobalContainerStack()
  66. if global_container_stack:
  67. definition = global_container_stack.getBottom()
  68. if definition.getProperty("machine_gcode_flavor", "value") == "UltiGCode" and not definition.getMetaDataEntry("has_materials", False):
  69. has_materials = global_container_stack.getProperty("machine_gcode_flavor", "value") != "UltiGCode"
  70. material_container = global_container_stack.findContainer({"type": "material"})
  71. material_index = global_container_stack.getContainerIndex(material_container)
  72. if has_materials:
  73. if "has_materials" in global_container_stack.getMetaData():
  74. global_container_stack.setMetaDataEntry("has_materials", True)
  75. else:
  76. global_container_stack.addMetaDataEntry("has_materials", True)
  77. # Set the material container to a sane default
  78. if material_container.getId() == "empty_material":
  79. search_criteria = { "type": "material", "definition": "fdmprinter", "id": "*pla*" }
  80. containers = self._container_registry.findInstanceContainers(**search_criteria)
  81. if containers:
  82. global_container_stack.replaceContainer(material_index, containers[0])
  83. else:
  84. # The metadata entry is stored in an ini, and ini files are parsed as strings only.
  85. # Because any non-empty string evaluates to a boolean True, we have to remove the entry to make it False.
  86. if "has_materials" in global_container_stack.getMetaData():
  87. global_container_stack.removeMetaDataEntry("has_materials")
  88. empty_material = self._container_registry.findInstanceContainers(id = "empty_material")[0]
  89. global_container_stack.replaceContainer(material_index, empty_material)
  90. Application.getInstance().globalContainerStackChanged.emit()