Added offset paths

This commit is contained in:
Mario Voigt 2020-08-23 03:13:23 +02:00
parent 67de370574
commit b9fb5042e4
2 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Ofsplot (Offset Paths)</name>
<id>fablabchemnitz.de.ofsplot</id>
<param name="count" type="int" min="0" max="100000" gui-text="Number of offset paths:">10</param>
<param name="offset" type="float" min="-1000" max="+1000" gui-text="Offset between two paths:">0.8</param>
<param name="init_offset" type="float" min="-1000" max="+1000" gui-text="Initial offset from original path:">0.8</param>
<param name="copy_org" type="bool" gui-text="Copy original path (=keep it)">false</param>
<param name="offset_increase" type="float" min="-1000" max="+1000" gui-text="Offset increase per iteration:">0.8</param>
<param name="miterlimit" type="float" min="0.0" max="1000" gui-text="Miter limit">3.0</param>
<param name="clipperscale" type="float" min="0.0" max="9999.0" gui-text="Scaling factor">1024.0</param>
<param name="jointype" appearance="combo" type="optiongroup" default="2" gui-text="Join type">
<option value="0">Square</option>
<option value="1">Round</option>
<option value="2">Miter</option>
</param>
<param name="settingsHelp" type="description">Python library pyclipper needs to be installed. Use Flatten Bezier plugin in advance of this plugin.</param>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu name="FabLab Chemnitz">
<submenu name="Shape/Pattern from existing Path(s)"/>
</submenu>
</effects-menu>
</effect>
<script>
<command location="inx" interpreter="python">fablabchemnitz_ofsplot.py</command>
</script>
</inkscape-extension>

View File

@ -0,0 +1,82 @@
#!/usr/bin/env python3
import inkex
import math
from inkex.paths import CubicSuperPath
import re
import pyclipper
class ofsplot(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.arg_parser.add_argument("--count", type=int, default=10, help="Number of offset operations")
self.arg_parser.add_argument("--offset", type=float, default=2.0, help="Offset amount")
self.arg_parser.add_argument("--init_offset", type=float, default=2.0, help="Initial Offset Amount")
self.arg_parser.add_argument("--copy_org", type=inkex.Boolean, default=True, help="copy original path")
self.arg_parser.add_argument("--offset_increase", type=float, default=2.0, help="Offset increase between iterations")
self.arg_parser.add_argument("--jointype", default="2", help="Join type")
self.arg_parser.add_argument("--miterlimit", type=float, default=3.0, help="Miter limit")
self.arg_parser.add_argument("--clipperscale", type=float, default=1024.0, help="Scaling factor")
def effect(self):
paths = self.svg.selection.filter(inkex.PathElement)
if len(paths) == 0:
inkex.errormsg("No paths selected.")
exit()
for path in paths:
if path.tag == inkex.addNS('path','svg'):
p = CubicSuperPath(path.get('d'))
scale_factor = self.options.clipperscale # 2 ** 32 = 1024 - see also https://github.com/fonttools/pyclipper/wiki/Deprecating-SCALING_FACTOR
pco = pyclipper.PyclipperOffset(self.options.miterlimit)
JT = pyclipper.JT_MITER
if self.options.jointype == "0":
JT = pyclipper.JT_SQUARE
elif self.options.jointype == "1":
JT = pyclipper.JT_ROUND
elif self.options.jointype == "2":
JT = pyclipper.JT_MITER
new = []
# load in initial paths
for sub in p:
sub_simple = []
for item in sub:
itemx = [float(z)*scale_factor for z in item[1]]
sub_simple.append(itemx)
pco.AddPath(sub_simple, JT, pyclipper.ET_CLOSEDPOLYGON)
# calculate offset paths for different offset amounts
offset_list = []
offset_list.append(self.options.init_offset)
for i in range(0,self.options.count+1):
ofs_inc = +math.pow(float(i)*self.options.offset_increase,2)
if self.options.offset_increase <0:
ofs_inc = -ofs_inc
offset_list.append(offset_list[0]+float(i)*self.options.offset+ofs_inc)
solutions = []
for offset in offset_list:
solution = pco.Execute(offset * scale_factor)
solutions.append(solution)
if len(solution)<=0:
continue # no more loops to go, will provide no results.
# re-arrange solutions to fit expected format & add to array
for solution in solutions:
for sol in solution:
solx = [[float(s[0]) / scale_factor, float(s[1]) / scale_factor] for s in sol]
sol_p = [[a,a,a] for a in solx]
sol_p.append(sol_p[0][:])
new.append(sol_p)
# add old, just to keep (make optional!)
if self.options.copy_org:
for sub in p:
new.append(sub)
path.set('d',CubicSuperPath(new))
ofsplot().run()