CuraStackBuilder.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.Logger import Logger
  4. from UM.Settings.DefinitionContainer import DefinitionContainer
  5. from UM.Settings.InstanceContainer import InstanceContainer
  6. from UM.Settings.ContainerRegistry import ContainerRegistry
  7. from .GlobalStack import GlobalStack
  8. from .ExtruderStack import ExtruderStack
  9. from typing import Optional
  10. ## Contains helper functions to create new machines.
  11. class CuraStackBuilder:
  12. ## Create a new instance of a machine.
  13. #
  14. # \param name The name of the new machine.
  15. # \param definition_id The ID of the machine definition to use.
  16. #
  17. # \return The new global stack or None if an error occurred.
  18. @classmethod
  19. def createMachine(cls, name: str, definition_id: str) -> Optional[GlobalStack]:
  20. registry = ContainerRegistry.getInstance()
  21. definitions = registry.findDefinitionContainers(id = definition_id)
  22. if not definitions:
  23. Logger.log("w", "Definition {definition} was not found!", definition = definition_id)
  24. return None
  25. machine_definition = definitions[0]
  26. name = registry.createUniqueName("machine", "", name, machine_definition.name)
  27. new_global_stack = cls.createGlobalStack(
  28. new_stack_id = name,
  29. definition = machine_definition,
  30. quality = "default",
  31. material = "default",
  32. variant = "default",
  33. )
  34. for extruder_definition in registry.findDefinitionContainers(machine = machine_definition.id):
  35. position = extruder_definition.getMetaDataEntry("position", None)
  36. if not position:
  37. Logger.log("w", "Extruder definition %s specifies no position metadata entry.", extruder_definition.id)
  38. new_extruder_id = registry.uniqueName(extruder_definition.id)
  39. new_extruder = cls.createExtruderStack(
  40. new_extruder_id,
  41. definition = extruder_definition,
  42. machine_definition = machine_definition,
  43. quality = "default",
  44. material = "default",
  45. variant = "default",
  46. next_stack = new_global_stack
  47. )
  48. return new_global_stack
  49. ## Create a new Extruder stack
  50. #
  51. # \param new_stack_id The ID of the new stack.
  52. # \param definition The definition to base the new stack on.
  53. # \param machine_definition The machine definition to use for the user container.
  54. # \param kwargs You can add keyword arguments to specify IDs of containers to use for a specific type, for example "variant": "0.4mm"
  55. #
  56. # \return A new Global stack instance with the specified parameters.
  57. @classmethod
  58. def createExtruderStack(cls, new_stack_id: str, definition: DefinitionContainer, machine_definition: DefinitionContainer, **kwargs) -> ExtruderStack:
  59. stack = ExtruderStack(new_stack_id)
  60. stack.setName(definition.getName())
  61. stack.setDefinition(definition)
  62. stack.addMetaDataEntry("position", definition.getMetaDataEntry("position"))
  63. user_container = InstanceContainer(new_stack_id + "_user")
  64. user_container.addMetaDataEntry("type", "user")
  65. user_container.addMetaDataEntry("extruder", new_stack_id)
  66. from cura.CuraApplication import CuraApplication
  67. user_container.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  68. user_container.setDefinition(machine_definition)
  69. stack.setUserChanges(user_container)
  70. if "next_stack" in kwargs:
  71. stack.setNextStack(kwargs["next_stack"])
  72. # Important! The order here matters, because that allows the stack to
  73. # assume the material and variant have already been set.
  74. if "definition_changes" in kwargs:
  75. stack.setDefinitionChangesById(kwargs["definition_changes"])
  76. if "variant" in kwargs:
  77. stack.setVariantById(kwargs["variant"])
  78. if "material" in kwargs:
  79. stack.setMaterialById(kwargs["material"])
  80. if "quality" in kwargs:
  81. stack.setQualityById(kwargs["quality"])
  82. if "quality_changes" in kwargs:
  83. stack.setQualityChangesById(kwargs["quality_changes"])
  84. # Only add the created containers to the registry after we have set all the other
  85. # properties. This makes the create operation more transactional, since any problems
  86. # setting properties will not result in incomplete containers being added.
  87. registry = ContainerRegistry.getInstance()
  88. registry.addContainer(stack)
  89. registry.addContainer(user_container)
  90. return stack
  91. ## Create a new Global stack
  92. #
  93. # \param new_stack_id The ID of the new stack.
  94. # \param definition The definition to base the new stack on.
  95. # \param kwargs You can add keyword arguments to specify IDs of containers to use for a specific type, for example "variant": "0.4mm"
  96. #
  97. # \return A new Global stack instance with the specified parameters.
  98. @classmethod
  99. def createGlobalStack(cls, new_stack_id: str, definition: DefinitionContainer, **kwargs) -> GlobalStack:
  100. stack = GlobalStack(new_stack_id)
  101. stack.setDefinition(definition)
  102. user_container = InstanceContainer(new_stack_id + "_user")
  103. user_container.addMetaDataEntry("type", "user")
  104. user_container.addMetaDataEntry("machine", new_stack_id)
  105. from cura.CuraApplication import CuraApplication
  106. user_container.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  107. user_container.setDefinition(definition)
  108. stack.setUserChanges(user_container)
  109. # Important! The order here matters, because that allows the stack to
  110. # assume the material and variant have already been set.
  111. if "definition_changes" in kwargs:
  112. stack.setDefinitionChangesById(kwargs["definition_changes"])
  113. if "variant" in kwargs:
  114. stack.setVariantById(kwargs["variant"])
  115. if "material" in kwargs:
  116. stack.setMaterialById(kwargs["material"])
  117. if "quality" in kwargs:
  118. stack.setQualityById(kwargs["quality"])
  119. if "quality_changes" in kwargs:
  120. stack.setQualityChangesById(kwargs["quality_changes"])
  121. registry = ContainerRegistry.getInstance()
  122. registry.addContainer(stack)
  123. registry.addContainer(user_container)
  124. return stack