VersionUpgrade460to462.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser
  4. import copy # To split up files.
  5. from typing import Tuple, List
  6. import io
  7. from UM.VersionUpgrade import VersionUpgrade
  8. renamed_nozzles = {
  9. "deltacomb_025_e3d": "deltacomb_dc20_fbe025",
  10. "deltacomb_040_e3d": "deltacomb_dc20_fbe040",
  11. "deltacomb_080_e3d": "deltacomb_dc20_vfbe080"
  12. }
  13. default_qualities_per_nozzle_and_material = {
  14. # Can't define defaults for user-defined materials, since we only have the material ID. Those will get reset to empty quality :(
  15. "deltacomb_dc20_fbe025": {
  16. "generic_pla_175": "deltacomb_FBE0.25_PLA_C",
  17. "generic_abs_175": "deltacomb_FBE0.25_ABS_C"
  18. },
  19. "deltacomb_dc20_fbe040": {
  20. "generic_pla_175": "deltacomb_FBE0.40_PLA_C",
  21. "generic_abs_175": "deltacomb_FBE0.40_ABS_C",
  22. "generic_petg_175": "deltacomb_FBE0.40_PETG_C",
  23. "generic_tpu_175": "deltacomb_FBE0.40_TPU_C"
  24. },
  25. "deltacomb_dc20_vfbe080": {
  26. "generic_pla_175": "deltacomb_VFBE0.80_PLA_D",
  27. "generic_abs_175": "deltacomb_VFBE0.80_ABS_D"
  28. }
  29. }
  30. class VersionUpgrade460to462(VersionUpgrade):
  31. def getCfgVersion(self, serialised: str) -> int:
  32. parser = configparser.ConfigParser(interpolation = None)
  33. parser.read_string(serialised)
  34. format_version = int(parser.get("general", "version")) # Explicitly give an exception when this fails. That means that the file format is not recognised.
  35. setting_version = int(parser.get("metadata", "setting_version", fallback = "0"))
  36. return format_version * 1000000 + setting_version
  37. def upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  38. """
  39. Upgrades preferences to have the new version number.
  40. :param serialized: The original contents of the preferences file.
  41. :param filename: The file name of the preferences file.
  42. :return: A list of new file names, and a list of the new contents for
  43. those files.
  44. """
  45. parser = configparser.ConfigParser(interpolation = None)
  46. parser.read_string(serialized)
  47. # Update version number.
  48. parser["metadata"]["setting_version"] = "14"
  49. result = io.StringIO()
  50. parser.write(result)
  51. return [filename], [result.getvalue()]
  52. def upgradeExtruderInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  53. """
  54. Upgrades per-extruder instance containers to the new version number.
  55. This applies all of the changes that are applied in other instance
  56. containers as well.
  57. In the case of Deltacomb printers, it splits the 2 extruders into 4 and
  58. changes the definition.
  59. :param serialized: The original contents of the instance container.
  60. :param filename: The original file name of the instance container.
  61. :return: A list of new file names, and a list of the new contents for
  62. those files.
  63. """
  64. parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
  65. parser.read_string(serialized)
  66. results = [(parser, filename)]
  67. if "general" in parser and "definition" in parser["general"]:
  68. if parser["general"]["definition"] == "deltacomb_extruder_0":
  69. parser["general"]["definition"] = "deltacomb_base_extruder_0"
  70. elif parser["general"]["definition"] == "deltacomb_extruder_1": # Split up the second Deltacomb extruder into 3, creating an extra two extruders.
  71. parser_e2 = configparser.ConfigParser(interpolation = None)
  72. parser_e3 = configparser.ConfigParser(interpolation = None)
  73. parser_e2.read_dict(parser)
  74. parser_e3.read_dict(parser)
  75. parser["general"]["definition"] = "deltacomb_base_extruder_1"
  76. parser_e2["general"]["definition"] = "deltacomb_base_extruder_2"
  77. parser_e3["general"]["definition"] = "deltacomb_base_extruder_3"
  78. results.append((parser_e2, filename + "_e2_upgrade")) # Hopefully not already taken.
  79. results.append((parser_e3, filename + "_e3_upgrade"))
  80. elif parser["general"]["definition"] == "deltacomb": # On the global stack, the per-extruder user container OR the per-extruder quality changes container.
  81. parser["general"]["definition"] = "deltacomb_dc20"
  82. if "metadata" in parser and ("extruder" in parser["metadata"] or "position" in parser["metadata"]): # Per-extruder user container or quality changes container.
  83. parser_e2 = configparser.ConfigParser(interpolation = None)
  84. parser_e3 = configparser.ConfigParser(interpolation = None)
  85. parser_e2.read_dict(parser)
  86. parser_e3.read_dict(parser)
  87. if "extruder" in parser["metadata"]:
  88. parser_e2["metadata"]["extruder"] += "_e2_upgrade"
  89. parser_e3["metadata"]["extruder"] += "_e3_upgrade"
  90. results.append((parser_e2, filename + "_e2_upgrade"))
  91. results.append((parser_e3, filename + "_e3_upgrade"))
  92. # Now go upgrade with the generic instance container method.
  93. final_serialized = [] # type: List[str]
  94. final_filenames = [] # type: List[str]
  95. for result_parser, result_filename in results:
  96. result_ss = io.StringIO()
  97. result_parser.write(result_ss)
  98. result_serialized = result_ss.getvalue()
  99. # The upgrade function itself might also return multiple files, so we need to append all of those into the final list.
  100. this_filenames_upgraded, this_serialized_upgraded = self.upgradeInstanceContainer(result_serialized, result_filename)
  101. final_serialized += this_serialized_upgraded
  102. final_filenames += this_filenames_upgraded
  103. return final_filenames, final_serialized
  104. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  105. """
  106. Upgrades instance containers to have the new version number.
  107. This changes the maximum deviation setting if that setting was present
  108. in the profile.
  109. :param serialized: The original contents of the instance container.
  110. :param filename: The original file name of the instance container.
  111. :return: A list of new file names, and a list of the new contents for
  112. those files.
  113. """
  114. parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
  115. parser.read_string(serialized)
  116. # Update version number.
  117. parser["metadata"]["setting_version"] = "14"
  118. if "values" in parser:
  119. # Maximum Deviation's effect was corrected. Previously the deviation
  120. # ended up being only half of what the user had entered. This was
  121. # fixed in Cura 4.7 so there we need to halve the deviation that the
  122. # user had entered. This halving was accidentally merged into 4.6 and had to be reverted
  123. # back in 4.6.2.
  124. if "meshfix_maximum_deviation" in parser["values"]:
  125. maximum_deviation = parser["values"]["meshfix_maximum_deviation"]
  126. if maximum_deviation.startswith("="):
  127. maximum_deviation = maximum_deviation[1:]
  128. maximum_deviation = "=(" + maximum_deviation + ") * 2"
  129. parser["values"]["meshfix_maximum_deviation"] = maximum_deviation
  130. result = io.StringIO()
  131. parser.write(result)
  132. return [filename], [result.getvalue()]
  133. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  134. """
  135. Upgrades stacks to have the new version number.
  136. This upgrades Deltacomb printers to their new profile structure, and
  137. gives them 4 extruders.
  138. :param serialized: The original contents of the stack.
  139. :param filename: The original file name of the stack.
  140. :return: A list of new file names, and a list of the new contents for
  141. those files.
  142. """
  143. parser = configparser.ConfigParser(interpolation = None)
  144. parser.read_string(serialized)
  145. results = [(parser, filename)]
  146. # Update version number.
  147. if "metadata" not in parser:
  148. parser["metadata"] = {}
  149. parser["metadata"]["setting_version"] = "14"
  150. if "containers" in parser and "7" in parser["containers"]:
  151. if parser["containers"]["7"] == "deltacomb_extruder_0" or parser["containers"]["7"] == "deltacomb_extruder_1": # Extruder stack.
  152. if "5" in parser["containers"]:
  153. parser["containers"]["5"] = renamed_nozzles.get(parser["containers"]["5"], parser["containers"]["5"])
  154. if "3" in parser["containers"] and "4" in parser["containers"] and parser["containers"]["3"] == "empty_quality":
  155. parser["containers"]["3"] = default_qualities_per_nozzle_and_material[parser["containers"]["5"]].get(parser["containers"]["4"], "empty_quality")
  156. if parser["containers"]["7"] == "deltacomb_extruder_0":
  157. parser["containers"]["7"] = "deltacomb_base_extruder_0"
  158. else:
  159. parser["containers"]["7"] = "deltacomb_base_extruder_1"
  160. # Copy this extruder to extruder 3 and 4.
  161. extruder3 = configparser.ConfigParser(interpolation=None)
  162. extruder4 = configparser.ConfigParser(interpolation=None)
  163. extruder3.read_dict(parser)
  164. extruder4.read_dict(parser)
  165. extruder3["general"]["id"] += "_e2_upgrade"
  166. extruder3["metadata"]["position"] = "2"
  167. extruder3["containers"]["0"] += "_e2_upgrade"
  168. if extruder3["containers"]["1"] != "empty_quality_changes":
  169. extruder3["containers"]["1"] += "_e2_upgrade"
  170. extruder3["containers"]["6"] += "_e2_upgrade"
  171. extruder3["containers"]["7"] = "deltacomb_base_extruder_2"
  172. results.append((extruder3, filename + "_e2_upgrade"))
  173. extruder4["general"]["id"] += "_e3_upgrade"
  174. extruder4["metadata"]["position"] = "3"
  175. extruder4["containers"]["0"] += "_e3_upgrade"
  176. if extruder4["containers"]["1"] != "empty_quality_changes":
  177. extruder4["containers"]["1"] += "_e3_upgrade"
  178. extruder4["containers"]["6"] += "_e3_upgrade"
  179. extruder4["containers"]["7"] = "deltacomb_base_extruder_3"
  180. results.append((extruder4, filename + "_e3_upgrade"))
  181. elif parser["containers"]["7"] == "deltacomb": # Global stack.
  182. parser["containers"]["7"] = "deltacomb_dc20"
  183. parser["containers"]["3"] = "deltacomb_global_C"
  184. result_serialized = []
  185. result_filenames = []
  186. for result_parser, result_filename in results:
  187. result_ss = io.StringIO()
  188. result_parser.write(result_ss)
  189. result_serialized.append(result_ss.getvalue())
  190. result_filenames.append(result_filename)
  191. return result_filenames, result_serialized