MachineSettingsManager.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. from cura.Machines.ContainerTree import ContainerTree
  7. if TYPE_CHECKING:
  8. from cura.CuraApplication import CuraApplication
  9. #
  10. # This manager provides (convenience) functions to the Machine Settings Dialog QML to update certain machine settings.
  11. #
  12. class MachineSettingsManager(QObject):
  13. def __init__(self, application: "CuraApplication", parent: Optional["QObject"] = None) -> None:
  14. super().__init__(parent)
  15. self._i18n_catalog = i18nCatalog("cura")
  16. self._application = application
  17. # Force rebuilding the build volume by reloading the global container stack. This is a bit of a hack, but it seems
  18. # quite enough.
  19. @pyqtSlot()
  20. def forceUpdate(self) -> None:
  21. self._application.getMachineManager().globalContainerChanged.emit()
  22. # Function for the Machine Settings panel (QML) to update the compatible material diameter after a user has changed
  23. # an extruder's compatible material diameter. This ensures that after the modification, changes can be notified
  24. # and updated right away.
  25. @pyqtSlot(int)
  26. def updateMaterialForDiameter(self, extruder_position: int) -> None:
  27. # Updates the material container to a material that matches the material diameter set for the printer
  28. self._application.getMachineManager().updateMaterialWithVariant(str(extruder_position))
  29. @pyqtSlot(int)
  30. def setMachineExtruderCount(self, extruder_count: int) -> None:
  31. # Note: this method was in this class before, but since it's quite generic and other plugins also need it
  32. # it was moved to the machine manager instead. Now this method just calls the machine manager.
  33. self._application.getMachineManager().setActiveMachineExtruderCount(extruder_count)
  34. # Function for the Machine Settings panel (QML) to update after the user changes "Number of Extruders".
  35. #
  36. # fieldOfView: The Ultimaker 2 family (not 2+) does not have materials in Cura by default, because the material is
  37. # to be set on the printer. But when switching to Marlin flavor, the printer firmware can not change/insert material
  38. # settings on the fly so they need to be configured in Cura. So when switching between gcode flavors, materials may
  39. # need to be enabled/disabled.
  40. @pyqtSlot()
  41. def updateHasMaterialsMetadata(self):
  42. machine_manager = self._application.getMachineManager()
  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. extruder = global_stack.extruderList[int(position)]
  63. approximate_diameter = extruder.getApproximateMaterialDiameter()
  64. variant_node = ContainerTree.getInstance().machines[global_stack.definition.getId()].variants[extruder.variant.getName()]
  65. material_node = variant_node.preferredMaterial(approximate_diameter)
  66. machine_manager.setMaterial(position, material_node)
  67. self.forceUpdate()