CuraStackBuilder.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 cura.Machines.VariantType import VariantType
  9. from .GlobalStack import GlobalStack
  10. from .ExtruderStack import ExtruderStack
  11. ## Contains helper functions to create new machines.
  12. class CuraStackBuilder:
  13. ## Create a new instance of a machine.
  14. #
  15. # \param name The name of the new machine.
  16. # \param definition_id The ID of the machine definition to use.
  17. #
  18. # \return The new global stack or None if an error occurred.
  19. @classmethod
  20. def createMachine(cls, name: str, definition_id: str) -> Optional[GlobalStack]:
  21. from cura.CuraApplication import CuraApplication
  22. application = CuraApplication.getInstance()
  23. variant_manager = application.getVariantManager()
  24. quality_manager = application.getQualityManager()
  25. registry = application.getContainerRegistry()
  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. # get variant container for the global stack
  33. global_variant_container = application.empty_variant_container
  34. global_variant_node = variant_manager.getDefaultVariantNode(machine_definition, VariantType.BUILD_PLATE)
  35. if global_variant_node:
  36. global_variant_container = global_variant_node.getContainer()
  37. if not global_variant_container:
  38. global_variant_container = application.empty_variant_container
  39. generated_name = registry.createUniqueName("machine", "", name, machine_definition.getName())
  40. # Make sure the new name does not collide with any definition or (quality) profile
  41. # createUniqueName() only looks at other stacks, but not at definitions or quality profiles
  42. # Note that we don't go for uniqueName() immediately because that function matches with ignore_case set to true
  43. if registry.findContainersMetadata(id = generated_name):
  44. generated_name = registry.uniqueName(generated_name)
  45. new_global_stack = cls.createGlobalStack(
  46. new_stack_id = generated_name,
  47. definition = machine_definition,
  48. variant_container = global_variant_container,
  49. material_container = application.empty_material_container,
  50. quality_container = application.empty_quality_container,
  51. )
  52. new_global_stack.setName(generated_name)
  53. # Create ExtruderStacks
  54. extruder_dict = machine_definition.getMetaDataEntry("machine_extruder_trains")
  55. for position in extruder_dict:
  56. cls.createExtruderStackWithDefaultSetup(new_global_stack, position)
  57. for new_extruder in new_global_stack.extruders.values(): #Only register the extruders if we're sure that all of them are correct.
  58. registry.addContainer(new_extruder)
  59. preferred_quality_type = machine_definition.getMetaDataEntry("preferred_quality_type")
  60. quality_group_dict = quality_manager.getQualityGroups(new_global_stack)
  61. if not quality_group_dict:
  62. # There is no available quality group, set all quality containers to empty.
  63. new_global_stack.quality = application.empty_quality_container
  64. for extruder_stack in new_global_stack.extruders.values():
  65. extruder_stack.quality = application.empty_quality_container
  66. else:
  67. # Set the quality containers to the preferred quality type if available, otherwise use the first quality
  68. # type that's available.
  69. if preferred_quality_type not in quality_group_dict:
  70. Logger.log("w", "The preferred quality {quality_type} doesn't exist for this set-up. Choosing a random one.".format(quality_type = preferred_quality_type))
  71. preferred_quality_type = next(iter(quality_group_dict))
  72. quality_group = quality_group_dict.get(preferred_quality_type)
  73. new_global_stack.quality = quality_group.node_for_global.getContainer()
  74. if not new_global_stack.quality:
  75. new_global_stack.quality = application.empty_quality_container
  76. for position, extruder_stack in new_global_stack.extruders.items():
  77. if position in quality_group.nodes_for_extruders and quality_group.nodes_for_extruders[position].getContainer():
  78. extruder_stack.quality = quality_group.nodes_for_extruders[position].getContainer()
  79. else:
  80. extruder_stack.quality = application.empty_quality_container
  81. # Register the global stack after the extruder stacks are created. This prevents the registry from adding another
  82. # extruder stack because the global stack didn't have one yet (which is enforced since Cura 3.1).
  83. registry.addContainer(new_global_stack)
  84. return new_global_stack
  85. ## Create a default Extruder Stack
  86. #
  87. # \param global_stack The global stack this extruder refers to.
  88. # \param extruder_position The position of the current extruder.
  89. @classmethod
  90. def createExtruderStackWithDefaultSetup(cls, global_stack: "GlobalStack", extruder_position: int) -> None:
  91. from cura.CuraApplication import CuraApplication
  92. application = CuraApplication.getInstance()
  93. variant_manager = application.getVariantManager()
  94. material_manager = application.getMaterialManager()
  95. registry = application.getContainerRegistry()
  96. # get variant container for extruders
  97. extruder_variant_container = application.empty_variant_container
  98. extruder_variant_node = variant_manager.getDefaultVariantNode(global_stack.definition, VariantType.NOZZLE,
  99. global_stack = global_stack)
  100. extruder_variant_name = None
  101. if extruder_variant_node:
  102. extruder_variant_container = extruder_variant_node.getContainer()
  103. if not extruder_variant_container:
  104. extruder_variant_container = application.empty_variant_container
  105. extruder_variant_name = extruder_variant_container.getName()
  106. extruder_definition_dict = global_stack.getMetaDataEntry("machine_extruder_trains")
  107. extruder_definition_id = extruder_definition_dict[str(extruder_position)]
  108. extruder_definition = registry.findDefinitionContainers(id = extruder_definition_id)[0]
  109. # get material container for extruders
  110. material_container = application.empty_material_container
  111. material_node = material_manager.getDefaultMaterial(global_stack, str(extruder_position), extruder_variant_name,
  112. extruder_definition = extruder_definition)
  113. if material_node and material_node.getContainer():
  114. material_container = material_node.getContainer()
  115. new_extruder_id = registry.uniqueName(extruder_definition_id)
  116. new_extruder = cls.createExtruderStack(
  117. new_extruder_id,
  118. extruder_definition = extruder_definition,
  119. machine_definition_id = global_stack.definition.getId(),
  120. position = extruder_position,
  121. variant_container = extruder_variant_container,
  122. material_container = material_container,
  123. quality_container = application.empty_quality_container
  124. )
  125. new_extruder.setNextStack(global_stack)
  126. registry.addContainer(new_extruder)
  127. ## Create a new Extruder stack
  128. #
  129. # \param new_stack_id The ID of the new stack.
  130. # \param extruder_definition The definition to base the new stack on.
  131. # \param machine_definition_id The ID of the machine definition to use for the user container.
  132. # \param position The position the extruder occupies in the machine.
  133. # \param variant_container The variant selected for the current extruder.
  134. # \param material_container The material selected for the current extruder.
  135. # \param quality_container The quality selected for the current extruder.
  136. #
  137. # \return A new Extruder stack instance with the specified parameters.
  138. @classmethod
  139. def createExtruderStack(cls, new_stack_id: str, extruder_definition: DefinitionContainerInterface,
  140. machine_definition_id: str,
  141. position: int,
  142. variant_container: "InstanceContainer",
  143. material_container: "InstanceContainer",
  144. quality_container: "InstanceContainer") -> ExtruderStack:
  145. from cura.CuraApplication import CuraApplication
  146. application = CuraApplication.getInstance()
  147. registry = application.getContainerRegistry()
  148. stack = ExtruderStack(new_stack_id)
  149. stack.setName(extruder_definition.getName())
  150. stack.setDefinition(extruder_definition)
  151. stack.setMetaDataEntry("position", str(position))
  152. user_container = cls.createUserChangesContainer(new_stack_id + "_user", machine_definition_id, new_stack_id,
  153. is_global_stack = False)
  154. stack.definitionChanges = cls.createDefinitionChangesContainer(stack, new_stack_id + "_settings")
  155. stack.variant = variant_container
  156. stack.material = material_container
  157. stack.quality = quality_container
  158. stack.qualityChanges = application.empty_quality_changes_container
  159. stack.userChanges = user_container
  160. # Only add the created containers to the registry after we have set all the other
  161. # properties. This makes the create operation more transactional, since any problems
  162. # setting properties will not result in incomplete containers being added.
  163. registry.addContainer(user_container)
  164. return stack
  165. ## Create a new Global stack
  166. #
  167. # \param new_stack_id The ID of the new stack.
  168. # \param definition The definition to base the new stack on.
  169. # \param kwargs You can add keyword arguments to specify IDs of containers to use for a specific type, for example "variant": "0.4mm"
  170. #
  171. # \return A new Global stack instance with the specified parameters.
  172. ## Create a new Global stack
  173. #
  174. # \param new_stack_id The ID of the new stack.
  175. # \param definition The definition to base the new stack on.
  176. # \param variant_container The variant selected for the current stack.
  177. # \param material_container The material selected for the current stack.
  178. # \param quality_container The quality selected for the current stack.
  179. #
  180. # \return A new Global stack instance with the specified parameters.
  181. @classmethod
  182. def createGlobalStack(cls, new_stack_id: str, definition: DefinitionContainerInterface,
  183. variant_container: "InstanceContainer",
  184. material_container: "InstanceContainer",
  185. quality_container: "InstanceContainer") -> GlobalStack:
  186. from cura.CuraApplication import CuraApplication
  187. application = CuraApplication.getInstance()
  188. registry = application.getContainerRegistry()
  189. stack = GlobalStack(new_stack_id)
  190. stack.setDefinition(definition)
  191. # Create user container
  192. user_container = cls.createUserChangesContainer(new_stack_id + "_user", definition.getId(), new_stack_id,
  193. is_global_stack = True)
  194. stack.definitionChanges = cls.createDefinitionChangesContainer(stack, new_stack_id + "_settings")
  195. stack.variant = variant_container
  196. stack.material = material_container
  197. stack.quality = quality_container
  198. stack.qualityChanges = application.empty_quality_changes_container
  199. stack.userChanges = user_container
  200. registry.addContainer(user_container)
  201. return stack
  202. @classmethod
  203. def createUserChangesContainer(cls, container_name: str, definition_id: str, stack_id: str,
  204. is_global_stack: bool) -> "InstanceContainer":
  205. from cura.CuraApplication import CuraApplication
  206. application = CuraApplication.getInstance()
  207. registry = application.getContainerRegistry()
  208. unique_container_name = registry.uniqueName(container_name)
  209. container = InstanceContainer(unique_container_name)
  210. container.setDefinition(definition_id)
  211. container.setMetaDataEntry("type", "user")
  212. container.setMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  213. metadata_key_to_add = "machine" if is_global_stack else "extruder"
  214. container.setMetaDataEntry(metadata_key_to_add, stack_id)
  215. return container
  216. @classmethod
  217. def createDefinitionChangesContainer(cls, container_stack, container_name):
  218. from cura.CuraApplication import CuraApplication
  219. application = CuraApplication.getInstance()
  220. registry = application.getContainerRegistry()
  221. unique_container_name = registry.uniqueName(container_name)
  222. definition_changes_container = InstanceContainer(unique_container_name)
  223. definition_changes_container.setDefinition(container_stack.getBottom().getId())
  224. definition_changes_container.setMetaDataEntry("type", "definition_changes")
  225. definition_changes_container.setMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  226. registry.addContainer(definition_changes_container)
  227. container_stack.definitionChanges = definition_changes_container
  228. return definition_changes_container