MachineSettingsManager.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from typing import Any, Dict, Optional, TYPE_CHECKING
  2. from PyQt5.QtCore import Qt, QObject, pyqtSlot
  3. from UM.i18n import i18nCatalog
  4. class MachineSettingsManager(QObject):
  5. def __init__(self, parent: Optional["QObject"] = None) -> None:
  6. super().__init__(parent)
  7. self._i18n_catalog = i18nCatalog("cura")
  8. from cura.CuraApplication import CuraApplication
  9. self._application = CuraApplication.getInstance()
  10. # Force rebuilding the build volume by reloading the global container stack. This is a bit of a hack, but it seems
  11. # quite enough.
  12. @pyqtSlot()
  13. def forceUpdate(self) -> None:
  14. self._application.getMachineManager().globalContainerChanged.emit()
  15. # Function for the Machine Settings panel (QML) to update the compatible material diameter after a user has changed
  16. # an extruder's compatible material diameter. This ensures that after the modification, changes can be notified
  17. # and updated right away.
  18. @pyqtSlot(int)
  19. def updateMaterialForDiameter(self, extruder_position: int) -> None:
  20. # Updates the material container to a material that matches the material diameter set for the printer
  21. self._application.getMachineManager().updateMaterialWithVariant(str(extruder_position))
  22. @pyqtSlot(int)
  23. def setMachineExtruderCount(self, extruder_count: int) -> None:
  24. # Note: this method was in this class before, but since it's quite generic and other plugins also need it
  25. # it was moved to the machine manager instead. Now this method just calls the machine manager.
  26. self._application.getMachineManager().setActiveMachineExtruderCount(extruder_count)
  27. # FIXME(Lipu): Better document what this function does, especially the fuzzy gcode flavor and has_materials logic
  28. # regarding UM2 and UM2+
  29. # Function for the Machine Settings panel (QML) to update after the usre changes "Number of Extruders".
  30. @pyqtSlot()
  31. def updateHasMaterialsMetadata(self):
  32. machine_manager = self._application.getMachineManager()
  33. material_manager = self._application.getMaterialManager()
  34. global_stack = machine_manager.activeMachine
  35. definition = global_stack.definition
  36. if definition.getProperty("machine_gcode_flavor", "value") != "UltiGCode" or definition.getMetaDataEntry(
  37. "has_materials", False):
  38. # In other words: only continue for the UM2 (extended), but not for the UM2+
  39. return
  40. extruder_positions = list(global_stack.extruders.keys())
  41. has_materials = global_stack.getProperty("machine_gcode_flavor", "value") != "UltiGCode"
  42. material_node = None
  43. if has_materials:
  44. global_stack.setMetaDataEntry("has_materials", True)
  45. else:
  46. # The metadata entry is stored in an ini, and ini files are parsed as strings only.
  47. # Because any non-empty string evaluates to a boolean True, we have to remove the entry to make it False.
  48. if "has_materials" in global_stack.getMetaData():
  49. global_stack.removeMetaDataEntry("has_materials")
  50. # set materials
  51. for position in extruder_positions:
  52. if has_materials:
  53. material_node = material_manager.getDefaultMaterial(global_stack, position, None)
  54. machine_manager.setMaterial(position, material_node)
  55. self.forceUpdate()