MachineSettingsManager.py 4.0 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. 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. has_materials = global_stack.getProperty("machine_gcode_flavor", "value") != "UltiGCode"
  50. material_node = None
  51. if has_materials:
  52. global_stack.setMetaDataEntry("has_materials", True)
  53. else:
  54. # The metadata entry is stored in an ini, and ini files are parsed as strings only.
  55. # Because any non-empty string evaluates to a boolean True, we have to remove the entry to make it False.
  56. if "has_materials" in global_stack.getMetaData():
  57. global_stack.removeMetaDataEntry("has_materials")
  58. # set materials
  59. for position, extruder in enumerate(global_stack.extruderList):
  60. if has_materials:
  61. approximate_diameter = extruder.getApproximateMaterialDiameter()
  62. variant_node = ContainerTree.getInstance().machines[global_stack.definition.getId()].variants[extruder.variant.getName()]
  63. material_node = variant_node.preferredMaterial(approximate_diameter)
  64. machine_manager.setMaterial(str(position), material_node)
  65. self.forceUpdate()