CuraStackBuilder.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # Copyright (c) 2018 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 UM.Settings.ContainerRegistry import ContainerRegistry
  9. from cura.Machines.VariantManager import VariantType
  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. variant_manager = application.getVariantManager()
  25. material_manager = application.getMaterialManager()
  26. quality_manager = application.getQualityManager()
  27. registry = ContainerRegistry.getInstance()
  28. definitions = registry.findDefinitionContainers(id = definition_id)
  29. if not definitions:
  30. ConfigurationErrorMessage.getInstance().addFaultyContainers(definition_id)
  31. Logger.log("w", "Definition {definition} was not found!", definition = definition_id)
  32. return None
  33. machine_definition = definitions[0]
  34. # get variant container for the global stack
  35. global_variant_container = application.empty_variant_container
  36. global_variant_node = variant_manager.getDefaultVariantNode(machine_definition, VariantType.BUILD_PLATE)
  37. if global_variant_node:
  38. global_variant_container = global_variant_node.getContainer()
  39. if not global_variant_container:
  40. global_variant_container = application.empty_variant_container
  41. # get variant container for extruders
  42. extruder_variant_container = application.empty_variant_container
  43. extruder_variant_node = variant_manager.getDefaultVariantNode(machine_definition, VariantType.NOZZLE)
  44. extruder_variant_name = None
  45. if extruder_variant_node:
  46. extruder_variant_container = extruder_variant_node.getContainer()
  47. if not extruder_variant_container:
  48. extruder_variant_container = application.empty_variant_container
  49. extruder_variant_name = extruder_variant_container.getName()
  50. generated_name = registry.createUniqueName("machine", "", name, machine_definition.getName())
  51. # Make sure the new name does not collide with any definition or (quality) profile
  52. # createUniqueName() only looks at other stacks, but not at definitions or quality profiles
  53. # Note that we don't go for uniqueName() immediately because that function matches with ignore_case set to true
  54. if registry.findContainersMetadata(id = generated_name):
  55. generated_name = registry.uniqueName(generated_name)
  56. new_global_stack = cls.createGlobalStack(
  57. new_stack_id = generated_name,
  58. definition = machine_definition,
  59. variant_container = global_variant_container,
  60. material_container = application.empty_material_container,
  61. quality_container = application.empty_quality_container,
  62. )
  63. new_global_stack.setName(generated_name)
  64. # get material container for extruders
  65. material_container = application.empty_material_container
  66. material_node = material_manager.getDefaultMaterial(new_global_stack, extruder_variant_name)
  67. if material_node and material_node.getContainer():
  68. material_container = material_node.getContainer()
  69. # Create ExtruderStacks
  70. extruder_dict = machine_definition.getMetaDataEntry("machine_extruder_trains")
  71. for position, extruder_definition_id in extruder_dict.items():
  72. # Sanity check: make sure that the positions in the extruder definitions are same as in the machine
  73. # definition
  74. extruder_definition = registry.findDefinitionContainers(id = extruder_definition_id)[0]
  75. position_in_extruder_def = extruder_definition.getMetaDataEntry("position")
  76. if position_in_extruder_def != position:
  77. ConfigurationErrorMessage.getInstance().addFaultyContainers(extruder_definition_id)
  78. return None #Don't return any container stack then, not the rest of the extruders either.
  79. new_extruder_id = registry.uniqueName(extruder_definition_id)
  80. new_extruder = cls.createExtruderStack(
  81. new_extruder_id,
  82. extruder_definition = extruder_definition,
  83. machine_definition_id = definition_id,
  84. position = position,
  85. variant_container = extruder_variant_container,
  86. material_container = material_container,
  87. quality_container = application.empty_quality_container,
  88. global_stack = new_global_stack,
  89. )
  90. new_extruder.setNextStack(new_global_stack)
  91. new_global_stack.addExtruder(new_extruder)
  92. for new_extruder in new_global_stack.extruders.values(): #Only register the extruders if we're sure that all of them are correct.
  93. registry.addContainer(new_extruder)
  94. preferred_quality_type = machine_definition.getMetaDataEntry("preferred_quality_type")
  95. quality_group_dict = quality_manager.getQualityGroups(new_global_stack)
  96. quality_group = quality_group_dict.get(preferred_quality_type)
  97. new_global_stack.quality = quality_group.node_for_global.getContainer()
  98. if not new_global_stack.quality:
  99. new_global_stack.quality = application.empty_quality_container
  100. for position, extruder_stack in new_global_stack.extruders.items():
  101. if position in quality_group.nodes_for_extruders and quality_group.nodes_for_extruders[position].getContainer():
  102. extruder_stack.quality = quality_group.nodes_for_extruders[position].getContainer()
  103. else:
  104. extruder_stack.quality = application.empty_quality_container
  105. # Register the global stack after the extruder stacks are created. This prevents the registry from adding another
  106. # extruder stack because the global stack didn't have one yet (which is enforced since Cura 3.1).
  107. registry.addContainer(new_global_stack)
  108. return new_global_stack
  109. ## Create a new Extruder stack
  110. #
  111. # \param new_stack_id The ID of the new stack.
  112. # \param definition The definition to base the new stack on.
  113. # \param machine_definition_id The ID of the machine definition to use for
  114. # the user container.
  115. # \param kwargs You can add keyword arguments to specify IDs of containers to use for a specific type, for example "variant": "0.4mm"
  116. #
  117. # \return A new Global stack instance with the specified parameters.
  118. @classmethod
  119. def createExtruderStack(cls, new_stack_id: str, extruder_definition: DefinitionContainerInterface, machine_definition_id: str,
  120. position: int,
  121. variant_container, material_container, quality_container, global_stack) -> ExtruderStack:
  122. from cura.CuraApplication import CuraApplication
  123. application = CuraApplication.getInstance()
  124. stack = ExtruderStack(new_stack_id, parent = global_stack)
  125. stack.setName(extruder_definition.getName())
  126. stack.setDefinition(extruder_definition)
  127. stack.addMetaDataEntry("position", position)
  128. user_container = cls.createUserChangesContainer(new_stack_id + "_user", machine_definition_id, new_stack_id,
  129. is_global_stack = False)
  130. stack.definitionChanges = cls.createDefinitionChangesContainer(stack, new_stack_id + "_settings")
  131. stack.variant = variant_container
  132. stack.material = material_container
  133. stack.quality = quality_container
  134. stack.qualityChanges = application.empty_quality_changes_container
  135. stack.userChanges = user_container
  136. # Only add the created containers to the registry after we have set all the other
  137. # properties. This makes the create operation more transactional, since any problems
  138. # setting properties will not result in incomplete containers being added.
  139. ContainerRegistry.getInstance().addContainer(user_container)
  140. return stack
  141. ## Create a new Global stack
  142. #
  143. # \param new_stack_id The ID of the new stack.
  144. # \param definition The definition to base the new stack on.
  145. # \param kwargs You can add keyword arguments to specify IDs of containers to use for a specific type, for example "variant": "0.4mm"
  146. #
  147. # \return A new Global stack instance with the specified parameters.
  148. @classmethod
  149. def createGlobalStack(cls, new_stack_id: str, definition: DefinitionContainerInterface,
  150. variant_container, material_container, quality_container) -> GlobalStack:
  151. from cura.CuraApplication import CuraApplication
  152. application = CuraApplication.getInstance()
  153. stack = GlobalStack(new_stack_id)
  154. stack.setDefinition(definition)
  155. # Create user container
  156. user_container = cls.createUserChangesContainer(new_stack_id + "_user", definition.getId(), new_stack_id,
  157. is_global_stack = True)
  158. stack.definitionChanges = cls.createDefinitionChangesContainer(stack, new_stack_id + "_settings")
  159. stack.variant = variant_container
  160. stack.material = material_container
  161. stack.quality = quality_container
  162. stack.qualityChanges = application.empty_quality_changes_container
  163. stack.userChanges = user_container
  164. ContainerRegistry.getInstance().addContainer(user_container)
  165. return stack
  166. @classmethod
  167. def createUserChangesContainer(cls, container_name: str, definition_id: str, stack_id: str,
  168. is_global_stack: bool) -> "InstanceContainer":
  169. from cura.CuraApplication import CuraApplication
  170. unique_container_name = ContainerRegistry.getInstance().uniqueName(container_name)
  171. container = InstanceContainer(unique_container_name)
  172. container.setDefinition(definition_id)
  173. container.addMetaDataEntry("type", "user")
  174. container.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  175. metadata_key_to_add = "machine" if is_global_stack else "extruder"
  176. container.addMetaDataEntry(metadata_key_to_add, stack_id)
  177. return container
  178. @classmethod
  179. def createDefinitionChangesContainer(cls, container_stack, container_name):
  180. from cura.CuraApplication import CuraApplication
  181. unique_container_name = ContainerRegistry.getInstance().uniqueName(container_name)
  182. definition_changes_container = InstanceContainer(unique_container_name)
  183. definition_changes_container.setDefinition(container_stack.getBottom().getId())
  184. definition_changes_container.addMetaDataEntry("type", "definition_changes")
  185. definition_changes_container.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  186. ContainerRegistry.getInstance().addContainer(definition_changes_container)
  187. container_stack.definitionChanges = definition_changes_container
  188. return definition_changes_container