VersionUpgrade41to42.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. _renamed_settings = {
  9. "support_minimal_diameter": "support_tower_maximum_supported_diameter"
  10. } # type: Dict[str, str]
  11. _removed_settings = ["prime_tower_circular", "max_feedrate_z_override"] # type: List[str]
  12. _renamed_profiles = {
  13. # Include CreawsomeMod profiles here as well for the people who installed that.
  14. # Definitions.
  15. "creawsome_base": "creality_base",
  16. "creawsome_cr10": "creality_cr10",
  17. "creawsome_cr10mini": "creality_cr10mini",
  18. "creawsome_cr10s": "creality_cr10s",
  19. "creawsome_cr10s4": "creality_cr10s4",
  20. "creawsome_cr10s5": "creality_cr10s5",
  21. "creawsome_cr10spro": "creality_cr10spro",
  22. "creawsome_cr20": "creality_cr20",
  23. "creawsome_cr20pro": "creality_cr20pro",
  24. "creawsome_ender2": "creality_ender2",
  25. "creawsome_ender3": "creality_ender3",
  26. "creawsome_ender4": "creality_ender4",
  27. "creawsome_ender5": "creality_ender5",
  28. # Extruder definitions.
  29. "creawsome_base_extruder_0": "creality_base_extruder_0",
  30. # Variants.
  31. "creawsome_base_0.2": "creality_base_0.2",
  32. "creawsome_base_0.3": "creality_base_0.3",
  33. "creawsome_base_0.4": "creality_base_0.4",
  34. "creawsome_base_0.5": "creality_base_0.5",
  35. "creawsome_base_0.6": "creality_base_0.6",
  36. "creawsome_base_0.8": "creality_base_0.8",
  37. "creawsome_base_1.0": "creality_base_1.0",
  38. "creawsome_cr10_0.2": "creality_cr10_0.2",
  39. "creawsome_cr10_0.3": "creality_cr10_0.3",
  40. "creawsome_cr10_0.4": "creality_cr10_0.4",
  41. "creawsome_cr10_0.5": "creality_cr10_0.5",
  42. "creawsome_cr10_0.6": "creality_cr10_0.6",
  43. "creawsome_cr10_0.8": "creality_cr10_0.8",
  44. "creawsome_cr10_1.0": "creality_cr10_1.0",
  45. "creawsome_cr10mini_0.2": "creality_cr10mini_0.2",
  46. "creawsome_cr10mini_0.3": "creality_cr10mini_0.3",
  47. "creawsome_cr10mini_0.4": "creality_cr10mini_0.4",
  48. "creawsome_cr10mini_0.5": "creality_cr10mini_0.5",
  49. "creawsome_cr10mini_0.6": "creality_cr10mini_0.6",
  50. "creawsome_cr10mini_0.8": "creality_cr10mini_0.8",
  51. "creawsome_cr10mini_1.0": "creality_cr10mini_1.0",
  52. "creawsome_cr10s4_0.2": "creality_cr10s4_0.2",
  53. "creawsome_cr10s4_0.3": "creality_cr10s4_0.3",
  54. "creawsome_cr10s4_0.4": "creality_cr10s4_0.4",
  55. "creawsome_cr10s4_0.5": "creality_cr10s4_0.5",
  56. "creawsome_cr10s4_0.6": "creality_cr10s4_0.6",
  57. "creawsome_cr10s4_0.8": "creality_cr10s4_0.8",
  58. "creawsome_cr10s4_1.0": "creality_cr10s4_1.0",
  59. "creawsome_cr10s5_0.2": "creality_cr10s5_0.2",
  60. "creawsome_cr10s5_0.3": "creality_cr10s5_0.3",
  61. "creawsome_cr10s5_0.4": "creality_cr10s5_0.4",
  62. "creawsome_cr10s5_0.5": "creality_cr10s5_0.5",
  63. "creawsome_cr10s5_0.6": "creality_cr10s5_0.6",
  64. "creawsome_cr10s5_0.8": "creality_cr10s5_0.8",
  65. "creawsome_cr10s5_1.0": "creality_cr10s5_1.0",
  66. "creawsome_cr10s_0.2": "creality_cr10s_0.2",
  67. "creawsome_cr10s_0.3": "creality_cr10s_0.3",
  68. "creawsome_cr10s_0.4": "creality_cr10s_0.4",
  69. "creawsome_cr10s_0.5": "creality_cr10s_0.5",
  70. "creawsome_cr10s_0.6": "creality_cr10s_0.6",
  71. "creawsome_cr10s_0.8": "creality_cr10s_0.8",
  72. "creawsome_cr10s_1.0": "creality_cr10s_1.0",
  73. "creawsome_cr10spro_0.2": "creality_cr10spro_0.2",
  74. "creawsome_cr10spro_0.3": "creality_cr10spro_0.3",
  75. "creawsome_cr10spro_0.4": "creality_cr10spro_0.4",
  76. "creawsome_cr10spro_0.5": "creality_cr10spro_0.5",
  77. "creawsome_cr10spro_0.6": "creality_cr10spro_0.6",
  78. "creawsome_cr10spro_0.8": "creality_cr10spro_0.8",
  79. "creawsome_cr10spro_1.0": "creality_cr10spro_1.0",
  80. "creawsome_cr20_0.2": "creality_cr20_0.2",
  81. "creawsome_cr20_0.3": "creality_cr20_0.3",
  82. "creawsome_cr20_0.4": "creality_cr20_0.4",
  83. "creawsome_cr20_0.5": "creality_cr20_0.5",
  84. "creawsome_cr20_0.6": "creality_cr20_0.6",
  85. "creawsome_cr20_0.8": "creality_cr20_0.8",
  86. "creawsome_cr20_1.0": "creality_cr20_1.0",
  87. "creawsome_cr20pro_0.2": "creality_cr20pro_0.2",
  88. "creawsome_cr20pro_0.3": "creality_cr20pro_0.3",
  89. "creawsome_cr20pro_0.4": "creality_cr20pro_0.4",
  90. "creawsome_cr20pro_0.5": "creality_cr20pro_0.5",
  91. "creawsome_cr20pro_0.6": "creality_cr20pro_0.6",
  92. "creawsome_cr20pro_0.8": "creality_cr20pro_0.8",
  93. "creawsome_cr20pro_1.0": "creality_cr20pro_1.0",
  94. "creawsome_ender2_0.2": "creality_ender2_0.2",
  95. "creawsome_ender2_0.3": "creality_ender2_0.3",
  96. "creawsome_ender2_0.4": "creality_ender2_0.4",
  97. "creawsome_ender2_0.5": "creality_ender2_0.5",
  98. "creawsome_ender2_0.6": "creality_ender2_0.6",
  99. "creawsome_ender2_0.8": "creality_ender2_0.8",
  100. "creawsome_ender2_1.0": "creality_ender2_1.0",
  101. "creawsome_ender3_0.2": "creality_ender3_0.2",
  102. "creawsome_ender3_0.3": "creality_ender3_0.3",
  103. "creawsome_ender3_0.4": "creality_ender3_0.4",
  104. "creawsome_ender3_0.5": "creality_ender3_0.5",
  105. "creawsome_ender3_0.6": "creality_ender3_0.6",
  106. "creawsome_ender3_0.8": "creality_ender3_0.8",
  107. "creawsome_ender3_1.0": "creality_ender3_1.0",
  108. "creawsome_ender4_0.2": "creality_ender4_0.2",
  109. "creawsome_ender4_0.3": "creality_ender4_0.3",
  110. "creawsome_ender4_0.4": "creality_ender4_0.4",
  111. "creawsome_ender4_0.5": "creality_ender4_0.5",
  112. "creawsome_ender4_0.6": "creality_ender4_0.6",
  113. "creawsome_ender4_0.8": "creality_ender4_0.8",
  114. "creawsome_ender4_1.0": "creality_ender4_1.0",
  115. "creawsome_ender5_0.2": "creality_ender5_0.2",
  116. "creawsome_ender5_0.3": "creality_ender5_0.3",
  117. "creawsome_ender5_0.4": "creality_ender5_0.4",
  118. "creawsome_ender5_0.5": "creality_ender5_0.5",
  119. "creawsome_ender5_0.6": "creality_ender5_0.6",
  120. "creawsome_ender5_0.8": "creality_ender5_0.8",
  121. "creawsome_ender5_1.0": "creality_ender5_1.0",
  122. # Upgrade for people who had the original Creality profiles from 4.1 and earlier.
  123. "creality_cr10_extruder_0": "creality_base_extruder_0",
  124. "creality_cr10s4_extruder_0": "creality_base_extruder_0",
  125. "creality_cr10s5_extruder_0": "creality_base_extruder_0",
  126. "creality_ender3_extruder_0": "creality_base_extruder_0"
  127. }
  128. # For legacy Creality printers, select the correct quality profile depending on the material.
  129. _creality_quality_per_material = {
  130. # Since legacy Creality printers didn't have different variants, we always pick the 0.4mm variant.
  131. "generic_abs_175": {
  132. "high": "base_0.4_ABS_super",
  133. "normal": "base_0.4_ABS_super",
  134. "fast": "base_0.4_ABS_super",
  135. "draft": "base_0.4_ABS_standard",
  136. "extra_fast": "base_0.4_ABS_low",
  137. "coarse": "base_0.4_ABS_low",
  138. "extra_coarse": "base_0.4_ABS_low"
  139. },
  140. "generic_petg_175": {
  141. "high": "base_0.4_PETG_super",
  142. "normal": "base_0.4_PETG_super",
  143. "fast": "base_0.4_PETG_super",
  144. "draft": "base_0.4_PETG_standard",
  145. "extra_fast": "base_0.4_PETG_low",
  146. "coarse": "base_0.4_PETG_low",
  147. "extra_coarse": "base_0.4_PETG_low"
  148. },
  149. "generic_pla_175": {
  150. "high": "base_0.4_PLA_super",
  151. "normal": "base_0.4_PLA_super",
  152. "fast": "base_0.4_PLA_super",
  153. "draft": "base_0.4_PLA_standard",
  154. "extra_fast": "base_0.4_PLA_low",
  155. "coarse": "base_0.4_PLA_low",
  156. "extra_coarse": "base_0.4_PLA_low"
  157. },
  158. "generic_tpu_175": {
  159. "high": "base_0.4_TPU_super",
  160. "normal": "base_0.4_TPU_super",
  161. "fast": "base_0.4_TPU_super",
  162. "draft": "base_0.4_TPU_standard",
  163. "extra_fast": "base_0.4_TPU_standard",
  164. "coarse": "base_0.4_TPU_standard",
  165. "extra_coarse": "base_0.4_TPU_standard"
  166. },
  167. "empty_material": { # For the global stack.
  168. "high": "base_global_super",
  169. "normal": "base_global_super",
  170. "fast": "base_global_super",
  171. "draft": "base_global_standard",
  172. "extra_fast": "base_global_low",
  173. "coarse": "base_global_low",
  174. "extra_coarse": "base_global_low"
  175. }
  176. }
  177. # Default variant to select for legacy Creality printers, now that we have variants.
  178. _default_variants = {
  179. "creality_cr10_extruder_0": "creality_cr10_0.4",
  180. "creality_cr10s4_extruder_0": "creality_cr10s4_0.4",
  181. "creality_cr10s5_extruder_0": "creality_cr10s5_0.4",
  182. "creality_ender3_extruder_0": "creality_ender3_0.4"
  183. }
  184. # Whether the quality changes profile belongs to one of the upgraded printers can only be recognised by how they start.
  185. # If they are, they must use the creality base definition so that they still belong to those printers.
  186. _quality_changes_to_creality_base = {
  187. "creality_cr10_extruder_0",
  188. "creality_cr10s4_extruder_0",
  189. "creality_cr10s5_extruder_0",
  190. "creality_ender3_extruder_0",
  191. "creality_cr10",
  192. "creality_cr10s",
  193. "creality_cr10s4",
  194. "creality_cr10s5",
  195. "creality_ender3",
  196. }
  197. _creality_limited_quality_type = {
  198. "high": "super",
  199. "normal": "super",
  200. "fast": "super",
  201. "draft": "draft",
  202. "extra_fast": "draft",
  203. "coarse": "draft",
  204. "extra_coarse": "draft"
  205. }
  206. class VersionUpgrade41to42(VersionUpgrade):
  207. """Upgrades configurations from the state they were in at version 4.1 to the
  208. state they should be in at version 4.2.
  209. """
  210. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  211. """Upgrades instance containers to have the new version number.
  212. This renames the renamed settings in the containers.
  213. """
  214. parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
  215. parser.read_string(serialized)
  216. # Update version number.
  217. parser["metadata"]["setting_version"] = "8"
  218. # Certain instance containers (such as definition changes) reference to a certain definition container
  219. # Since a number of those changed name, we also need to update those.
  220. old_definition = parser["general"]["definition"]
  221. if old_definition in _renamed_profiles:
  222. parser["general"]["definition"] = _renamed_profiles[old_definition]
  223. # Rename settings.
  224. if "values" in parser:
  225. for old_name, new_name in _renamed_settings.items():
  226. if old_name in parser["values"]:
  227. parser["values"][new_name] = parser["values"][old_name]
  228. del parser["values"][old_name]
  229. # Remove settings.
  230. for key in _removed_settings:
  231. if key in parser["values"]:
  232. del parser["values"][key]
  233. # For quality-changes profiles made for Creality printers, change the definition to the creality_base and make sure that the quality is something we have a profile for.
  234. if parser["metadata"].get("type", "") == "quality_changes":
  235. for possible_printer in _quality_changes_to_creality_base:
  236. if os.path.basename(filename).startswith(possible_printer + "_"):
  237. parser["general"]["definition"] = "creality_base"
  238. parser["metadata"]["quality_type"] = _creality_limited_quality_type.get(parser["metadata"]["quality_type"], "draft")
  239. break
  240. result = io.StringIO()
  241. parser.write(result)
  242. return [filename], [result.getvalue()]
  243. def upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  244. """Upgrades Preferences to have the new version number.
  245. This renames the renamed settings in the list of visible settings.
  246. """
  247. parser = configparser.ConfigParser(interpolation = None)
  248. parser.read_string(serialized)
  249. # Update version number.
  250. parser["metadata"]["setting_version"] = "8"
  251. # Renamed settings.
  252. if "visible_settings" in parser["general"]:
  253. visible_settings = parser["general"]["visible_settings"]
  254. visible_setting_set = set(visible_settings.split(";"))
  255. for old_name, new_name in _renamed_settings.items():
  256. if old_name in visible_setting_set:
  257. visible_setting_set.remove(old_name)
  258. visible_setting_set.add(new_name)
  259. for removed_key in _removed_settings:
  260. if removed_key in visible_setting_set:
  261. visible_setting_set.remove(removed_key)
  262. parser["general"]["visible_settings"] = ";".join(visible_setting_set)
  263. result = io.StringIO()
  264. parser.write(result)
  265. return [filename], [result.getvalue()]
  266. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  267. """Upgrades stacks to have the new version number."""
  268. parser = configparser.ConfigParser(interpolation = None)
  269. parser.read_string(serialized)
  270. # Update version number.
  271. parser["metadata"]["setting_version"] = "8"
  272. # Change renamed profiles.
  273. if "containers" in parser:
  274. # For legacy Creality printers, change the variant to 0.4.
  275. definition_id = parser["containers"]["6"]
  276. if parser["metadata"].get("type", "machine") == "extruder_train":
  277. if parser["containers"]["4"] == "empty_variant": # Necessary for people entering from CreawsomeMod who already had a variant.
  278. if definition_id in _default_variants:
  279. parser["containers"]["4"] = _default_variants[definition_id]
  280. if definition_id == "creality_cr10_extruder_0": # We can't disambiguate between Creality CR-10 and Creality-CR10S since they share the same extruder definition. Have to go by the name.
  281. if "cr-10s" in parser["metadata"].get("machine", "Creality CR-10").lower(): # Not perfect, since the user can change this name :(
  282. parser["containers"]["4"] = "creality_cr10s_0.4"
  283. # Also change the quality to go along with it.
  284. material_id = parser["containers"]["3"]
  285. old_quality_id = parser["containers"]["2"]
  286. if material_id in _creality_quality_per_material and old_quality_id in _creality_quality_per_material[material_id]:
  287. if definition_id == "creality_cr10_extruder_0": # We can't disambiguate between Creality CR-10 and Creality-CR10S since they share the same extruder definition. Have to go by the name.
  288. if "cr-10s" in parser["metadata"].get("machine", "Creality CR-10").lower(): # Not perfect, since the user can change this name :(
  289. parser["containers"]["2"] = _creality_quality_per_material[material_id][old_quality_id]
  290. stack_copy = {} # type: Dict[str, str] # Make a copy so that we don't modify the dict we're iterating over.
  291. stack_copy.update(parser["containers"])
  292. for position, profile_id in stack_copy.items():
  293. if profile_id in _renamed_profiles:
  294. parser["containers"][position] = _renamed_profiles[profile_id]
  295. result = io.StringIO()
  296. parser.write(result)
  297. return [filename], [result.getvalue()]