MachineSettingsManager.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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):
  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. # FIXME(Lipu): Better document what this function does, especially the fuzzy gcode flavor and has_materials logic
  23. # regarding UM2 and UM2+
  24. # Function for the Machine Settings panel (QML) to update after the usre changes "Number of Extruders".
  25. @pyqtSlot()
  26. def updateHasMaterialsMetadata(self):
  27. machine_manager = self._application.getMachineManager()
  28. material_manager = self._application.getMaterialManager()
  29. global_stack = material_manager.activeMachine
  30. definition = global_stack.definition
  31. if definition.getProperty("machine_gcode_flavor", "value") != "UltiGCode" or definition.getMetaDataEntry(
  32. "has_materials", False):
  33. # In other words: only continue for the UM2 (extended), but not for the UM2+
  34. return
  35. extruder_positions = list(global_stack.extruders.keys())
  36. has_materials = global_stack.getProperty("machine_gcode_flavor", "value") != "UltiGCode"
  37. material_node = None
  38. if has_materials:
  39. global_stack.setMetaDataEntry("has_materials", True)
  40. else:
  41. # The metadata entry is stored in an ini, and ini files are parsed as strings only.
  42. # Because any non-empty string evaluates to a boolean True, we have to remove the entry to make it False.
  43. if "has_materials" in global_stack.getMetaData():
  44. global_stack.removeMetaDataEntry("has_materials")
  45. # set materials
  46. for position in extruder_positions:
  47. if has_materials:
  48. material_node = material_manager.getDefaultMaterial(global_stack, position, None)
  49. machine_manager.setMaterial(position, material_node)
  50. self.forceUpdate()