summaryrefslogtreecommitdiff
path: root/lib/extensions/flip.py
blob: 65dbdc1fc0ac43961958725823d1efd157cfd8b2 (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
import inkex
import cubicsuperpath
from shapely import geometry as shgeo

from .base import InkstitchExtension
from ..i18n import _
from ..elements import SatinColumn


class Flip(InkstitchExtension):
    def subpath_to_linestring(self, subpath):
        return shgeo.LineString()

    def flip(self, satin):
        csp = satin.path

        if len(csp) > 1:
            flattened = satin.flatten(csp)

            # find the rails (the two longest paths) and swap them
            indices = range(len(csp))
            indices.sort(key=lambda i: shgeo.LineString(flattened[i]).length, reverse=True)

            first = indices[0]
            second = indices[1]
            csp[first], csp[second] = csp[second], csp[first]

            satin.node.set("d", cubicsuperpath.formatPath(csp))

    def effect(self):
        if not self.get_elements():
            return

        if not self.selected:
            inkex.errormsg(_("Please select one or more satin columns to flip."))
            return

        for element in self.elements:
            if isinstance(element, SatinColumn):
                self.flip(element)