VersionUpgrade413to50.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Copyright (c) 2022 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. _removed_settings = {
  8. "travel_compensate_overlapping_walls_enabled",
  9. "travel_compensate_overlapping_walls_0_enabled",
  10. "travel_compensate_overlapping_walls_x_enabled",
  11. "fill_perimeter_gaps",
  12. "filter_out_tiny_gaps",
  13. "wall_min_flow",
  14. "wall_min_flow_retract",
  15. "speed_equalize_flow_max"
  16. }
  17. _transformed_settings = { # These settings have been changed to a new topic, but may have different data type. Used only for setting visibility; the rest is handled separately.
  18. "outer_inset_first": "inset_direction",
  19. "speed_equalize_flow_enabled": "speed_equalize_flow_width_factor"
  20. }
  21. class VersionUpgrade413to50(VersionUpgrade):
  22. def upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  23. """
  24. Upgrades preferences to remove from the visibility list the settings that were removed in this version.
  25. It also changes the preferences to have the new version number.
  26. This removes any settings that were removed in the new Cura version.
  27. :param serialized: The original contents of the preferences file.
  28. :param filename: The file name of the preferences file.
  29. :return: A list of new file names, and a list of the new contents for
  30. those files.
  31. """
  32. parser = configparser.ConfigParser(interpolation = None)
  33. parser.read_string(serialized)
  34. # Update version number.
  35. parser["metadata"]["setting_version"] = "20"
  36. # Remove deleted settings from the visible settings list.
  37. if "general" in parser and "visible_settings" in parser["general"]:
  38. visible_settings = set(parser["general"]["visible_settings"].split(";"))
  39. for removed in _removed_settings:
  40. if removed in visible_settings:
  41. visible_settings.remove(removed)
  42. # Replace equivalent settings that have been transformed.
  43. for old, new in _transformed_settings.items():
  44. if old in visible_settings:
  45. visible_settings.remove(old)
  46. visible_settings.add(new)
  47. parser["general"]["visible_settings"] = ";".join(visible_settings)
  48. result = io.StringIO()
  49. parser.write(result)
  50. return [filename], [result.getvalue()]
  51. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  52. """
  53. Upgrades instance containers to remove the settings that were removed in this version.
  54. It also changes the instance containers to have the new version number.
  55. This removes any settings that were removed in the new Cura version and updates settings that need to be updated
  56. with a new value.
  57. :param serialized: The original contents of the instance container.
  58. :param filename: The original file name of the instance container.
  59. :return: A list of new file names, and a list of the new contents for
  60. those files.
  61. """
  62. parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
  63. parser.read_string(serialized)
  64. # Update version number.
  65. parser["metadata"]["setting_version"] = "20"
  66. if "values" in parser:
  67. # Remove deleted settings from the instance containers.
  68. for removed in _removed_settings:
  69. if removed in parser["values"]:
  70. del parser["values"][removed]
  71. # Replace Outer Before Inner Walls with equivalent setting.
  72. if "outer_inset_first" in parser["values"]:
  73. old_value = parser["values"]["outer_inset_first"]
  74. if old_value.startswith("="): # Was already a formula.
  75. old_value = old_value[1:]
  76. parser["values"]["inset_direction"] = f"='outside_in' if ({old_value}) else 'inside_out'" # Makes it work both with plain setting values and formulas.
  77. # Replace Equalize Filament Flow with equivalent setting.
  78. if "speed_equalize_flow_enabled" in parser["values"]:
  79. old_value = parser["values"]["speed_equalize_flow_enabled"]
  80. if old_value.startswith("="): # Was already a formula.
  81. old_value = old_value[1:]
  82. parser["values"]["speed_equalize_flow_width_factor"] = f"=100 if ({old_value}) else 0" # If it used to be enabled, set it to 100%. Otherwise 0%.
  83. result = io.StringIO()
  84. parser.write(result)
  85. return [filename], [result.getvalue()]
  86. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  87. """
  88. Upgrades stacks to have the new version number.
  89. :param serialized: The original contents of the stack.
  90. :param filename: The original file name of the stack.
  91. :return: A list of new file names, and a list of the new contents for
  92. those files.
  93. """
  94. parser = configparser.ConfigParser(interpolation = None)
  95. parser.read_string(serialized)
  96. # Update version number.
  97. if "metadata" not in parser:
  98. parser["metadata"] = {}
  99. parser["metadata"]["setting_version"] = "20"
  100. result = io.StringIO()
  101. parser.write(result)
  102. return [filename], [result.getvalue()]