1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2005,2007,2008 Aaron Spike, aaron@ekips.org
4 Copyright (C) 2008 Alvin Penner, penner@vaxxine.com
6 - template dxf_outlines.dxf added Feb 2008 by Alvin Penner
7 - ROBO-Master output option added Aug 2008 by Alvin Penner
8 - ROBO-Master multispline output added Sept 2008 by Alvin Penner
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 '''
24 import inkex, simplepath, cubicsuperpath, dxf_templates, math
25 import gettext
26 _ = gettext.gettext
28 try:
29 from numpy import *
30 from numpy.linalg import solve
31 except:
32 inkex.errormsg(_("Failed to import the numpy or numpy.linalg modules. These modules are required by this extension. Please install them and try again."))
33 inkex.sys.exit()
35 def pointdistance((x1,y1),(x2,y2)):
36 return math.sqrt(((x2 - x1) ** 2) + ((y2 - y1) ** 2))
38 def get_fit(u, csp, col):
39 return (1-u)**3*csp[0][col] + 3*(1-u)**2*u*csp[1][col] + 3*(1-u)*u**2*csp[2][col] + u**3*csp[3][col]
41 def get_matrix(u, i, j):
42 if j == i + 2:
43 return (u[i]-u[i-1])*(u[i]-u[i-1])/(u[i+2]-u[i-1])/(u[i+1]-u[i-1])
44 elif j == i + 1:
45 return ((u[i]-u[i-1])*(u[i+2]-u[i])/(u[i+2]-u[i-1]) + (u[i+1]-u[i])*(u[i]-u[i-2])/(u[i+1]-u[i-2]))/(u[i+1]-u[i-1])
46 elif j == i:
47 return (u[i+1]-u[i])*(u[i+1]-u[i])/(u[i+1]-u[i-2])/(u[i+1]-u[i-1])
48 else:
49 return 0
51 class MyEffect(inkex.Effect):
52 def __init__(self):
53 inkex.Effect.__init__(self)
54 self.OptionParser.add_option("-R", "--ROBO", action="store", type="string", dest="ROBO")
55 self.dxf = []
56 self.handle = 255 # handle for DXF ENTITY
57 self.csp_old = [[0.0,0.0]]*4 # previous spline
58 self.d = array([0], float) # knot vector
59 def output(self):
60 print ''.join(self.dxf)
61 def dxf_add(self, str):
62 self.dxf.append(str)
63 def dxf_line(self,csp):
64 self.dxf_add(" 0\nLINE\n 5\n%x\n100\nAcDbEntity\n 8\n0\n100\nAcDbLine\n" % self.handle)
65 self.dxf_add(" 10\n%f\n 20\n%f\n 30\n0.0\n 11\n%f\n 21\n%f\n 31\n0.0\n" % (csp[0][0],csp[0][1],csp[1][0],csp[1][1]))
66 def dxf_spline(self,csp):
67 knots = 8
68 ctrls = 4
69 self.dxf_add(" 0\nSPLINE\n 5\n%x\n100\nAcDbEntity\n 8\n0\n100\nAcDbSpline\n" % self.handle)
70 self.dxf_add(" 70\n8\n 71\n3\n 72\n%d\n 73\n%d\n 74\n0\n" % (knots, ctrls))
71 for i in range(2):
72 for j in range(4):
73 self.dxf_add(" 40\n%d\n" % i)
74 for i in csp:
75 self.dxf_add(" 10\n%f\n 20\n%f\n 30\n0.0\n" % (i[0],i[1]))
76 def ROBO_spline(self,csp):
77 # this spline has zero curvature at the endpoints, as in ROBO-Master
78 if (abs(csp[0][0] - self.csp_old[3][0]) > .0001
79 or abs(csp[0][1] - self.csp_old[3][1]) > .0001
80 or abs((csp[1][1]-csp[0][1])*(self.csp_old[3][0]-self.csp_old[2][0]) - (csp[1][0]-csp[0][0])*(self.csp_old[3][1]-self.csp_old[2][1])) > .001):
81 self.ROBO_output() # terminate current spline
82 self.xfit = array([csp[0][0]], float) # initiallize new spline
83 self.yfit = array([csp[0][1]], float)
84 self.d = array([0], float)
85 self.xfit = concatenate((self.xfit, zeros((3)))) # append to current spline
86 self.yfit = concatenate((self.yfit, zeros((3))))
87 self.d = concatenate((self.d, zeros((3))))
88 for i in range(1, 4):
89 j = len(self.d) + i - 4
90 self.xfit[j] = get_fit(i/3.0, csp, 0)
91 self.yfit[j] = get_fit(i/3.0, csp, 1)
92 self.d[j] = self.d[j-1] + pointdistance((self.xfit[j-1],self.yfit[j-1]),(self.xfit[j],self.yfit[j]))
93 self.csp_old = csp
94 def ROBO_output(self):
95 if len(self.d) == 1:
96 return
97 fits = len(self.d)
98 ctrls = fits + 2
99 knots = ctrls + 4
100 self.xfit = concatenate((self.xfit, zeros((2)))) # pad with 2 endpoint constraints
101 self.yfit = concatenate((self.yfit, zeros((2)))) # pad with 2 endpoint constraints
102 self.d = concatenate((self.d, zeros((6)))) # pad with 3 duplicates at each end
103 self.d[fits+2] = self.d[fits+1] = self.d[fits] = self.d[fits-1]
104 solmatrix = zeros((ctrls,ctrls), dtype=float)
105 for i in range(fits):
106 solmatrix[i,i] = get_matrix(self.d, i, i)
107 solmatrix[i,i+1] = get_matrix(self.d, i, i+1)
108 solmatrix[i,i+2] = get_matrix(self.d, i, i+2)
109 solmatrix[fits, 0] = self.d[2]/self.d[fits-1] # curvature at start = 0
110 solmatrix[fits, 1] = -(self.d[1] + self.d[2])/self.d[fits-1]
111 solmatrix[fits, 2] = self.d[1]/self.d[fits-1]
112 solmatrix[fits+1, fits-1] = (self.d[fits-1] - self.d[fits-2])/self.d[fits-1] # curvature at end = 0
113 solmatrix[fits+1, fits] = (self.d[fits-3] + self.d[fits-2] - 2*self.d[fits-1])/self.d[fits-1]
114 solmatrix[fits+1, fits+1] = (self.d[fits-1] - self.d[fits-3])/self.d[fits-1]
115 xctrl = solve(solmatrix, self.xfit)
116 yctrl = solve(solmatrix, self.yfit)
117 self.dxf_add(" 0\nSPLINE\n 5\n%x\n100\nAcDbEntity\n 8\n0\n100\nAcDbSpline\n" % self.handle)
118 self.dxf_add(" 70\n0\n 71\n3\n 72\n%d\n 73\n%d\n 74\n%d\n" % (knots, ctrls, fits))
119 for i in range(knots):
120 self.dxf_add(" 40\n%f\n" % self.d[i-3])
121 for i in range(ctrls):
122 self.dxf_add(" 10\n%f\n 20\n%f\n 30\n0.0\n" % (xctrl[i],yctrl[i]))
123 for i in range(fits):
124 self.dxf_add(" 11\n%f\n 21\n%f\n 31\n0.0\n" % (self.xfit[i],self.yfit[i]))
126 def effect(self):
127 #References: Minimum Requirements for Creating a DXF File of a 3D Model By Paul Bourke
128 # NURB Curves: A Guide for the Uninitiated By Philip J. Schneider
129 # The NURBS Book By Les Piegl and Wayne Tiller (Springer, 1995)
130 self.dxf_add("999\nDXF created by Inkscape\n")
131 self.dxf_add(dxf_templates.r14_header)
133 scale = 25.4/90.0
134 h = inkex.unittouu(self.document.getroot().xpath('@height', namespaces=inkex.NSS)[0])
135 path = '//svg:path'
136 for node in self.document.getroot().xpath(path, namespaces=inkex.NSS):
137 d = node.get('d')
138 sim = simplepath.parsePath(d)
139 if len(sim):
140 simplepath.scalePath(sim,scale,-scale)
141 simplepath.translatePath(sim,0,h*scale)
142 p = cubicsuperpath.CubicSuperPath(sim)
143 for sub in p:
144 for i in range(len(sub)-1):
145 # generate unique handle for DXF ENTITY
146 self.handle += 1
147 s = sub[i]
148 e = sub[i+1]
149 if s[1] == s[2] and e[0] == e[1]:
150 self.dxf_line([s[1],e[1]])
151 elif (self.options.ROBO == 'true'):
152 self.ROBO_spline([s[1],s[2],e[0],e[1]])
153 else:
154 self.dxf_spline([s[1],s[2],e[0],e[1]])
155 if self.options.ROBO == 'true':
156 self.handle += 1
157 self.ROBO_output()
158 self.dxf_add(dxf_templates.r14_footer)
160 if __name__ == '__main__':
161 e = MyEffect()
162 e.affect()
165 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99