1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2008 Aaron Spike, aaron@ekips.org
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 '''
19 import inkex, cubicsuperpath, simplepath, simplestyle, cspsubdiv
21 class MyEffect(inkex.Effect):
22 def __init__(self):
23 inkex.Effect.__init__(self)
24 self.OptionParser.add_option("-f", "--flatness",
25 action="store", type="float",
26 dest="flat", default=0.2,
27 help="Minimum flatness of the subdivided curves")
28 self.OptionParser.add_option("-m", "--mirror",
29 action="store", type="inkbool",
30 dest="mirror", default="FALSE",
31 help="Mirror Y-Axis")
32 self.OptionParser.add_option("-x", "--xOrigin",
33 action="store", type="float",
34 dest="xOrigin", default=0.0,
35 help="X Origin (pixels)")
36 self.OptionParser.add_option("-y", "--yOrigin",
37 action="store", type="float",
38 dest="yOrigin", default=0.0,
39 help="Y Origin (pixels)")
40 self.OptionParser.add_option("-r", "--resolution",
41 action="store", type="int",
42 dest="resolution", default=1016,
43 help="Resolution (dpi)")
44 self.OptionParser.add_option("-n", "--pen",
45 action="store", type="int",
46 dest="pen", default=1,
47 help="Pen number")
48 self.OptionParser.add_option("-p", "--plotInvisibleLayers",
49 action="store", type="inkbool",
50 dest="plotInvisibleLayers", default="FALSE",
51 help="Plot invisible layers")
53 def output(self):
54 print ''.join(self.hpgl)
55 def effect(self):
56 self.hpgl = ['IN;SP%d;' % self.options.pen]
57 x0 = self.options.xOrigin
58 y0 = self.options.yOrigin
59 scale = float(self.options.resolution)/90
60 mirror = 1.0
61 if self.options.mirror:
62 mirror = -1.0
63 if self.document.getroot().get('height'):
64 y0 -= float(self.document.getroot().get('height'))
65 i = 0
66 layerPath = '//svg:g[@inkscape:groupmode="layer"]'
67 for layer in self.document.getroot().xpath(layerPath, namespaces=inkex.NSS):
68 i += 1
69 style = layer.get('style')
70 if style:
71 style = simplestyle.parseStyle(style)
72 if style['display']=='none':
73 if not self.options.plotInvisibleLayers:
74 continue
75 nodePath = ('//svg:g[@inkscape:groupmode="layer"][%d]/descendant::svg:path') % i
76 for node in self.document.getroot().xpath(nodePath, namespaces=inkex.NSS):
77 d = node.get('d')
78 if len(simplepath.parsePath(d)):
79 p = cubicsuperpath.parsePath(d)
80 cspsubdiv.cspsubdiv(p, self.options.flat)
81 for sp in p:
82 first = True
83 for csp in sp:
84 cmd = 'PD'
85 if first:
86 cmd = 'PU'
87 first = False
88 self.hpgl.append('%s%d,%d;' % (cmd,(csp[1][0] - x0)*scale,(csp[1][1]*mirror - y0)*scale))
89 self.hpgl.append('PU;')
91 if __name__ == '__main__': #pragma: no cover
92 e = MyEffect()
93 e.affect()
95 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99