From f64b03efc752b682b1cbe8cfb114f19e3362ef76 Mon Sep 17 00:00:00 2001 From: jaseg Date: Fri, 17 Feb 2023 00:03:04 +0100 Subject: Add CLI --- gerbonara/graphic_objects.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'gerbonara/graphic_objects.py') diff --git a/gerbonara/graphic_objects.py b/gerbonara/graphic_objects.py index 0e433cf..4fa5fce 100644 --- a/gerbonara/graphic_objects.py +++ b/gerbonara/graphic_objects.py @@ -105,6 +105,21 @@ class GraphicObject: dx, dy = self.unit(dx, unit), self.unit(dy, unit) self._offset(dx, dy) + def scale(self, sx, sy, unit=MM): + """ Scale this feature in both its dimensions and location. + + .. note:: The scale values are scalars, and the unit argument is irrelevant, but is kept for API consistency. + + .. note:: If this object references an aperture, this aperture is not modified. You will have to transform this + aperture yourself. + + :param float sx: X scale, 1 to keep the object as is, larger values to enlarge, smaller values to shrink. + Negative values are permitted. + :param float sy: Y scale as above. + """ + + self._scale(sx, sy) + def rotate(self, rotation, cx=0, cy=0, unit=MM): """ Rotate this object. The center of rotation can be given in either unit, and is automatically converted into this object's local unit. @@ -112,6 +127,9 @@ class GraphicObject: .. note:: The center's Y coordinate as well as the angle's polarity are flipped compared to computer graphics convention since Gerber uses a bottom-to-top Y axis. + .. note:: If this object references an aperture, this aperture is not modified. You will have to transform this + aperture yourself. + :param float rotation: rotation in radians clockwise. :param float cx: X coordinate of center of rotation in *unit* units. :param float cy: Y coordinate of center of rotation. (0,0) is at the bottom left of the image. @@ -214,6 +232,10 @@ class Flash(GraphicObject): def _rotate(self, rotation, cx=0, cy=0): self.x, self.y = gp.rotate_point(self.x, self.y, rotation, cx, cy) + def _scale(self, sx, sy): + self.x *= sx + self.y *= sy + def to_primitives(self, unit=None): conv = self.converted(unit) yield from self.aperture.flash(conv.x, conv.y, unit, self.polarity_dark) @@ -281,6 +303,12 @@ class Region(GraphicObject): (arc[0], gp.rotate_point(*arc[1], angle, cx-p[0], cy-p[1])) if arc else None for p, arc in zip(self.outline, self.arc_centers) ] + def _scale(self, sx, sy): + self.outline = [ (x*sx, y*sy) for x, y in self.outline ] + self.arc_centers = [ + (arc[0], (arc[1][0]*sx, arc[1][1]*sy)) if arc else None + for p, arc in zip(self.outline, self.arc_centers) ] + def append(self, obj): if obj.unit != self.unit: obj = obj.converted(self.unit) @@ -379,6 +407,12 @@ class Line(GraphicObject): self.x1, self.y1 = gp.rotate_point(self.x1, self.y1, rotation, cx, cy) self.x2, self.y2 = gp.rotate_point(self.x2, self.y2, rotation, cx, cy) + def _scale(self, sx=1, sy=1): + self.x1 *= sx + self.y1 *= sy + self.x2 *= sx + self.y2 *= sy + @property def p1(self): """ Convenience alias for ``(self.x1, self.y1)`` returning start point of the line. """ @@ -611,6 +645,14 @@ class Arc(GraphicObject): self.x2, self.y2 = gp.rotate_point(self.x2, self.y2, rotation, cx, cy) self.cx, self.cy = new_cx - self.x1, new_cy - self.y1 + def _scale(self, sx=1, sy=1): + self.x1 *= sx + self.y1 *= sy + self.x2 *= sx + self.y2 *= sy + self.cx *= sx + self.cy *= sy + def as_primitive(self, unit=None): conv = self.converted(unit) w = self.aperture.equivalent_width(unit) if self.aperture else 0.1 # for debugging -- cgit