VersionUpgrade460to462.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  32. """
  33. Upgrades preferences to have the new version number.
  34. :param serialized: The original contents of the preferences file.
  35. :param filename: The file name of the preferences file.
  36. :return: A list of new file names, and a list of the new contents for
  37. those files.
  38. """
  39. parser = configparser.ConfigParser(interpolation = None)
  40. parser.read_string(serialized)
  41. # Update version number.
  42. parser["metadata"]["setting_version"] = "14"
  43. result = io.StringIO()
  44. parser.write(result)
  45. return [filename], [result.getvalue()]
  46. def upgradeExtruderInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  47. """
  48. Upgrades per-extruder instance containers to the new version number.
  49. This applies all of the changes that are applied in other instance
  50. containers as well.
  51. In the case of Deltacomb printers, it splits the 2 extruders into 4 and
  52. changes the definition.
  53. :param serialized: The original contents of the instance container.
  54. :param filename: The original file name of the instance container.
  55. :return: A list of new file names, and a list of the new contents for
  56. those files.
  57. """
  58. parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
  59. parser.read_string(serialized)
  60. results = [(parser, filename)]
  61. if "general" in parser and "definition" in parser["general"]:
  62. if parser["general"]["definition"] == "deltacomb_extruder_0":
  63. parser["general"]["definition"] = "deltacomb_base_extruder_0"
  64. elif parser["general"]["definition"] == "deltacomb_extruder_1": # Split up the second Deltacomb extruder into 3, creating an extra two extruders.
  65. parser_e2 = configparser.ConfigParser(interpolation = None)
  66. parser_e3 = configparser.ConfigParser(interpolation = None)
  67. parser_e2.read_dict(parser)
  68. parser_e3.read_dict(parser)
  69. parser["general"]["definition"] = "deltacomb_base_extruder_1"
  70. parser_e2["general"]["definition"] = "deltacomb_base_extruder_2"
  71. parser_e3["general"]["definition"] = "deltacomb_base_extruder_3"
  72. results.append((parser_e2, filename + "_e2_upgrade")) # Hopefully not already taken.
  73. results.append((parser_e3, filename + "_e3_upgrade"))
  74. elif parser["general"]["definition"] == "deltacomb": # On the global stack, the per-extruder user container OR the per-extruder quality changes container.
  75. parser["general"]["definition"] = "deltacomb_dc20"
  76. if "metadata" in parser and ("extruder" in parser["metadata"] or "position" in parser["metadata"]): # Per-extruder user container or quality changes container.
  77. parser_e2 = configparser.ConfigParser(interpolation = None)
  78. parser_e3 = configparser.ConfigParser(interpolation = None)
  79. parser_e2.read_dict(parser)
  80. parser_e3.read_dict(parser)
  81. if "extruder" in parser["metadata"]:
  82. parser_e2["metadata"]["extruder"] += "_e2_upgrade"
  83. parser_e3["metadata"]["extruder"] += "_e3_upgrade"
  84. results.append((parser_e2, filename + "_e2_upgrade"))
  85. results.append((parser_e3, filename + "_e3_upgrade"))
  86. # Now go upgrade with the generic instance container method.
  87. final_serialized = [] # type: List[str]
  88. final_filenames = [] # type: List[str]
  89. for result_parser, result_filename in results:
  90. result_ss = io.StringIO()
  91. result_parser.write(result_ss)
  92. result_serialized = result_ss.getvalue()
  93. # The upgrade function itself might also return multiple files, so we need to append all of those into the final list.
  94. this_filenames_upgraded, this_serialized_upgraded = self.upgradeInstanceContainer(result_serialized, result_filename)
  95. final_serialized += this_serialized_upgraded
  96. final_filenames += this_filenames_upgraded
  97. return final_filenames, final_serialized
  98. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  99. """
  100. Upgrades instance containers to have the new version number.
  101. This changes the maximum deviation setting if that setting was present
  102. in the profile.
  103. :param serialized: The original contents of the instance container.
  104. :param filename: The original file name of the instance container.
  105. :return: A list of new file names, and a list of the new contents for
  106. those files.
  107. """
  108. parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
  109. parser.read_string(serialized)
  110. # Update version number.
  111. parser["metadata"]["setting_version"] = "14"
  112. if "values" in parser:
  113. # Maximum Deviation's effect was corrected. Previously the deviation
  114. # ended up being only half of what the user had entered. This was
  115. # fixed in Cura 4.7 so there we need to halve the deviation that the
  116. # user had entered. This halving was accidentally merged into 4.6 and had to be reverted
  117. # back in 4.6.2.
  118. if "meshfix_maximum_deviation" in parser["values"]:
  119. maximum_deviation = parser["values"]["meshfix_maximum_deviation"]
  120. if maximum_deviation.startswith("="):
  121. maximum_deviation = maximum_deviation[1:]
  122. maximum_deviation = "=(" + maximum_deviation + ") * 2"
  123. parser["values"]["meshfix_maximum_deviation"] = maximum_deviation
  124. result = io.StringIO()
  125. parser.write(result)
  126. return [filename], [result.getvalue()]
  127. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  128. """
  129. Upgrades stacks to have the new version number.
  130. This upgrades Deltacomb printers to their new profile structure, and
  131. gives them 4 extruders.
  132. :param serialized: The original contents of the stack.
  133. :param filename: The original file name of the stack.
  134. :return: A list of new file names, and a list of the new contents for
  135. those files.
  136. """
  137. parser = configparser.ConfigParser(interpolation = None)
  138. parser.read_string(serialized)
  139. results = [(parser, filename)]
  140. # Update version number.
  141. if "metadata" not in parser:
  142. parser["metadata"] = {}
  143. parser["metadata"]["setting_version"] = "14"
  144. if "containers" in parser and "7" in parser["containers"]:
  145. if parser["containers"]["7"] == "deltacomb_extruder_0" or parser["containers"]["7"] == "deltacomb_extruder_1": # Extruder stack.
  146. if "5" in parser["containers"]:
  147. parser["containers"]["5"] = renamed_nozzles.get(parser["containers"]["5"], parser["containers"]["5"])
  148. if "3" in parser["containers"] and "4" in parser["containers"] and parser["containers"]["3"] == "empty_quality":
  149. parser["containers"]["3"] = default_qualities_per_nozzle_and_material[parser["containers"]["5"]].get(parser["containers"]["4"], "empty_quality")
  150. if parser["containers"]["7"] == "deltacomb_extruder_0":
  151. parser["containers"]["7"] = "deltacomb_base_extruder_0"
  152. else:
  153. parser["containers"]["7"] = "deltacomb_base_extruder_1"
  154. # Copy this extruder to extruder 3 and 4.
  155. extruder3 = configparser.ConfigParser(interpolation=None)
  156. extruder4 = configparser.ConfigParser(interpolation=None)
  157. extruder3.read_dict(parser)
  158. extruder4.read_dict(parser)
  159. extruder3["general"]["id"] += "_e2_upgrade"
  160. extruder3["metadata"]["position"] = "2"
  161. extruder3["containers"]["0"] += "_e2_upgrade"
  162. if extruder3["containers"]["1"] != "empty_quality_changes":
  163. extruder3["containers"]["1"] += "_e2_upgrade"
  164. extruder3["containers"]["6"] += "_e2_upgrade"
  165. extruder3["containers"]["7"] = "deltacomb_base_extruder_2"
  166. results.append((extruder3, filename + "_e2_upgrade"))
  167. extruder4["general"]["id"] += "_e3_upgrade"
  168. extruder4["metadata"]["position"] = "3"
  169. extruder4["containers"]["0"] += "_e3_upgrade"
  170. if extruder4["containers"]["1"] != "empty_quality_changes":
  171. extruder4["containers"]["1"] += "_e3_upgrade"
  172. extruder4["containers"]["6"] += "_e3_upgrade"
  173. extruder4["containers"]["7"] = "deltacomb_base_extruder_3"
  174. results.append((extruder4, filename + "_e3_upgrade"))
  175. elif parser["containers"]["7"] == "deltacomb": # Global stack.
  176. parser["containers"]["7"] = "deltacomb_dc20"
  177. parser["containers"]["3"] = "deltacomb_global_C"
  178. result_serialized = []
  179. result_filenames = []
  180. for result_parser, result_filename in results:
  181. result_ss = io.StringIO()
  182. result_parser.write(result_ss)
  183. result_serialized.append(result_ss.getvalue())
  184. result_filenames.append(result_filename)
  185. return result_filenames, result_serialized