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, penner@vaxxine.com
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 '''
22 import inkex, simplepath, cubicsuperpath, dxf_templates
24 class MyEffect(inkex.Effect):
25 def __init__(self):
26 inkex.Effect.__init__(self)
27 self.dxf = []
28 self.handle = 255 # initiallize handle for DXF ENTITY
29 def output(self):
30 print ''.join(self.dxf)
31 def dxf_add(self, str):
32 self.dxf.append(str)
33 def dxf_line(self,csp):
34 self.dxf_add(" 0\nLINE\n 5\n%x\n100\nAcDbEntity\n 8\n0\n100\nAcDbLine\n" % self.handle)
35 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]))
36 def dxf_spline(self,csp):
37 knots = 8
38 ctrls = 4
39 self.dxf_add(" 0\nSPLINE\n 5\n%x\n100\nAcDbEntity\n 8\n0\n100\nAcDbSpline\n" % self.handle)
40 self.dxf_add(" 70\n8\n 71\n3\n 72\n%d\n 73\n%d\n 74\n0\n" % (knots, ctrls))
41 for i in range(2):
42 for j in range(4):
43 self.dxf_add(" 40\n%d\n" % i)
44 for i in csp:
45 self.dxf_add(" 10\n%f\n 20\n%f\n 30\n0.0\n" % (i[0],i[1]))
46 def effect(self):
47 #References: Minimum Requirements for Creating a DXF File of a 3D Model By Paul Bourke
48 # NURB Curves: A Guide for the Uninitiated By Philip J. Schneider
49 self.dxf_add("999\nDXF created by Inkscape\n")
50 self.dxf_add(dxf_templates.r14_header)
52 scale = 25.4/90.0
53 h = inkex.unittouu(self.document.getroot().xpath('@height', namespaces=inkex.NSS)[0])
54 path = '//svg:path'
55 for node in self.document.getroot().xpath(path, namespaces=inkex.NSS):
56 d = node.get('d')
57 sim = simplepath.parsePath(d)
58 simplepath.scalePath(sim,scale,-scale)
59 simplepath.translatePath(sim,0,h*scale)
60 p = cubicsuperpath.CubicSuperPath(sim)
61 for sub in p:
62 for i in range(len(sub)-1):
63 # generate unique handle for DXF ENTITY
64 self.handle += 1
65 s = sub[i]
66 e = sub[i+1]
67 if s[1] == s[2] and e[0] == e[1]:
68 self.dxf_line([s[1],e[1]])
69 else:
70 self.dxf_spline([s[1],s[2],e[0],e[1]])
72 self.dxf_add(dxf_templates.r14_footer)
74 e = MyEffect()
75 e.affect()