MachineSettingsManager.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 QObject, pyqtSlot
  5. from UM.i18n import i18nCatalog
  6. if TYPE_CHECKING:
  7. from cura.CuraApplication import CuraApplication
  8. #
  9. # This manager provides (convenience) functions to the Machine Settings Dialog QML to update certain machine settings.
  10. #
  11. class MachineSettingsManager(QObject):
  12. def __init__(self, application: "CuraApplication", parent: Optional["QObject"] = None) -> None:
  13. super().__init__(parent)
  14. self._i18n_catalog = i18nCatalog("cura")
  15. self._application = application
  16. # Force rebuilding the build volume by reloading the global container stack. This is a bit of a hack, but it seems
  17. # quite enough.
  18. @pyqtSlot()
  19. def forceUpdate(self) -> None:
  20. self._application.getMachineManager().globalContainerChanged.emit()
  21. # Function for the Machine Settings panel (QML) to update the compatible material diameter after a user has changed
  22. # an extruder's compatible material diameter. This ensures that after the modification, changes can be notified
  23. # and updated right away.
  24. @pyqtSlot(int)
  25. def updateMaterialForDiameter(self, extruder_position: int) -> None:
  26. # Updates the material container to a material that matches the material diameter set for the printer
  27. self._application.getMachineManager().updateMaterialWithVariant(str(extruder_position))
  28. @pyqtSlot(int)
  29. def setMachineExtruderCount(self, extruder_count: int) -> None:
  30. # Note: this method was in this class before, but since it's quite generic and other plugins also need it
  31. # it was moved to the machine manager instead. Now this method just calls the machine manager.
  32. self._application.getMachineManager().setActiveMachineExtruderCount(extruder_count)
  33. # Function for the Machine Settings panel (QML) to update after the usre changes "Number of Extruders".
  34. #
  35. # fieldOfView: The Ultimaker 2 family (not 2+) does not have materials in Cura by default, because the material is
  36. # to be set on the printer. But when switching to Marlin flavor, the printer firmware can not change/insert material
  37. # settings on the fly so they need to be configured in Cura. So when switching between gcode flavors, materials may
  38. # need to be enabled/disabled.
  39. @pyqtSlot()
  40. def updateHasMaterialsMetadata(self):
  41. machine_manager = self._application.getMachineManager()
  42. material_manager = self._application.getMaterialManager()
  43. global_stack = machine_manager.activeMachine
  44. definition = global_stack.definition
  45. if definition.getProperty("machine_gcode_flavor", "value") != "UltiGCode" or definition.getMetaDataEntry(
  46. "has_materials", False):
  47. # In other words: only continue for the UM2 (extended), but not for the UM2+
  48. return
  49. extruder_positions = list(global_stack.extruders.keys())
  50. has_materials = global_stack.getProperty("machine_gcode_flavor", "value") != "UltiGCode"
  51. material_node = None
  52. if has_materials:
  53. global_stack.setMetaDataEntry("has_materials", True)
  54. else:
  55. # The metadata entry is stored in an ini, and ini files are parsed as strings only.
  56. # Because any non-empty string evaluates to a boolean True, we have to remove the entry to make it False.
  57. if "has_materials" in global_stack.getMetaData():
  58. global_stack.removeMetaDataEntry("has_materials")
  59. # set materials
  60. for position in extruder_positions:
  61. if has_materials:
  62. material_node = material_manager.getDefaultMaterial(global_stack, position, None)
  63. machine_manager.setMaterial(position, material_node)
  64. self.forceUpdate()