1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2005 Aaron Spike, aaron@ekips.org
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 '''
19 import inkex, simplepath, cubicsuperpath, re
20 \r
21 uuconv = {'in':90.0, 'pt':1.25, 'px':1, 'mm':3.5433070866, 'cm':35.433070866, 'pc':15.0}\r
22 def unittouu(string):\r
23 unit = re.compile('(%s)$' % '|'.join(uuconv.keys()))\r
24 param = re.compile(r'(([-+]?[0-9]+(\.[0-9]*)?|[-+]?\.[0-9]+)([eE][-+]?[0-9]+)?)')\r
25 \r
26 p = param.match(string)\r
27 u = unit.search(string) \r
28 if p:\r
29 retval = float(p.string[p.start():p.end()])\r
30 else:\r
31 retval = 0.0\r
32 if u:\r
33 try:\r
34 return retval * uuconv[u.string[u.start():u.end()]]\r
35 except KeyError:\r
36 pass\r
37 return retval\r
39 class MyEffect(inkex.Effect):
40 def __init__(self):
41 inkex.Effect.__init__(self)
42 self.dxf = ''
43 def output(self):
44 print self.dxf
45 def dxf_add(self, str):
46 self.dxf += str
47 def dxf_line(self,csp):
48 line = "\n0\nLINE\n8\n2\n62\n4\n10\n%f\n20\n%f\n30\n0\n11\n%f\n21\n%f\n31\n0" % (csp[0][0],csp[0][1],csp[1][0],csp[1][1])
49 self.dxf_add(line)
50 def dxf_spline(self,csp):
51 knots = 8
52 ctrls = 4
53 self.dxf_add("\n 0\nSPLINE\n 5\n43\n 8\n0\n 62\n256\n370\n-1\n 6\nByLayer")
54 self.dxf_add("\n100\nAcDbEntity\n100\nAcDbSpline\n 70\n8\n 71\n3\n 72\n%d\n 73\n%d\n 74\n0" % (knots, ctrls))
55 for i in range(2):
56 for j in range(4):
57 self.dxf_add("\n 40\n%d" % i)
58 for i in csp:
59 self.dxf_add("\n 10\n%f\n 20\n%f\n 30\n0" % (i[0],i[1]))
60 def effect(self):
61 #References: Minimum Requirements for Creating a DXF File of a 3D Model By Paul Bourke
62 # NURB Curves: A Guide for the Uninitiated By Philip J. Schneider
63 self.dxf_add("999\nDXF created by Inkscape\n0\nSECTION\n2\nENTITIES")\r
64 \r
65 scale = 25.4/90.0
66 h = unittouu(inkex.xml.xpath.Evaluate('/svg/@height',self.document)[0].value)\r
68 path = '//path'
69 for node in inkex.xml.xpath.Evaluate(path,self.document):\r
70 d = node.attributes.getNamedItem('d').value\r
71 sim = simplepath.parsePath(d)\r
72 simplepath.scalePath(sim,scale,-scale)\r
73 simplepath.translatePath(sim,0,h*scale)
74 p = cubicsuperpath.CubicSuperPath(sim)
75 for sub in p:
76 for i in range(len(sub)-1):
77 s = sub[i]
78 e = sub[i+1]
79 if s[1] == s[2] and e[0] == e[1]:
80 self.dxf_line([s[1],e[1]])
81 else:
82 self.dxf_spline([s[1],s[2],e[0],e[1]])
83 self.dxf_add("\n0\nENDSEC\n0\nEOF\n")
86 e = MyEffect()
87 e.affect()