1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2007
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 sys, os
20 sys.path.append(os.path.dirname(sys.argv[0])) #?
22 import inkex, simplepath, simpletransform, sys, cubicsuperpath
24 class Extrude(inkex.Effect):
25 def __init__(self):
26 inkex.Effect.__init__(self)
27 opts = [('-m', '--mode', 'string', 'mode', 'Lines',
28 'Join paths with lines or polygons'),
29 ]
30 for o in opts:
31 self.OptionParser.add_option(o[0], o[1], action="store", type=o[2],
32 dest=o[3], default=o[4], help=o[5])
34 def effect(self):
35 paths = []
36 for id, node in self.selected.iteritems():
37 if node.tag == '{http://www.w3.org/2000/svg}path':
38 paths.append(node)
39 if len(paths) == 2:
40 break
41 else:
42 sys.stderr.write('Need 2 paths selected\n')
43 return
46 pts = [cubicsuperpath.parsePath(paths[i].get('d'))
47 for i in (0,1)]
49 for i in (0,1):
50 if 'transform' in paths[i].keys():
51 trans = paths[i].get('transform')
52 trans = simpletransform.parseTransform(trans)
53 simpletransform.applyTransformToPath(trans, pts[i])
55 verts = []
56 for i in range(0, min(map(len, pts))):
57 comp = []
58 for j in range(0, min(len(pts[0][i]), len(pts[1][i]))):
59 comp.append([pts[0][i][j][1][-2:], pts[1][i][j][1][-2:]])
60 verts.append(comp)
62 if self.options.mode.lower() == 'lines':
63 line = []
64 for comp in verts:
65 for n,v in enumerate(comp):
66 line += [('M', v[0])]
67 line += [('L', v[1])]
68 ele = inkex.etree.Element('{http://www.w3.org/2000/svg}path')
69 paths[0].xpath('..')[0].append(ele)
70 ele.set('d', simplepath.formatPath(line))
71 ele.set('style', 'fill:none;stroke:#000000;stroke-opacity:1;stroke-width:1;')
72 elif self.options.mode.lower() == 'polygons':
73 g = inkex.etree.Element('{http://www.w3.org/2000/svg}g')
74 g.set('style', 'fill:#000000;stroke:#000000;fill-opacity:0.3;stroke-width:2;stroke-opacity:0.6;')
75 paths[0].xpath('..')[0].append(g)
76 for comp in verts:
77 for n,v in enumerate(comp):
78 nn = n+1
79 if nn == len(comp): nn = 0
80 line = []
81 line += [('M', comp[n][0])]
82 line += [('L', comp[n][1])]
83 line += [('L', comp[nn][1])]
84 line += [('L', comp[nn][0])]
85 line += [('L', comp[n][0])]
86 ele = inkex.etree.Element('{http://www.w3.org/2000/svg}path')
87 g.append(ele)
88 ele.set('d', simplepath.formatPath(line))
91 e = Extrude()
92 e.affect()