diff options
Diffstat (limited to 'gerbonara/gerber/panelize')
-rw-r--r-- | gerbonara/gerber/panelize/__init__.py | 1 | ||||
-rw-r--r-- | gerbonara/gerber/panelize/am_expression.py | 219 | ||||
-rw-r--r-- | gerbonara/gerber/panelize/am_primitive.py | 399 | ||||
-rw-r--r-- | gerbonara/gerber/panelize/common.py | 36 |
4 files changed, 0 insertions, 655 deletions
diff --git a/gerbonara/gerber/panelize/__init__.py b/gerbonara/gerber/panelize/__init__.py index 645b632..7185aea 100644 --- a/gerbonara/gerber/panelize/__init__.py +++ b/gerbonara/gerber/panelize/__init__.py @@ -3,6 +3,5 @@ # Copyright 2019 Hiroshi Murayama <opiopan@gmail.com> -from .common import read, loads # , rectangle from .composition import GerberComposition, DrillComposition # from .dxf import DxfFile diff --git a/gerbonara/gerber/panelize/am_expression.py b/gerbonara/gerber/panelize/am_expression.py deleted file mode 100644 index a0a4114..0000000 --- a/gerbonara/gerber/panelize/am_expression.py +++ /dev/null @@ -1,219 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -# Copyright 2019 Hiroshi Murayama <opiopan@gmail.com> - -import operator - -from ..utils import * -from ..am_eval import OpCode -from ..am_statements import * - -class AMExpression(object): - CONSTANT = 1 - VARIABLE = 2 - OPERATOR = 3 - - def __init__(self, kind): - self.kind = kind - - @property - def value(self): - return self - - def optimize(self): - return self - - def to_inch(self): - return AMOperatorExpression.div(self, MILLIMETERS_PER_INCH) - - def to_metric(self): - return AMOperatorExpression.mul(self, MILLIMETERS_PER_INCH) - - #def to_gerber(self, settings=None): - # pass - - #def to_instructions(self): - # pass - -class AMConstantExpression(AMExpression): - def __init__(self, value): - super(AMConstantExpression, self).__init__(AMExpression.CONSTANT) - self._value = value - - @property - def value(self): - return self._value - - def __float__(self): - return float(self._value) - - @staticmethod - def _amex_val(other): - return float(other) if isinstance(other, AMConstantExpression) else other - - def __eq__(self, val): - return self._value == AMConstantExpression._amex_val(val) - - def __ne__(self, val): - return self._value != AMConstantExpression._amex_val(val) - - def __lt__(self, val): - return self._value < AMConstantExpression._amex_val(val) - - def __gt__(self, val): - return self._value > AMConstantExpression._amex_val(val) - - def __le__(self, val): - return self._value <= AMConstantExpression._amex_val(val) - - def __ge__(self, val): - return self._value >= AMConstantExpression._amex_val(val) - - def to_gerber(self, settings=None): - if isinstance(self._value, str): - return self._value - return f'{self.value:.6f}'.rstrip('0').rstrip('.') - - def to_instructions(self): - return [(OpCode.PUSH, self._value)] - -class AMVariableExpression(AMExpression): - def __init__(self, number): - super(AMVariableExpression, self).__init__(AMExpression.VARIABLE) - self.number = number - - def to_gerber(self, settings=None): - return f'${self.number}' - - def to_instructions(self): - return (OpCode.LOAD, self.number) - -class AMOperatorExpression(AMExpression): - def __init__(self, op, lvalue, rvalue): - super(AMOperatorExpression, self).__init__(AMExpression.OPERATOR) - self.op = op - self.lvalue = AMConstantExpression(lvalue) if isinstance(lvalue, (int, float)) else lvalue - self.rvalue = AMConstantExpression(rvalue) if isinstance(rvalue, (int, float)) else rvalue - - @classmethod - def add(kls, lvalue, rvalue): - return kls(operator.add, lvalue, rvalue) - - @classmethod - def sub(kls, lvalue, rvalue): - return kls(operator.sub, lvalue, rvalue) - - @classmethod - def mul(kls, lvalue, rvalue): - return kls(operator.mul, lvalue, rvalue) - - @classmethod - def div(kls, lvalue, rvalue): - return kls(operator.truediv, lvalue, rvalue) - - def optimize(self): - l = self.lvalue = self.lvalue.optimize() - r = self.rvalue = self.rvalue.optimize() - - if isinstance(l, AMConstantExpression) and isinstance(r, AMConstantExpression): - return AMConstantExpression(self.op(float(r), float(l))) - - elif self.op == operator.ADD: - if r == 0: - return l - elif l == 0: - return r - - elif self.op == operator.SUB: - if r == 0: - return l - elif l == 0 and isinstance(r, AMConstantExpression): - return AMConstantExpression(-float(r)) - - elif self.op == operator.MUL: - if r == 1: - return l - elif l == 1: - return r - elif l == 0 or r == 0: - return AMConstantExpression(0) - - elif self.op == operator.TRUEDIV: - if r == 1: - return self.lvalue - elif l == 0: - return AMConstantExpression(0) - - return self - - def to_gerber(self, settings=None): - lval = self.lvalue.to_gerber(settings) - rval = self.rvalue.to_gerber(settings)) - op = {AMOperatorExpression.ADD: '+', - AMOperatorExpression.SUB: '-', - AMOperatorExpression.MUL: 'x', - AMOperatorExpression.DIV: '/'} [self.op] - return '(' + lval + op + rval + ')' - - def to_instructions(self): - for i in self.lvalue.to_instructions(): - yield i - - for i in self.rvalue.to_instructions(): - yield i - - op = {AMOperatorExpression.ADD: OpCode.ADD, - AMOperatorExpression.SUB: OpCode.SUB, - AMOperatorExpression.MUL: OpCode.MUL, - AMOperatorExpression.DIV: OpCode.DIV} [self.op] - yield (op, None) - -def eval_macro(instructions): - stack = [] - - def pop(): - return stack.pop() - - def push(op): - stack.append(op) - - def top(): - return stack[-1] - - def empty(): - return len(stack) == 0 - - for opcode, argument in instructions: - if opcode == OpCode.PUSH: - push(AMConstantExpression(argument)) - - elif opcode == OpCode.LOAD: - push(AMVariableExpression(argument)) - - elif opcode == OpCode.STORE: - yield (-argument, [pop()]) - - elif opcode == OpCode.ADD: - op1 = pop() - op2 = pop() - push(AMOperatorExpression(AMOperatorExpression.ADD, op2, op1)) - - elif opcode == OpCode.SUB: - op1 = pop() - op2 = pop() - push(AMOperatorExpression(AMOperatorExpression.SUB, op2, op1)) - - elif opcode == OpCode.MUL: - op1 = pop() - op2 = pop() - push(AMOperatorExpression(AMOperatorExpression.MUL, op2, op1)) - - elif opcode == OpCode.DIV: - op1 = pop() - op2 = pop() - push(AMOperatorExpression(AMOperatorExpression.DIV, op2, op1)) - - elif opcode == OpCode.PRIM: - yield (argument, stack) - stack = [] diff --git a/gerbonara/gerber/panelize/am_primitive.py b/gerbonara/gerber/panelize/am_primitive.py deleted file mode 100644 index 5d8458b..0000000 --- a/gerbonara/gerber/panelize/am_primitive.py +++ /dev/null @@ -1,399 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -# Copyright 2019 Hiroshi Murayama <opiopan@gmail.com> - -from ..utils import * -from ..am_statements import * -from ..am_eval import OpCode - -from .am_expression import eval_macro, AMConstantExpression, AMOperatorExpression - -class AMCommentPrimitiveDef(AMPrimitiveDef): - @classmethod - def from_modifiers(cls, code, modifiers): - return cls(code, modifiers[0]) - - def __init__(self, code, comment): - super(AMCommentPrimitiveDef, self).__init__(code) - self.comment = comment - -class AMCirclePrimitiveDef(AMPrimitiveDef): - @classmethod - def from_modifiers(cls, code, modifiers): - exposure = 'on' if modifiers[0].value == 1 else 'off' - diameter = modifiers[1] - center_x = modifiers[2] - center_y = modifiers[3] - rotation = modifiers[4] if len(modifiers)>4 else AMConstantExpression(float(0)) - return cls(code, exposure, diameter, center_x, center_y, rotation) - - def __init__(self, code, exposure, diameter, center_x, center_y, rotation): - super(AMCirclePrimitiveDef, self).__init__(code, exposure, rotation) - self.diameter = diameter - self.center_x = center_x - self.center_y = center_y - - def to_inch(self): - self.diameter = self.diameter.to_inch().optimize() - self.center_x = self.center_x.to_inch().optimize() - self.center_y = self.center_y.to_inch().optimize() - - def to_metric(self): - self.diameter = self.diameter.to_metric().optimize() - self.center_x = self.center_x.to_metric().optimize() - self.center_y = self.center_y.to_metric().optimize() - - def to_gerber(self, settings=None): - data = dict(code = self.code, - exposure = 1 if self.exposure == 'on' else 0, - diameter = self.diameter.to_gerber(settings), - x = self.center_x.to_gerber(settings), - y = self.center_y.to_gerber(settings), - rotation = self.rotation.to_gerber(settings)) - return '{code},{exposure},{diameter},{x},{y},{rotation}*'.format(**data) - - def to_instructions(self): - yield (OpCode.PUSH, 1 if self.exposure == 'on' else 0) - for modifier in [self.diameter, self.center_x, self.center_y, self.rotation]: - for i in modifier.to_instructions(): - yield i - yield (OpCode.PRIM, self.code) - -class AMVectorLinePrimitiveDef(AMPrimitiveDef): - @classmethod - def from_modifiers(cls, code, modifiers): - code = code - exposure = 'on' if modifiers[0].value == 1 else 'off' - width = modifiers[1] - start_x = modifiers[2] - start_y = modifiers[3] - end_x = modifiers[4] - end_y = modifiers[5] - rotation = modifiers[6] - return cls(code, exposure, width, start_x, start_y, end_x, end_y, rotation) - - def __init__(self, code, exposure, width, start_x, start_y, end_x, end_y, rotation): - super(AMVectorLinePrimitiveDef, self).__init__(code, exposure, rotation) - self.width = width - self.start_x = start_x - self.start_y = start_y - self.end_x = end_x - self.end_y = end_y - - def to_inch(self): - self.width = self.width.to_inch().optimize() - self.start_x = self.start_x.to_inch().optimize() - self.start_y = self.start_y.to_inch().optimize() - self.end_x = self.end_x.to_inch().optimize() - self.end_y = self.end_y.to_inch().optimize() - - def to_metric(self): - self.width = self.width.to_metric().optimize() - self.start_x = self.start_x.to_metric().optimize() - self.start_y = self.start_y.to_metric().optimize() - self.end_x = self.end_x.to_metric().optimize() - self.end_y = self.end_y.to_metric().optimize() - - def to_gerber(self, settings=None): - data = dict(code = self.code, - exposure = 1 if self.exposure == 'on' else 0, - width = self.width.to_gerber(settings), - start_x = self.start_x.to_gerber(settings), - start_y = self.start_y.to_gerber(settings), - end_x = self.end_x.to_gerber(settings), - end_y = self.end_y.to_gerber(settings), - rotation = self.rotation.to_gerber(settings)) - return '{code},{exposure},{width},{start_x},{start_y},{end_x},{end_y},{rotation}*'.format(**data) - - def to_instructions(self): - yield (OpCode.PUSH, 1 if self.exposure == 'on' else 0) - modifiers = [self.width, self.start_x, self.start_y, self.end_x, self.end_y, self.rotation] - for modifier in modifiers: - for i in modifier.to_instructions(): - yield i - yield (OpCode.PRIM, self.code) - -class AMCenterLinePrimitiveDef(AMPrimitiveDef): - @classmethod - def from_modifiers(cls, code, modifiers): - code = code - exposure = 'on' if modifiers[0].value == 1 else 'off' - width = modifiers[1] - height = modifiers[2] - x = modifiers[3] - y = modifiers[4] - rotation = modifiers[5] - return cls(code, exposure, width, height, x, y, rotation) - - def __init__(self, code, exposure, width, height, x, y, rotation): - super(AMCenterLinePrimitiveDef, self).__init__(code, exposure, rotation) - self.width = width - self.height = height - self.x = x - self.y = y - - def to_inch(self): - self.width = self.width.to_inch().optimize() - self.height = self.height.to_inch().optimize() - self.x = self.x.to_inch().optimize() - self.y = self.y.to_inch().optimize() - - def to_metric(self): - self.width = self.width.to_metric().optimize() - self.height = self.height.to_metric().optimize() - self.x = self.x.to_metric().optimize() - self.y = self.y.to_metric().optimize() - - def to_gerber(self, settings=None): - data = dict(code = self.code, - exposure = 1 if self.exposure == 'on' else 0, - width = self.width.to_gerber(settings), - height = self.height.to_gerber(settings), - x = self.x.to_gerber(settings), - y = self.y.to_gerber(settings), - rotation = self.rotation.to_gerber(settings)) - return '{code},{exposure},{width},{height},{x},{y},{rotation}*'.format(**data) - - def to_instructions(self): - yield (OpCode.PUSH, 1 if self.exposure == 'on' else 0) - modifiers = [self.width, self.height, self.x, self.y, self.rotation] - for modifier in modifiers: - for i in modifier.to_instructions(): - yield i - yield (OpCode.PRIM, self.code) - -class AMOutlinePrimitiveDef(AMPrimitiveDef): - @classmethod - def from_modifiers(cls, code, modifiers): - num_points = int(modifiers[1].value + 1) - code = code - exposure = 'on' if modifiers[0].value == 1 else 'off' - addrs = modifiers[2:num_points * 2 + 2] - rotation = modifiers[2 + num_points * 2] - return cls(code, exposure, addrs, rotation) - - def __init__(self, code, exposure, addrs, rotation): - super(AMOutlinePrimitiveDef, self).__init__(code, exposure, rotation) - self.addrs = addrs - - def to_inch(self): - self.addrs = [i.to_inch().optimize() for i in self.addrs] - - def to_metric(self): - self.addrs = [i.to_metric().optimize() for i in self.addrs] - - def to_gerber(self, settings=None): - def strs(): - yield '%d,%d,%d' % (self.code, - 1 if self.exposure == 'on' else 0, - len(self.addrs) / 2 - 1) - for i in self.addrs: - yield i.to_gerber(settings) - yield self.rotation.to_gerber(settings) - - return '%s*' % ','.join(strs()) - - def to_instructions(self): - yield (OpCode.PUSH, 1 if self.exposure == 'on' else 0) - yield (OpCode.PUSH, int(len(self.addrs) / 2 - 1)) - for modifier in self.addrs: - for i in modifier.to_instructions(): - yield i - for i in self.rotation.to_instructions(): - yield i - yield (OpCode.PRIM, self.code) - -class AMPolygonPrimitiveDef(AMPrimitiveDef): - @classmethod - def from_modifiers(cls, code, modifiers): - code = code - exposure = 'on' if modifiers[0].value == 1 else 'off' - vertices = modifiers[1] - x = modifiers[2] - y = modifiers[3] - diameter = modifiers[4] - rotation = modifiers[5] - return cls(code, exposure, vertices, x, y, diameter, rotation) - - def __init__(self, code, exposure, vertices, x, y, diameter, rotation): - super(AMPolygonPrimitiveDef, self).__init__(code, exposure, rotation) - self.vertices = vertices - self.x = x - self.y = y - self.diameter = diameter - - def to_inch(self): - self.x = self.x.to_inch().optimize() - self.y = self.y.to_inch().optimize() - self.diameter = self.diameter.to_inch().optimize() - - def to_metric(self): - self.x = self.x.to_metric().optimize() - self.y = self.y.to_metric().optimize() - self.diameter = self.diameter.to_metric().optimize() - - def to_gerber(self, settings=None): - data = dict(code = self.code, - exposure = 1 if self.exposure == 'on' else 0, - vertices = self.vertices.to_gerber(settings), - x = self.x.to_gerber(settings), - y = self.y.to_gerber(settings), - diameter = self.diameter.to_gerber(settings), - rotation = self.rotation.to_gerber(settings)) - return '{code},{exposure},{vertices},{x},{y},{diameter},{rotation}*'.format(**data) - - def to_instructions(self): - yield (OpCode.PUSH, 1 if self.exposure == 'on' else 0) - modifiers = [self.vertices, self.x, self.y, self.diameter, self.rotation] - for modifier in modifiers: - for i in modifier.to_instructions(): - yield i - yield (OpCode.PRIM, self.code) - -class AMMoirePrimitiveDef(AMPrimitiveDef): - @classmethod - def from_modifiers(cls, code, modifiers): - code = code - exposure = 'on' - x = modifiers[0] - y = modifiers[1] - diameter = modifiers[2] - ring_thickness = modifiers[3] - gap = modifiers[4] - max_rings = modifiers[5] - crosshair_thickness = modifiers[6] - crosshair_length = modifiers[7] - rotation = modifiers[8] - return cls(code, exposure, x, y, diameter, ring_thickness, gap, - max_rings, crosshair_thickness, crosshair_length, rotation) - - def __init__(self, code, exposure, x, y, diameter, ring_thickness, gap, max_rings, crosshair_thickness, crosshair_length, rotation): - super(AMMoirePrimitiveDef, self).__init__(code, exposure, rotation) - self.x = x - self.y = y - self.diameter = diameter - self.ring_thickness = ring_thickness - self.gap = gap - self.max_rings = max_rings - self.crosshair_thickness = crosshair_thickness - self.crosshair_length = crosshair_length - - def to_inch(self): - self.x = self.x.to_inch().optimize() - self.y = self.y.to_inch().optimize() - self.diameter = self.diameter.to_inch().optimize() - self.ring_thickness = self.ring_thickness.to_inch().optimize() - self.gap = self.gap.to_inch().optimize() - self.crosshair_thickness = self.crosshair_thickness.to_inch().optimize() - self.crosshair_length = self.crosshair_length.to_inch().optimize() - - def to_metric(self): - self.x = self.x.to_metric().optimize() - self.y = self.y.to_metric().optimize() - self.diameter = self.diameter.to_metric().optimize() - self.ring_thickness = self.ring_thickness.to_metric().optimize() - self.gap = self.gap.to_metric().optimize() - self.crosshair_thickness = self.crosshair_thickness.to_metric().optimize() - self.crosshair_length = self.crosshair_length.to_metric().optimize() - - def to_gerber(self, settings=None): - data = dict(code = self.code, - x = self.x.to_gerber(settings), - y = self.y.to_gerber(settings), - diameter = self.diameter.to_gerber(settings), - ring_thickness = self.ring_thickness.to_gerber(settings), - gap = self.gap.to_gerber(settings), - max_rings = self.max_rings.to_gerber(settings), - crosshair_thickness = self.crosshair_thickness.to_gerber(settings), - crosshair_length = self.crosshair_length.to_gerber(settings), - rotation = self.rotation.to_gerber(settings)) - return '{code},{x},{y},{diameter},{ring_thickness},{gap},{max_rings},'\ - '{crosshair_thickness},{crosshair_length},{rotation}*'.format(**data) - - def to_instructions(self): - modifiers = [self.x, self.y, self.diameter, - self.ring_thickness, self.gap, self.max_rings, - self.crosshair_thickness, self.crosshair_length, - self.rotation] - for modifier in modifiers: - for i in modifier.to_instructions(): - yield i - yield (OpCode.PRIM, self.code) - -class AMThermalPrimitiveDef(AMPrimitiveDef): - @classmethod - def from_modifiers(cls, code, modifiers): - code = code - exposure = 'on' - x = modifiers[0] - y = modifiers[1] - outer_diameter = modifiers[2] - inner_diameter = modifiers[3] - gap = modifiers[4] - rotation = modifiers[5] - return cls(code, exposure, x, y, outer_diameter, inner_diameter, gap, rotation) - - def __init__(self, code, exposure, x, y, outer_diameter, inner_diameter, gap, rotation): - super(AMThermalPrimitiveDef, self).__init__(code, exposure, rotation) - self.x = x - self.y = y - self.outer_diameter = outer_diameter - self.inner_diameter = inner_diameter - self.gap = gap - - def to_inch(self): - self.x = self.x.to_inch().optimize() - self.y = self.y.to_inch().optimize() - self.outer_diameter = self.outer_diameter.to_inch().optimize() - self.inner_diameter = self.inner_diameter.to_inch().optimize() - self.gap = self.gap.to_inch().optimize() - - def to_metric(self): - self.x = self.x.to_metric().optimize() - self.y = self.y.to_metric().optimize() - self.outer_diameter = self.outer_diameter.to_metric().optimize() - self.inner_diameter = self.inner_diameter.to_metric().optimize() - self.gap = self.gap.to_metric().optimize() - - def to_gerber(self, settings=None): - data = dict(code = self.code, - x = self.x.to_gerber(settings), - y = self.y.to_gerber(settings), - outer_diameter = self.outer_diameter.to_gerber(settings), - inner_diameter = self.inner_diameter.to_gerber(settings), - gap = self.gap.to_gerber(settings), - rotation = self.rotation.to_gerber(settings)) - return '{code},{x},{y},{outer_diameter},{inner_diameter},'\ - '{gap},{rotation}*'.format(**data) - - def to_instructions(self): - modifiers = [self.x, self.y, self.outer_diameter, - self.inner_diameter, self.gap, self.rotation] - for modifier in modifiers: - for i in modifier.to_instructions(): - yield i - yield (OpCode.PRIM, self.code) - -class AMVariableDef(object): - def __init__(self, number, value): - self.number = number - self.value = value - - def to_inch(self): - return self - - def to_metric(self): - return self - - def to_gerber(self, settings=None): - return '$%d=%s*' % (self.number, self.value.to_gerber(settings)) - - def to_instructions(self): - for i in self.value.to_instructions(): - yield i - yield (OpCode.STORE, self.number) - - def rotate(self, angle, center=None): - pass - diff --git a/gerbonara/gerber/panelize/common.py b/gerbonara/gerber/panelize/common.py deleted file mode 100644 index aa3ba0e..0000000 --- a/gerbonara/gerber/panelize/common.py +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -# Copyright 2019 Hiroshi Murayama <opiopan@gmail.com> - -import os -from ..common import loads as loads_org -from ..exceptions import ParseError -from ..utils import detect_file_format -from .. import rs274x -from .. import ipc356 - -from . import excellon - -def read(filename, format=None): - with open(filename, 'r') as f: - data = f.read() - return loads(data, filename, format=format) - - -def loads(data, filename=None, format=None): - # if os.path.splitext(filename if filename else '')[1].lower() == '.dxf': - # return dxf.loads(data, filename) - - fmt = detect_file_format(data) - if fmt == 'excellon': - return excellon.loads(data, filename=filename, format=format) - elif fmt == 'ipc_d_356': - return ipc356.loads(data, filename=filename) - else: - raise ParseError('Unable to detect file format') - - -# def rectangle(width, height, left=0, bottom=0, units='metric', draw_mode=None, filename=None): -# return dxf.DxfFile.rectangle( -# width, height, left, bottom, units, draw_mode, filename) |