summaryrefslogtreecommitdiff
path: root/gerbonara/gerber/gerber_statements.py
diff options
context:
space:
mode:
Diffstat (limited to 'gerbonara/gerber/gerber_statements.py')
-rw-r--r--gerbonara/gerber/gerber_statements.py890
1 files changed, 128 insertions, 762 deletions
diff --git a/gerbonara/gerber/gerber_statements.py b/gerbonara/gerber/gerber_statements.py
index 9296d1b..d2f67c1 100644
--- a/gerbonara/gerber/gerber_statements.py
+++ b/gerbonara/gerber/gerber_statements.py
@@ -29,7 +29,7 @@ from .am_primitive import eval_macro
from .primitives import AMGroup
-class Statement(object):
+class Statement:
""" Gerber statement Base class
The statement class provides a type attribute.
@@ -45,10 +45,6 @@ class Statement(object):
String identifying the statement type.
"""
- def __init__(self, stype, units='inch'):
- self.type = stype
- self.units = units
-
def __str__(self):
s = "<{0} ".format(self.__class__.__name__)
@@ -58,12 +54,6 @@ class Statement(object):
s = s.rstrip() + ">"
return s
- def to_inch(self):
- self.units = 'inch'
-
- def to_metric(self):
- self.units = 'metric'
-
def offset(self, x_offset=0, y_offset=0):
pass
@@ -72,201 +62,51 @@ class Statement(object):
class ParamStmt(Statement):
- """ Gerber parameter statement Base class
-
- The parameter statement class provides a parameter type attribute.
-
- Parameters
- ----------
- param : string
- two-character code identifying the parameter statement type.
-
- Attributes
- ----------
- param : string
- Parameter type code
- """
-
- def __init__(self, param):
- Statement.__init__(self, "PARAM")
- self.param = param
-
-
-class FSParamStmt(ParamStmt):
- """ FS - Gerber Format Specification Statement
- """
+ pass
- @classmethod
- def from_settings(cls, settings):
+class FormatSpecStmt(ParamStmt):
+ """ FS - Gerber Format Specification Statement """
+ code = 'FS'
- return cls('FS', settings.zero_suppression, settings.notation, settings.format)
+ 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])
- @classmethod
- def from_dict(cls, stmt_dict):
- """
- """
- param = stmt_dict.get('param')
-
- if stmt_dict.get('zero') == 'L':
- zeros = 'leading'
- elif stmt_dict.get('zero') == 'T':
- zeros = 'trailing'
- else:
- zeros = 'none'
-
- notation = 'absolute' if stmt_dict.get('notation') == 'A' else 'incremental'
- fmt = tuple(map(int, stmt_dict.get('x')))
- return cls(param, zeros, notation, fmt)
-
- def __init__(self, param, zero_suppression='leading',
- notation='absolute', format=(2, 4)):
- """ Initialize FSParamStmt class
-
- .. note::
- The FS command specifies the format of the coordinate data. It
- must only be used once at the beginning of a file. It must be
- specified before the first use of coordinate data.
-
- Parameters
- ----------
- param : string
- Parameter.
-
- zero_suppression : string
- Zero-suppression mode. May be either 'leading', 'trailing' or 'none' (all zeros are present)
-
- notation : string
- Notation mode. May be either 'absolute' or 'incremental'
-
- format : tuple (int, int)
- Gerber precision format expressed as a tuple containing:
- (number of integer-part digits, number of decimal-part digits)
-
- Returns
- -------
- ParamStmt : FSParamStmt
- Initialized FSParamStmt class.
-
- """
- ParamStmt.__init__(self, param)
- self.zero_suppression = zero_suppression
- self.notation = notation
- self.format = format
-
- def to_gerber(self, settings=None):
- if settings:
- zero_suppression = 'L' if settings.zero_suppression == 'leading' else 'T'
- notation = 'A' if settings.notation == 'absolute' else 'I'
- fmt = ''.join(map(str, settings.format))
- else:
- zero_suppression = 'L' if self.zero_suppression == 'leading' else 'T'
- notation = 'A' if self.notation == 'absolute' else 'I'
- fmt = ''.join(map(str, self.format))
-
- return '%FS{0}{1}X{2}Y{3}*%'.format(zero_suppression, notation, fmt, fmt)
+ return f'%FS{zeros}{notation}X{number_format}Y{number_format}*%'
def __str__(self):
- return ('<Format Spec: %d:%d %s zero suppression %s notation>' %
- (self.format[0], self.format[1], self.zero_suppression, self.notation))
+ return '<FS Format Specification>'
-class MOParamStmt(ParamStmt):
- """ MO - Gerber Mode (measurement units) Statement.
- """
+class UnitStmt(ParamStmt):
+ """ MO - Coordinate unit mode statement """
- @classmethod
- def from_units(cls, units):
- return cls(None, units)
-
- @classmethod
- def from_dict(cls, stmt_dict):
- param = stmt_dict.get('param')
- if stmt_dict.get('mo') is None:
- mo = None
- elif stmt_dict.get('mo').lower() not in ('in', 'mm'):
- raise ValueError('Mode may be mm or in')
- elif stmt_dict.get('mo').lower() == 'in':
- mo = 'inch'
- else:
- mo = 'metric'
- return cls(param, mo)
-
- def __init__(self, param, mo):
- """ Initialize MOParamStmt class
-
- Parameters
- ----------
- param : string
- Parameter.
-
- mo : string
- Measurement units. May be either 'inch' or 'metric'
-
- Returns
- -------
- ParamStmt : MOParamStmt
- Initialized MOParamStmt class.
-
- """
- ParamStmt.__init__(self, param)
- self.mode = mo
-
- def to_gerber(self, settings=None):
- mode = 'MM' if self.mode == 'metric' else 'IN'
- return '%MO{0}*%'.format(mode)
-
- def to_inch(self):
- self.mode = 'inch'
-
- def to_metric(self):
- self.mode = 'metric'
+ def to_gerber(self, settings):
+ return '%MOMM*%' if settings.units == 'mm' else '%MOIN*%'
def __str__(self):
- mode_str = 'millimeters' if self.mode == 'metric' else 'inches'
- return ('<Mode: %s>' % mode_str)
-
-
-class LPParamStmt(ParamStmt):
- """ LP - Gerber Level Polarity statement
- """
-
- @classmethod
- def from_dict(cls, stmt_dict):
- param = stmt_dict['param']
- lp = 'clear' if stmt_dict.get('lp') == 'C' else 'dark'
- return cls(param, lp)
+ return ('<MO Coordinate unit mode statement>' % mode_str)
- def __init__(self, param, lp):
- """ Initialize LPParamStmt class
- Parameters
- ----------
- param : string
- Parameter
+class LoadPolarityStmt(ParamStmt):
+ """ LP - Gerber Load Polarity statement """
- lp : string
- Level polarity. May be either 'clear' or 'dark'
-
- Returns
- -------
- ParamStmt : LPParamStmt
- Initialized LPParamStmt class.
-
- """
- ParamStmt.__init__(self, param)
- self.lp = lp
+ def __init__(self, dark):
+ self.dark = dark
def to_gerber(self, settings=None):
- lp = 'C' if self.lp == 'clear' else 'D'
- return '%LP{0}*%'.format(lp)
+ lp = 'D' if self.dark else 'C'
+ return f'%LP{lp}*%'
def __str__(self):
- return '<Level Polarity: %s>' % self.lp
+ lp = 'dark' if self.dark else 'clear'
+ return f'<LP Level Polarity: {lp}>'
class ADParamStmt(ParamStmt):
- """ AD - Gerber Aperture Definition Statement
- """
+ """ AD - Aperture Definition Statement """
@classmethod
def rect(cls, dcode, width, height, hole_diameter=None, hole_width=None, hole_height=None):
@@ -418,10 +258,7 @@ class AMParamStmt(ParamStmt):
self.name = name
self.macro = macro
self.units = units
- self.primitives = list(eval_macro(self.instructions))
-
- def read(self, macro):
- return read_macro(macro)
+ self.primitives = list(eval_macro(read_macro(macro), units))
@classmethod
def circle(cls, name, units):
@@ -457,20 +294,8 @@ class AMParamStmt(ParamStmt):
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_inch(self):
- if self.units == 'metric':
- self.units = 'inch'
- for primitive in self.primitives:
- primitive.to_inch()
-
- def to_metric(self):
- if self.units == 'inch':
- self.units = 'metric'
- for primitive in self.primitives:
- primitive.to_metric()
-
- def to_gerber(self, settings=None):
- primitive_defs = '\n'.join(primitive.to_gerber() for primitive in self.primitives)
+ 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):
@@ -478,444 +303,81 @@ class AMParamStmt(ParamStmt):
primitive_def.rotate(angle, center)
def __str__(self):
- return '<Aperture Macro %s: %s>' % (self.name, self.macro)
-
-
-class ASParamStmt(ParamStmt):
- """ AS - Axis Select. (Deprecated) """
- @classmethod
- def from_dict(cls, stmt_dict):
- param = stmt_dict.get('param')
- mode = stmt_dict.get('mode')
- return cls(param, mode)
+ return '<AM Aperture Macro %s: %s>' % (self.name, self.macro)
- def __init__(self, param, mode):
- """ Initialize ASParamStmt class
- Parameters
- ----------
- param : string
- Parameter string.
+class AxisSelectionStmt(ParamStmt):
+ """ AS - Axis Selection Statement. (Deprecated) """
- mode : string
- Axis select. May be either 'AXBY' or 'AYBX'
-
- Returns
- -------
- ParamStmt : ASParamStmt
- Initialized ASParamStmt class.
-
- """
- ParamStmt.__init__(self, param)
- self.mode = mode
-
- def to_gerber(self, settings=None):
- return '%AS{0}*%'.format(self.mode)
+ def to_gerber(self, settings):
+ return f'%AS{settings.output_axes}*%'
def __str__(self):
- return ('<Axis Select: %s>' % self.mode)
-
-
-class INParamStmt(ParamStmt):
- """ IN - Image Name Statement (Deprecated)
- """
- @classmethod
- def from_dict(cls, stmt_dict):
- return cls(**stmt_dict)
-
- def __init__(self, param, name):
- """ Initialize INParamStmt class
-
- Parameters
- ----------
- param : string
- Parameter code
+ return '<AS Axis Select>'
- name : string
- Image name
+class ImagePolarityStmt(ParamStmt):
+ """ IP - Image Polarity Statement. (Deprecated) """
- Returns
- -------
- ParamStmt : INParamStmt
- Initialized INParamStmt class.
-
- """
- ParamStmt.__init__(self, param)
- self.name = name
-
- def to_gerber(self, settings=None):
- return '%IN{0}*%'.format(self.name)
+ def to_gerber(self, settings):
+ ip = 'POS' if settings.image_polarity == 'positive' else 'NEG'
+ return f'%IP{ip}*%'
def __str__(self):
- return '<Image Name: %s>' % self.name
-
-
-class IPParamStmt(ParamStmt):
- """ IP - Gerber Image Polarity Statement. (Deprecated)
- """
- @classmethod
- def from_dict(cls, stmt_dict):
- param = stmt_dict.get('param')
- ip = 'positive' if stmt_dict.get('ip') == 'POS' else 'negative'
- return cls(param, ip)
+ return '<IP Image Polarity>'
- def __init__(self, param, ip):
- """ Initialize IPParamStmt class
- Parameters
- ----------
- param : string
- Parameter string.
-
- ip : string
- Image polarity. May be either'positive' or 'negative'
-
- Returns
- -------
- ParamStmt : IPParamStmt
- Initialized IPParamStmt class.
-
- """
- ParamStmt.__init__(self, param)
- self.ip = ip
+class ImageRotationStmt(ParamStmt):
+ """ IR - Image Rotation Statement. (Deprecated) """
- def to_gerber(self, settings=None):
- ip = 'POS' if self.ip == 'positive' else 'NEG'
- return '%IP{0}*%'.format(ip)
+ def to_gerber(self, settings):
+ return f'%IR{settings.image_rotation}*%'
def __str__(self):
- return ('<Image Polarity: %s>' % self.ip)
-
-
-class IRParamStmt(ParamStmt):
- """ IR - Image Rotation Param (Deprecated)
- """
- @classmethod
- def from_dict(cls, stmt_dict):
- angle = int(stmt_dict['angle'])
- return cls(stmt_dict['param'], angle)
-
- def __init__(self, param, angle):
- """ Initialize IRParamStmt class
-
- Parameters
- ----------
- param : string
- Parameter code
-
- angle : int
- Image angle
+ return '<IR Image Rotation>'
- Returns
- -------
- ParamStmt : IRParamStmt
- Initialized IRParamStmt class.
-
- """
- ParamStmt.__init__(self, param)
- self.angle = angle
+class MirrorImageStmt(ParamStmt):
+ """ MI - Mirror Image Statement. (Deprecated) """
- def to_gerber(self, settings=None):
- return '%IR{0}*%'.format(self.angle)
+ 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 '<Image Angle: %s>' % self.angle
+ return '<MI Mirror Image>'
+class OffsetStmt(ParamStmt):
+ """ OF - File Offset Statement. (Deprecated) """
-class MIParamStmt(ParamStmt):
- """ MI - Image Mirror Param (Deprecated)
- """
- @classmethod
- def from_dict(cls, stmt_dict):
- param = stmt_dict.get('param')
- a = int(stmt_dict.get('a', 0))
- b = int(stmt_dict.get('b', 0))
- return cls(param, a, b)
-
- def __init__(self, param, a, b):
- """ Initialize MIParamStmt class
-
- Parameters
- ----------
- param : string
- Parameter code
-
- a : int
- Mirror for A output devices axis (0=disabled, 1=mirrored)
-
- b : int
- Mirror for B output devices axis (0=disabled, 1=mirrored)
-
- Returns
- -------
- ParamStmt : MIParamStmt
- Initialized MIParamStmt class.
-
- """
- ParamStmt.__init__(self, param)
- self.a = a
- self.b = b
-
- def to_gerber(self, settings=None):
- ret = "%MI"
- if self.a is not None:
- ret += "A{0}".format(self.a)
- if self.b is not None:
- ret += "B{0}".format(self.b)
- ret += "*%"
- return ret
-
- def __str__(self):
- return '<Image Mirror: A=%d B=%d>' % (self.a, self.b)
-
-
-class OFParamStmt(ParamStmt):
- """ OF - Gerber Offset statement (Deprecated)
- """
-
- @classmethod
- def from_dict(cls, stmt_dict):
- param = stmt_dict.get('param')
- a = float(stmt_dict.get('a', 0))
- b = float(stmt_dict.get('b', 0))
- return cls(param, a, b)
-
- def __init__(self, param, a, b):
- """ Initialize OFParamStmt class
-
- Parameters
- ----------
- param : string
- Parameter
-
- a : float
- Offset along the output device A axis
-
- b : float
- Offset along the output device B axis
-
- Returns
- -------
- ParamStmt : OFParamStmt
- Initialized OFParamStmt class.
-
- """
- ParamStmt.__init__(self, param)
- self.a = a
- self.b = b
+ def __init__(self, a, b):
+ self.a, self.b = a, b
def to_gerber(self, settings=None):
- ret = '%OF'
- if self.a is not None:
- ret += 'A' + decimal_string(self.a, precision=5)
- if self.b is not None:
- ret += 'B' + decimal_string(self.b, precision=5)
- return ret + '*%'
-
- def to_inch(self):
- if self.units == 'metric':
- self.units = 'inch'
- if self.a is not None:
- self.a = inch(self.a)
- if self.b is not None:
- self.b = inch(self.b)
-
- def to_metric(self):
- if self.units == 'inch':
- self.units = 'metric'
- if self.a is not None:
- self.a = metric(self.a)
- if self.b is not None:
- self.b = metric(self.b)
-
- def offset(self, x_offset=0, y_offset=0):
- if self.a is not None:
- self.a += x_offset
- if self.b is not None:
- self.b += y_offset
+ # FIXME unit conversion
+ return f'%OFA{decimal_string(self.a, precision=5)}B{decimal_string(self.b, precision=5)}*%'
def __str__(self):
- offset_str = ''
- if self.a is not None:
- offset_str += ('X: %f ' % self.a)
- if self.b is not None:
- offset_str += ('Y: %f ' % self.b)
- return ('<Offset: %s>' % offset_str)
+ return f'<OF Offset a={self.a} b={self.b}>'
class SFParamStmt(ParamStmt):
- """ SF - Scale Factor Param (Deprecated)
- """
-
- @classmethod
- def from_dict(cls, stmt_dict):
- param = stmt_dict.get('param')
- a = float(stmt_dict.get('a', 1))
- b = float(stmt_dict.get('b', 1))
- return cls(param, a, b)
-
- def __init__(self, param, a, b):
- """ Initialize OFParamStmt class
-
- Parameters
- ----------
- param : string
- Parameter
-
- a : float
- Scale factor for the output device A axis
-
- b : float
- Scale factor for the output device B axis
-
- Returns
- -------
- ParamStmt : SFParamStmt
- Initialized SFParamStmt class.
-
- """
- ParamStmt.__init__(self, param)
- self.a = a
- self.b = b
-
- def to_gerber(self, settings=None):
- ret = '%SF'
- if self.a is not None:
- ret += 'A' + decimal_string(self.a, precision=5)
- if self.b is not None:
- ret += 'B' + decimal_string(self.b, precision=5)
- return ret + '*%'
-
- def to_inch(self):
- if self.units == 'metric':
- self.units = 'inch'
- if self.a is not None:
- self.a = inch(self.a)
- if self.b is not None:
- self.b = inch(self.b)
-
- def to_metric(self):
- if self.units == 'inch':
- self.units = 'metric'
- if self.a is not None:
- self.a = metric(self.a)
- if self.b is not None:
- self.b = metric(self.b)
-
- def offset(self, x_offset=0, y_offset=0):
- if self.a is not None:
- self.a += x_offset
- if self.b is not None:
- self.b += y_offset
-
- def __str__(self):
- scale_factor = ''
- if self.a is not None:
- scale_factor += ('X: %g ' % self.a)
- if self.b is not None:
- scale_factor += ('Y: %g' % self.b)
- return ('<Scale Factor: %s>' % scale_factor)
-
-
-class LNParamStmt(ParamStmt):
- """ LN - Level Name Statement (Deprecated)
- """
- @classmethod
- def from_dict(cls, stmt_dict):
- return cls(**stmt_dict)
-
- def __init__(self, param, name):
- """ Initialize LNParamStmt class
-
- Parameters
- ----------
- param : string
- Parameter code
-
- name : string
- Level name
-
- Returns
- -------
- ParamStmt : LNParamStmt
- Initialized LNParamStmt class.
-
- """
- ParamStmt.__init__(self, param)
- self.name = name
-
- def to_gerber(self, settings=None):
- return '%LN{0}*%'.format(self.name)
-
- def __str__(self):
- return '<Level Name: %s>' % self.name
-
+ """ SF - Scale Factor Statement. (Deprecated) """
-class DeprecatedStmt(Statement):
- """ Unimportant deprecated statement, will be parsed but not emitted.
- """
- @classmethod
- def from_gerber(cls, line):
- return cls(line)
-
- def __init__(self, line):
- """ Initialize DeprecatedStmt class
-
- Parameters
- ----------
- line : string
- Deprecated statement text
-
- Returns
- -------
- DeprecatedStmt
- Initialized DeprecatedStmt class.
-
- """
- Statement.__init__(self, "DEPRECATED")
- self.line = line
+ def __init__(self, a, b):
+ self.a, self.b = a, b
def to_gerber(self, settings=None):
- return self.line
+ return '%SFA{decimal_string(self.a, precision=5)}B{decimal_string(self.b, precision=5)}*%'
def __str__(self):
- return '<Deprecated Statement: \'%s\'>' % self.line
-
+ return '<SF Scale Factor>'
class CoordStmt(Statement):
- """ Coordinate Data Block
- """
-
- OP_DRAW = 'D01'
- OP_MOVE = 'D02'
- OP_FLASH = 'D03'
-
- FUNC_LINEAR = 'G01'
- FUNC_ARC_CW = 'G02'
- FUNC_ARC_CCW = 'G03'
+ """ D01 - D03 operation statements """
- @classmethod
- def from_dict(cls, stmt_dict, settings):
- function = stmt_dict['function']
- x = stmt_dict.get('x')
- y = stmt_dict.get('y')
- i = stmt_dict.get('i')
- j = stmt_dict.get('j')
- op = stmt_dict.get('op')
-
- if x is not None:
- x = parse_gerber_value(stmt_dict.get('x'), settings.format,
- settings.zero_suppression)
- if y is not None:
- y = parse_gerber_value(stmt_dict.get('y'), settings.format,
- settings.zero_suppression)
- if i is not None:
- i = parse_gerber_value(stmt_dict.get('i'), settings.format,
- settings.zero_suppression)
- if j is not None:
- j = parse_gerber_value(stmt_dict.get('j'), settings.format,
- settings.zero_suppression)
- return cls(function, x, y, i, j, op, settings)
+ 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):
@@ -943,94 +405,20 @@ class CoordStmt(Statement):
else:
return cls(None, None, None, None, None, CoordStmt.OP_FLASH, None)
- def __init__(self, function, x, y, i, j, op, settings):
- """ Initialize CoordStmt class
-
- Parameters
- ----------
- function : string
- function
-
- x : float
- X coordinate
-
- y : float
- Y coordinate
-
- i : float
- Coordinate offset in the X direction
-
- j : float
- Coordinate offset in the Y direction
-
- op : string
- Operation code
-
- settings : dict {'zero_suppression', 'format'}
- Gerber file coordinate format
-
- Returns
- -------
- Statement : CoordStmt
- Initialized CoordStmt class.
-
- """
- Statement.__init__(self, "COORD")
- self.function = function
- self.x = x
- self.y = y
- self.i = i
- self.j = j
- self.op = op
-
def to_gerber(self, settings=None):
ret = ''
- if self.function:
- ret += self.function
if self.x is not None:
- ret += 'X{0}'.format(write_gerber_value(self.x, settings.format,
- settings.zero_suppression))
+ 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))
+ 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))
+ 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))
+ ret += 'J{0}'.format(write_gerber_value(self.j, settings.format, settings.zero_suppression))
if self.op:
ret += self.op
return ret + '*'
- def to_inch(self):
- if self.units == 'metric':
- self.units = 'inch'
- if self.x is not None:
- self.x = inch(self.x)
- if self.y is not None:
- self.y = inch(self.y)
- if self.i is not None:
- self.i = inch(self.i)
- if self.j is not None:
- self.j = inch(self.j)
- if self.function == "G71":
- self.function = "G70"
-
- def to_metric(self):
- if self.units == 'inch':
- self.units = 'metric'
- if self.x is not None:
- self.x = metric(self.x)
- if self.y is not None:
- self.y = metric(self.y)
- if self.i is not None:
- self.i = metric(self.i)
- if self.j is not None:
- self.j = metric(self.j)
- if self.function == "G70":
- self.function = "G71"
-
def offset(self, x_offset=0, y_offset=0):
if self.x is not None:
self.x += x_offset
@@ -1076,13 +464,56 @@ class CoordStmt(Statement):
# 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 ApertureStmt(Statement):
- """ Aperture Statement
- """
+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'
- def __init__(self, d, deprecated=None):
- Statement.__init__(self, "APERTURE")
+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
@@ -1097,23 +528,20 @@ class ApertureStmt(Statement):
class CommentStmt(Statement):
- """ Comment Statment
- """
+ """ G04 Comment Statment """
def __init__(self, comment):
- Statement.__init__(self, "COMMENT")
self.comment = comment if comment is not None else ""
def to_gerber(self, settings=None):
- return 'G04{0}*'.format(self.comment)
+ return f'G04{self.comment}*'
def __str__(self):
- return '<Comment: %s>' % self.comment
+ return f'<G04 Comment: {self.comment}>'
class EofStmt(Statement):
- """ EOF Statement
- """
+ """ M02 EOF Statement """
def __init__(self):
Statement.__init__(self, "EOF")
@@ -1122,76 +550,14 @@ class EofStmt(Statement):
return 'M02*'
def __str__(self):
- return '<EOF Statement>'
-
-
-class QuadrantModeStmt(Statement):
-
- @classmethod
- def single(cls):
- return cls('single-quadrant')
-
- @classmethod
- def multi(cls):
- return cls('multi-quadrant')
-
- @classmethod
- def from_gerber(cls, line):
- if 'G74' not in line and 'G75' not in line:
- raise ValueError('%s is not a valid quadrant mode statement'
- % line)
- return (cls('single-quadrant') if line[:3] == 'G74'
- else cls('multi-quadrant'))
-
- def __init__(self, mode):
- super(QuadrantModeStmt, self).__init__('QuadrantMode')
- mode = mode.lower()
- if mode not in ['single-quadrant', 'multi-quadrant']:
- raise ValueError('Quadrant mode must be "single-quadrant" \
- or "multi-quadrant"')
- self.mode = mode
-
- def to_gerber(self, settings=None):
- return 'G74*' if self.mode == 'single-quadrant' else 'G75*'
-
-
-class RegionModeStmt(Statement):
-
- @classmethod
- def from_gerber(cls, line):
- if 'G36' not in line and 'G37' not in line:
- raise ValueError('%s is not a valid region mode statement' % line)
- return (cls('on') if line[:3] == 'G36' else cls('off'))
-
- @classmethod
- def on(cls):
- return cls('on')
-
- @classmethod
- def off(cls):
- return cls('off')
-
- def __init__(self, mode):
- super(RegionModeStmt, self).__init__('RegionMode')
- mode = mode.lower()
- if mode not in ['on', 'off']:
- raise ValueError('Valid modes are "on" or "off"')
- self.mode = mode
-
- def to_gerber(self, settings=None):
- return 'G36*' if self.mode == 'on' else 'G37*'
-
+ return '<M02 EOF Statement>'
class UnknownStmt(Statement):
- """ Unknown Statement
- """
-
def __init__(self, line):
- Statement.__init__(self, "UNKNOWN")
self.line = line
- def to_gerber(self, settings=None):
+ def to_gerber(self, settings):
return self.line
def __str__(self):
- return '<Unknown Statement: \'%s\'>' % self.line
+ return f'<Unknown Statement: "{self.line}">'