VersionUpgrade21to22.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser #To get version numbers from config files.
  4. from UM.VersionUpgrade import VersionUpgrade # Superclass of the plugin.
  5. from . import MachineInstance # To upgrade machine instances.
  6. from . import Preferences #To upgrade preferences.
  7. from . import Profile # To upgrade profiles.
  8. ## Which machines have material-specific profiles in the new version?
  9. #
  10. # These are the 2.1 machine identities with "has_machine_materials": true in
  11. # their definitions in Cura 2.2. So these are the machines for which profiles
  12. # need to split into multiple profiles, one for each material and variant.
  13. #
  14. # Each machine has the materials and variants listed in which it needs to
  15. # split, since those might be different per machine.
  16. #
  17. # This should contain the definition as they are stated in the profiles. The
  18. # inheritance structure cannot be found at this stage, since the definitions
  19. # may have changed in later versions than 2.2.
  20. _machines_with_machine_quality = {
  21. "ultimaker2plus": {
  22. "materials": { "generic_abs", "generic_cpe", "generic_pla", "generic_pva", "generic_cpe_plus", "generic_nylon", "generic_pc", "generic_tpu" },
  23. "variants": { "0.25 mm", "0.4 mm", "0.6 mm", "0.8 mm" }
  24. },
  25. "ultimaker2_extended_plus": {
  26. "materials": { "generic_abs", "generic_cpe", "generic_pla", "generic_pva", "generic_cpe_plus", "generic_nylon", "generic_pc", "generic_tpu" },
  27. "variants": { "0.25 mm", "0.4 mm", "0.6 mm", "0.8 mm" }
  28. }
  29. }
  30. ## How to translate material names from the old version to the new.
  31. _material_translations = {
  32. "PLA": "generic_pla",
  33. "ABS": "generic_abs",
  34. "CPE": "generic_cpe",
  35. "CPE+": "generic_cpe_plus",
  36. "Nylon": "generic_nylon",
  37. "PC": "generic_pc",
  38. "TPU": "generic_tpu",
  39. }
  40. ## How to translate material names for in the profile names.
  41. _material_translations_profiles = {
  42. "PLA": "pla",
  43. "ABS": "abs",
  44. "CPE": "cpe",
  45. "CPE+": "cpep",
  46. "Nylon": "nylon",
  47. "PC": "pc",
  48. "TPU": "tpu",
  49. }
  50. ## How to translate printer names from the old version to the new.
  51. _printer_translations = {
  52. "ultimaker2plus": "ultimaker2_plus"
  53. }
  54. _printer_translations_profiles = {
  55. "ultimaker2plus": "um2p", #Does NOT get included in PLA profiles!
  56. "ultimaker2_extended_plus": "um2ep" #Has no profiles for CPE+, Nylon, PC and TPU!
  57. }
  58. ## How to translate profile names from the old version to the new.
  59. #
  60. # This must have an entry for every built-in profile, since it also services
  61. # as a set for which profiles were built-in.
  62. _profile_translations = {
  63. "Low Quality": "low",
  64. "Normal Quality": "normal",
  65. "High Quality": "high",
  66. "Ulti Quality": "high", #This one doesn't have an equivalent. Map it to high.
  67. "abs_0.25_normal": "um2p_abs_0.25_normal",
  68. "abs_0.4_fast": "um2p_abs_0.4_fast",
  69. "abs_0.4_high": "um2p_abs_0.4_high",
  70. "abs_0.4_normal": "um2p_abs_0.4_normal",
  71. "abs_0.6_normal": "um2p_abs_0.6_normal",
  72. "abs_0.8_normal": "um2p_abs_0.8_normal",
  73. "cpe_0.25_normal": "um2p_cpe_0.25_normal",
  74. "cpe_0.4_fast": "um2p_cpe_0.4_fast",
  75. "cpe_0.4_high": "um2p_cpe_0.4_high",
  76. "cpe_0.4_normal": "um2p_cpe_0.4_normal",
  77. "cpe_0.6_normal": "um2p_cpe_0.6_normal",
  78. "cpe_0.8_normal": "um2p_cpe_0.8_normal",
  79. "cpep_0.4_draft": "um2p_cpep_0.4_draft",
  80. "cpep_0.4_normal": "um2p_cpep_0.4_normal",
  81. "cpep_0.6_draft": "um2p_cpep_0.6_draft",
  82. "cpep_0.6_normal": "um2p_cpep_0.6_normal",
  83. "cpep_0.8_draft": "um2p_cpep_0.8_draft",
  84. "cpep_0.8_normal": "um2p_cpep_0.8_normal",
  85. "nylon_0.25_high": "um2p_nylon_0.25_high",
  86. "nylon_0.25_normal": "um2p_nylon_0.25_normal",
  87. "nylon_0.4_fast": "um2p_nylon_0.4_fast",
  88. "nylon_0.4_normal": "um2p_nylon_0.4_normal",
  89. "nylon_0.6_fast": "um2p_nylon_0.6_fast",
  90. "nylon_0.6_normal": "um2p_nylon_0.6_normal",
  91. "nylon_0.8_draft": "um2p_nylon_0.8_draft",
  92. "nylon_0.8_normal": "um2p_nylon_0.8_normal",
  93. "pc_0.25_high": "um2p_pc_0.25_high",
  94. "pc_0.25_normal": "um2p_pc_0.25_normal",
  95. "pc_0.4_fast": "um2p_pc_0.4_fast",
  96. "pc_0.4_normal": "um2p_pc_0.4_normal",
  97. "pc_0.6_fast": "um2p_pc_0.6_fast",
  98. "pc_0.6_normal": "um2p_pc_0.6_normal",
  99. "pc_0.8_draft": "um2p_pc_0.8_draft",
  100. "pc_0.8_normal": "um2p_pc_0.8_normal",
  101. "pla_0.25_normal": "pla_0.25_normal", #Note that the PLA profiles don't get the um2p_ prefix, though they are for UM2+.
  102. "pla_0.4_fast": "pla_0.4_fast",
  103. "pla_0.4_high": "pla_0.4_high",
  104. "pla_0.4_normal": "pla_0.4_normal",
  105. "pla_0.6_normal": "pla_0.6_normal",
  106. "pla_0.8_normal": "pla_0.8_normal",
  107. "tpu_0.25_high": "um2p_tpu_0.25_high",
  108. "tpu_0.4_normal": "um2p_tpu_0.4_normal",
  109. "tpu_0.6_fast": "um2p_tpu_0.6_fast"
  110. }
  111. ## Settings that are no longer in the new version.
  112. _removed_settings = {
  113. "fill_perimeter_gaps",
  114. "support_area_smoothing"
  115. }
  116. ## How to translate setting names from the old version to the new.
  117. _setting_name_translations = {
  118. "remove_overlapping_walls_0_enabled": "travel_compensate_overlapping_walls_0_enabled",
  119. "remove_overlapping_walls_enabled": "travel_compensate_overlapping_walls_enabled",
  120. "remove_overlapping_walls_x_enabled": "travel_compensate_overlapping_walls_x_enabled",
  121. "retraction_hop": "retraction_hop_enabled",
  122. "skin_overlap": "infill_overlap",
  123. "skirt_line_width": "skirt_brim_line_width",
  124. "skirt_minimal_length": "skirt_brim_minimal_length",
  125. "skirt_speed": "skirt_brim_speed",
  126. "speed_support_lines": "speed_support_infill",
  127. "speed_support_roof": "speed_support_interface",
  128. "support_roof_density": "support_interface_density",
  129. "support_roof_enable": "support_interface_enable",
  130. "support_roof_extruder_nr": "support_interface_extruder_nr",
  131. "support_roof_line_distance": "support_interface_line_distance",
  132. "support_roof_line_width": "support_interface_line_width",
  133. "support_roof_pattern": "support_interface_pattern"
  134. }
  135. ## Custom profiles become quality_changes. This dictates which quality to base
  136. # the quality_changes profile on.
  137. #
  138. # Which quality profile to base the quality_changes on depends on the machine,
  139. # material and nozzle.
  140. #
  141. # If a current configuration is missing, fall back to "normal".
  142. _quality_fallbacks = {
  143. "ultimaker2_plus": {
  144. "ultimaker2_plus_0.25": {
  145. "generic_abs": "um2p_abs_0.25_normal",
  146. "generic_cpe": "um2p_cpe_0.25_normal",
  147. #No CPE+.
  148. "generic_nylon": "um2p_nylon_0.25_normal",
  149. "generic_pc": "um2p_pc_0.25_normal",
  150. "generic_pla": "pla_0.25_normal",
  151. "generic_tpu": "um2p_tpu_0.25_high"
  152. },
  153. "ultimaker2_plus_0.4": {
  154. "generic_abs": "um2p_abs_0.4_normal",
  155. "generic_cpe": "um2p_cpe_0.4_normal",
  156. "generic_cpep": "um2p_cpep_0.4_normal",
  157. "generic_nylon": "um2p_nylon_0.4_normal",
  158. "generic_pc": "um2p_pc_0.4_normal",
  159. "generic_pla": "pla_0.4_normal",
  160. "generic_tpu": "um2p_tpu_0.4_normal"
  161. },
  162. "ultimaker2_plus_0.6": {
  163. "generic_abs": "um2p_abs_0.6_normal",
  164. "generic_cpe": "um2p_cpe_0.6_normal",
  165. "generic_cpep": "um2p_cpep_0.6_normal",
  166. "generic_nylon": "um2p_nylon_0.6_normal",
  167. "generic_pc": "um2p_pc_0.6_normal",
  168. "generic_pla": "pla_0.6_normal",
  169. "generic_tpu": "um2p_tpu_0.6_fast",
  170. },
  171. "ultimaker2_plus_0.8": {
  172. "generic_abs": "um2p_abs_0.8_normal",
  173. "generic_cpe": "um2p_cpe_0.8_normal",
  174. "generic_cpep": "um2p_cpep_0.8_normal",
  175. "generic_nylon": "um2p_nylon_0.8_normal",
  176. "generic_pc": "um2p_pc_0.8_normal",
  177. "generic_pla": "pla_0.8_normal",
  178. #No TPU.
  179. }
  180. }
  181. }
  182. ## How to translate variants of specific machines from the old version to the
  183. # new.
  184. _variant_translations = {
  185. "ultimaker2_plus": {
  186. "0.25 mm": "ultimaker2_plus_0.25",
  187. "0.4 mm": "ultimaker2_plus_0.4",
  188. "0.6 mm": "ultimaker2_plus_0.6",
  189. "0.8 mm": "ultimaker2_plus_0.8"
  190. },
  191. "ultimaker2_extended_plus": {
  192. "0.25 mm": "ultimaker2_extended_plus_0.25",
  193. "0.4 mm": "ultimaker2_extended_plus_0.4",
  194. "0.6 mm": "ultimaker2_extended_plus_0.6",
  195. "0.8 mm": "ultimaker2_extended_plus_0.8"
  196. }
  197. }
  198. ## How to translate variant names for in the profile names.
  199. _variant_translations_profiles = {
  200. "0.25 mm": "0.25",
  201. "0.4 mm": "0.4",
  202. "0.6 mm": "0.6",
  203. "0.8 mm": "0.8"
  204. }
  205. ## Cura 2.2's material profiles use a different naming scheme for variants.
  206. #
  207. # Getting pretty stressed out by this sort of thing...
  208. _variant_translations_materials = {
  209. "ultimaker2_plus": {
  210. "0.25 mm": "ultimaker2_plus_0.25_mm",
  211. "0.4 mm": "ultimaker2_plus_0.4_mm",
  212. "0.6 mm": "ultimaker2_plus_0.6_mm",
  213. "0.8 mm": "ultimaker2_plus_0.8_mm"
  214. },
  215. "ultimaker2_extended_plus": {
  216. "0.25 mm": "ultimaker2_plus_0.25_mm",
  217. "0.4 mm": "ultimaker2_plus_0.4_mm",
  218. "0.6 mm": "ultimaker2_plus_0.6_mm",
  219. "0.8 mm": "ultimaker2_plus_0.8_mm"
  220. }
  221. }
  222. ## Converts configuration from Cura 2.1's file formats to Cura 2.2's.
  223. #
  224. # It converts the machine instances and profiles.
  225. class VersionUpgrade21to22(VersionUpgrade):
  226. ## Gets the version number from a config file.
  227. #
  228. # In all config files that concern this version upgrade, the version
  229. # number is stored in general/version, so get the data from that key.
  230. #
  231. # \param serialised The contents of a config file.
  232. # \return \type{int} The version number of that config file.
  233. def getCfgVersion(self, serialised):
  234. parser = configparser.ConfigParser(interpolation = None)
  235. parser.read_string(serialised)
  236. format_version = int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
  237. setting_version = int(parser.get("metadata", "setting_version", fallback = 0))
  238. return format_version * 1000000 + setting_version
  239. ## Gets the fallback quality to use for a specific machine-variant-material
  240. # combination.
  241. #
  242. # For custom profiles we fall back onto this quality profile, since we
  243. # don't know which quality profile it was based on.
  244. #
  245. # \param machine The machine ID of the user's configuration in 2.2.
  246. # \param variant The variant ID of the user's configuration in 2.2.
  247. # \param material The material ID of the user's configuration in 2.2.
  248. @staticmethod
  249. def getQualityFallback(machine, variant, material):
  250. if machine not in _quality_fallbacks:
  251. return "normal"
  252. if variant not in _quality_fallbacks[machine]:
  253. return "normal"
  254. if material not in _quality_fallbacks[machine][variant]:
  255. return "normal"
  256. return _quality_fallbacks[machine][variant][material]
  257. ## Gets the set of built-in profile names in Cura 2.1.
  258. #
  259. # This is required to test if profiles should be converted to a quality
  260. # profile or a quality-changes profile.
  261. @staticmethod
  262. def builtInProfiles():
  263. return _profile_translations.keys()
  264. ## Gets a set of the machines which now have per-material quality profiles.
  265. #
  266. # \return A set of machine identifiers.
  267. @staticmethod
  268. def machinesWithMachineQuality():
  269. return _machines_with_machine_quality
  270. ## Converts machine instances from format version 1 to version 2.
  271. #
  272. # \param serialised The serialised machine instance in version 1.
  273. # \param filename The supposed file name of the machine instance, without
  274. # extension.
  275. # \return A tuple containing the new filename and the serialised machine
  276. # instance in version 2, or None if the input was not of the correct
  277. # format.
  278. def upgradeMachineInstance(self, serialised, filename):
  279. machine_instance = MachineInstance.importFrom(serialised, filename)
  280. if not machine_instance: #Invalid file format.
  281. return filename, None
  282. return machine_instance.export()
  283. ## Converts preferences from format version 2 to version 3.
  284. #
  285. # \param serialised The serialised preferences file in version 2.
  286. # \param filename The supposed file name of the preferences file, without
  287. # extension.
  288. # \return A tuple containing the new filename and the serialised
  289. # preferences in version 3, or None if the input was not of the correct
  290. # format.
  291. def upgradePreferences(self, serialised, filename):
  292. preferences = Preferences.importFrom(serialised, filename)
  293. if not preferences: #Invalid file format.
  294. return filename, None
  295. return preferences.export()
  296. ## Converts profiles from format version 1 to version 2.
  297. #
  298. # \param serialised The serialised profile in version 1.
  299. # \param filename The supposed file name of the profile, without
  300. # extension.
  301. # \return A tuple containing the new filename and the serialised profile
  302. # in version 2, or None if the input was not of the correct format.
  303. def upgradeProfile(self, serialised, filename):
  304. profile = Profile.importFrom(serialised, filename)
  305. if not profile: # Invalid file format.
  306. return filename, None
  307. return profile.export()
  308. ## Translates a material name for the change from Cura 2.1 to 2.2.
  309. #
  310. # \param material A material name in Cura 2.1.
  311. # \return The name of the corresponding material in Cura 2.2.
  312. @staticmethod
  313. def translateMaterial(material):
  314. if material in _material_translations:
  315. return _material_translations[material]
  316. return material
  317. ## Translates a material name for the change from Cura 2.1 to 2.2 in
  318. # quality profile names.
  319. #
  320. # \param material A material name in Cura 2.1.
  321. # \return The name of the corresponding material in the quality profiles
  322. # in Cura 2.2.
  323. @staticmethod
  324. def translateMaterialForProfiles(material):
  325. if material in _material_translations_profiles:
  326. return _material_translations_profiles[material]
  327. return material
  328. ## Translates a printer name that might have changed since the last
  329. # version.
  330. #
  331. # \param printer A printer name in Cura 2.1.
  332. # \return The name of the corresponding printer in Cura 2.2.
  333. @staticmethod
  334. def translatePrinter(printer):
  335. if printer in _printer_translations:
  336. return _printer_translations[printer]
  337. return printer #Doesn't need to be translated.
  338. ## Translates a printer name for the change from Cura 2.1 to 2.2 in quality
  339. # profile names.
  340. #
  341. # \param printer A printer name in 2.1.
  342. # \return The name of the corresponding printer in Cura 2.2.
  343. @staticmethod
  344. def translatePrinterForProfile(printer):
  345. if printer in _printer_translations_profiles:
  346. return _printer_translations_profiles[printer]
  347. return printer
  348. ## Translates a built-in profile name that might have changed since the
  349. # last version.
  350. #
  351. # \param profile A profile name in the old version.
  352. # \return The corresponding profile name in the new version.
  353. @staticmethod
  354. def translateProfile(profile):
  355. if profile in _profile_translations:
  356. return _profile_translations[profile]
  357. return profile #Doesn't need to be translated.
  358. ## Updates settings for the change from Cura 2.1 to 2.2.
  359. #
  360. # The keys and values of settings are changed to what they should be in
  361. # the new version. Each setting is changed in-place in the provided
  362. # dictionary. This changes the input parameter.
  363. #
  364. # \param settings A dictionary of settings (as key-value pairs) to update.
  365. # \return The same dictionary.
  366. @staticmethod
  367. def translateSettings(settings):
  368. new_settings = {}
  369. for key, value in settings.items():
  370. if key in _removed_settings:
  371. continue
  372. if key == "retraction_combing": #Combing was made into an enum instead of a boolean.
  373. new_settings[key] = "off" if (value == "False") else "all"
  374. continue
  375. if key == "cool_fan_full_layer": #Layer counting was made one-indexed.
  376. new_settings[key] = str(int(value) + 1)
  377. continue
  378. if key in _setting_name_translations:
  379. new_settings[_setting_name_translations[key]] = value
  380. continue
  381. new_settings[key] = value
  382. return new_settings
  383. ## Translates a setting name for the change from Cura 2.1 to 2.2.
  384. #
  385. # \param setting The name of a setting in Cura 2.1.
  386. # \return The name of the corresponding setting in Cura 2.2.
  387. @staticmethod
  388. def translateSettingName(setting):
  389. if setting in _setting_name_translations:
  390. return _setting_name_translations[setting]
  391. return setting #Doesn't need to be translated.
  392. ## Translates a variant name for the change from Cura 2.1 to 2.2
  393. #
  394. # \param variant The name of a variant in Cura 2.1.
  395. # \param machine The name of the machine this variant is part of in Cura
  396. # 2.2's naming.
  397. # \return The name of the corresponding variant in Cura 2.2.
  398. @staticmethod
  399. def translateVariant(variant, machine):
  400. if machine in _variant_translations and variant in _variant_translations[machine]:
  401. return _variant_translations[machine][variant]
  402. return variant
  403. ## Translates a variant name for the change from Cura 2.1 to 2.2 in
  404. # material profiles.
  405. #
  406. # \param variant The name of the variant in Cura 2.1.
  407. # \param machine The name of the machine this variant is part of in Cura
  408. # 2.2's naming.
  409. # \return The name of the corresponding variant for in material profiles
  410. # in Cura 2.2.
  411. @staticmethod
  412. def translateVariantForMaterials(variant, machine):
  413. if machine in _variant_translations_materials and variant in _variant_translations_materials[machine]:
  414. return _variant_translations_materials[machine][variant]
  415. return variant
  416. ## Translates a variant name for the change from Cura 2.1 to 2.2 in quality
  417. # profiles.
  418. #
  419. # \param variant The name of the variant in Cura 2.1.
  420. # \return The name of the corresponding variant for in quality profiles in
  421. # Cura 2.2.
  422. @staticmethod
  423. def translateVariantForProfiles(variant):
  424. if variant in _variant_translations_profiles:
  425. return _variant_translations_profiles[variant]
  426. return variant