CuraStackBuilder.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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, 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. global_stack.addExtruder(new_extruder)
  127. registry.addContainer(new_extruder)
  128. ## Create a new Extruder stack
  129. #
  130. # \param new_stack_id The ID of the new stack.
  131. # \param extruder_definition The definition to base the new stack on.
  132. # \param machine_definition_id The ID of the machine definition to use for the user container.
  133. # \param position The position the extruder occupies in the machine.
  134. # \param variant_container The variant selected for the current extruder.
  135. # \param material_container The material selected for the current extruder.
  136. # \param quality_container The quality selected for the current extruder.
  137. #
  138. # \return A new Extruder stack instance with the specified parameters.
  139. @classmethod
  140. def createExtruderStack(cls, new_stack_id: str, extruder_definition: DefinitionContainerInterface,
  141. machine_definition_id: str,
  142. position: int,
  143. variant_container: "InstanceContainer",
  144. material_container: "InstanceContainer",
  145. quality_container: "InstanceContainer") -> ExtruderStack:
  146. from cura.CuraApplication import CuraApplication
  147. application = CuraApplication.getInstance()
  148. registry = application.getContainerRegistry()
  149. stack = ExtruderStack(new_stack_id)
  150. stack.setName(extruder_definition.getName())
  151. stack.setDefinition(extruder_definition)
  152. stack.setMetaDataEntry("position", str(position))
  153. user_container = cls.createUserChangesContainer(new_stack_id + "_user", machine_definition_id, new_stack_id,
  154. is_global_stack = False)
  155. stack.definitionChanges = cls.createDefinitionChangesContainer(stack, new_stack_id + "_settings")
  156. stack.variant = variant_container
  157. stack.material = material_container
  158. stack.quality = quality_container
  159. stack.qualityChanges = application.empty_quality_changes_container
  160. stack.userChanges = user_container
  161. # Only add the created containers to the registry after we have set all the other
  162. # properties. This makes the create operation more transactional, since any problems
  163. # setting properties will not result in incomplete containers being added.
  164. registry.addContainer(user_container)
  165. return stack
  166. ## Create a new Global stack
  167. #
  168. # \param new_stack_id The ID of the new stack.
  169. # \param definition The definition to base the new stack on.
  170. # \param kwargs You can add keyword arguments to specify IDs of containers to use for a specific type, for example "variant": "0.4mm"
  171. #
  172. # \return A new Global stack instance with the specified parameters.
  173. ## Create a new Global stack
  174. #
  175. # \param new_stack_id The ID of the new stack.
  176. # \param definition The definition to base the new stack on.
  177. # \param variant_container The variant selected for the current stack.
  178. # \param material_container The material selected for the current stack.
  179. # \param quality_container The quality selected for the current stack.
  180. #
  181. # \return A new Global stack instance with the specified parameters.
  182. @classmethod
  183. def createGlobalStack(cls, new_stack_id: str, definition: DefinitionContainerInterface,
  184. variant_container: "InstanceContainer",
  185. material_container: "InstanceContainer",
  186. quality_container: "InstanceContainer") -> GlobalStack:
  187. from cura.CuraApplication import CuraApplication
  188. application = CuraApplication.getInstance()
  189. registry = application.getContainerRegistry()
  190. stack = GlobalStack(new_stack_id)
  191. stack.setDefinition(definition)
  192. # Create user container
  193. user_container = cls.createUserChangesContainer(new_stack_id + "_user", definition.getId(), new_stack_id,
  194. is_global_stack = True)
  195. stack.definitionChanges = cls.createDefinitionChangesContainer(stack, new_stack_id + "_settings")
  196. stack.variant = variant_container
  197. stack.material = material_container
  198. stack.quality = quality_container
  199. stack.qualityChanges = application.empty_quality_changes_container
  200. stack.userChanges = user_container
  201. registry.addContainer(user_container)
  202. return stack
  203. @classmethod
  204. def createUserChangesContainer(cls, container_name: str, definition_id: str, stack_id: str,
  205. is_global_stack: bool) -> "InstanceContainer":
  206. from cura.CuraApplication import CuraApplication
  207. application = CuraApplication.getInstance()
  208. registry = application.getContainerRegistry()
  209. unique_container_name = registry.uniqueName(container_name)
  210. container = InstanceContainer(unique_container_name)
  211. container.setDefinition(definition_id)
  212. container.setMetaDataEntry("type", "user")
  213. container.setMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  214. metadata_key_to_add = "machine" if is_global_stack else "extruder"
  215. container.setMetaDataEntry(metadata_key_to_add, stack_id)
  216. return container
  217. @classmethod
  218. def createDefinitionChangesContainer(cls, container_stack, container_name):
  219. from cura.CuraApplication import CuraApplication
  220. application = CuraApplication.getInstance()
  221. registry = application.getContainerRegistry()
  222. unique_container_name = registry.uniqueName(container_name)
  223. definition_changes_container = InstanceContainer(unique_container_name)
  224. definition_changes_container.setDefinition(container_stack.getBottom().getId())
  225. definition_changes_container.setMetaDataEntry("type", "definition_changes")
  226. definition_changes_container.setMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  227. registry.addContainer(definition_changes_container)
  228. container_stack.definitionChanges = definition_changes_container
  229. return definition_changes_container