CuraStackBuilder.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional
  4. from UM.ConfigurationErrorMessage import ConfigurationErrorMessage
  5. from UM.Logger import Logger
  6. from UM.Settings.Interfaces import DefinitionContainerInterface
  7. from UM.Settings.InstanceContainer import InstanceContainer
  8. from cura.Machines.ContainerTree import ContainerTree
  9. from cura.Machines.MachineNode import MachineNode
  10. from .GlobalStack import GlobalStack
  11. from .ExtruderStack import ExtruderStack
  12. ## Contains helper functions to create new machines.
  13. class CuraStackBuilder:
  14. ## Create a new instance of a machine.
  15. #
  16. # \param name The name of the new machine.
  17. # \param definition_id The ID of the machine definition to use.
  18. #
  19. # \return The new global stack or None if an error occurred.
  20. @classmethod
  21. def createMachine(cls, name: str, definition_id: str) -> Optional[GlobalStack]:
  22. from cura.CuraApplication import CuraApplication
  23. application = CuraApplication.getInstance()
  24. registry = application.getContainerRegistry()
  25. container_tree = ContainerTree.getInstance()
  26. definitions = registry.findDefinitionContainers(id = definition_id)
  27. if not definitions:
  28. ConfigurationErrorMessage.getInstance().addFaultyContainers(definition_id)
  29. Logger.log("w", "Definition {definition} was not found!", definition = definition_id)
  30. return None
  31. machine_definition = definitions[0]
  32. machine_node = container_tree.machines[machine_definition.getId()]
  33. generated_name = registry.createUniqueName("machine", "", name, machine_definition.getName())
  34. # Make sure the new name does not collide with any definition or (quality) profile
  35. # createUniqueName() only looks at other stacks, but not at definitions or quality profiles
  36. # Note that we don't go for uniqueName() immediately because that function matches with ignore_case set to true
  37. if registry.findContainersMetadata(id = generated_name):
  38. generated_name = registry.uniqueName(generated_name)
  39. new_global_stack = cls.createGlobalStack(
  40. new_stack_id = generated_name,
  41. definition = machine_definition,
  42. variant_container = application.empty_variant_container,
  43. material_container = application.empty_material_container,
  44. quality_container = machine_node.preferredGlobalQuality().container,
  45. )
  46. new_global_stack.setName(generated_name)
  47. # Create ExtruderStacks
  48. extruder_dict = machine_definition.getMetaDataEntry("machine_extruder_trains")
  49. for position in extruder_dict:
  50. cls.createExtruderStackWithDefaultSetup(new_global_stack, position)
  51. for new_extruder in new_global_stack.extruders.values(): # Only register the extruders if we're sure that all of them are correct.
  52. registry.addContainer(new_extruder)
  53. # Register the global stack after the extruder stacks are created. This prevents the registry from adding another
  54. # extruder stack because the global stack didn't have one yet (which is enforced since Cura 3.1).
  55. registry.addContainer(new_global_stack)
  56. return new_global_stack
  57. ## Create a default Extruder Stack
  58. #
  59. # \param global_stack The global stack this extruder refers to.
  60. # \param extruder_position The position of the current extruder.
  61. @classmethod
  62. def createExtruderStackWithDefaultSetup(cls, global_stack: "GlobalStack", extruder_position: int) -> None:
  63. from cura.CuraApplication import CuraApplication
  64. application = CuraApplication.getInstance()
  65. registry = application.getContainerRegistry()
  66. # Get the extruder definition.
  67. extruder_definition_dict = global_stack.getMetaDataEntry("machine_extruder_trains")
  68. extruder_definition_id = extruder_definition_dict[str(extruder_position)]
  69. try:
  70. extruder_definition = registry.findDefinitionContainers(id = extruder_definition_id)[0]
  71. except IndexError:
  72. # It still needs to break, but we want to know what extruder ID made it break.
  73. msg = "Unable to find extruder definition with the id [%s]" % extruder_definition_id
  74. Logger.logException("e", msg)
  75. raise IndexError(msg)
  76. # Find out what filament diameter we need.
  77. approximate_diameter = round(extruder_definition.getProperty("material_diameter", "value")) # Can't be modified by definition changes since we are just initialising the stack here.
  78. # Find the preferred containers.
  79. machine_node = ContainerTree.getInstance().machines[global_stack.definition.getId()]
  80. extruder_variant_node = machine_node.variants.get(machine_node.preferred_variant_name)
  81. if not extruder_variant_node:
  82. Logger.log("w", "Could not find preferred nozzle {nozzle_name}. Falling back to {fallback}.".format(nozzle_name = machine_node.preferred_variant_name, fallback = next(iter(machine_node.variants))))
  83. extruder_variant_node = next(iter(machine_node.variants.values()))
  84. extruder_variant_container = extruder_variant_node.container
  85. material_node = extruder_variant_node.preferredMaterial(approximate_diameter)
  86. material_container = material_node.container
  87. quality_node = material_node.preferredQuality()
  88. new_extruder_id = registry.uniqueName(extruder_definition_id)
  89. new_extruder = cls.createExtruderStack(
  90. new_extruder_id,
  91. extruder_definition = extruder_definition,
  92. machine_definition_id = global_stack.definition.getId(),
  93. position = extruder_position,
  94. variant_container = extruder_variant_container,
  95. material_container = material_container,
  96. quality_container = quality_node.container
  97. )
  98. new_extruder.setNextStack(global_stack)
  99. registry.addContainer(new_extruder)
  100. ## Create a new Extruder stack
  101. #
  102. # \param new_stack_id The ID of the new stack.
  103. # \param extruder_definition The definition to base the new stack on.
  104. # \param machine_definition_id The ID of the machine definition to use for the user container.
  105. # \param position The position the extruder occupies in the machine.
  106. # \param variant_container The variant selected for the current extruder.
  107. # \param material_container The material selected for the current extruder.
  108. # \param quality_container The quality selected for the current extruder.
  109. #
  110. # \return A new Extruder stack instance with the specified parameters.
  111. @classmethod
  112. def createExtruderStack(cls, new_stack_id: str, extruder_definition: DefinitionContainerInterface,
  113. machine_definition_id: str,
  114. position: int,
  115. variant_container: "InstanceContainer",
  116. material_container: "InstanceContainer",
  117. quality_container: "InstanceContainer") -> ExtruderStack:
  118. from cura.CuraApplication import CuraApplication
  119. application = CuraApplication.getInstance()
  120. registry = application.getContainerRegistry()
  121. stack = ExtruderStack(new_stack_id)
  122. stack.setName(extruder_definition.getName())
  123. stack.setDefinition(extruder_definition)
  124. stack.setMetaDataEntry("position", str(position))
  125. user_container = cls.createUserChangesContainer(new_stack_id + "_user", machine_definition_id, new_stack_id,
  126. is_global_stack = False)
  127. stack.definitionChanges = cls.createDefinitionChangesContainer(stack, new_stack_id + "_settings")
  128. stack.variant = variant_container
  129. stack.material = material_container
  130. stack.quality = quality_container
  131. stack.intent = application.empty_intent_container
  132. stack.qualityChanges = application.empty_quality_changes_container
  133. stack.userChanges = user_container
  134. # Only add the created containers to the registry after we have set all the other
  135. # properties. This makes the create operation more transactional, since any problems
  136. # setting properties will not result in incomplete containers being added.
  137. registry.addContainer(user_container)
  138. return stack
  139. ## Create a new Global stack
  140. #
  141. # \param new_stack_id The ID of the new stack.
  142. # \param definition The definition to base the new stack on.
  143. # \param kwargs You can add keyword arguments to specify IDs of containers to use for a specific type, for example "variant": "0.4mm"
  144. #
  145. # \return A new Global stack instance with the specified parameters.
  146. ## Create a new Global stack
  147. #
  148. # \param new_stack_id The ID of the new stack.
  149. # \param definition The definition to base the new stack on.
  150. # \param variant_container The variant selected for the current stack.
  151. # \param material_container The material selected for the current stack.
  152. # \param quality_container The quality selected for the current stack.
  153. #
  154. # \return A new Global stack instance with the specified parameters.
  155. @classmethod
  156. def createGlobalStack(cls, new_stack_id: str, definition: DefinitionContainerInterface,
  157. variant_container: "InstanceContainer",
  158. material_container: "InstanceContainer",
  159. quality_container: "InstanceContainer") -> GlobalStack:
  160. from cura.CuraApplication import CuraApplication
  161. application = CuraApplication.getInstance()
  162. registry = application.getContainerRegistry()
  163. stack = GlobalStack(new_stack_id)
  164. stack.setDefinition(definition)
  165. # Create user container
  166. user_container = cls.createUserChangesContainer(new_stack_id + "_user", definition.getId(), new_stack_id,
  167. is_global_stack = True)
  168. stack.definitionChanges = cls.createDefinitionChangesContainer(stack, new_stack_id + "_settings")
  169. stack.variant = variant_container
  170. stack.material = material_container
  171. stack.quality = quality_container
  172. stack.intent = application.empty_intent_container
  173. stack.qualityChanges = application.empty_quality_changes_container
  174. stack.userChanges = user_container
  175. registry.addContainer(user_container)
  176. return stack
  177. @classmethod
  178. def createUserChangesContainer(cls, container_name: str, definition_id: str, stack_id: str,
  179. is_global_stack: bool) -> "InstanceContainer":
  180. from cura.CuraApplication import CuraApplication
  181. application = CuraApplication.getInstance()
  182. registry = application.getContainerRegistry()
  183. unique_container_name = registry.uniqueName(container_name)
  184. container = InstanceContainer(unique_container_name)
  185. container.setDefinition(definition_id)
  186. container.setMetaDataEntry("type", "user")
  187. container.setMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  188. metadata_key_to_add = "machine" if is_global_stack else "extruder"
  189. container.setMetaDataEntry(metadata_key_to_add, stack_id)
  190. return container
  191. @classmethod
  192. def createDefinitionChangesContainer(cls, container_stack, container_name):
  193. from cura.CuraApplication import CuraApplication
  194. application = CuraApplication.getInstance()
  195. registry = application.getContainerRegistry()
  196. unique_container_name = registry.uniqueName(container_name)
  197. definition_changes_container = InstanceContainer(unique_container_name)
  198. definition_changes_container.setDefinition(container_stack.getBottom().getId())
  199. definition_changes_container.setMetaDataEntry("type", "definition_changes")
  200. definition_changes_container.setMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  201. registry.addContainer(definition_changes_container)
  202. container_stack.definitionChanges = definition_changes_container
  203. return definition_changes_container