Code

add support for ROBO-Master multi-spline
[inkscape.git] / share / extensions / dxf_outlines.py
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
26 try:
27     from numpy import *
28     from numpy.linalg import solve
29 except:
30     inkex.errormsg("Failed to import the numpy or numpy.linalg modules. These modules are required by this extension. Please install them and try again.")
31     inkex.sys.exit()
33 def pointdistance((x1,y1),(x2,y2)):
34     return math.sqrt(((x2 - x1) ** 2) + ((y2 - y1) ** 2))
36 def get_fit(u, csp, col):
37     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]
39 def get_matrix(u, i, j):
40     if j == i + 2:
41         return (u[i]-u[i-1])*(u[i]-u[i-1])/(u[i+2]-u[i-1])/(u[i+1]-u[i-1])
42     elif j == i + 1:
43         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])
44     elif j == i:
45         return (u[i+1]-u[i])*(u[i+1]-u[i])/(u[i+1]-u[i-2])/(u[i+1]-u[i-1])
46     else:
47         return 0
49 class MyEffect(inkex.Effect):
50     def __init__(self):
51         inkex.Effect.__init__(self)
52         self.OptionParser.add_option("-R", "--ROBO", action="store", type="string", dest="ROBO")
53         self.dxf = []
54         self.handle = 255                       # handle for DXF ENTITY
55         self.csp_old = [[0.0,0.0]]*4            # previous spline
56         self.d = array([0], float)              # knot vector
57     def output(self):
58         print ''.join(self.dxf)
59     def dxf_add(self, str):
60         self.dxf.append(str)
61     def dxf_line(self,csp):
62         self.dxf_add("  0\nLINE\n  5\n%x\n100\nAcDbEntity\n  8\n0\n100\nAcDbLine\n" % self.handle)
63         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]))
64     def dxf_spline(self,csp):
65         knots = 8
66         ctrls = 4
67         self.dxf_add("  0\nSPLINE\n  5\n%x\n100\nAcDbEntity\n  8\n0\n100\nAcDbSpline\n" % self.handle)
68         self.dxf_add(" 70\n8\n 71\n3\n 72\n%d\n 73\n%d\n 74\n0\n" % (knots, ctrls))
69         for i in range(2):
70             for j in range(4):
71                 self.dxf_add(" 40\n%d\n" % i)
72         for i in csp:
73             self.dxf_add(" 10\n%f\n 20\n%f\n 30\n0.0\n" % (i[0],i[1]))
74     def ROBO_spline(self,csp):
75         # this spline has zero curvature at the endpoints, as in ROBO-Master
76         if (abs(csp[0][0] - self.csp_old[3][0]) > .0001
77             or abs(csp[0][1] - self.csp_old[3][1]) > .0001
78             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):
79             self.ROBO_output()                              # terminate current spline
80             self.xfit = array([csp[0][0]], float)           # initiallize new spline
81             self.yfit = array([csp[0][1]], float)
82             self.d = array([0], float)
83         self.xfit = concatenate((self.xfit, zeros((3))))    # append to current spline
84         self.yfit = concatenate((self.yfit, zeros((3))))
85         self.d = concatenate((self.d, zeros((3))))
86         for i in range(1, 4):
87             j = len(self.d) + i - 4
88             self.xfit[j] = get_fit(i/3.0, csp, 0)
89             self.yfit[j] = get_fit(i/3.0, csp, 1)
90             self.d[j] = self.d[j-1] + pointdistance((self.xfit[j-1],self.yfit[j-1]),(self.xfit[j],self.yfit[j]))
91         self.csp_old = csp
92     def ROBO_output(self):
93         if len(self.d) == 1:
94             return
95         fits = len(self.d)
96         ctrls = fits + 2
97         knots = ctrls + 4
98         self.xfit = concatenate((self.xfit, zeros((2))))    # pad with 2 endpoint constraints
99         self.yfit = concatenate((self.yfit, zeros((2))))    # pad with 2 endpoint constraints
100         self.d = concatenate((self.d, zeros((6))))          # pad with 3 duplicates at each end
101         self.d[fits+2] = self.d[fits+1] = self.d[fits] = self.d[fits-1]
102         solmatrix = zeros((ctrls,ctrls), dtype=float)
103         for i in range(fits):
104             solmatrix[i,i]   = get_matrix(self.d, i, i)
105             solmatrix[i,i+1] = get_matrix(self.d, i, i+1)
106             solmatrix[i,i+2] = get_matrix(self.d, i, i+2)
107         solmatrix[fits, 0]   = self.d[2]/self.d[fits-1]     # curvature at start = 0
108         solmatrix[fits, 1]   = -(self.d[1] + self.d[2])/self.d[fits-1]
109         solmatrix[fits, 2]   = self.d[1]/self.d[fits-1]
110         solmatrix[fits+1, fits-1] = (self.d[fits-1] - self.d[fits-2])/self.d[fits-1]   # curvature at end = 0
111         solmatrix[fits+1, fits]   = (self.d[fits-3] + self.d[fits-2] - 2*self.d[fits-1])/self.d[fits-1]
112         solmatrix[fits+1, fits+1] = (self.d[fits-1] - self.d[fits-3])/self.d[fits-1]
113         xctrl = solve(solmatrix, self.xfit)
114         yctrl = solve(solmatrix, self.yfit)
115         self.dxf_add("  0\nSPLINE\n  5\n%x\n100\nAcDbEntity\n  8\n0\n100\nAcDbSpline\n" % self.handle)
116         self.dxf_add(" 70\n0\n 71\n3\n 72\n%d\n 73\n%d\n 74\n%d\n" % (knots, ctrls, fits))
117         for i in range(knots):
118             self.dxf_add(" 40\n%f\n" % self.d[i-3])
119         for i in range(ctrls):
120             self.dxf_add(" 10\n%f\n 20\n%f\n 30\n0.0\n" % (xctrl[i],yctrl[i]))
121         for i in range(fits):
122             self.dxf_add(" 11\n%f\n 21\n%f\n 31\n0.0\n" % (self.xfit[i],self.yfit[i]))
124     def effect(self):
125         #References:   Minimum Requirements for Creating a DXF File of a 3D Model By Paul Bourke
126         #              NURB Curves: A Guide for the Uninitiated By Philip J. Schneider
127         #              The NURBS Book By Les Piegl and Wayne Tiller (Springer, 1995)
128         self.dxf_add("999\nDXF created by Inkscape\n")
129         self.dxf_add(dxf_templates.r14_header)
131         scale = 25.4/90.0
132         h = inkex.unittouu(self.document.getroot().xpath('@height', namespaces=inkex.NSS)[0])
133         path = '//svg:path'
134         for node in self.document.getroot().xpath(path, namespaces=inkex.NSS):
135             d = node.get('d')
136             sim = simplepath.parsePath(d)
137             simplepath.scalePath(sim,scale,-scale)
138             simplepath.translatePath(sim,0,h*scale)            
139             p = cubicsuperpath.CubicSuperPath(sim)
140             for sub in p:
141                 for i in range(len(sub)-1):
142                     # generate unique handle for DXF ENTITY
143                     self.handle += 1
144                     s = sub[i]
145                     e = sub[i+1]
146                     if s[1] == s[2] and e[0] == e[1]:
147                         self.dxf_line([s[1],e[1]])
148                     elif (self.options.ROBO == 'true'):
149                         self.ROBO_spline([s[1],s[2],e[0],e[1]])
150                     else:
151                         self.dxf_spline([s[1],s[2],e[0],e[1]])
152         if self.options.ROBO == 'true':
153             self.handle += 1
154             self.ROBO_output()
155         self.dxf_add(dxf_templates.r14_footer)
157 if __name__ == '__main__':
158     e = MyEffect()
159     e.affect()
162 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99