123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- from typing import Any, List, Optional, TYPE_CHECKING
- from UM.Settings.PropertyEvaluationContext import PropertyEvaluationContext
- from UM.Settings.SettingFunction import SettingFunction
- if TYPE_CHECKING:
- from cura.CuraApplication import CuraApplication
- from cura.Settings.CuraContainerStack import CuraContainerStack
- class CuraFormulaFunctions:
- def __init__(self, application: "CuraApplication") -> None:
- self._application = application
-
-
-
-
- def getDefaultExtruderPosition(self) -> str:
- machine_manager = self._application.getMachineManager()
- return machine_manager.defaultExtruderPosition
-
- def getValueInExtruder(self, extruder_position: int, property_key: str,
- context: Optional["PropertyEvaluationContext"] = None) -> Any:
- machine_manager = self._application.getMachineManager()
- if extruder_position == -1:
- extruder_position = int(machine_manager.defaultExtruderPosition)
- global_stack = machine_manager.activeMachine
- extruder_stack = global_stack.extruders[str(extruder_position)]
- value = extruder_stack.getRawProperty(property_key, "value", context = context)
- if isinstance(value, SettingFunction):
- value = value(extruder_stack, context = context)
- return value
-
- def getValuesInAllExtruders(self, property_key: str,
- context: Optional["PropertyEvaluationContext"] = None) -> List[Any]:
- machine_manager = self._application.getMachineManager()
- extruder_manager = self._application.getExtruderManager()
- global_stack = machine_manager.activeMachine
- result = []
- for extruder in extruder_manager.getActiveExtruderStacks():
- if not extruder.isEnabled:
- continue
-
- if int(extruder.getMetaDataEntry("position")) >= global_stack.getProperty("machine_extruder_count", "value", context = context):
- continue
- value = extruder.getRawProperty(property_key, "value", context = context)
- if value is None:
- continue
- if isinstance(value, SettingFunction):
- value = value(extruder, context = context)
- result.append(value)
- if not result:
- result.append(global_stack.getProperty(property_key, "value", context = context))
- return result
-
- def getResolveOrValue(self, property_key: str, context: Optional["PropertyEvaluationContext"] = None) -> Any:
- machine_manager = self._application.getMachineManager()
- global_stack = machine_manager.activeMachine
- resolved_value = global_stack.getProperty(property_key, "value", context = context)
- return resolved_value
-
-
- def getDefaultValueInExtruder(self, extruder_position: int, property_key: str) -> Any:
- machine_manager = self._application.getMachineManager()
- global_stack = machine_manager.activeMachine
- extruder_stack = global_stack.extruders[str(extruder_position)]
- context = self.createContextForDefaultValueEvaluation(extruder_stack)
- return self.getValueInExtruder(extruder_position, property_key, context = context)
-
-
- def getDefaultValuesInAllExtruders(self, property_key: str) -> List[Any]:
- machine_manager = self._application.getMachineManager()
- global_stack = machine_manager.activeMachine
- context = self.createContextForDefaultValueEvaluation(global_stack)
- return self.getValuesInAllExtruders(property_key, context = context)
-
- def getDefaultResolveOrValue(self, property_key: str) -> Any:
- machine_manager = self._application.getMachineManager()
- global_stack = machine_manager.activeMachine
- context = self.createContextForDefaultValueEvaluation(global_stack)
- return self.getResolveOrValue(property_key, context = context)
-
- def createContextForDefaultValueEvaluation(self, source_stack: "CuraContainerStack") -> "PropertyEvaluationContext":
- context = PropertyEvaluationContext(source_stack)
- context.context["evaluate_from_container_index"] = 1
- context.context["override_operators"] = {
- "extruderValue": self.getDefaultValueInExtruder,
- "extruderValues": self.getDefaultValuesInAllExtruders,
- "resolveOrValue": self.getDefaultResolveOrValue,
- }
- return context
|