extrude.py 4.34 KB
Newer Older
Bob Jamison's avatar
Bob Jamison committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/env python 
'''
Copyright (C) 2007

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Bob Jamison's avatar
Bob Jamison committed
18
'''
19 20 21
# local library
import inkex
import simplepath
22
import simplestyle
23 24
import simpletransform
import cubicsuperpath
Bob Jamison's avatar
Bob Jamison committed
25 26 27 28 29 30 31 32 33 34 35 36 37

class Extrude(inkex.Effect):
    def __init__(self):
        inkex.Effect.__init__(self)
        opts = [('-m', '--mode', 'string', 'mode', 'Lines',
                 'Join paths with lines or polygons'),
                ]
        for o in opts:
            self.OptionParser.add_option(o[0], o[1], action="store", type=o[2],
                                         dest=o[3], default=o[4], help=o[5])

    def effect(self):
        paths = []
Thomas Holder's avatar
Thomas Holder committed
38
        for id, node in self.selected.items():
Bob Jamison's avatar
Bob Jamison committed
39 40
            if node.tag == '{http://www.w3.org/2000/svg}path':
                paths.append(node)
41 42
        if len(paths) < 2:
            inkex.errormsg(_('Need at least 2 paths selected'))
Bob Jamison's avatar
Bob Jamison committed
43 44 45
            return

        pts = [cubicsuperpath.parsePath(paths[i].get('d'))
46
               for i in range(len(paths))]
Bob Jamison's avatar
Bob Jamison committed
47

48
        for i in range(len(paths)):
Bob Jamison's avatar
Bob Jamison committed
49 50 51 52 53
            if 'transform' in paths[i].keys():
                trans = paths[i].get('transform')
                trans = simpletransform.parseTransform(trans)
                simpletransform.applyTransformToPath(trans, pts[i])

54 55 56 57 58 59 60 61
        for n1 in range(0, len(paths)):
            for n2 in range(n1 + 1, len(paths)):
                verts = []
                for i in range(0, min(map(len, pts))):
                    comp = []
                    for j in range(0, min(len(pts[n1][i]), len(pts[n2][i]))):
                        comp.append([pts[n1][i][j][1][-2:], pts[n2][i][j][1][-2:]])
                    verts.append(comp)
Bob Jamison's avatar
Bob Jamison committed
62

63
                if self.options.mode.lower() == 'lines':
Bob Jamison's avatar
Bob Jamison committed
64
                    line = []
65 66 67 68
                    for comp in verts:
                        for n,v in enumerate(comp):
                            line += [('M', v[0])]
                            line += [('L', v[1])]
Bob Jamison's avatar
Bob Jamison committed
69
                    ele = inkex.etree.Element('{http://www.w3.org/2000/svg}path')
70
                    paths[0].xpath('..')[0].append(ele)
Bob Jamison's avatar
Bob Jamison committed
71
                    ele.set('d', simplepath.formatPath(line))
72 73 74 75 76 77 78
                    style = {
                        'fill': 'none',
                        'stroke': '#000000',
                        'stroke-opacity': 1,
                        'stroke-width': self.unittouu('1px'),
                    }
                    ele.set('style', simplestyle.formatStyle(style))
79 80
                elif self.options.mode.lower() == 'polygons':
                    g = inkex.etree.Element('{http://www.w3.org/2000/svg}g')
81 82 83 84 85 86 87 88
                    style = {
                        'fill': '#000000',
                        'fill-opacity': 0.3,
                        'stroke': '#000000',
                        'stroke-opacity': 0.6,
                        'stroke-width': self.unittouu('2px'),
                    }
                    g.set('style', simplestyle.formatStyle(style))
89 90 91 92 93 94 95 96 97 98 99 100 101 102
                    paths[0].xpath('..')[0].append(g)
                    for comp in verts:
                        for n,v in enumerate(comp):
                            nn = n+1
                            if nn == len(comp): nn = 0
                            line = []
                            line += [('M', comp[n][0])]
                            line += [('L', comp[n][1])]
                            line += [('L', comp[nn][1])]
                            line += [('L', comp[nn][0])]
                            line += [('L', comp[n][0])]
                            ele = inkex.etree.Element('{http://www.w3.org/2000/svg}path')
                            g.append(ele)
                            ele.set('d', simplepath.formatPath(line))
Bob Jamison's avatar
Bob Jamison committed
103

104 105 106 107

if __name__ == '__main__':   #pragma: no cover
    e = Extrude()
    e.affect()