VersionUpgrade462to47.py 6.9 KB

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