CustomSettingFunctions.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Any, List, Optional, TYPE_CHECKING
  4. from UM.Settings.PropertyEvaluationContext import PropertyEvaluationContext
  5. from UM.Settings.SettingFunction import SettingFunction
  6. if TYPE_CHECKING:
  7. from cura.CuraApplication import CuraApplication
  8. from cura.Settings.CuraContainerStack import CuraContainerStack
  9. #
  10. # This class contains all Cura-related custom setting functions. Some functions requires information such as the
  11. # currently active machine, so this is made into a class instead of standalone functions.
  12. #
  13. class CustomSettingFunctions:
  14. def __init__(self, application: "CuraApplication") -> None:
  15. self._application = application
  16. # ================
  17. # Custom Functions
  18. # ================
  19. # Gets the default extruder position of the currently active machine.
  20. def getDefaultExtruderPosition(self) -> str:
  21. machine_manager = self._application.getMachineManager()
  22. return machine_manager.defaultExtruderPosition
  23. # Gets the given setting key from the given extruder position.
  24. def getValueInExtruder(self, extruder_position: int, property_key: str,
  25. context: Optional["PropertyEvaluationContext"] = None) -> Any:
  26. machine_manager = self._application.getMachineManager()
  27. if extruder_position == -1:
  28. extruder_position = int(machine_manager.defaultExtruderPosition)
  29. global_stack = machine_manager.activeMachine
  30. extruder_stack = global_stack.extruders[str(extruder_position)]
  31. if extruder_stack:
  32. value = extruder_stack.getRawProperty(property_key, "value", context = context)
  33. if isinstance(value, SettingFunction):
  34. value = value(extruder_stack, context = context)
  35. else:
  36. # Just a value from global.
  37. value = global_stack.getProperty(property_key, "value", context = context)
  38. return value
  39. # Gets all extruder values as a list for the given property.
  40. def getValuesInAllExtruders(self, property_key: str,
  41. context: Optional["PropertyEvaluationContext"] = None) -> List[Any]:
  42. machine_manager = self._application.getMachineManager()
  43. extruder_manager = self._application.getExtruderManager()
  44. global_stack = machine_manager.activeMachine
  45. result = []
  46. for extruder in extruder_manager.getActiveExtruderStacks():
  47. if not extruder.isEnabled:
  48. continue
  49. # only include values from extruders that are "active" for the current machine instance
  50. if int(extruder.getMetaDataEntry("position")) >= global_stack.getProperty("machine_extruder_count", "value", context = context):
  51. continue
  52. value = extruder.getRawProperty(property_key, "value", context = context)
  53. if value is None:
  54. continue
  55. if isinstance(value, SettingFunction):
  56. value = value(extruder, context= context)
  57. result.append(value)
  58. if not result:
  59. result.append(global_stack.getProperty(property_key, "value", context = context))
  60. return result
  61. # Get the resolve value or value for a given key.
  62. def getResolveOrValue(self, property_key: str, context: Optional["PropertyEvaluationContext"] = None) -> Any:
  63. machine_manager = self._application.getMachineManager()
  64. global_stack = machine_manager.activeMachine
  65. resolved_value = global_stack.getProperty(property_key, "value", context = context)
  66. return resolved_value
  67. # Gets the default setting value from given extruder position. The default value is what excludes the values in
  68. # the user_changes container.
  69. def getDefaultValueInExtruder(self, extruder_position: int, property_key: str) -> Any:
  70. machine_manager = self._application.getMachineManager()
  71. global_stack = machine_manager.activeMachine
  72. extruder_stack = global_stack.extruders[str(extruder_position)]
  73. context = self.createContextForDefaultValueEvaluation(extruder_stack)
  74. return self.getValueInExtruder(extruder_position, property_key, context = context)
  75. # Gets all default setting values as a list from all extruders of the currently active machine.
  76. # The default values are those excluding the values in the user_changes container.
  77. def getDefaultValuesInAllExtruders(self, property_key: str) -> List[Any]:
  78. machine_manager = self._application.getMachineManager()
  79. global_stack = machine_manager.activeMachine
  80. context = self.createContextForDefaultValueEvaluation(global_stack)
  81. return self.getValuesInAllExtruders(property_key, context = context)
  82. # Gets the resolve value or value for a given key without looking the first container (user container).
  83. def getDefaultResolveOrValue(self, property_key: str) -> Any:
  84. machine_manager = self._application.getMachineManager()
  85. global_stack = machine_manager.activeMachine
  86. context = self.createContextForDefaultValueEvaluation(global_stack)
  87. return self.getResolveOrValue(property_key, context = context)
  88. # Creates a context for evaluating default values (skip the user_changes container).
  89. def createContextForDefaultValueEvaluation(self, source_stack: "CuraContainerStack") -> "PropertyEvaluationContext":
  90. context = PropertyEvaluationContext(source_stack)
  91. context.context["evaluate_from_container_index"] = 1 # skip the user settings container
  92. context.context["override_operators"] = {
  93. "extruderValue": self.getDefaultValueInExtruder,
  94. "extruderValues": self.getDefaultValuesInAllExtruders,
  95. "resolveOrValue": self.getDefaultResolveOrValue,
  96. }
  97. return context