summaryrefslogtreecommitdiff
path: root/gerbonara/gerber/panelize
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2021-10-07 11:13:12 +0200
committerjaseg <git@jaseg.de>2021-10-07 11:13:12 +0200
commit4565712869ef4269d62de593a245ca8d001c4ea9 (patch)
tree32a8a2245e11c8dfad9bbd486ab7678a32b969c1 /gerbonara/gerber/panelize
parentdd8944709c5a48ccb52ba41a2310218770eb1669 (diff)
downloadgerbonara-4565712869ef4269d62de593a245ca8d001c4ea9.tar.gz
gerbonara-4565712869ef4269d62de593a245ca8d001c4ea9.tar.bz2
gerbonara-4565712869ef4269d62de593a245ca8d001c4ea9.zip
WIP
Diffstat (limited to 'gerbonara/gerber/panelize')
-rw-r--r--gerbonara/gerber/panelize/__init__.py4
-rw-r--r--gerbonara/gerber/panelize/am_expression.py155
-rw-r--r--gerbonara/gerber/panelize/am_primitive.py49
-rw-r--r--gerbonara/gerber/panelize/common.py17
-rw-r--r--gerbonara/gerber/panelize/composition.py8
-rw-r--r--gerbonara/gerber/panelize/excellon.py13
-rw-r--r--gerbonara/gerber/panelize/gerber_statements.py66
-rw-r--r--gerbonara/gerber/panelize/rs274x.py0
8 files changed, 113 insertions, 199 deletions
diff --git a/gerbonara/gerber/panelize/__init__.py b/gerbonara/gerber/panelize/__init__.py
index a3de2c1..645b632 100644
--- a/gerbonara/gerber/panelize/__init__.py
+++ b/gerbonara/gerber/panelize/__init__.py
@@ -3,6 +3,6 @@
# Copyright 2019 Hiroshi Murayama <opiopan@gmail.com>
-from .common import read, loads, rectangle
+from .common import read, loads # , rectangle
from .composition import GerberComposition, DrillComposition
-from .dxf import DxfFile
+# from .dxf import DxfFile
diff --git a/gerbonara/gerber/panelize/am_expression.py b/gerbonara/gerber/panelize/am_expression.py
index 9e19f71..a0a4114 100644
--- a/gerbonara/gerber/panelize/am_expression.py
+++ b/gerbonara/gerber/panelize/am_expression.py
@@ -3,6 +3,8 @@
# Copyright 2019 Hiroshi Murayama <opiopan@gmail.com>
+import operator
+
from ..utils import *
from ..am_eval import OpCode
from ..am_statements import *
@@ -20,21 +22,19 @@ class AMExpression(object):
return self
def optimize(self):
- pass
+ return self
def to_inch(self):
- return AMOperatorExpression(AMOperatorExpression.DIV, self,
- AMConstantExpression(MILLIMETERS_PER_INCH))
+ return AMOperatorExpression.div(self, MILLIMETERS_PER_INCH)
def to_metric(self):
- return AMOperatorExpression(AMOperatorExpression.MUL, self,
- AMConstantExpression(MILLIMETERS_PER_INCH))
+ return AMOperatorExpression.mul(self, MILLIMETERS_PER_INCH)
- def to_gerber(self, settings=None):
- pass
+ #def to_gerber(self, settings=None):
+ # pass
- def to_instructions(self):
- pass
+ #def to_instructions(self):
+ # pass
class AMConstantExpression(AMExpression):
def __init__(self, value):
@@ -45,14 +45,35 @@ class AMConstantExpression(AMExpression):
def value(self):
return self._value
- def optimize(self):
- return self
-
+ 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
- gerber = '%.6g' % self._value
- return '%.6f' % self._value if 'e' in gerber else gerber
+ return f'{self.value:.6f}'.rstrip('0').rstrip('.')
def to_instructions(self):
return [(OpCode.PUSH, self._value)]
@@ -62,76 +83,90 @@ class AMVariableExpression(AMExpression):
super(AMVariableExpression, self).__init__(AMExpression.VARIABLE)
self.number = number
- def optimize(self):
- return self
-
def to_gerber(self, settings=None):
- return '$%d' % self.number
+ return f'${self.number}'
def to_instructions(self):
return (OpCode.LOAD, self.number)
class AMOperatorExpression(AMExpression):
- ADD = '+'
- SUB = '-'
- MUL = 'X'
- DIV = '/'
-
def __init__(self, op, lvalue, rvalue):
super(AMOperatorExpression, self).__init__(AMExpression.OPERATOR)
self.op = op
- self.lvalue = lvalue
- self.rvalue = rvalue
+ 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):
- self.lvalue = self.lvalue.optimize()
- self.rvalue = self.rvalue.optimize()
-
- if isinstance(self.lvalue, AMConstantExpression) and isinstance(self.rvalue, AMConstantExpression):
- lvalue = float(self.lvalue.value)
- rvalue = float(self.rvalue.value)
- value = lvalue + rvalue if self.op == self.ADD else \
- lvalue - rvalue if self.op == self.SUB else \
- lvalue * rvalue if self.op == self.MUL else \
- lvalue / rvalue if self.op == self.DIV else None
- return AMConstantExpression(value)
- elif self.op == self.ADD:
- if self.rvalue.value == 0:
- return self.lvalue
- elif self.lvalue.value == 0:
- return self.rvalue
- elif self.op == self.SUB:
- if self.rvalue.value == 0:
- return self.lvalue
- elif self.lvalue.value == 0 and isinstance(self.rvalue, AMConstantExpression):
- return AMConstantExpression(-self.rvalue.value)
- elif self.op == self.MUL:
- if self.rvalue.value == 1:
- return self.lvalue
- elif self.lvalue.value == 1:
- return self.rvalue
- elif self.lvalue == 0 or self.rvalue == 0:
+ 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 == self.DIV:
- if self.rvalue.value == 1:
+
+ elif self.op == operator.TRUEDIV:
+ if r == 1:
return self.lvalue
- elif self.lvalue.value == 0:
+ elif l == 0:
return AMConstantExpression(0)
return self
def to_gerber(self, settings=None):
- return '(%s)%s(%s)' % (self.lvalue.to_gerber(settings), self.op, self.rvalue.to_gerber(settings))
+ 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 = OpCode.ADD if self.op == self.ADD else\
- OpCode.SUB if self.op == self.SUB else\
- OpCode.MUL if self.op == self.MUL else\
- OpCode.DIV
+
+ 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):
diff --git a/gerbonara/gerber/panelize/am_primitive.py b/gerbonara/gerber/panelize/am_primitive.py
index 123f030..5d8458b 100644
--- a/gerbonara/gerber/panelize/am_primitive.py
+++ b/gerbonara/gerber/panelize/am_primitive.py
@@ -9,31 +9,6 @@ from ..am_eval import OpCode
from .am_expression import eval_macro, AMConstantExpression, AMOperatorExpression
-class AMPrimitiveDef(AMPrimitive):
- def __init__(self, code, exposure=None, rotation=None):
- super(AMPrimitiveDef, self).__init__(code, exposure)
- if not rotation:
- rotation = AMConstantExpression(0)
- self.rotation = rotation
-
- def rotate(self, angle, center=None):
- self.rotation = AMOperatorExpression(AMOperatorExpression.ADD,
- self.rotation,
- AMConstantExpression(float(angle)))
- self.rotation = self.rotation.optimize()
-
- def to_inch(self):
- pass
-
- def to_metric(self):
- pass
-
- def to_gerber(self, settings=None):
- pass
-
- def to_instructions(self):
- pass
-
class AMCommentPrimitiveDef(AMPrimitiveDef):
@classmethod
def from_modifiers(cls, code, modifiers):
@@ -42,12 +17,6 @@ class AMCommentPrimitiveDef(AMPrimitiveDef):
def __init__(self, code, comment):
super(AMCommentPrimitiveDef, self).__init__(code)
self.comment = comment
-
- def to_gerber(self, settings=None):
- return '%d %s*' % (self.code, self.comment.to_gerber())
-
- def to_instructions(self):
- return [(OpCode.PUSH, self.comment), (OpCode.PRIM, self.code)]
class AMCirclePrimitiveDef(AMPrimitiveDef):
@classmethod
@@ -428,21 +397,3 @@ class AMVariableDef(object):
def rotate(self, angle, center=None):
pass
-def to_primitive_defs(instructions):
- classes = {
- 0: AMCommentPrimitiveDef,
- 1: AMCirclePrimitiveDef,
- 2: AMVectorLinePrimitiveDef,
- 20: AMVectorLinePrimitiveDef,
- 21: AMCenterLinePrimitiveDef,
- 4: AMOutlinePrimitiveDef,
- 5: AMPolygonPrimitiveDef,
- 6: AMMoirePrimitiveDef,
- 7: AMThermalPrimitiveDef,
- }
- for code, modifiers in eval_macro(instructions):
- if code < 0:
- yield AMVariableDef(-code, modifiers[0])
- else:
- primitive = classes[code]
- yield primitive.from_modifiers(code, modifiers)
diff --git a/gerbonara/gerber/panelize/common.py b/gerbonara/gerber/panelize/common.py
index ac25138..aa3ba0e 100644
--- a/gerbonara/gerber/panelize/common.py
+++ b/gerbonara/gerber/panelize/common.py
@@ -10,9 +10,7 @@ from ..utils import detect_file_format
from .. import rs274x
from .. import ipc356
-from . import rs274x as ex_rs274x
from . import excellon
-from . import dxf
def read(filename, format=None):
with open(filename, 'r') as f:
@@ -21,14 +19,11 @@ def read(filename, format=None):
def loads(data, filename=None, format=None):
- if os.path.splitext(filename if filename else '')[1].lower() == '.dxf':
- return dxf.loads(data, filename)
+ # if os.path.splitext(filename if filename else '')[1].lower() == '.dxf':
+ # return dxf.loads(data, filename)
fmt = detect_file_format(data)
- if fmt == 'rs274x':
- file = ex_rs274x.loads(data, filename=filename)
- return ex_rs274x.GerberFile.from_gerber_file(file)
- elif fmt == 'excellon':
+ if fmt == 'excellon':
return excellon.loads(data, filename=filename, format=format)
elif fmt == 'ipc_d_356':
return ipc356.loads(data, filename=filename)
@@ -36,6 +31,6 @@ def loads(data, filename=None, format=None):
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)
+# 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)
diff --git a/gerbonara/gerber/panelize/composition.py b/gerbonara/gerber/panelize/composition.py
index 619a0cf..a30f959 100644
--- a/gerbonara/gerber/panelize/composition.py
+++ b/gerbonara/gerber/panelize/composition.py
@@ -8,9 +8,9 @@ from ..cam import FileSettings
from ..gerber_statements import EofStmt
from ..excellon_statements import *
from ..excellon import DrillSlot, DrillHit
-from . import rs274x
+from .. import rs274x
from . import excellon
-from . import dxf
+# from . import dxf
class Composition(object):
def __init__(self, settings = None, comments = None):
@@ -29,8 +29,8 @@ class GerberComposition(Composition):
def merge(self, file):
if isinstance(file, rs274x.GerberFile):
self._merge_gerber(file)
- elif isinstance(file, dxf.DxfFile):
- self._merge_dxf(file)
+# elif isinstance(file, dxf.DxfFile):
+# self._merge_dxf(file)
else:
raise Exception('unsupported file type')
diff --git a/gerbonara/gerber/panelize/excellon.py b/gerbonara/gerber/panelize/excellon.py
index ae0b68e..e6cfcd0 100644
--- a/gerbonara/gerber/panelize/excellon.py
+++ b/gerbonara/gerber/panelize/excellon.py
@@ -13,8 +13,7 @@ from ..excellon_statements import ExcellonStatement, UnitStmt, CoordinateStmt, U
RetractWithClampingStmt, RetractWithoutClampingStmt, \
EndOfProgramStmt
from ..cam import FileSettings
-from ..utils import inch, metric, write_gerber_value, parse_gerber_value
-from .utility import rotate
+from ..utils import inch, metric, write_gerber_value, parse_gerber_value, rotate_point
def loads(data, filename=None, settings=None, tools=None, format=None):
if not settings:
@@ -221,7 +220,7 @@ class DrillHitEx(DrillHit):
self.position = tuple(map(metric, self.position))
def rotate(self, angle, center=(0, 0)):
- self.position = rotate(*self.position, angle, center)
+ self.position = rotate_point(self.position, angle, center)
def to_excellon(self, settings):
return CoordinateStmtEx(*self.position).to_excellon(settings)
@@ -236,8 +235,8 @@ class DrillSlotEx(DrillSlot):
self.end = tuple(map(metric, self.end))
def rotate(self, angle, center=(0,0)):
- self.start = rotate(*self.start, angle, center)
- self.end = rotate(*self.end, angle, center)
+ self.start = rotate_point(self.start, angle, center)
+ self.end = rotate_point(self.end, angle, center)
def to_excellon(self, settings):
return SlotStmt(*self.start, *self.end).to_excellon(settings)
@@ -295,9 +294,9 @@ class DrillRout(object):
def rotate(self, angle, center=(0, 0)):
for node in self.nodes:
- node.position = rotate(*node.position, angle, center)
+ node.position = rotate_point(node.position, angle, center)
if node.center_offset is not None:
- node.center_offset = rotate(*node.center_offset, angle, (0., 0.))
+ node.center_offset = rotate_point(node.center_offset, angle, (0., 0.))
class UnitStmtEx(UnitStmt):
@classmethod
diff --git a/gerbonara/gerber/panelize/gerber_statements.py b/gerbonara/gerber/panelize/gerber_statements.py
index 875d656..208660e 100644
--- a/gerbonara/gerber/panelize/gerber_statements.py
+++ b/gerbonara/gerber/panelize/gerber_statements.py
@@ -7,72 +7,6 @@ from ..gerber_statements import AMParamStmt, ADParamStmt
from ..utils import inch, metric
from .am_primitive import to_primitive_defs
-class AMParamStmtEx(AMParamStmt):
- @classmethod
- def from_stmt(cls, stmt):
- return cls(stmt.param, stmt.name, stmt.macro, stmt.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 __init__(self, param, name, macro, units):
- super(AMParamStmtEx, self).__init__(param, name, macro)
- self.units = units
- self.primitive_defs = list(to_primitive_defs(self.instructions))
-
- def to_inch(self):
- if self.units == 'metric':
- self.units = 'inch'
- for p in self.primitive_defs:
- p.to_inch()
-
- def to_metric(self):
- if self.units == 'inch':
- self.units = 'metric'
- for p in self.primitive_defs:
- p.to_metric()
-
- def to_gerber(self, settings = None):
- def plist():
- for p in self.primitive_defs:
- yield p.to_gerber(settings)
- return "%%AM%s*\n%s%%" % (self.name, '\n'.join(plist()))
-
- def rotate(self, angle, center=None):
- for primitive_def in self.primitive_defs:
- primitive_def.rotate(angle, center)
-
class ADParamStmtEx(ADParamStmt):
GEOMETRIES = {
'C': [0,1],
diff --git a/gerbonara/gerber/panelize/rs274x.py b/gerbonara/gerber/panelize/rs274x.py
deleted file mode 100644
index e69de29..0000000
--- a/gerbonara/gerber/panelize/rs274x.py
+++ /dev/null