Code

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