From 27a992f1c8c0a37245168e23db160412494d0e18 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sat, 1 Jan 2022 17:47:50 +0100 Subject: Add dilation code --- gerbonara/gerber/aperture_macros/expression.py | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'gerbonara/gerber/aperture_macros/expression.py') diff --git a/gerbonara/gerber/aperture_macros/expression.py b/gerbonara/gerber/aperture_macros/expression.py index 390b7b7..fb399d3 100644 --- a/gerbonara/gerber/aperture_macros/expression.py +++ b/gerbonara/gerber/aperture_macros/expression.py @@ -90,6 +90,47 @@ class UnitExpression(Expression): else: raise ValueError(f'invalid unit {unit}, must be "inch" or "mm".') + def __add__(self, other): + if not isinstance(other, UnitExpression): + raise ValueError('Unit mismatch: Can only add/subtract UnitExpression from UnitExpression, not scalar.') + + if self.unit == other.unit or self.unit is None or other.unit is None: + return UnitExpression(self._expr + other._expr, self.unit) + + if other.unit == 'mm': # -> and self.unit == 'inch' + return UnitExpression(self._expr + (other._expr / MILLIMETERS_PER_INCH), self.unit) + else: # other.unit == 'inch' and self.unit == 'mm' + return UnitExpression(self._expr + (other._expr * MILLIMETERS_PER_INCH), self.unit) + + def __radd__(self, other): + # left hand side cannot have been an UnitExpression or __radd__ would not have been called + raise ValueError('Unit mismatch: Can only add/subtract UnitExpression from UnitExpression, not scalar.') + + def __sub__(self, other): + return (self + (-other)).optimize() + + def __rsub__(self, other): + # see __radd__ above + raise ValueError('Unit mismatch: Can only add/subtract UnitExpression from UnitExpression, not scalar.') + + def __mul__(self, other): + return UnitExpression(self._expr * other, self.unit) + + def __rmul__(self, other): + return UnitExpression(other * self._expr, self.unit) + + def __truediv__(self, other): + return UnitExpression(self._expr / other, self.unit) + + def __rtruediv__(self, other): + return UnitExpression(other / self._expr, self.unit) + + def __neg__(self): + return UnitExpression(-self._expr, self.unit) + + def __pos__(self): + return self + class ConstantExpression(Expression): def __init__(self, value): -- cgit