|
| 1 | +import math |
| 2 | + |
| 3 | +sin = lambda x: ScadValue(f'sin({x})') if isinstance(x, ScadValue) else math.sin(x) |
| 4 | +cos = lambda x: ScadValue(f'cos({x})') if isinstance(x, ScadValue) else math.cos(x) |
| 5 | +tan = lambda x: ScadValue(f'tan({x})') if isinstance(x, ScadValue) else math.tan(x) |
| 6 | +asin = lambda x: ScadValue(f'asin({x})') if isinstance(x, ScadValue) else math.asin(x) |
| 7 | +acos = lambda x: ScadValue(f'acos({x})') if isinstance(x, ScadValue) else math.acos(x) |
| 8 | +atan = lambda x: ScadValue(f'atan({x})') if isinstance(x, ScadValue) else math.atan(x) |
| 9 | +sqrt = lambda x: ScadValue(f'sqrt({x})') if isinstance(x, ScadValue) else math.sqrt(x) |
| 10 | +not_ = lambda x: ScadValue(f'!{x}') |
| 11 | + |
| 12 | +class ScadValue: |
| 13 | + def __init__(self, value): |
| 14 | + self.value = value |
| 15 | + |
| 16 | + def __repr__(self): |
| 17 | + return f'{self.value}' |
| 18 | + |
| 19 | + def __operator_base__(self, op, other): |
| 20 | + return ScadValue(f'({self} {op} {other})') |
| 21 | + |
| 22 | + def __unary_operator_base__(self, op): |
| 23 | + return ScadValue(f'({op}{self})') |
| 24 | + |
| 25 | + #basic operators +, -, *, /, %, ** |
| 26 | + def __add__(self, other): return self.__operator_base__("+", other) |
| 27 | + def __sub__(self, other): return self.__operator_base__("-", other) |
| 28 | + def __mul__(self, other): return self.__operator_base__("*", other) |
| 29 | + def __mod__(self, other): return self.__operator_base__("%", other) |
| 30 | + def __pow__(self, other): return self.__operator_base__("^", other) |
| 31 | + def __truediv__(self, other): return self.__operator_base__("/", other) |
| 32 | + |
| 33 | + def __radd__(self, other): return self.__operator_base__("+", other) |
| 34 | + def __rsub__(self, other): return self.__operator_base__("-", other) |
| 35 | + def __rmul__(self, other): return self.__operator_base__("*", other) |
| 36 | + def __rmod__(self, other): return self.__operator_base__("%", other) |
| 37 | + def __rpow__(self, other): return self.__operator_base__("^", other) |
| 38 | + def __rtruediv__(self, other): return self.__operator_base__("/", other) |
| 39 | + |
| 40 | + #unary operators |
| 41 | + def __neg__(self): return self.__unary_operator_base__("-") |
| 42 | + |
| 43 | + #other operators |
| 44 | + def __abs__(self): return ScadValue(f'abs({self})') |
| 45 | + |
| 46 | + def __bool__(self): |
| 47 | + raise Exception("You can't use scad variables as truth statement because " +\ |
| 48 | + "we don't know the value of a customized variable at SolidPython " +\ |
| 49 | + "runtime.") |
| 50 | + |
| 51 | + def __illegal_operator__(self): |
| 52 | + raise Exception("You can't compare a ScadValue with something else, " +\ |
| 53 | + "because we don't know the customized value at SolidPythons runtime " +\ |
| 54 | + "because it might get customized at OpenSCAD runtime.") |
| 55 | + |
| 56 | + def __eq__(self, other): return self.__illegal_operator__() |
| 57 | + def __ne__(self, other): return self.__illegal_operator__() |
| 58 | + def __le__(self, other): return self.__illegal_operator__() |
| 59 | + def __ge__(self, other): return self.__illegal_operator__() |
| 60 | + def __lt__(self, other): return self.__illegal_operator__() |
| 61 | + def __gt__(self, other): return self.__illegal_operator__() |
| 62 | + |
| 63 | +class ScadVariable(ScadValue): |
| 64 | + registered_variables = {} |
| 65 | + |
| 66 | + def __init__(self, name, default_value, options_str='', label='', tab=''): |
| 67 | + super().__init__(name) |
| 68 | + |
| 69 | + if name in self.registered_variables.keys(): |
| 70 | + raise ValueError("Multiple instances of ScadVariable with the same name.") |
| 71 | + |
| 72 | + def_str = self.get_definition(name, default_value, options_str, label, tab) |
| 73 | + self.registered_variables.update({name : def_str}) |
| 74 | + |
| 75 | + def get_definition(self, name, default_value, options_str, label, tab): |
| 76 | + tab = tab and f'/* [{tab}] */\n' |
| 77 | + label = label and f'//{label}\n' |
| 78 | + options_str = options_str and f' //{options_str}' |
| 79 | + |
| 80 | + if isinstance(default_value, str): |
| 81 | + default_value = f'"{default_value}"' |
| 82 | + |
| 83 | + return f'{tab}{label}{name} = {default_value};{options_str}' |
| 84 | + |
0 commit comments