CuraStackBuilder.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. extruder_definition = registry.findDefinitionContainers(machine = machine_definition.getId())
  41. if not extruder_definition:
  42. # create extruder stack for single extrusion machines that have no separate extruder definition files
  43. extruder_definition = registry.findDefinitionContainers(id = "fdmextruder")[0]
  44. new_extruder_id = registry.uniqueName(machine_definition.getName() + " " + 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. new_global_stack.addExtruder(new_extruder)
  55. else:
  56. # create extruder stack for each found extruder definition
  57. for extruder_definition in registry.findDefinitionContainers(machine = machine_definition.id):
  58. position = extruder_definition.getMetaDataEntry("position", None)
  59. if not position:
  60. Logger.log("w", "Extruder definition %s specifies no position metadata entry.", extruder_definition.id)
  61. new_extruder_id = registry.uniqueName(extruder_definition.id)
  62. new_extruder = cls.createExtruderStack(
  63. new_extruder_id,
  64. definition = extruder_definition,
  65. machine_definition = machine_definition,
  66. quality = "default",
  67. material = "default",
  68. variant = "default",
  69. next_stack = new_global_stack
  70. )
  71. new_global_stack.addExtruder(new_extruder)
  72. return new_global_stack
  73. ## Create a new Extruder stack
  74. #
  75. # \param new_stack_id The ID of the new stack.
  76. # \param definition The definition to base the new stack on.
  77. # \param machine_definition The machine definition to use for the user container.
  78. # \param kwargs You can add keyword arguments to specify IDs of containers to use for a specific type, for example "variant": "0.4mm"
  79. #
  80. # \return A new Global stack instance with the specified parameters.
  81. @classmethod
  82. def createExtruderStack(cls, new_stack_id: str, definition: DefinitionContainer, machine_definition: DefinitionContainer, **kwargs) -> ExtruderStack:
  83. stack = ExtruderStack(new_stack_id)
  84. stack.setName(definition.getName())
  85. stack.setDefinition(definition)
  86. stack.addMetaDataEntry("position", definition.getMetaDataEntry("position"))
  87. if "next_stack" in kwargs:
  88. # Add stacks before containers are added, since they may trigger a setting update.
  89. stack.setNextStack(kwargs["next_stack"])
  90. user_container = InstanceContainer(new_stack_id + "_user")
  91. user_container.addMetaDataEntry("type", "user")
  92. user_container.addMetaDataEntry("extruder", new_stack_id)
  93. from cura.CuraApplication import CuraApplication
  94. user_container.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  95. user_container.setDefinition(definition)
  96. stack.setUserChanges(user_container)
  97. # Important! The order here matters, because that allows the stack to
  98. # assume the material and variant have already been set.
  99. if "definition_changes" in kwargs:
  100. stack.setDefinitionChangesById(kwargs["definition_changes"])
  101. else:
  102. stack.setDefinitionChanges(cls.createDefinitionChangesContainer(stack, new_stack_id + "_settings"))
  103. if "variant" in kwargs:
  104. stack.setVariantById(kwargs["variant"])
  105. if "material" in kwargs:
  106. stack.setMaterialById(kwargs["material"])
  107. if "quality" in kwargs:
  108. stack.setQualityById(kwargs["quality"])
  109. if "quality_changes" in kwargs:
  110. stack.setQualityChangesById(kwargs["quality_changes"])
  111. # Only add the created containers to the registry after we have set all the other
  112. # properties. This makes the create operation more transactional, since any problems
  113. # setting properties will not result in incomplete containers being added.
  114. registry = ContainerRegistry.getInstance()
  115. registry.addContainer(stack)
  116. registry.addContainer(user_container)
  117. return stack
  118. ## Create a new Global stack
  119. #
  120. # \param new_stack_id The ID of the new stack.
  121. # \param definition The definition to base the new stack on.
  122. # \param kwargs You can add keyword arguments to specify IDs of containers to use for a specific type, for example "variant": "0.4mm"
  123. #
  124. # \return A new Global stack instance with the specified parameters.
  125. @classmethod
  126. def createGlobalStack(cls, new_stack_id: str, definition: DefinitionContainer, **kwargs) -> GlobalStack:
  127. stack = GlobalStack(new_stack_id)
  128. stack.setDefinition(definition)
  129. user_container = InstanceContainer(new_stack_id + "_user")
  130. user_container.addMetaDataEntry("type", "user")
  131. user_container.addMetaDataEntry("machine", new_stack_id)
  132. from cura.CuraApplication import CuraApplication
  133. user_container.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  134. user_container.setDefinition(definition)
  135. stack.setUserChanges(user_container)
  136. # Important! The order here matters, because that allows the stack to
  137. # assume the material and variant have already been set.
  138. if "definition_changes" in kwargs:
  139. stack.setDefinitionChangesById(kwargs["definition_changes"])
  140. else:
  141. stack.setDefinitionChanges(cls.createDefinitionChangesContainer(stack, new_stack_id + "_settings"))
  142. if "variant" in kwargs:
  143. stack.setVariantById(kwargs["variant"])
  144. if "material" in kwargs:
  145. stack.setMaterialById(kwargs["material"])
  146. if "quality" in kwargs:
  147. stack.setQualityById(kwargs["quality"])
  148. if "quality_changes" in kwargs:
  149. stack.setQualityChangesById(kwargs["quality_changes"])
  150. registry = ContainerRegistry.getInstance()
  151. registry.addContainer(stack)
  152. registry.addContainer(user_container)
  153. return stack
  154. @classmethod
  155. def createDefinitionChangesContainer(cls, container_stack, container_name, container_index = None):
  156. from cura.CuraApplication import CuraApplication
  157. unique_container_name = ContainerRegistry.getInstance().uniqueName(container_name)
  158. definition_changes_container = InstanceContainer(unique_container_name)
  159. definition = container_stack.getBottom()
  160. definition_changes_container.setDefinition(definition)
  161. definition_changes_container.addMetaDataEntry("type", "definition_changes")
  162. definition_changes_container.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  163. ContainerRegistry.getInstance().addContainer(definition_changes_container)
  164. container_stack.definitionChanges = definition_changes_container
  165. return definition_changes_container