CompatibleMachineModel.py 3.7 KB

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