-
Notifications
You must be signed in to change notification settings - Fork 99
/
close_curves.py
146 lines (101 loc) · 4.11 KB
/
close_curves.py
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
#!/usr/bin/env python
"""
Copyright (C) 2009 Nick Drobchenko, [email protected]
based on gcode.py (C) 2007 hugomatic...
based on addnodes.py (C) 2005,2007 Aaron Spike, [email protected]
based on dots.py (C) 2005 Aaron Spike, [email protected]
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
#
# This extension close paths by adding 'z' before each 'm' if it is not
# the first 'm' and it is not prepended by 'z' already.
#
import inkex, re, cubicsuperpath
class CloseCurves(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.OptionParser.add_option("-s", "--subpaths", action="store", type="inkbool", dest="subpaths", default=True)
self.OptionParser.add_option("-f", "--tolerance", action="store", type="float", dest="tolerance", default=".01")
def effect(self):
def point_d2(p1,p2):
return (p1[0]-p2[0])**2+(p1[1]-p2[1])**2
def close_csp(csp, tolerance):
csp = [i for i in csp if i!=[]]
done_smth = True
while done_smth:
done_smth = False
i = 0
while i<len(csp) :
j = i+1
while j<len(csp) :
#inkex.errormsg("%s \n---------\n %s\n\n"%(csp[i],csp[j]))
if point_d2(csp[i][0][1],csp[j][-1][1])<tolerance :
csp[i][0][0] = csp[i][0][1]
csp[j][-1][2] = csp[j][-1][1]
csp[i] = csp[j] + csp[i]
csp[j:j+1] = []
done_smth = True
continue
if point_d2(csp[i][-1][1],csp[j][0][1])<tolerance :
csp[i][-1][2] = csp[i][-1][1]
csp[j][0][0] = csp[j][1][1]
csp[i] = csp[i] + csp[j]
csp[j:j+1] = []
done_smth = True
continue
if point_d2(csp[i][0][1],csp[j][0][1])<tolerance :
csp[i][0][0] = csp[i][0][1]
csp[j][0][0] = csp[j][1][1]
csp[i] = reverse_csp([csp[i]])[0]
csp[i] = csp[i] + csp[j]
csp[j:j+1] = []
done_smth = True
continue
if point_d2(csp[i][-1][1],csp[j][-1][1])<tolerance :
csp[i][-1][2] = csp[i][-1][1]
csp[j][-1][2] = csp[j][-1][1]
csp[i] = reverse_csp([csp[j]])[0]
csp[i] = csp[i] + csp[j]
csp[j:j+1] = []
done_smth = True
continue
j += 1
i += 1
return csp
def reverse_csp(csp) :
res = [ [ [point[2],point[1],point[0]] for point in subpath] for subpath in csp]
res.reverse()
return res
if self.options.subpaths :
csp = []
for id, node in self.selected.iteritems():
if node.tag == inkex.addNS('path','svg'):
if "d" not in node.keys() :
inkex.errormsg("Warning: One or more paths do not have 'd' parameter, try to Ungroup (Ctrl+Shift+G) and Object to Path (Ctrl+Shift+C)!"+"\n")
continue
csp += cubicsuperpath.parsePath(node.get('d'))
csp = close_csp(csp,self.options.tolerance**2)
attributes = { 'd': cubicsuperpath.formatPath(csp) }
inkex.etree.SubElement( self.document.getroot(), inkex.addNS('path','svg'), attributes)
else :
for id, node in self.selected.iteritems():
if node.tag == inkex.addNS('path','svg'):
if "d" not in node.keys() :
inkex.errormsg("Warning: One or more paths do not have 'd' parameter, try to Ungroup (Ctrl+Shift+G) and Object to Path (Ctrl+Shift+C)!"+"\n")
continue
csp = cubicsuperpath.parsePath(node.get('d'))
csp = close_csp(csp,self.options.tolerance**2)
attributes = { 'd':cubicsuperpath.formatPath(csp) }
inkex.etree.SubElement( self.document.getroot(), inkex.addNS('path','svg'), attributes)
e = CloseCurves()
e.affect()