summaryrefslogtreecommitdiffstats
path: root/share/extensions/extrude.py
blob: 169d50cd80b70410d1252f06b2659e68253a5bec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
# coding=utf-8
#
# 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
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
"""Join paths with lines or polygons"""


import inkex

class Extrude(inkex.EffectExtension):
    def add_arguments(self, pars):
        pars.add_argument("--mode", default="Lines", help="Join paths with lines or polygons")

    def effect(self):
        paths = []
        for node in self.svg.selection.filter(inkex.PathElement).values():
            paths.append(node)
        if len(paths) < 2:
            raise inkex.AbortExtension("Need at least 2 paths selected")

        for path in paths:
            path.apply_transform()

        pts = [node.path.to_superpath() for node in paths]

        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)

                if self.options.mode.lower() == 'lines':
                    line = []
                    for comp in verts:
                        for n, v in enumerate(comp):
                            line += [('M', v[0])]
                            line += [('L', v[1])]
                    ele = inkex.PathElement()
                    paths[0].xpath('..')[0].append(ele)
                    ele.set('d', str(inkex.Path(line)))
                    style = {
                        'fill': 'none',
                        'stroke': '#000000',
                        'stroke-opacity': 1,
                        'stroke-width': self.svg.unittouu('1px'),
                    }
                    ele.set('style', str(inkex.Style(style)))
                elif self.options.mode.lower() == 'polygons':
                    g = inkex.Group()
                    style = {
                        'fill': '#000000',
                        'fill-opacity': 0.3,
                        'stroke': '#000000',
                        'stroke-opacity': 0.6,
                        'stroke-width': self.svg.unittouu('2px'),
                    }
                    g.set('style', str(inkex.Style(style)))
                    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.PathElement()
                            g.append(ele)
                            ele.set('d', str(inkex.Path(line)))


if __name__ == '__main__':
    Extrude().run()