CompatibleMachineModel.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright (c) 2022 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional
  4. from PyQt6.QtCore import Qt, QObject, pyqtSlot, pyqtProperty, pyqtSignal
  5. from UM.Logger import Logger
  6. from UM.Qt.ListModel import ListModel
  7. from UM.i18n import i18nCatalog
  8. class CompatibleMachineModel(ListModel):
  9. NameRole = Qt.ItemDataRole.UserRole + 1
  10. UniqueIdRole = Qt.ItemDataRole.UserRole + 2
  11. ExtrudersRole = Qt.ItemDataRole.UserRole + 3
  12. def __init__(self, parent: Optional[QObject] = None) -> None:
  13. super().__init__(parent)
  14. self._catalog = i18nCatalog("cura")
  15. self.addRoleName(self.NameRole, "name")
  16. self.addRoleName(self.UniqueIdRole, "unique_id")
  17. self.addRoleName(self.ExtrudersRole, "extruders")
  18. self._update()
  19. from cura.CuraApplication import CuraApplication
  20. machine_manager = CuraApplication.getInstance().getMachineManager()
  21. machine_manager.globalContainerChanged.connect(self._update)
  22. machine_manager.outputDevicesChanged.connect(self._update)
  23. @pyqtSlot()
  24. def forceUpdate(self):
  25. self._update()
  26. def _update(self) -> None:
  27. self.clear()
  28. def _makeMaterial(brand, name, color):
  29. if name.lower() in ["", "empty"]:
  30. return {"brand": "", "name": "(empty)", "hexcolor": "#ffffff"}
  31. else:
  32. return {"brand": brand, "name": name, "hexcolor": color}
  33. from cura.CuraApplication import CuraApplication
  34. machine_manager = CuraApplication.getInstance().getMachineManager()
  35. # Loop over the output-devices, not the stacks; need all applicable configurations, not just the current loaded one.
  36. for output_device in machine_manager.printerOutputDevices:
  37. for printer in output_device.printers:
  38. extruder_configs = dict()
  39. # initialize & add current active material:
  40. for extruder in printer.extruders:
  41. materials = [_makeMaterial(
  42. extruder.activeMaterial.brand, extruder.activeMaterial.name, extruder.activeMaterial.color)]
  43. extruder_configs[extruder.getPosition()] = {
  44. "position": extruder.getPosition(),
  45. "core": extruder.hotendID,
  46. "materials": materials
  47. }
  48. # add currently inactive, but possible materials:
  49. for configuration in printer.availableConfigurations:
  50. for extruder in configuration.extruderConfigurations:
  51. if not extruder.position in extruder_configs:
  52. Logger.log("w", f"No active extruder for position {extruder.position}.")
  53. continue
  54. entry = _makeMaterial(extruder.material.brand, extruder.material.name, extruder.material.color)
  55. if entry not in extruder_configs[extruder.position]["materials"]:
  56. extruder_configs[extruder.position]["materials"].append(entry)
  57. if any([len(extruder["materials"]) > 0 for extruder in extruder_configs.values()]):
  58. self.appendItem({
  59. "name": printer.name,
  60. "unique_id": printer.name, # <- Can assume the cloud doesn't have duplicate names?
  61. "extruders": list(extruder_configs.values())
  62. })