CuraFormulaFunctions.py 5.8 KB

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