summaryrefslogtreecommitdiff
path: root/gerbonara/gerber/gerber_statements.py
blob: d2f67c151d12fdff552e255254a693ffcf3bf856 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# copyright 2014 Hamilton Kibbe <ham@hamiltonkib.be>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Gerber (RS-274X) Statements
===========================
**Gerber RS-274X file statement classes**

"""
from .utils import (parse_gerber_value, write_gerber_value, decimal_string,
                    inch, metric)

from .am_statements import *
from .am_read import read_macro
from .am_primitive import eval_macro
from .primitives import AMGroup


class Statement:
    """ Gerber statement Base class

    The statement class provides a type attribute.

    Parameters
    ----------
    type : string
        String identifying the statement type.

    Attributes
    ----------
    type : string
        String identifying the statement type.
    """

    def __str__(self):
        s = "<{0} ".format(self.__class__.__name__)

        for key, value in self.__dict__.items():
            s += "{0}={1} ".format(key, value)

        s = s.rstrip() + ">"
        return s

    def offset(self, x_offset=0, y_offset=0):
        pass

    def __eq__(self, other):
        return self.__dict__ == other.__dict__


class ParamStmt(Statement):
    pass

class FormatSpecStmt(ParamStmt):
    """ FS - Gerber Format Specification Statement """
    code = 'FS'

    def to_gerber(self, settings):
        zeros = 'L' if settings.zero_suppression == 'leading' else 'T'
        notation = 'A' if settings.notation == 'absolute' else 'I'
        fmt = settings.number_format
        number_format = str(settings.number_format[0]) + str(settings.number_format[1])

        return f'%FS{zeros}{notation}X{number_format}Y{number_format}*%'

    def __str__(self):
        return '<FS Format Specification>'


class UnitStmt(ParamStmt):
    """ MO - Coordinate unit mode statement """

    def to_gerber(self, settings):
        return '%MOMM*%' if settings.units == 'mm' else '%MOIN*%'

    def __str__(self):
        return ('<MO Coordinate unit mode statement>' % mode_str)


class LoadPolarityStmt(ParamStmt):
    """ LP - Gerber Load Polarity statement """

    def __init__(self, dark):
        self.dark = dark

    def to_gerber(self, settings=None):
        lp = 'D' if self.dark else 'C'
        return f'%LP{lp}*%'

    def __str__(self):
        lp = 'dark' if self.dark else 'clear'
        return f'<LP Level Polarity: {lp}>'


class ADParamStmt(ParamStmt):
    """ AD - Aperture Definition Statement """

    @classmethod
    def rect(cls, dcode, width, height, hole_diameter=None, hole_width=None, hole_height=None):
        '''Create a rectangular aperture definition statement'''
        if hole_diameter is not None and hole_diameter > 0:
            return cls('AD', dcode, 'R', ([width, height, hole_diameter],))
        elif (hole_width is not None and hole_width > 0
              and hole_height is not None and hole_height > 0):
            return cls('AD', dcode, 'R', ([width, height, hole_width, hole_height],))
        return cls('AD', dcode, 'R', ([width, height],))

    @classmethod
    def circle(cls, dcode, diameter, hole_diameter=None, hole_width=None, hole_height=None):
        '''Create a circular aperture definition statement'''
        if hole_diameter is not None and hole_diameter > 0:
            return cls('AD', dcode, 'C', ([diameter, hole_diameter],))
        elif (hole_width is not None and hole_width > 0
              and hole_height is not None and hole_height > 0):
            return cls('AD', dcode, 'C', ([diameter, hole_width, hole_height],))
        return cls('AD', dcode, 'C', ([diameter],))

    @classmethod
    def obround(cls, dcode, width, height, hole_diameter=None, hole_width=None, hole_height=None):
        '''Create an obround aperture definition statement'''
        if hole_diameter is not None and hole_diameter > 0:
            return cls('AD', dcode, 'O', ([width, height, hole_diameter],))
        elif (hole_width is not None and hole_width > 0
              and hole_height is not None and hole_height > 0):
            return cls('AD', dcode, 'O', ([width, height, hole_width, hole_height],))
        return cls('AD', dcode, 'O', ([width, height],))

    @classmethod
    def polygon(cls, dcode, diameter, num_vertices, rotation, hole_diameter=None, hole_width=None, hole_height=None):
        '''Create a polygon aperture definition statement'''
        if hole_diameter is not None and hole_diameter > 0:
            return cls('AD', dcode, 'P', ([diameter, num_vertices, rotation, hole_diameter],))
        elif (hole_width is not None and hole_width > 0
              and hole_height is not None and hole_height > 0):
            return cls('AD', dcode, 'P', ([diameter, num_vertices, rotation, hole_width, hole_height],))
        return cls('AD', dcode, 'P', ([diameter, num_vertices, rotation],))


    @classmethod
    def macro(cls, dcode, name):
        return cls('AD', dcode, name, '')

    @classmethod
    def from_dict(cls, stmt_dict):
        param = stmt_dict.get('param')
        d = int(stmt_dict.get('d'))
        shape = stmt_dict.get('shape')
        modifiers = stmt_dict.get('modifiers')
        return cls(param, d, shape, modifiers)

    def __init__(self, param, d, shape, modifiers):
        """ Initialize ADParamStmt class

        Parameters
        ----------
        param : string
            Parameter code

        d : int
            Aperture D-code

        shape : string
            aperture name

        modifiers : list of lists of floats
            Shape modifiers

        Returns
        -------
        ParamStmt : ADParamStmt
            Initialized ADParamStmt class.

        """
        ParamStmt.__init__(self, param)
        self.d = d
        self.shape = shape
        if isinstance(modifiers, tuple):
            self.modifiers = modifiers
        elif modifiers:
            self.modifiers = [tuple([float(x) for x in m.split("X") if len(x)])
                              for m in modifiers.split(",") if len(m)]
        else:
            self.modifiers = [tuple()]

    def to_inch(self):
        if self.units == 'metric':
            self.units = 'inch'
            self.modifiers = [tuple([inch(x) for x in modifier])
                              for modifier in self.modifiers]

    def to_metric(self):
        if self.units == 'inch':
            self.units = 'metric'
            self.modifiers = [tuple([metric(x) for x in modifier])
                              for modifier in self.modifiers]

    def to_gerber(self, settings=None):
        if any(self.modifiers):
            return '%ADD{0}{1},{2}*%'.format(self.d, self.shape, ','.join(['X'.join(["%.4g" % x for x in modifier]) for modifier in self.modifiers]))
        else:
            return '%ADD{0}{1}*%'.format(self.d, self.shape)

    def __str__(self):
        if self.shape == 'C':
            shape = 'circle'
        elif self.shape == 'R':
            shape = 'rectangle'
        elif self.shape == 'O':
            shape = 'obround'
        else:
            shape = self.shape

        return '<Aperture Definition: %d: %s>' % (self.d, shape)


class AMParamStmt(ParamStmt):
    """ AM - Aperture Macro Statement
    """

    @classmethod
    def from_dict(cls, stmt_dict, units):
        return cls(**stmt_dict, units=units)

    def __init__(self, param, name, macro, units):
        """ Initialize AMParamStmt class

        Parameters
        ----------
        param : string
            Parameter code

        name : string
            Aperture macro name

        macro : string
            Aperture macro string

        Returns
        -------
        ParamStmt : AMParamStmt
            Initialized AMParamStmt class.

        """
        ParamStmt.__init__(self, param)
        self.name = name
        self.macro = macro
        self.units = units
        self.primitives = list(eval_macro(read_macro(macro), units))

    @classmethod
    def circle(cls, name, units):
        return cls('AM', name, '1,1,$1,0,0,0*1,0,$2,0,0,0', units)

    @classmethod
    def rectangle(cls, name, units):
        return cls('AM', name, '21,1,$1,$2,0,0,0*1,0,$3,0,0,0', units)
    
    @classmethod
    def landscape_obround(cls, name, units):
        return cls(
            'AM', name,
            '$4=$1-$2*'
            '$5=$1-$4*'
            '21,1,$5,$2,0,0,0*'
            '1,1,$4,$4/2,0,0*'
            '1,1,$4,-$4/2,0,0*'
            '1,0,$3,0,0,0', units)

    @classmethod
    def portrate_obround(cls, name, units):
        return cls(
            'AM', name,
            '$4=$2-$1*'
            '$5=$2-$4*'
            '21,1,$1,$5,0,0,0*'
            '1,1,$4,0,$4/2,0*'
            '1,1,$4,0,-$4/2,0*'
            '1,0,$3,0,0,0', units)
    
    @classmethod
    def polygon(cls, name, units):
        return cls('AM', name, '5,1,$2,0,0,$1,$3*1,0,$4,0,0,0', units)

    def to_gerber(self, unit=None):
        primitive_defs = '\n'.join(primitive.to_gerber(unit=unit) for primitive in self.primitives)
        return f'%AM{self.name}*\n{primitive_defs}%'

    def rotate(self, angle, center=None):
        for primitive_def in self.primitives:
            primitive_def.rotate(angle, center)

    def __str__(self):
        return '<AM Aperture Macro %s: %s>' % (self.name, self.macro)


class AxisSelectionStmt(ParamStmt):
    """ AS - Axis Selection Statement. (Deprecated) """

    def to_gerber(self, settings):
        return f'%AS{settings.output_axes}*%'

    def __str__(self):
        return '<AS Axis Select>'

class ImagePolarityStmt(ParamStmt):
    """ IP - Image Polarity Statement. (Deprecated) """

    def to_gerber(self, settings):
        ip = 'POS' if settings.image_polarity == 'positive' else 'NEG'
        return f'%IP{ip}*%'

    def __str__(self):
        return '<IP Image Polarity>'


class ImageRotationStmt(ParamStmt):
    """ IR - Image Rotation Statement. (Deprecated) """

    def to_gerber(self, settings):
        return f'%IR{settings.image_rotation}*%'

    def __str__(self):
        return '<IR Image Rotation>'

class MirrorImageStmt(ParamStmt):
    """ MI - Mirror Image Statement. (Deprecated) """

    def to_gerber(self, settings):
        return f'%SFA{int(bool(settings.mirror_image[0]))}B{int(bool(settings.mirror_image[1]))}*%'

    def __str__(self):
        return '<MI Mirror Image>'

class OffsetStmt(ParamStmt):
    """ OF - File Offset Statement. (Deprecated) """

    def __init__(self, a, b):
        self.a, self.b = a, b

    def to_gerber(self, settings=None):
        # FIXME unit conversion
        return f'%OFA{decimal_string(self.a, precision=5)}B{decimal_string(self.b, precision=5)}*%'

    def __str__(self):
        return f'<OF Offset a={self.a} b={self.b}>'


class SFParamStmt(ParamStmt):
    """ SF - Scale Factor Statement. (Deprecated) """

    def __init__(self, a, b):
        self.a, self.b = a, b

    def to_gerber(self, settings=None):
        return '%SFA{decimal_string(self.a, precision=5)}B{decimal_string(self.b, precision=5)}*%'

    def __str__(self):
        return '<SF Scale Factor>'

class CoordStmt(Statement):
    """ D01 - D03 operation statements """

    def __init__(self, x, y, i, j):
        self.x = x
        self.y = y
        self.i = i
        self.j = j

    @classmethod
    def move(cls, func, point):
        if point:
            return cls(func, point[0], point[1], None, None, CoordStmt.OP_MOVE, None)
        # No point specified, so just write the function. This is normally for ending a region (D02*)
        return cls(func, None, None, None, None, CoordStmt.OP_MOVE, None)

    @classmethod
    def line(cls, func, point):
        return cls(func, point[0], point[1], None, None, CoordStmt.OP_DRAW, None)

    @classmethod
    def mode(cls, func):
        return cls(func, None, None, None, None, None, None)

    @classmethod
    def arc(cls, func, point, center):
        return cls(func, point[0], point[1], center[0], center[1], CoordStmt.OP_DRAW, None)

    @classmethod
    def flash(cls, point):
        if point:
            return cls(None, point[0], point[1], None, None, CoordStmt.OP_FLASH, None)
        else:
            return cls(None, None, None, None, None, CoordStmt.OP_FLASH, None)

    def to_gerber(self, settings=None):
        ret = ''
        if self.x is not None:
            ret += 'X{0}'.format(write_gerber_value(self.x, settings.format, settings.zero_suppression))
        if self.y is not None:
            ret += 'Y{0}'.format(write_gerber_value(self.y, settings.format, settings.zero_suppression))
        if self.i is not None:
            ret += 'I{0}'.format(write_gerber_value(self.i, settings.format, settings.zero_suppression))
        if self.j is not None:
            ret += 'J{0}'.format(write_gerber_value(self.j, settings.format, settings.zero_suppression))
        if self.op:
            ret += self.op
        return ret + '*'

    def offset(self, x_offset=0, y_offset=0):
        if self.x is not None:
            self.x += x_offset
        if self.y is not None:
            self.y += y_offset
        if self.i is not None:
            self.i += x_offset
        if self.j is not None:
            self.j += y_offset

    def __str__(self):
        coord_str = ''
        if self.function:
            coord_str += 'Fn: %s ' % self.function
        if self.x is not None:
            coord_str += 'X: %g ' % self.x
        if self.y is not None:
            coord_str += 'Y: %g ' % self.y
        if self.i is not None:
            coord_str += 'I: %g ' % self.i
        if self.j is not None:
            coord_str += 'J: %g ' % self.j
        if self.op:
            if self.op == 'D01':
                op = 'Lights On'
            elif self.op == 'D02':
                op = 'Lights Off'
            elif self.op == 'D03':
                op = 'Flash'
            else:
                op = self.op
            coord_str += 'Op: %s' % op

        return '<Coordinate Statement: %s>' % coord_str

    @property
    def only_function(self):
        """
        Returns if the statement only set the function.
        """

        # TODO I would like to refactor this so that the function is handled separately and then
        # TODO this isn't required
        return self.function != None and self.op == None and self.x == None and self.y == None and self.i == None and self.j == None

class InterpolateStmt(CoordStmt):
    """ D01 interpolation operation """
    code = 'D01'

class MoveStmt(CoordStmt):
    """ D02 move operation """
    code = 'D02'

class FlashStmt(CoordStmt):
    """ D03 flash operation """
    code = 'D03'

class InterpolationStmt(Statement):
    """ G01 / G02 / G03 interpolation mode statement """
    def to_gerber(self, **_kwargs):
        return self.code + '*'

    def __str__(self):
        return f'<{self.__doc__.strip()}>'

class LinearModeStmt(InterpolationStmt):
    """ G01 linear interpolation mode statement """
    code = 'G01'

class CircularCWModeStmt(InterpolationStmt):
    """ G02 circular interpolation mode statement """
    code = 'G02'

class CircularCCWModeStmt(InterpolationStmt):
    """ G03 circular interpolation mode statement """
    code = 'G03'

class SingleQuadrantModeStmt(InterpolationStmt):
    """ G75 single-quadrant arc interpolation mode statement """
    code = 'G75'

class MultiQuadrantModeStmt(InterpolationStmt):
    """ G74 multi-quadrant arc interpolation mode statement """
    code = 'G74'

class RegionStartStatement(InterpolationStmt):
    """ G36 Region Mode Start Statement. """
    code = 'G36'

class RegionEndStatement(InterpolationStmt):
    """ G37 Region Mode End Statement. """
    code = 'G37'

class ApertureStmt(Statement):
    def __init__(self, d):
        self.d = int(d)
        self.deprecated = True if deprecated is not None and deprecated is not False else False

    def to_gerber(self, settings=None):
        if self.deprecated:
            return 'G54D{0}*'.format(self.d)
        else:
            return 'D{0}*'.format(self.d)

    def __str__(self):
        return '<Aperture: %d>' % self.d


class CommentStmt(Statement):
    """ G04 Comment Statment """

    def __init__(self, comment):
        self.comment = comment if comment is not None else ""

    def to_gerber(self, settings=None):
        return f'G04{self.comment}*'

    def __str__(self):
        return f'<G04 Comment: {self.comment}>'


class EofStmt(Statement):
    """ M02 EOF Statement """

    def __init__(self):
        Statement.__init__(self, "EOF")

    def to_gerber(self, settings=None):
        return 'M02*'

    def __str__(self):
        return '<M02 EOF Statement>'

class UnknownStmt(Statement):
    def __init__(self, line):
        self.line = line

    def to_gerber(self, settings):
        return self.line

    def __str__(self):
        return f'<Unknown Statement: "{self.line}">'