VersionUpgrade21to22.py 8.0 KB

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