CuraStackBuilder.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 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. generated_name = registry.createUniqueName("machine", "", name, machine_definition.name)
  27. # Make sure the new name does not collide with any definition or (quality) profile
  28. # createUniqueName() only looks at other stacks, but not at definitions or quality profiles
  29. # Note that we don't go for uniqueName() immediately because that function matches with ignore_case set to true
  30. if registry.findContainers(id = generated_name):
  31. generated_name = registry.uniqueName(generated_name)
  32. new_global_stack = cls.createGlobalStack(
  33. new_stack_id = generated_name,
  34. definition = machine_definition,
  35. quality = "default",
  36. material = "default",
  37. variant = "default",
  38. )
  39. new_global_stack.setName(generated_name)
  40. for extruder_definition in registry.findDefinitionContainers(machine = machine_definition.id):
  41. position = extruder_definition.getMetaDataEntry("position", None)
  42. if not position:
  43. Logger.log("w", "Extruder definition %s specifies no position metadata entry.", extruder_definition.id)
  44. new_extruder_id = registry.uniqueName(extruder_definition.id)
  45. new_extruder = cls.createExtruderStack(
  46. new_extruder_id,
  47. definition = extruder_definition,
  48. machine_definition = machine_definition,
  49. quality = "default",
  50. material = "default",
  51. variant = "default",
  52. next_stack = new_global_stack
  53. )
  54. return new_global_stack
  55. ## Create a new Extruder stack
  56. #
  57. # \param new_stack_id The ID of the new stack.
  58. # \param definition The definition to base the new stack on.
  59. # \param machine_definition The machine definition to use for the user container.
  60. # \param kwargs You can add keyword arguments to specify IDs of containers to use for a specific type, for example "variant": "0.4mm"
  61. #
  62. # \return A new Global stack instance with the specified parameters.
  63. @classmethod
  64. def createExtruderStack(cls, new_stack_id: str, definition: DefinitionContainer, machine_definition: DefinitionContainer, **kwargs) -> ExtruderStack:
  65. stack = ExtruderStack(new_stack_id)
  66. stack.setName(definition.getName())
  67. stack.setDefinition(definition)
  68. stack.addMetaDataEntry("position", definition.getMetaDataEntry("position"))
  69. if "next_stack" in kwargs: #Add stacks before containers are added, since they may trigger a setting update.
  70. stack.setNextStack(kwargs["next_stack"])
  71. user_container = InstanceContainer(new_stack_id + "_user")
  72. user_container.addMetaDataEntry("type", "user")
  73. user_container.addMetaDataEntry("extruder", new_stack_id)
  74. from cura.CuraApplication import CuraApplication
  75. user_container.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  76. user_container.setDefinition(machine_definition)
  77. stack.setUserChanges(user_container)
  78. # Important! The order here matters, because that allows the stack to
  79. # assume the material and variant have already been set.
  80. if "definition_changes" in kwargs:
  81. stack.setDefinitionChangesById(kwargs["definition_changes"])
  82. else:
  83. stack.setDefinitionChanges(cls.createDefinitionChangesContainer(stack, new_stack_id + "_settings"))
  84. if "variant" in kwargs:
  85. stack.setVariantById(kwargs["variant"])
  86. if "material" in kwargs:
  87. stack.setMaterialById(kwargs["material"])
  88. if "quality" in kwargs:
  89. stack.setQualityById(kwargs["quality"])
  90. if "quality_changes" in kwargs:
  91. stack.setQualityChangesById(kwargs["quality_changes"])
  92. # Only add the created containers to the registry after we have set all the other
  93. # properties. This makes the create operation more transactional, since any problems
  94. # setting properties will not result in incomplete containers being added.
  95. registry = ContainerRegistry.getInstance()
  96. registry.addContainer(stack)
  97. registry.addContainer(user_container)
  98. return stack
  99. ## Create a new Global stack
  100. #
  101. # \param new_stack_id The ID of the new stack.
  102. # \param definition The definition to base the new stack on.
  103. # \param kwargs You can add keyword arguments to specify IDs of containers to use for a specific type, for example "variant": "0.4mm"
  104. #
  105. # \return A new Global stack instance with the specified parameters.
  106. @classmethod
  107. def createGlobalStack(cls, new_stack_id: str, definition: DefinitionContainer, **kwargs) -> GlobalStack:
  108. stack = GlobalStack(new_stack_id)
  109. stack.setDefinition(definition)
  110. user_container = InstanceContainer(new_stack_id + "_user")
  111. user_container.addMetaDataEntry("type", "user")
  112. user_container.addMetaDataEntry("machine", new_stack_id)
  113. from cura.CuraApplication import CuraApplication
  114. user_container.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  115. user_container.setDefinition(definition)
  116. stack.setUserChanges(user_container)
  117. # Important! The order here matters, because that allows the stack to
  118. # assume the material and variant have already been set.
  119. if "definition_changes" in kwargs:
  120. stack.setDefinitionChangesById(kwargs["definition_changes"])
  121. else:
  122. stack.setDefinitionChanges(cls.createDefinitionChangesContainer(stack, new_stack_id + "_settings"))
  123. if "variant" in kwargs:
  124. stack.setVariantById(kwargs["variant"])
  125. if "material" in kwargs:
  126. stack.setMaterialById(kwargs["material"])
  127. if "quality" in kwargs:
  128. stack.setQualityById(kwargs["quality"])
  129. if "quality_changes" in kwargs:
  130. stack.setQualityChangesById(kwargs["quality_changes"])
  131. registry = ContainerRegistry.getInstance()
  132. registry.addContainer(stack)
  133. registry.addContainer(user_container)
  134. return stack
  135. @classmethod
  136. def createDefinitionChangesContainer(cls, container_stack, container_name, container_index = None):
  137. from cura.CuraApplication import CuraApplication
  138. unique_container_name = ContainerRegistry.getInstance().uniqueName(container_name)
  139. definition_changes_container = InstanceContainer(unique_container_name)
  140. definition = container_stack.getBottom()
  141. definition_changes_container.setDefinition(definition)
  142. definition_changes_container.addMetaDataEntry("type", "definition_changes")
  143. definition_changes_container.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  144. ContainerRegistry.getInstance().addContainer(definition_changes_container)
  145. container_stack.definitionChanges = definition_changes_container
  146. return definition_changes_container