VersionUpgrade21to22.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 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. ## How to translate printer names from the old version to the new.
  9. _printer_translations = {
  10. "ultimaker2plus": "ultimaker2_plus"
  11. }
  12. ## How to translate profile names from the old version to the new.
  13. _profile_translations = {
  14. "PLA": "generic_pla",
  15. "ABS": "generic_abs",
  16. "CPE": "generic_cpe",
  17. "Low Quality": "low",
  18. "Normal Quality": "normal",
  19. "High Quality": "high",
  20. "Ulti Quality": "high" #This one doesn't have an equivalent. Map it to high.
  21. }
  22. ## How to translate setting names from the old version to the new.
  23. _setting_name_translations = {
  24. "remove_overlapping_walls_0_enabled": "travel_compensate_overlapping_walls_0_enabled",
  25. "remove_overlapping_walls_enabled": "travel_compensate_overlapping_walls_enabled",
  26. "remove_overlapping_walls_x_enabled": "travel_compensate_overlapping_walls_x_enabled",
  27. "retraction_hop": "retraction_hop_enabled",
  28. "skirt_speed": "skirt_brim_speed",
  29. "speed_support_lines": "speed_support_infill"
  30. }
  31. ## How to translate variants of specific machines from the old version to the
  32. # new.
  33. _variant_translations = {
  34. "ultimaker2_plus": {
  35. "0.25 mm": "ultimaker2_plus_0.25",
  36. "0.4 mm": "ultimaker2_plus_0.4",
  37. "0.6 mm": "ultimaker2_plus_0.6",
  38. "0.8 mm": "ultimaker2_plus_0.8"
  39. },
  40. "ultimaker2_extended_plus": {
  41. "0.25 mm": "ultimaker2_extended_plus_0.25",
  42. "0.4 mm": "ultimaker2_extended_plus_0.4",
  43. "0.6 mm": "ultimaker2_extended_plus_0.6",
  44. "0.8 mm": "ultimaker2_extended_plus_0.8"
  45. }
  46. }
  47. ## Converts configuration from Cura 2.1's file formats to Cura 2.2's.
  48. #
  49. # It converts the machine instances and profiles.
  50. class VersionUpgrade21to22(VersionUpgrade):
  51. ## Gets the version number from a config file.
  52. #
  53. # In all config files that concern this version upgrade, the version
  54. # number is stored in general/version, so get the data from that key.
  55. #
  56. # \param serialised The contents of a config file.
  57. # \return \type{int} The version number of that config file.
  58. def getCfgVersion(self, serialised):
  59. parser = configparser.ConfigParser(interpolation = None)
  60. parser.read_string(serialised)
  61. return int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
  62. ## Converts machine instances from format version 1 to version 2.
  63. #
  64. # \param serialised The serialised machine instance in version 1.
  65. # \param filename The supposed file name of the machine instance, without
  66. # extension.
  67. # \return A tuple containing the new filename and the serialised machine
  68. # instance in version 2, or None if the input was not of the correct
  69. # format.
  70. def upgradeMachineInstance(self, serialised, filename):
  71. machine_instance = MachineInstance.importFrom(serialised, filename)
  72. if not machine_instance: #Invalid file format.
  73. return filename, None
  74. return machine_instance.export()
  75. ## Converts preferences from format version 2 to version 3.
  76. #
  77. # \param serialised The serialised preferences file in version 2.
  78. # \param filename THe supposed file name of the preferences file, without
  79. # extension.
  80. # \return A tuple containing the new filename and the serialised
  81. # preferences in version 3, or None if the input was not of the correct
  82. # format.
  83. def upgradePreferences(self, serialised, filename):
  84. preferences = Preferences.importFrom(serialised, filename)
  85. if not preferences: #Invalid file format.
  86. return filename, None
  87. return preferences.export()
  88. ## Converts profiles from format version 1 to version 2.
  89. #
  90. # \param serialised The serialised profile in version 1.
  91. # \param filename The supposed file name of the profile, without
  92. # extension.
  93. # \return A tuple containing the new filename and the serialised profile
  94. # in version 2, or None if the input was not of the correct format.
  95. def upgradeProfile(self, serialised, filename):
  96. profile = Profile.importFrom(serialised, filename)
  97. if not profile: # Invalid file format.
  98. return filename, None
  99. return profile.export()
  100. ## Translates a printer name that might have changed since the last
  101. # version.
  102. #
  103. # \param printer A printer name in Cura 2.1.
  104. # \return The name of the corresponding printer in Cura 2.2.
  105. @staticmethod
  106. def translatePrinter(printer):
  107. if printer in _printer_translations:
  108. return _printer_translations[printer]
  109. return printer #Doesn't need to be translated.
  110. ## Translates a built-in profile name that might have changed since the
  111. # last version.
  112. #
  113. # \param profile A profile name in the old version.
  114. # \return The corresponding profile name in the new version.
  115. @staticmethod
  116. def translateProfile(profile):
  117. if profile in _profile_translations:
  118. return _profile_translations[profile]
  119. return profile #Doesn't need to be translated.
  120. ## Updates settings for the change from Cura 2.1 to 2.2.
  121. #
  122. # The keys and values of settings are changed to what they should be in
  123. # the new version. Each setting is changed in-place in the provided
  124. # dictionary. This changes the input parameter.
  125. #
  126. # \param settings A dictionary of settings (as key-value pairs) to update.
  127. # \return The same dictionary.
  128. @staticmethod
  129. def translateSettings(settings):
  130. for key, value in settings.items():
  131. if key == "fill_perimeter_gaps": #Setting is removed.
  132. del settings[key]
  133. elif key == "remove_overlapping_walls_0_enabled": #Setting is functionally replaced.
  134. del settings[key]
  135. settings["travel_compensate_overlapping_walls_0_enabled"] = value
  136. elif key == "remove_overlapping_walls_enabled": #Setting is functionally replaced.
  137. del settings[key]
  138. settings["travel_compensate_overlapping_walls_enabled"] = value
  139. elif key == "remove_overlapping_walls_x_enabled": #Setting is functionally replaced.
  140. del settings[key]
  141. settings["travel_compensate_overlapping_walls_x_enabled"] = value
  142. elif key == "retraction_combing": #Combing was made into an enum instead of a boolean.
  143. settings[key] = "off" if (value == "False") else "all"
  144. elif key == "retraction_hop": #Setting key was changed.
  145. del settings[key]
  146. settings["retraction_hop_enabled"] = value
  147. elif key == "skirt_speed": #Setting key was changed.
  148. del settings[key]
  149. settings["skirt_brim_speed"] = value
  150. elif key == "speed_support_lines": #Setting key was changed.
  151. del settings[key]
  152. settings["speed_support_infill"] = value
  153. return settings
  154. ## Translates a setting name for the change from Cura 2.1 to 2.2.
  155. #
  156. # \param setting The name of a setting in Cura 2.1.
  157. # \return The name of the corresponding setting in Cura 2.2.
  158. @staticmethod
  159. def translateSettingName(setting):
  160. if setting in _setting_name_translations:
  161. return _setting_name_translations[setting]
  162. return setting #Doesn't need to be translated.
  163. ## Translates a variant name for the change from Cura 2.1 to 2.2
  164. #
  165. # \param variant The name of a variant in Cura 2.1.
  166. # \param machine The name of the machine this variant is part of in Cura
  167. # 2.2's naming.
  168. # \return The name of the corresponding variant in Cura 2.2.
  169. @staticmethod
  170. def translateVariant(variant, machine):
  171. if machine in _variant_translations and variant in _variant_translations[machine]:
  172. return _variant_translations[machine][variant]
  173. return variant