summaryrefslogtreecommitdiff
path: root/gerbonara/gerber/graphic_objects.py
blob: 82aac67f61330e19bf0e97745c8174f8d16f11ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import math
from dataclasses import dataclass, KW_ONLY, astuple, replace, fields

from . import graphic_primitives as gp
from .gerber_statements import *


def convert(value, src, dst):
        if src == dst or src is None or dst is None or value is None:
            return value
        elif dst == 'mm':
            return value * 25.4
        else:
            return value / 25.4

class Length:
    def __init__(self, obj_type):
        self.type = obj_type

@dataclass
class GerberObject:
    _ : KW_ONLY
    polarity_dark : bool = True
    unit : str = None

    def converted(self, unit):
        return replace(self, 
                **{
                    f.name: convert(getattr(self, f.name), self.unit, unit)
                    for f in fields(self) if type(f.type) is Length
                    })

    def _conv(self, value, unit):
        return convert(value, src=unit, dst=self.unit)

    def with_offset(self, dx, dy, unit='mm'):
        dx, dy = self._conv(dx, unit), self._conv(dy, unit)
        return self._with_offset(dx, dy)

    def rotate(self, rotation, cx=0, cy=0, unit='mm'):
        cx, cy = self._conv(cx, unit), self._conv(cy, unit)
        self._rotate(rotation, cx, cy)

    def bounding_box(self, unit=None):
        bboxes = [ p.bounding_box() for p in self.to_primitives(unit) ]
        min_x = min(min_x for (min_x, _min_y), _ in bboxes)
        min_y = min(min_y for (_min_x, min_y), _ in bboxes)
        max_x = max(max_x for _, (max_x, _max_y) in bboxes)
        max_y = max(max_y for _, (_max_x, max_y) in bboxes)
        return ((min_x, min_y), (max_x, max_y))

    def to_primitives(self, unit=None):
        raise NotImplementedError()

@dataclass
class Flash(GerberObject):
    x : Length(float)
    y : Length(float)
    aperture : object

    def _with_offset(self, dx, dy):
        return replace(self, x=self.x+dx, y=self.y+dy)

    def _rotate(self, rotation, cx=0, cy=0):
        self.x, self.y = gp.rotate_point(self.x, self.y, rotation, cx, cy)

    def to_primitives(self, unit=None):
        conv = self.converted(unit)
        yield from self.aperture.flash(conv.x, conv.y, unit)

    def to_statements(self, gs):
        yield from gs.set_polarity(self.polarity_dark)
        yield from gs.set_aperture(self.aperture)
        yield FlashStmt(self.x, self.y, unit=self.unit)
        gs.update_point(self.x, self.y, unit=self.unit)

class Region(GerberObject):
    def __init__(self, outline=None, arc_centers=None, *, unit, polarity_dark):
        super().__init__(unit=unit, polarity_dark=polarity_dark)
        outline = [] if outline is None else outline
        arc_centers = [] if arc_centers is None else arc_centers
        self.poly = gp.ArcPoly(outline, arc_centers)

    def __len__(self):
        return len(self.poly)

    def __bool__(self):
        return bool(self.poly)

    def _with_offset(self, dx, dy):
        return Region([ (x+dx, y+dy) for x, y in self.poly.outline ],
                self.poly.arc_centers,
                polarity_dark=self.polarity_dark,
                unit=self.unit)

    def _rotate(self, angle, cx=0, cy=0):
        self.poly.outline = [ gp.rotate_point(x, y, angle, cx, cy) for x, y in self.poly.outline ]
        self.poly.arc_centers = [
                (arc[0], gp.rotate_point(*arc[1], angle, cx, cy)) if arc else None
                for arc in self.poly.arc_centers ]

    def append(self, obj):
        if obj.unit != self.unit:
            raise ValueError('Cannot append Polyline with "{obj.unit}" coords to Region with "{self.unit}" coords.')
        if not self.poly.outline:
            self.poly.outline.append(obj.p1)
        self.poly.outline.append(obj.p2)

        if isinstance(obj, Arc):
            self.poly.arc_centers.append((obj.clockwise, obj.center))
        else:
            self.poly.arc_centers.append(None)

    def to_primitives(self, unit=None):
        self.poly.polarity_dark = self.polarity_dark # FIXME: is this the right spot to do this?
        if unit == self.unit:
            yield self.poly
        else:
            conv_outline = [ (convert(x, self.unit, unit), convert(y, self.unit, unit))
                    for x, y in self.poly.outline ]
            convert_entry = lambda entry: (entry[0], (convert(entry[1][0], self.unit, unit), convert(entry[1][1], self.unit, unit)))
            conv_arc = [ None if entry is None else convert_entry(entry) for entry in self.poly.arc_centers ]

            yield gp.ArcPoly(conv_outline, conv_arc)

    def to_statements(self, gs):
        yield from gs.set_polarity(self.polarity_dark)
        yield RegionStartStmt()

        yield from gs.set_current_point(self.poly.outline[0], unit=self.unit)

        for point, arc_center in zip(self.poly.outline[1:], self.poly.arc_centers):
            if arc_center is None:
                yield from gs.set_interpolation_mode(LinearModeStmt)
                yield InterpolateStmt(*point, unit=self.unit)
                gs.update_point(*point, unit=self.unit)

            else:
                clockwise, (cx, cy) = arc_center
                x2, y2 = point
                yield from gs.set_interpolation_mode(CircularCWModeStmt if clockwise else CircularCCWModeStmt)
                yield InterpolateStmt(x2, y2, cx-x2, cy-y2, unit=self.unit)
                gs.update_point(x2, y2, unit=self.unit)

        yield RegionEndStmt()


