VersionUpgrade462to47.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser
  4. from typing import Tuple, List, Dict, Set
  5. import io
  6. from UM.VersionUpgrade import VersionUpgrade
  7. # Renamed definition files
  8. _RENAMED_DEFINITION_DICT = {
  9. "dagoma_discoeasy200": "dagoma_discoeasy200_bicolor",
  10. } # type: Dict[str, str]
  11. _removed_settings = {
  12. "spaghetti_infill_enabled",
  13. "spaghetti_infill_stepped",
  14. "spaghetti_max_infill_angle",
  15. "spaghetti_max_height",
  16. "spaghetti_inset",
  17. "spaghetti_flow",
  18. "spaghetti_infill_extra_volume"
  19. } # type: Set[str]
  20. class VersionUpgrade462to47(VersionUpgrade):
  21. def upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  22. """
  23. Upgrades preferences to have the new version number.
  24. :param serialized: The original contents of the preferences file.
  25. :param filename: The file name of the preferences file.
  26. :return: A list of new file names, and a list of the new contents for
  27. those files.
  28. """
  29. parser = configparser.ConfigParser(interpolation = None)
  30. parser.read_string(serialized)
  31. # Update version number.
  32. parser["metadata"]["setting_version"] = "15"
  33. # Remove deleted settings from the visible settings list.
  34. if "general" in parser and "visible_settings" in parser["general"]:
  35. parser["general"]["visible_settings"] = ";".join(
  36. set(parser["general"]["visible_settings"].split(";")).difference(_removed_settings))
  37. result = io.StringIO()
  38. parser.write(result)
  39. return [filename], [result.getvalue()]
  40. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  41. """
  42. Upgrades instance containers to have the new version number.
  43. This changes the maximum deviation setting if that setting was present
  44. in the profile.
  45. :param serialized: The original contents of the instance container.
  46. :param filename: The original file name of the instance container.
  47. :return: A list of new file names, and a list of the new contents for
  48. those files.
  49. """
  50. parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
  51. parser.read_string(serialized)
  52. # Update version number.
  53. parser["metadata"]["setting_version"] = "15"
  54. if "values" in parser:
  55. # Maximum Deviation's effect was corrected. Previously the deviation
  56. # ended up being only half of what the user had entered. This was
  57. # fixed in Cura 4.7 so there we need to halve the deviation that the
  58. # user had entered.
  59. #
  60. # This got accidentally merged in Cura 4.6.0. In 4.6.2 we removed
  61. # that. In 4.7 it's not unmerged, so there we need to revert all
  62. # that again.
  63. if "meshfix_maximum_deviation" in parser["values"]:
  64. maximum_deviation = parser["values"]["meshfix_maximum_deviation"]
  65. if maximum_deviation.startswith("="):
  66. maximum_deviation = maximum_deviation[1:]
  67. maximum_deviation = "=(" + maximum_deviation + ") / 2"
  68. parser["values"]["meshfix_maximum_deviation"] = maximum_deviation
  69. # Ironing inset is now based on the flow-compensated line width to make the setting have a more logical UX.
  70. # Adjust so that the actual print result remains the same.
  71. if "ironing_inset" in parser["values"]:
  72. ironing_inset = parser["values"]["ironing_inset"]
  73. if ironing_inset.startswith("="):
  74. ironing_inset = ironing_inset[1:]
  75. if "ironing_pattern" in parser["values"] and parser["values"]["ironing_pattern"] == "concentric":
  76. correction = " + ironing_line_spacing - skin_line_width * (1.0 + ironing_flow / 100) / 2"
  77. else: # If ironing_pattern doesn't exist, it means the default (zigzag) is selected
  78. correction = " + skin_line_width * (1.0 - ironing_flow / 100) / 2"
  79. ironing_inset = "=(" + ironing_inset + ")" + correction
  80. parser["values"]["ironing_inset"] = ironing_inset
  81. for removed in set(parser["values"].keys()).intersection(_removed_settings):
  82. del parser["values"][removed]
  83. # Check renamed definitions
  84. if "definition" in parser["general"] and parser["general"]["definition"] in _RENAMED_DEFINITION_DICT:
  85. parser["general"]["definition"] = _RENAMED_DEFINITION_DICT[parser["general"]["definition"]]
  86. result = io.StringIO()
  87. parser.write(result)
  88. return [filename], [result.getvalue()]
  89. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  90. """
  91. Upgrades stacks to have the new version number.
  92. :param serialized: The original contents of the stack.
  93. :param filename: The original file name of the stack.
  94. :return: A list of new file names, and a list of the new contents for
  95. those files.
  96. """
  97. parser = configparser.ConfigParser(interpolation = None)
  98. parser.read_string(serialized)
  99. # Update version number.
  100. if "metadata" not in parser:
  101. parser["metadata"] = {}
  102. parser["metadata"]["setting_version"] = "15"
  103. # Update Pause at Height script parameters if present.
  104. if "post_processing_scripts" in parser["metadata"]:
  105. new_scripts_entries = []
  106. for script_str in parser["metadata"]["post_processing_scripts"].split("\n"):
  107. if not script_str:
  108. continue
  109. script_str = script_str.replace(r"\\\n", "\n").replace(r"\\\\", "\\\\") # Unescape escape sequences.
  110. script_parser = configparser.ConfigParser(interpolation=None)
  111. script_parser.optionxform = str # type: ignore # Don't transform the setting keys as they are case-sensitive.
  112. script_parser.read_string(script_str)
  113. # Unify all Pause at Height
  114. script_id = script_parser.sections()[0]
  115. if script_id in ["BQ_PauseAtHeight", "PauseAtHeightRepRapFirmwareDuet", "PauseAtHeightforRepetier"]:
  116. script_settings = script_parser.items(script_id)
  117. script_settings.append(("pause_method", {
  118. "BQ_PauseAtHeight": "bq",
  119. "PauseAtHeightforRepetier": "repetier",
  120. "PauseAtHeightRepRapFirmwareDuet": "reprap"
  121. }[script_id]))
  122. # Since we cannot rename a section, we remove the original section and create a new section with the new script id.
  123. script_parser.remove_section(script_id)
  124. script_id = "PauseAtHeight"
  125. script_parser.add_section(script_id)
  126. for setting_tuple in script_settings:
  127. script_parser.set(script_id, setting_tuple[0], setting_tuple[1])
  128. # Update redo_layers to redo_layer
  129. if "PauseAtHeight" in script_parser:
  130. if "redo_layers" in script_parser["PauseAtHeight"]:
  131. script_parser["PauseAtHeight"]["redo_layer"] = str(int(script_parser["PauseAtHeight"]["redo_layers"]) > 0)
  132. del script_parser["PauseAtHeight"]["redo_layers"] # Has been renamed to without the S.
  133. script_io = io.StringIO()
  134. script_parser.write(script_io)
  135. script_str = script_io.getvalue()
  136. script_str = script_str.replace("\\\\", r"\\\\").replace("\n", r"\\\n") # Escape newlines because configparser sees those as section delimiters.
  137. new_scripts_entries.append(script_str)
  138. parser["metadata"]["post_processing_scripts"] = "\n".join(new_scripts_entries)
  139. # check renamed definition
  140. if parser.has_option("containers", "7") and parser["containers"]["7"] in _RENAMED_DEFINITION_DICT:
  141. parser["containers"]["7"] = _RENAMED_DEFINITION_DICT[parser["containers"]["7"]]
  142. result = io.StringIO()
  143. parser.write(result)
  144. return [filename], [result.getvalue()]