AbstractMachine.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from typing import List
  2. from UM.Settings.ContainerStack import ContainerStack
  3. from UM.Util import parseBool
  4. from cura.PrinterOutput.PrinterOutputDevice import ConnectionType
  5. from cura.Settings.GlobalStack import GlobalStack
  6. from UM.MimeTypeDatabase import MimeType, MimeTypeDatabase
  7. from UM.Settings.ContainerRegistry import ContainerRegistry
  8. class AbstractMachine(GlobalStack):
  9. """ Represents a group of machines of the same type. This allows the user to select settings before selecting a printer. """
  10. def __init__(self, container_id: str) -> None:
  11. super().__init__(container_id)
  12. self.setMetaDataEntry("type", "abstract_machine")
  13. @classmethod
  14. def getMachines(cls, abstract_machine: ContainerStack, online_only = False) -> List[ContainerStack]:
  15. """ Fetches all container stacks that match definition_id with an abstract machine.
  16. :param abstractMachine: The abstract machine stack.
  17. :return: A list of Containers or an empty list if abstract_machine is not an "abstract_machine"
  18. """
  19. if not abstract_machine.getMetaDataEntry("type") == "abstract_machine":
  20. return []
  21. from cura.CuraApplication import CuraApplication # In function to avoid circular import
  22. application = CuraApplication.getInstance()
  23. registry = application.getContainerRegistry()
  24. machines = registry.findContainerStacks(type="machine")
  25. # Filter machines that match definition
  26. machines = filter(lambda machine: machine.definition.id == abstract_machine.definition.getId(), machines)
  27. # Filter only LAN and Cloud printers
  28. machines = filter(lambda machine: ConnectionType.CloudConnection in machine.configuredConnectionTypes or ConnectionType.NetworkConnection in machine.configuredConnectionTypes, machines)
  29. if online_only:
  30. # LAN printers have is_online = False but should still be included
  31. machines = filter(lambda machine: parseBool(machine.getMetaDataEntry("is_online", False) or ConnectionType.NetworkConnection in machine.configuredConnectionTypes), machines)
  32. return list(machines)
  33. ## private:
  34. _abstract_machine_mime = MimeType(
  35. name = "application/x-cura-abstract-machine",
  36. comment = "Cura Abstract Machine",
  37. suffixes = ["global.cfg"]
  38. )
  39. MimeTypeDatabase.addMimeType(_abstract_machine_mime)
  40. ContainerRegistry.addContainerTypeByName(AbstractMachine, "abstract_machine", _abstract_machine_mime.name)