Code

(no commit message)
[inkscape.git] / share / extensions / dxf_outlines.py
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, cubicsuperpath
21 class MyEffect(inkex.Effect):
22         def __init__(self):
23                 inkex.Effect.__init__(self)
24                 self.dxf = ''
25         def output(self):
26                 print self.dxf
27         def dxf_add(self, str):
28                 self.dxf += str
29         def dxf_line(self,csp):
30                 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])
31                 self.dxf_add(line)
32         def dxf_spline(self,csp):
33                 knots = 8
34                 ctrls = 4
35                 self.dxf_add("\n  0\nSPLINE\n  5\n43\n  8\n0\n 62\n256\n370\n-1\n  6\nByLayer")
36                 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))
37                 for i in range(2):
38                         for j in range(4): 
39                                 self.dxf_add("\n 40\n%d" % i)
40                 for i in csp:
41                          self.dxf_add("\n 10\n%f\n 20\n%f\n 30\n0" % (i[0],i[1]))
42         def effect(self):
43                 #References:   Minimum Requirements for Creating a DXF File of a 3D Model By Paul Bourke
44                 #              NURB Curves: A Guide for the Uninitiated By Philip J. Schneider
45                 self.dxf_add("999\nDXF created by Inkscape\n0\nSECTION\n2\nENTITIES")
47                 path = '//path'
48                 for node in inkex.xml.xpath.Evaluate(path,self.document):
49                         p = cubicsuperpath.parsePath(node.attributes.getNamedItem('d').value)
50                         for sub in p:
51                                 for i in range(len(sub)-1):
52                                         s = sub[i]
53                                         e = sub[i+1]
54                                         if s[1] == s[2] and e[0] == e[1]:
55                                                 self.dxf_line([s[1],e[1]])
56                                         else:
57                                                 self.dxf_spline([s[1],s[2],e[0],e[1]])
58                 self.dxf_add("\n0\nENDSEC\n0\nEOF\n")
59                         
61 e = MyEffect()
62 e.affect()