VersionUpgrade42to43.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import configparser
  2. import io
  3. from typing import Dict, Tuple, List
  4. from UM.VersionUpgrade import VersionUpgrade
  5. _renamed_profiles = {"generic_pla_0.4_coarse": "jbo_generic_pla_0.4_coarse",
  6. "generic_pla_0.4_fine": "jbo_generic_pla_fine",
  7. "generic_pla_0.4_medium": "jbo_generic_pla_medium",
  8. "generic_pla_0.4_ultrafine": "jbo_generic_pla_ultrafine",
  9. "generic_petg_0.4_coarse": "jbo_generic_petg_0.4_coarse",
  10. "generic_petg_0.4_fine": "jbo_generic_petg_fine",
  11. "generic_petg_0.4_medium": "jbo_generic_petg_medium",
  12. }
  13. # - The variant "imade3d jellybox 0.4 mm 2-fans" for machine definition "imade3d_jellybox"
  14. # is now "0.4 mm" for machine definition "imade3d jellybox_2".
  15. # - Materials "imade3d_petg_green" and "imade3d_petg_pink" are now "imade3d_petg_175".
  16. # - Materials "imade3d_pla_green" and "imade3d_pla_pink" are now "imade3d_petg_175".
  17. #
  18. # Note: Theoretically, the old material profiles with "_2-fans" at the end should be updated to:
  19. # - machine definition: imade3d_jellybox_2
  20. # - variant: 0.4 mm (for jellybox 2)
  21. # - material: (as an example) imade3d_petg_175_imade3d_jellybox_2_0.4_mm
  22. #
  23. # But this involves changing the definition of the global stack and the extruder stacks, which can cause more trouble
  24. # than what we can fix. So, here, we update all material variants, regardless of having "_2-fans" at the end or not, to
  25. # jellybox_0.4_mm.
  26. #
  27. _renamed_material_profiles = { # PETG
  28. "imade3d_petg_green": "imade3d_petg_175",
  29. "imade3d_petg_green_imade3d_jellybox": "imade3d_petg_175_imade3d_jellybox",
  30. "imade3d_petg_green_imade3d_jellybox_0.4_mm": "imade3d_petg_175_imade3d_jellybox_0.4_mm",
  31. "imade3d_petg_green_imade3d_jellybox_0.4_mm_2-fans": "imade3d_petg_175_imade3d_jellybox_0.4_mm",
  32. "imade3d_petg_pink": "imade3d_petg_175",
  33. "imade3d_petg_pink_imade3d_jellybox": "imade3d_petg_175_imade3d_jellybox",
  34. "imade3d_petg_pink_imade3d_jellybox_0.4_mm": "imade3d_petg_175_imade3d_jellybox_0.4_mm",
  35. "imade3d_petg_pink_imade3d_jellybox_0.4_mm_2-fans": "imade3d_petg_175_imade3d_jellybox_0.4_mm",
  36. # PLA
  37. "imade3d_pla_green": "imade3d_pla_175",
  38. "imade3d_pla_green_imade3d_jellybox": "imade3d_pla_175_imade3d_jellybox",
  39. "imade3d_pla_green_imade3d_jellybox_0.4_mm": "imade3d_pla_175_imade3d_jellybox_0.4_mm",
  40. "imade3d_pla_green_imade3d_jellybox_0.4_mm_2-fans": "imade3d_pla_175_imade3d_jellybox_0.4_mm",
  41. "imade3d_pla_pink": "imade3d_pla_175",
  42. "imade3d_pla_pink_imade3d_jellybox": "imade3d_pla_175_imade3d_jellybox",
  43. "imade3d_pla_pink_imade3d_jellybox_0.4_mm": "imade3d_pla_175_imade3d_jellybox_0.4_mm",
  44. "imade3d_pla_pink_imade3d_jellybox_0.4_mm_2-fans": "imade3d_pla_175_imade3d_jellybox_0.4_mm",
  45. }
  46. _removed_settings = {
  47. "start_layers_at_same_position"
  48. }
  49. _renamed_settings = {
  50. "support_infill_angle": "support_infill_angles"
  51. } # type: Dict[str, str]
  52. class VersionUpgrade42to43(VersionUpgrade):
  53. """Upgrades configurations from the state they were in at version 4.2 to the
  54. state they should be in at version 4.3.
  55. """
  56. def upgradePreferences(self, serialized: str, filename: str):
  57. parser = configparser.ConfigParser(interpolation = None)
  58. parser.read_string(serialized)
  59. if "camera_perspective_mode" in parser["general"] and parser["general"]["camera_perspective_mode"] == "orthogonal":
  60. parser["general"]["camera_perspective_mode"] = "orthographic"
  61. # Fix renamed settings for visibility
  62. if "visible_settings" in parser["general"]:
  63. all_setting_keys = parser["general"]["visible_settings"].strip().split(";")
  64. if all_setting_keys:
  65. for idx, key in enumerate(all_setting_keys):
  66. if key in _renamed_settings:
  67. all_setting_keys[idx] = _renamed_settings[key]
  68. parser["general"]["visible_settings"] = ";".join(all_setting_keys)
  69. parser["metadata"]["setting_version"] = "9"
  70. result = io.StringIO()
  71. parser.write(result)
  72. return [filename], [result.getvalue()]
  73. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  74. """Upgrades instance containers to have the new version number.
  75. This renames the renamed settings in the containers.
  76. """
  77. parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
  78. parser.read_string(serialized)
  79. # Update version number.
  80. parser["metadata"]["setting_version"] = "9"
  81. if "values" in parser:
  82. for old_name, new_name in _renamed_settings.items():
  83. if old_name in parser["values"]:
  84. parser["values"][new_name] = parser["values"][old_name]
  85. del parser["values"][old_name]
  86. for key in _removed_settings:
  87. if key in parser["values"]:
  88. del parser["values"][key]
  89. if "support_infill_angles" in parser["values"]:
  90. old_value = float(parser["values"]["support_infill_angles"])
  91. new_value = [int(round(old_value))]
  92. parser["values"]["support_infill_angles"] = str(new_value)
  93. result = io.StringIO()
  94. parser.write(result)
  95. return [filename], [result.getvalue()]
  96. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  97. """Upgrades stacks to have the new version number."""
  98. parser = configparser.ConfigParser(interpolation = None)
  99. parser.read_string(serialized)
  100. # Update version number.
  101. parser["metadata"]["setting_version"] = "9"
  102. # Handle changes for the imade3d jellybox. The machine was split up into parts (eg; a 2 fan version and a single
  103. # fan version. Previously it used variants for this. The only upgrade we can do here is strip that variant.
  104. # This is because we only upgrade per stack (and to fully do these changes, we'd need to switch out something
  105. # in the global container based on changes made to the extruder stack)
  106. if parser["containers"]["6"] == "imade3d_jellybox_extruder_0":
  107. quality_id = parser["containers"]["2"]
  108. if quality_id.endswith("_2-fans"):
  109. parser["containers"]["2"] = quality_id.replace("_2-fans", "")
  110. if parser["containers"]["2"] in _renamed_profiles:
  111. parser["containers"]["2"] = _renamed_profiles[parser["containers"]["2"]]
  112. material_id = parser["containers"]["3"]
  113. if material_id in _renamed_material_profiles:
  114. parser["containers"]["3"] = _renamed_material_profiles[material_id]
  115. variant_id = parser["containers"]["4"]
  116. if variant_id.endswith("_2-fans"):
  117. parser["containers"]["4"] = variant_id.replace("_2-fans", "")
  118. result = io.StringIO()
  119. parser.write(result)
  120. return [filename], [result.getvalue()]