VersionUpgrade49to410.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser
  4. import io
  5. import os.path # To get the file ID.
  6. from typing import Dict, List, Tuple
  7. from UM.VersionUpgrade import VersionUpgrade
  8. class VersionUpgrade49to410(VersionUpgrade):
  9. """
  10. Upgrades configurations from the state they were in at version 4.9 to the state they should be in at version 4.10.
  11. """
  12. _renamed_profiles = {
  13. # Definitions.
  14. "twotrees_bluer" : "two_trees_bluer",
  15. # Upgrade for people who had the original TwoTrees Bluer profiles from 4.9 and earlier.
  16. "twotrees_bluer_extruder_0": "two_trees_base_extruder_0",
  17. "twotrees_bluer_extruder_1": "two_trees_base_extruder_0"
  18. }
  19. _quality_changes_to_two_trees_base = {
  20. "twotrees_bluer_extruder_0",
  21. "twotrees_bluer_extruder_1",
  22. "twotrees_bluer"
  23. }
  24. # Default variant to select for legacy TwoTrees printers, now that we have variants.
  25. _default_variants = {
  26. "twotrees_bluer_extruder_0" : "two_trees_bluer_0.4",
  27. "twotrees_bluer_extruder_1" : "two_trees_bluer_0.4"
  28. }
  29. _two_trees_bluer_quality_type_conversion = {
  30. "high" : "ultra",
  31. "normal" : "super",
  32. "fast" : "standard",
  33. "draft" : "standard",
  34. "extra_fast" : "low",
  35. "coarse" : "draft",
  36. "extra_coarse": "draft"
  37. }
  38. _two_trees_quality_per_material = {
  39. # Since legacy TwoTrees printers didn't have different variants, we always pick the 0.4mm variant.
  40. "generic_abs_175" : {
  41. "high" : "two_trees_0.4_ABS_super",
  42. "normal" : "two_trees_0.4_ABS_super",
  43. "fast" : "two_trees_0.4_ABS_super",
  44. "draft" : "two_trees_0.4_ABS_standard",
  45. "extra_fast" : "two_trees_0.4_ABS_low",
  46. "coarse" : "two_trees_0.4_ABS_low",
  47. "extra_coarse": "two_trees_0.4_ABS_low"
  48. },
  49. "generic_petg_175": {
  50. "high" : "two_trees_0.4_PETG_super",
  51. "normal" : "two_trees_0.4_PETG_super",
  52. "fast" : "two_trees_0.4_PETG_super",
  53. "draft" : "two_trees_0.4_PETG_standard",
  54. "extra_fast" : "two_trees_0.4_PETG_low",
  55. "coarse" : "two_trees_0.4_PETG_low",
  56. "extra_coarse": "two_trees_0.4_PETG_low"
  57. },
  58. "generic_pla_175" : {
  59. "high" : "two_trees_0.4_PLA_super",
  60. "normal" : "two_trees_0.4_PLA_super",
  61. "fast" : "two_trees_0.4_PLA_super",
  62. "draft" : "two_trees_0.4_PLA_standard",
  63. "extra_fast" : "two_trees_0.4_PLA_low",
  64. "coarse" : "two_trees_0.4_PLA_low",
  65. "extra_coarse": "two_trees_0.4_PLA_low"
  66. },
  67. "generic_tpu_175" : {
  68. "high" : "two_trees_0.4_TPU_super",
  69. "normal" : "two_trees_0.4_TPU_super",
  70. "fast" : "two_trees_0.4_TPU_super",
  71. "draft" : "two_trees_0.4_TPU_standard",
  72. "extra_fast" : "two_trees_0.4_TPU_standard",
  73. "coarse" : "two_trees_0.4_TPU_standard",
  74. "extra_coarse": "two_trees_0.4_TPU_standard"
  75. },
  76. "empty_material" : { # For the global stack.
  77. "high" : "two_trees_global_super",
  78. "normal" : "two_trees_global_super",
  79. "fast" : "two_trees_global_super",
  80. "draft" : "two_trees_global_standard",
  81. "extra_fast" : "two_trees_global_low",
  82. "coarse" : "two_trees_global_low",
  83. "extra_coarse": "two_trees_global_low"
  84. }
  85. }
  86. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  87. """Upgrades instance containers to have the new version number.
  88. This renames the renamed settings in the containers.
  89. """
  90. parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
  91. parser.read_string(serialized)
  92. # Update setting version number.
  93. parser["metadata"]["setting_version"] = "17"
  94. # Certain instance containers (such as definition changes) reference to a certain definition container
  95. # Since a number of those changed name, we also need to update those.
  96. old_definition = parser["general"]["definition"]
  97. if old_definition in self._renamed_profiles:
  98. parser["general"]["definition"] = self._renamed_profiles[old_definition]
  99. # For quality-changes profiles made for TwoTrees Bluer printers, change the definition to the two_trees_base and make sure that the quality is something we have a profile for.
  100. if parser["metadata"].get("type", "") == "quality_changes":
  101. for possible_printer in self._quality_changes_to_two_trees_base:
  102. if os.path.basename(filename).startswith(possible_printer + "_"):
  103. parser["general"]["definition"] = "two_trees_base"
  104. parser["metadata"]["quality_type"] = self._two_trees_bluer_quality_type_conversion.get(parser["metadata"]["quality_type"], "standard")
  105. break
  106. result = io.StringIO()
  107. parser.write(result)
  108. return [filename], [result.getvalue()]
  109. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  110. """Upgrades stacks to have the new version number."""
  111. parser = configparser.ConfigParser(interpolation = None)
  112. parser.read_string(serialized)
  113. # Update setting version number.
  114. parser["metadata"]["setting_version"] = "17"
  115. # Change renamed profiles.
  116. if "containers" in parser:
  117. # For legacy TwoTrees printers, change the variant to 0.4.
  118. definition_id = parser["containers"]["7"]
  119. material_id = parser["containers"]["4"]
  120. old_quality_id = parser["containers"]["3"]
  121. if parser["metadata"].get("type", "machine") == "extruder_train":
  122. if parser["containers"]["5"] == "empty_variant":
  123. if definition_id in self._default_variants:
  124. parser["containers"]["5"] = self._default_variants[definition_id]
  125. # Also change the quality to go along with it.
  126. if material_id in self._two_trees_quality_per_material and old_quality_id in self._two_trees_quality_per_material[material_id]:
  127. parser["containers"]["3"] = self._two_trees_quality_per_material[material_id][old_quality_id]
  128. stack_copy = {} # type: Dict[str, str] # Make a copy so that we don't modify the dict we're iterating over.
  129. stack_copy.update(parser["containers"])
  130. for position, profile_id in stack_copy.items():
  131. if profile_id in self._renamed_profiles:
  132. parser["containers"][position] = self._renamed_profiles[profile_id]
  133. result = io.StringIO()
  134. parser.write(result)
  135. return [filename], [result.getvalue()]