Skip to content

Commit dfa0613

Browse files
author
mehtank
committed
Added option to convert-to-dashes before running (resolves #11)
1 parent 5291d3e commit dfa0613

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

sendto_silhouette.inx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Pressure values of 19 or more make the machine misbehave. Beware.
5050
<param name="speed" type="int" min="0" max="10" _gui-text="Speed">0</param>
5151
<param name="pressure" type="int" min="0" max="18" _gui-text="Pressure">0</param>
5252
<param name="speed_help" type="description">Use speed=0, pressure=0 to take the media defaults.</param>
53+
<param name="dashes" type="boolean" _gui-text="Convert to dashes">false</param> <param name="dashes_help" type="description">Convert paths with dashed strokes to separate subpaths for perforated cuts.</param>
5354
<param name="autocrop" type="boolean" _gui-text="Trim margins">false</param> <param name="autocrop_help" type="description">Shift to the top lefthand corner, then do offsets.</param>
5455
<param name="bbox_only" type="boolean" _gui-text="Draft Bounding Box Only">false</param>
5556
<param name="bbox_help" type="description">To see the used area, tick the checkmark above and use pressure=1 (or better remove tool)</param>

sendto_silhouette.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119

120120
from silhouette.Graphtec import SilhouetteCameo
121121
from silhouette.Strategy import MatFree
122+
from silhouette.convert2dashes import splitPath
122123

123124
N_PAGE_WIDTH = 3200
124125
N_PAGE_HEIGHT = 800
@@ -254,6 +255,9 @@ def __init__(self):
254255

255256
self.OptionParser.add_option('--active-tab', action = 'store', dest = 'active_tab',
256257
help=SUPPRESS_HELP)
258+
self.OptionParser.add_option('-d', '--dashes',
259+
action = 'store', dest = 'dashes', type = 'inkbool', default = False,
260+
help='convert paths with dashed strokes to separate subpaths for perforated cuts')
257261
self.OptionParser.add_option('-a', '--autocrop',
258262
action = 'store', dest = 'autocrop', type = 'inkbool', default = False,
259263
help='trim away top and left margin (before adding offsets)')
@@ -508,6 +512,9 @@ def recursivelyTraverseSvg( self, aNodeList,
508512

509513
elif node.tag == inkex.addNS( 'path', 'svg' ):
510514

515+
if self.options.dashes:
516+
splitPath(inkex, node)
517+
511518
self.pathcount += 1
512519

513520
# if we're in resume mode AND self.pathcount < self.svgLastPath,

silhouette/convert2dashes.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python
2+
3+
# Extracted from inkscape extension; original comments below:
4+
'''
5+
This extension converts a path into a dashed line using 'stroke-dasharray'
6+
It is a modification of the file addnodes.py
7+
8+
Copyright (C) 2005,2007 Aaron Spike, aaron@ekips.org
9+
Copyright (C) 2009 Alvin Penner, penner@vaxxine.com
10+
11+
This program is free software; you can redistribute it and/or modify
12+
it under the terms of the GNU General Public License as published by
13+
the Free Software Foundation; either version 2 of the License, or
14+
(at your option) any later version.
15+
16+
This program is distributed in the hope that it will be useful,
17+
but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
GNU General Public License for more details.
20+
21+
You should have received a copy of the GNU General Public License
22+
along with this program; if not, write to the Free Software
23+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24+
'''
25+
# local library
26+
import cubicsuperpath
27+
import bezmisc
28+
import simplestyle
29+
30+
def tpoint((x1,y1), (x2,y2), t = 0.5):
31+
return [x1+t*(x2-x1),y1+t*(y2-y1)]
32+
def cspbezsplit(sp1, sp2, t = 0.5):
33+
m1=tpoint(sp1[1],sp1[2],t)
34+
m2=tpoint(sp1[2],sp2[0],t)
35+
m3=tpoint(sp2[0],sp2[1],t)
36+
m4=tpoint(m1,m2,t)
37+
m5=tpoint(m2,m3,t)
38+
m=tpoint(m4,m5,t)
39+
return [[sp1[0][:],sp1[1][:],m1], [m4,m,m5], [m3,sp2[1][:],sp2[2][:]]]
40+
def cspbezsplitatlength(sp1, sp2, l = 0.5, tolerance = 0.001):
41+
bez = (sp1[1][:],sp1[2][:],sp2[0][:],sp2[1][:])
42+
t = bezmisc.beziertatlength(bez, l, tolerance)
43+
return cspbezsplit(sp1, sp2, t)
44+
def cspseglength(sp1,sp2, tolerance = 0.001):
45+
bez = (sp1[1][:],sp1[2][:],sp2[0][:],sp2[1][:])
46+
return bezmisc.bezierlength(bez, tolerance)
47+
48+
def splitPath(inkex, node):
49+
dashes = []
50+
style = simplestyle.parseStyle(node.get('style'))
51+
if style.has_key('stroke-dasharray'):
52+
if style['stroke-dasharray'].find(',') > 0:
53+
dashes = [float (dash) for dash in style['stroke-dasharray'].split(',') if dash]
54+
if dashes:
55+
p = cubicsuperpath.parsePath(node.get('d'))
56+
new = []
57+
for sub in p:
58+
idash = 0
59+
dash = dashes[0]
60+
length = 0
61+
new.append([sub[0][:]])
62+
i = 1
63+
while i < len(sub):
64+
dash = dash - length
65+
length = cspseglength(new[-1][-1], sub[i])
66+
while dash < length:
67+
new[-1][-1], next, sub[i] = cspbezsplitatlength(new[-1][-1], sub[i], dash/length)
68+
if idash % 2: # create a gap
69+
new.append([next[:]])
70+
else: # splice the curve
71+
new[-1].append(next[:])
72+
length = length - dash
73+
idash = (idash + 1) % len(dashes)
74+
dash = dashes[idash]
75+
if idash % 2:
76+
new.append([sub[i]])
77+
else:
78+
new[-1].append(sub[i])
79+
i+=1
80+
node.set('d',cubicsuperpath.formatPath(new))
81+
del style['stroke-dasharray']
82+
node.set('style', simplestyle.formatStyle(style))
83+
if node.get(inkex.addNS('type','sodipodi')):
84+
del node.attrib[inkex.addNS('type', 'sodipodi')]

0 commit comments

Comments
 (0)