VersionUpgrade21to22.py 7.3 KB

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