CompatibleMachineModel.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. if not extruder.activeMaterial:
  42. continue
  43. materials = [_makeMaterial(
  44. extruder.activeMaterial.brand, extruder.activeMaterial.name, extruder.activeMaterial.color)]
  45. extruder_configs[extruder.getPosition()] = {
  46. "position": extruder.getPosition(),
  47. "core": extruder.hotendID,
  48. "materials": materials
  49. }
  50. # add currently inactive, but possible materials:
  51. for configuration in printer.availableConfigurations:
  52. for extruder in configuration.extruderConfigurations:
  53. if not extruder.position in extruder_configs:
  54. Logger.log("w", f"No active extruder for position {extruder.position}.")
  55. continue
  56. entry = _makeMaterial(extruder.material.brand, extruder.material.name, extruder.material.color)
  57. if entry not in extruder_configs[extruder.position]["materials"]:
  58. extruder_configs[extruder.position]["materials"].append(entry)
  59. if any([len(extruder["materials"]) > 0 for extruder in extruder_configs.values()]):
  60. self.appendItem({
  61. "name": printer.name,
  62. "unique_id": printer.name, # <- Can assume the cloud doesn't have duplicate names?
  63. "extruders": list(extruder_configs.values())
  64. })