@dataclass
class Line(GerberObject):
    # Line with *round* end caps.
    x1 : Length(float)
    y1 : Length(float)
    x2 : Length(float)
    y2 : Length(float)
    aperture : object

    def _with_offset(self, dx, dy):
        return replace(self, x1=self.x1+dx, y1=self.y1+dy, x2=self.x2+dx, y2=self.y2+dy)

    def _rotate(self, rotation, cx=0, cy=0):
        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)

    @property
    def p1(self):
        return self.x1, self.y1

    @property
    def p2(self):
        return self.x2, self.y2

    def to_primitives(self, unit=None):
        conv = self.converted(unit)
        yield gp.Line(*conv.p1, *conv.p2, self.aperture.equivalent_width(unit), polarity_dark=self.polarity_dark)

    def to_statements(self, gs):
        yield from gs.set_polarity(self.polarity_dark)
        yield from gs.set_aperture(self.aperture)
        yield from gs.set_interpolation_mode(LinearModeStmt)
        yield from gs.set_current_point(self.p1, unit=self.unit)
        yield InterpolateStmt(*self.p2, unit=self.unit)
        gs.update_point(*self.p2, unit=self.unit)


@dataclass
class Drill(GerberObject):
    x : Length(float)
    y : Length(float)
    diameter : Length(float)

    def _with_offset(self, dx, dy):
        return replace(self, x=self.x+dx, y=self.y+dy)

    def _rotate(self, angle, cx=0, cy=0):
        self.x, self.y = gp.rotate_point(self.x, self.y, angle, cx, cy)

    def to_primitives(self, unit=None):
        conv = self.converted(unit)
        yield gp.Circle(conv.x, conv.y, conv.diameter/2)


@dataclass
class Slot(GerberObject):
    x1 : Length(float)
    y1 : Length(float)
    x2 : Length(float)
    y2 : Length(float)
    width : Length(float)

    def _with_offset(self, dx, dy):
        return replace(self, x1=self.x1+dx, y1=self.y1+dy, x2=self.x2+dx, y2=self.y2+dy)

    def _rotate(self, rotation, cx=0, cy=0):
        if cx is None:
            cx = (self.x1 + self.x2) / 2
            cy = (self.y1 + self.y2) / 2
        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)

    @property
    def p1(self):
        return self.x1, self.y1

    @property
    def p2(self):
        return self.x2, self.y2

    def to_primitives(self, unit=None):
        conv = self.converted(unit)
        yield gp.Line(*conv.p1, *conv.p2, conv.width, polarity_dark=self.polarity_dark)


@dataclass
class Arc(GerberObject):
    x1 : Length(float)
    y1 : Length(float)
    x2 : Length(float)
    y2 : Length(float)
    # relative to (x1, x2)
    cx : Length(float)
    cy : Length(float)
    clockwise : bool
    aperture : object

    def _with_offset(self, dx, dy):
        return replace(self, x1=self.x1+dx, y1=self.y1+dy, x2=self.x2+dx, y2=self.y2+dy)

    @property
    def p1(self):
        return self.x1, self.y1

    @property
    def p2(self):
        return self.x2, self.y2

    @property
    def center(self):
        return self.cx + self.x1, self.cy + self.y1

    def _rotate(self, rotation, cx=0, cy=0):
        # rotate center first since we need old x1, y1 here
        new_cx, new_cy = gp.rotate_point(*self.center, rotation, cx, cy)
        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)
        self.cx, self.cy = new_cx - self.x1, new_cy - self.y1

    def to_primitives(self, unit=None):
        conv = self.converted(unit)
        yield gp.Arc(x1=conv.x1, y1=conv.y1,
                x2=conv.x2, y2=conv.y2,
                cx=conv.cx+conv.x1, cy=conv.cy+conv.y1,
                clockwise=self.clockwise,
                width=self.aperture.equivalent_width(unit),
                polarity_dark=self.polarity_dark)

    def to_statements(self, gs):
        yield from gs.set_polarity(self.polarity_dark)
        yield from gs.set_aperture(self.aperture)
        yield from gs.set_interpolation_mode(CircularCCWModeStmt)
        yield from gs.set_current_point(self.p1, unit=self.unit)
        yield InterpolateStmt(self.x2, self.y2, self.cx, self.cy, unit=self.unit)
        gs.update_point(*self.p2, unit=self.unit)