Settings.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from dataclasses import asdict
  4. from typing import cast, Dict, TYPE_CHECKING
  5. from UM.Settings.InstanceContainer import InstanceContainer
  6. from UM.Settings.SettingFunction import SettingFunction
  7. from cura.Settings.GlobalStack import GlobalStack
  8. if TYPE_CHECKING:
  9. from cura.CuraApplication import CuraApplication
  10. class Settings:
  11. """The Interface.Settings API provides a version-proof bridge
  12. between Cura's
  13. (currently) sidebar UI and plug-ins that hook into it.
  14. Usage:
  15. .. code-block:: python
  16. from cura.API import CuraAPI
  17. api = CuraAPI()
  18. api.interface.settings.getContextMenuItems()
  19. data = {
  20. "name": "My Plugin Action",
  21. "iconName": "my-plugin-icon",
  22. "actions": my_menu_actions,
  23. "menu_item": MyPluginAction(self)
  24. }
  25. api.interface.settings.addContextMenuItem(data)
  26. """
  27. def __init__(self, application: "CuraApplication") -> None:
  28. self.application = application
  29. def addContextMenuItem(self, menu_item: dict) -> None:
  30. """Add items to the sidebar context menu.
  31. :param menu_item: dict containing the menu item to add.
  32. """
  33. self.application.addSidebarCustomMenuItem(menu_item)
  34. def getContextMenuItems(self) -> list:
  35. """Get all custom items currently added to the sidebar context menu.
  36. :return: List containing all custom context menu items.
  37. """
  38. return self.application.getSidebarCustomMenuItems()
  39. def getSliceMetadata(self) -> Dict[str, Dict[str, Dict[str, str]]]:
  40. """Get all changed settings and all settings. For each extruder and the global stack"""
  41. print_information = self.application.getPrintInformation()
  42. machine_manager = self.application.getMachineManager()
  43. settings = {
  44. "material": {
  45. "length": print_information.materialLengths,
  46. "weight": print_information.materialWeights,
  47. "cost": print_information.materialCosts,
  48. },
  49. "global": {
  50. "changes": {},
  51. "all_settings": {},
  52. },
  53. "quality": asdict(machine_manager.activeQualityDisplayNameMap()),
  54. }
  55. def _retrieveValue(container: InstanceContainer, setting_: str):
  56. value_ = container.getProperty(setting_, "value")
  57. for _ in range(0, 1024): # Prevent possibly endless loop by not using a limit.
  58. if not isinstance(value_, SettingFunction):
  59. return value_ # Success!
  60. value_ = value_(container)
  61. return 0 # Fallback value after breaking possibly endless loop.
  62. global_stack = cast(GlobalStack, self.application.getGlobalContainerStack())
  63. # Add global user or quality changes
  64. global_flattened_changes = InstanceContainer.createMergedInstanceContainer(global_stack.userChanges, global_stack.qualityChanges)
  65. for setting in global_flattened_changes.getAllKeys():
  66. settings["global"]["changes"][setting] = _retrieveValue(global_flattened_changes, setting)
  67. # Get global all settings values without user or quality changes
  68. for setting in global_stack.getAllKeys():
  69. settings["global"]["all_settings"][setting] = _retrieveValue(global_stack, setting)
  70. for i, extruder in enumerate(global_stack.extruderList):
  71. # Add extruder fields to settings dictionary
  72. settings[f"extruder_{i}"] = {
  73. "changes": {},
  74. "all_settings": {},
  75. }
  76. # Add extruder user or quality changes
  77. extruder_flattened_changes = InstanceContainer.createMergedInstanceContainer(extruder.userChanges, extruder.qualityChanges)
  78. for setting in extruder_flattened_changes.getAllKeys():
  79. settings[f"extruder_{i}"]["changes"][setting] = _retrieveValue(extruder_flattened_changes, setting)
  80. # Get extruder all settings values without user or quality changes
  81. for setting in extruder.getAllKeys():
  82. settings[f"extruder_{i}"]["all_settings"][setting] = _retrieveValue(extruder, setting)
  83. return settings