summaryrefslogtreecommitdiffstats
path: root/share/extensions/extrude.py
blob: 34eeaf889602a29b377ac8abbe499e27c1e56053 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
# coding=utf-8
#
# Copyright (C) 2022 Jonathan Neuhauser, jonathan.neuhauser@outlook.com
#
# 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 itertools
from typing import List, Union

import inkex
from inkex.localization import inkex_gettext as _
from inkex.paths import ZoneClose, zoneClose, Line, Move, move
from inkex.turtle import PathGroupBuilder, PathBuilder


class Extrude(inkex.EffectExtension):
    """This effect draws lines between each nth node of each selected path.
    It can be chosen whether these regions are filled and whether the fill uses rectangles
    or copies of the path segments.
    The lines will be inserted between the two elements.
    """

    def add_arguments(self, pars):
        pars.add_argument("--tab")
        pars.add_argument(
            "-m",
            "--mode",
            default="lines",
            choices=["lines", "polygons", "snug"],
            help='Join paths with lines, polygons or copies of the segments ("snug")',
        )
        pars.add_argument(
            "-s",
            "--subpaths",
            default=True,
            type=inkex.Boolean,
            help="""If true, connecting lines will be inserted as subpaths of a single path.
                        If false, they will be inserted in newly created group. 
                        Only applies to mode=lines""",
        )

    @staticmethod
    def _handle_lines(manager, com1, com2):
        if not (
            isinstance(com1.command, (ZoneClose, zoneClose))
            or isinstance(com2.command, (ZoneClose, zoneClose))
        ):
            # For a closed subpath, the first line has already been drawn.
            manager.Move_to(*com1.end_point)
            manager.Line_to(*com2.end_point)

    @staticmethod
    def _handle_polygons(manager, com1, com2):
        if not (
            isinstance(com1.command, (Move, move))
            or isinstance(com2.command, (Move, move))
        ):
            # We skip if one of either commands is a "Move" command
            manager.Move_to(*com1.previous_end_point)
            for point in [
                com1.end_point,
                com2.end_point,
                com2.previous_end_point,
                com1.previous_end_point,
            ]:
                manager.Line_to(*point)

    @staticmethod
    def _handle_snug(manager, com1, com2):
        if not (
            isinstance(com1.command, (Move, move))
            or isinstance(com2.command, (Move, move))
        ):
            # We skip if one of either commands is a "Move" command
            manager.Move_to(*com1.previous_end_point)
            com1r = com1.command
            com2r = com2.reverse()
            doflag = True
            if isinstance(com1r, (ZoneClose, zoneClose)):
                # ZoneClose can not be used directly, must be converted to line
                com1r = Line(*com1.first_point)
                if com1.previous_end_point.is_close(com1.end_point):
                    doflag = False
            if doflag:
                manager.add([com1r, Line(*com2.end_point), com2r, ZoneClose()])

    def effect(self):
        paths: List[inkex.PathElement] = []
        for node in self.svg.selection.rendering_order().filter(inkex.ShapeElement):
            if isinstance(node, inkex.PathElement):
                node.apply_transform()
            paths.append(node)
        if len(paths) < 2:
            raise inkex.AbortExtension(_("Need at least 2 paths selected"))
        lines = self.options.mode.lower() == "lines"
        subpaths = self.options.subpaths and lines

        mode = (
            self._handle_lines
            if lines
            else (
                self._handle_polygons
                if self.options.mode.lower() == "polygons"
                else self._handle_snug
            )
        )

        if lines:
            style = {
                "fill": "none",
                "stroke": "#000000",
                "stroke-opacity": 1,
                "stroke-width": "1px",
            }
        else:
            style = {
                "fill": "#000000",
                "fill-opacity": 0.3,
                "stroke": "#000000",
                "stroke-opacity": 0.6,
                "stroke-width": "1px",
                "stroke-linejoin": "round",
            }

        for pa1, pa2 in itertools.combinations(paths, 2):
            manager = PathBuilder(style) if subpaths else PathGroupBuilder(style)
            for com1, com2 in zip(pa1.path.proxy_iterator(), pa2.path.proxy_iterator()):
                mode(manager, com1, com2)
                manager.terminate()
            manager.append_next(pa1)


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