CuraFormulaFunctions.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 functions that can be used in formulas. Some functions requires
  11. # information such as the currently active machine, so this is made into a class instead of standalone functions.
  12. #
  13. class CuraFormulaFunctions:
  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. value = extruder_stack.getRawProperty(property_key, "value", context = context)
  32. if isinstance(value, SettingFunction):
  33. value = value(extruder_stack, context = context)
  34. return value
  35. # Gets all extruder values as a list for the given property.
  36. def getValuesInAllExtruders(self, property_key: str,
  37. context: Optional["PropertyEvaluationContext"] = None) -> List[Any]:
  38. machine_manager = self._application.getMachineManager()
  39. extruder_manager = self._application.getExtruderManager()
  40. global_stack = machine_manager.activeMachine
  41. result = []
  42. for extruder in extruder_manager.getActiveExtruderStacks():
  43. if not extruder.isEnabled:
  44. continue
  45. # only include values from extruders that are "active" for the current machine instance
  46. if int(extruder.getMetaDataEntry("position")) >= global_stack.getProperty("machine_extruder_count", "value", context = context):
  47. continue
  48. value = extruder.getRawProperty(property_key, "value", context = context)
  49. if value is None:
  50. continue
  51. if isinstance(value, SettingFunction):
  52. value = value(extruder, context = context)
  53. result.append(value)
  54. if not result:
  55. result.append(global_stack.getProperty(property_key, "value", context = context))
  56. return result
  57. # Get the resolve value or value for a given key.
  58. def getResolveOrValue(self, property_key: str, context: Optional["PropertyEvaluationContext"] = None) -> Any:
  59. machine_manager = self._application.getMachineManager()
  60. global_stack = machine_manager.activeMachine
  61. resolved_value = global_stack.getProperty(property_key, "value", context = context)
  62. return resolved_value
  63. # Gets the default setting value from given extruder position. The default value is what excludes the values in
  64. # the user_changes container.
  65. def getDefaultValueInExtruder(self, extruder_position: int, property_key: str) -> Any:
  66. machine_manager = self._application.getMachineManager()
  67. global_stack = machine_manager.activeMachine
  68. extruder_stack = global_stack.extruders[str(extruder_position)]
  69. context = self.createContextForDefaultValueEvaluation(extruder_stack)
  70. return self.getValueInExtruder(extruder_position, property_key, context = context)
  71. # Gets all default setting values as a list from all extruders of the currently active machine.
  72. # The default values are those excluding the values in the user_changes container.
  73. def getDefaultValuesInAllExtruders(self, property_key: str) -> List[Any]:
  74. machine_manager = self._application.getMachineManager()
  75. global_stack = machine_manager.activeMachine
  76. context = self.createContextForDefaultValueEvaluation(global_stack)
  77. return self.getValuesInAllExtruders(property_key, context = context)
  78. # Gets the resolve value or value for a given key without looking the first container (user container).
  79. def getDefaultResolveOrValue(self, property_key: str) -> Any:
  80. machine_manager = self._application.getMachineManager()
  81. global_stack = machine_manager.activeMachine
  82. context = self.createContextForDefaultValueEvaluation(global_stack)
  83. return self.getResolveOrValue(property_key, context = context)
  84. # Creates a context for evaluating default values (skip the user_changes container).
  85. def createContextForDefaultValueEvaluation(self, source_stack: "CuraContainerStack") -> "PropertyEvaluationContext":
  86. context = PropertyEvaluationContext(source_stack)
  87. context.context["evaluate_from_container_index"] = 1 # skip the user settings container
  88. context.context["override_operators"] = {
  89. "extruderValue": self.getDefaultValueInExtruder,
  90. "extruderValues": self.getDefaultValuesInAllExtruders,
  91. "resolveOrValue": self.getDefaultResolveOrValue,
  92. }
  93. return context