1#!/usr/local/bin/python3.8
2# coding=utf-8
3#
4# Copyright (C) 2007
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19#
20"""Join paths with lines or polygons"""
21
22
23import inkex
24
25class Extrude(inkex.EffectExtension):
26    def add_arguments(self, pars):
27        pars.add_argument("--mode", default="Lines", help="Join paths with lines or polygons")
28
29    def effect(self):
30        paths = []
31        for node in self.svg.selection.filter(inkex.PathElement):
32            paths.append(node)
33        if len(paths) < 2:
34            raise inkex.AbortExtension("Need at least 2 paths selected")
35
36        for path in paths:
37            path.apply_transform()
38
39        pts = [node.path.to_superpath() for node in paths]
40
41        for n1 in range(0, len(paths)):
42            for n2 in range(n1 + 1, len(paths)):
43                verts = []
44                for i in range(0, min(map(len, pts))):
45                    comp = []
46                    for j in range(0, min(len(pts[n1][i]), len(pts[n2][i]))):
47                        comp.append([pts[n1][i][j][1][-2:], pts[n2][i][j][1][-2:]])
48                    verts.append(comp)
49
50                if self.options.mode.lower() == 'lines':
51                    line = []
52                    for comp in verts:
53                        for n, v in enumerate(comp):
54                            line += [('M', v[0])]
55                            line += [('L', v[1])]
56                    ele = inkex.PathElement()
57                    paths[0].xpath('..')[0].append(ele)
58                    ele.set('d', str(inkex.Path(line)))
59                    style = {
60                        'fill': 'none',
61                        'stroke': '#000000',
62                        'stroke-opacity': 1,
63                        'stroke-width': self.svg.unittouu('1px'),
64                    }
65                    ele.set('style', str(inkex.Style(style)))
66                elif self.options.mode.lower() == 'polygons':
67                    g = inkex.Group()
68                    style = {
69                        'fill': '#000000',
70                        'fill-opacity': 0.3,
71                        'stroke': '#000000',
72                        'stroke-opacity': 0.6,
73                        'stroke-width': self.svg.unittouu('2px'),
74                    }
75                    g.set('style', str(inkex.Style(style)))
76                    paths[0].xpath('..')[0].append(g)
77                    for comp in verts:
78                        for n, v in enumerate(comp):
79                            nn = n + 1
80                            if nn == len(comp):
81                                nn = 0
82                            line = []
83                            line += [('M', comp[n][0])]
84                            line += [('L', comp[n][1])]
85                            line += [('L', comp[nn][1])]
86                            line += [('L', comp[nn][0])]
87                            line += [('L', comp[n][0])]
88                            ele = inkex.PathElement()
89                            g.append(ele)
90                            ele.set('d', str(inkex.Path(line)))
91
92
93if __name__ == '__main__':
94    Extrude().run()
95