VersionUpgrade21to22.py 19 KB

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