From 5153e08404c87662317e4ab0e71d05aab89b1daf Mon Sep 17 00:00:00 2001 From: Mario Voigt Date: Sun, 23 Aug 2020 00:48:27 +0200 Subject: [PATCH] Added VH To Line extension --- extensions/fablabchemnitz_vh_to_line.inx | 16 ++++++ extensions/fablabchemnitz_vh_to_line.py | 64 ++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 extensions/fablabchemnitz_vh_to_line.inx create mode 100644 extensions/fablabchemnitz_vh_to_line.py diff --git a/extensions/fablabchemnitz_vh_to_line.inx b/extensions/fablabchemnitz_vh_to_line.inx new file mode 100644 index 00000000..85d43175 --- /dev/null +++ b/extensions/fablabchemnitz_vh_to_line.inx @@ -0,0 +1,16 @@ + + + Convert Vertical/Horizontal To Line + fablabchemnitz.de.vh_to_line + + path + + + + + + + + \ No newline at end of file diff --git a/extensions/fablabchemnitz_vh_to_line.py b/extensions/fablabchemnitz_vh_to_line.py new file mode 100644 index 00000000..be2f977f --- /dev/null +++ b/extensions/fablabchemnitz_vh_to_line.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 + +#M 60.403,71.0937 V 89.268022 135.773 + +""" +Extension for InkScape 1.X + +This extension converts an SVG path's d attribute the following way: find each V (vertical line) and each H (horizontal line) and replace it by a generic line (L type). +A lot of extensions do not work with V and H, but with L commands. So this is just a helper extension for other extensions :-) + +Example conversion: +from: M 60.403,71.0937 V 89.268022 135.773 +to: M 60.403 71.0937 L 60.403 89.268 L 60.403 135.773 + +Author: Mario Voigt / FabLab Chemnitz +Mail: mario.voigt@stadtfabrikanten.org +Date: 23.08.2020 +Last patch: 23.08.2020 +License: GNU GPL v3 +""" + +from math import * +import inkex +from inkex.paths import Path, CubicSuperPath + +class VHToLine(inkex.Effect): + def __init__(self): + inkex.Effect.__init__(self) + + def effect(self): + if len(self.svg.selected) == 0: exit("Please select at least one path.") + for obj in self.svg.selected: # The objects are the paths, which may be compound + curr = self.svg.selected[obj] + raw = Path(curr.get("d")).to_arrays() + subpaths, prev = [], 0 + for i in range(len(raw)): # Breaks compound paths into simple paths + if raw[i][0] == 'M' and i != 0: + subpaths.append(raw[prev:i]) + prev = i + subpaths.append(raw[prev:]) + + seg = [] + for simpath in subpaths: + closed = False + if simpath[-1][0] == 'Z': + closed = True + if simpath[-2][0] == 'L': simpath[-1][1] = simpath[0][1] + else: simpath.pop() + for i in range(len(simpath)): + if simpath[i][0] == 'V': # vertical and horizontal lines only have one point in args, but 2 are required + #inkex.utils.debug(simpath[i][0]) + simpath[i][0]='L' #overwrite V with regular L command + add=simpath[i-1][1][0] #read the X value from previous segment + simpath[i][1].append(simpath[i][1][0]) #add the second (missing) argument by taking argument from previous segment + simpath[i][1][0]=add #replace with recent X after Y was appended + if simpath[i][0] == 'H': # vertical and horizontal lines only have one point in args, but 2 are required + #inkex.utils.debug(simpath[i][0]) + simpath[i][0]='L' #overwrite H with regular L command + simpath[i][1].append(simpath[i-1][1][1]) #add the second (missing) argument by taking argument from previous segment + #inkex.utils.debug(simpath[i]) + seg.append(simpath[i]) + curr.set("d", Path(seg)) + +VHToLine().run() \ No newline at end of file