aboutsummaryrefslogtreecommitdiff
path: root/gerberex/rs274x.py
diff options
context:
space:
mode:
authoropiopan <opiopan@gmail.com>2019-03-23 21:59:13 +0900
committeropiopan <opiopan@gmail.com>2019-03-23 21:59:13 +0900
commit690df56bb71020901167605a87ec451081fa18d7 (patch)
treec1faba9ba0cb75d64d8353a94d87edbcbb8174c3 /gerberex/rs274x.py
parent9febca7da6a730b3b3ca3a54129a9f88e5c44d14 (diff)
downloadpcb-tools-extension-690df56bb71020901167605a87ec451081fa18d7.tar.gz
pcb-tools-extension-690df56bb71020901167605a87ec451081fa18d7.tar.bz2
pcb-tools-extension-690df56bb71020901167605a87ec451081fa18d7.zip
add rotation fuction
Diffstat (limited to 'gerberex/rs274x.py')
-rw-r--r--gerberex/rs274x.py65
1 files changed, 64 insertions, 1 deletions
diff --git a/gerberex/rs274x.py b/gerberex/rs274x.py
index e9d82cd..4b477d3 100644
--- a/gerberex/rs274x.py
+++ b/gerberex/rs274x.py
@@ -4,7 +4,9 @@
# Copyright 2019 Hiroshi Murayama <opiopan@gmail.com>
import gerber.rs274x
-from gerberex.statements import (AMParamStmt, AMParamStmtEx)
+from gerber.gerber_statements import ADParamStmt, CoordStmt
+from gerberex.statements import AMParamStmt, AMParamStmtEx
+from gerberex.utility import rotate
class GerberFile(gerber.rs274x.GerberFile):
@classmethod
@@ -23,3 +25,64 @@ class GerberFile(gerber.rs274x.GerberFile):
def __init__(self, statements, settings, primitives, apertures, filename=None):
super(GerberFile, self).__init__(statements, settings, primitives, apertures, filename)
+
+ def rotate(self, angle, center=(0,0)):
+ if angle % 360 == 0:
+ return
+ self._generalize_aperture()
+ for statement in self.statements:
+ if isinstance(statement, AMParamStmtEx):
+ statement.rotate(angle, center)
+ elif isinstance(statement, CoordStmt) and statement.x != None and statement.y != None:
+ statement.x, statement.y = rotate(statement.x, statement.y, angle, center)
+
+ def _generalize_aperture(self):
+ RECTANGLE = 0
+ LANDSCAPE_OBROUND = 1
+ PORTRATE_OBROUND = 2
+ POLYGON = 3
+ macro_defs = [
+ ('MACR', AMParamStmtEx.rectangle),
+ ('MACLO', AMParamStmtEx.landscape_obround),
+ ('MACPO', AMParamStmtEx.portrate_obround),
+ ('MACP', AMParamStmtEx.polygon)
+ ]
+
+ need_to_change = False
+ insert_point = 0
+ last_aperture = 0
+ macros = {}
+ for idx in range(0, len(self.statements)):
+ statement = self.statements[idx]
+ if isinstance(statement, AMParamStmtEx):
+ macros[statement.name] = statement
+ if not need_to_change:
+ insert_point = idx + 1
+ if isinstance(statement, ADParamStmt) and statement.shape in ['R', 'O', 'P']:
+ need_to_change = True
+ last_aperture = idx
+
+ if need_to_change:
+ for idx in range(0, len(macro_defs)):
+ macro_def = macro_defs[idx]
+ name = macro_def[0]
+ num = 1
+ while name in macros:
+ name = '%s_%d' % (macro_def[0], num)
+ num += 1
+ self.statements.insert(insert_point, macro_def[1](name))
+ macro_defs[idx] = (name, macro_def[1])
+ for idx in range(insert_point, last_aperture + len(macro_defs) + 1):
+ statement = self.statements[idx]
+ if isinstance(statement, ADParamStmt):
+ if statement.shape == 'R':
+ statement.shape = macro_defs[RECTANGLE][0]
+ elif statement.shape == 'O':
+ x = statement.modifiers[0] \
+ if len(statement.modifiers) > 0 else 0
+ y = statement.modifiers[1] \
+ if len(statement.modifiers) > 1 else 0
+ statement.shape = macro_defs[LANDSCAPE_OBROUND][0] \
+ if x > y else macro_defs[PORTRATE_OBROUND][0]
+ elif statement.shape == 'P':
+ statement.shape = macro_defs[POLYGON][0]