Code

add scaling and translation so that the DXFs open properly in robomaster
[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, simplepath, 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")\r
46         \r
47         scale = 5.0/18.0
48         h = float(inkex.xml.xpath.Evaluate('/svg/@height',self.document)[0].value)\r
50         path = '//path'
51         for node in inkex.xml.xpath.Evaluate(path,self.document):\r
52             d = node.attributes.getNamedItem('d').value\r
53             sim = simplepath.parsePath(d)\r
54             simplepath.scalePath(sim,scale,-scale)\r
55             simplepath.translatePath(sim,0,h*scale)            
56             p = cubicsuperpath.CubicSuperPath(sim)
57             for sub in p:
58                 for i in range(len(sub)-1):
59                     s = sub[i]
60                     e = sub[i+1]
61                     if s[1] == s[2] and e[0] == e[1]:
62                         self.dxf_line([s[1],e[1]])
63                     else:
64                         self.dxf_spline([s[1],s[2],e[0],e[1]])
65         self.dxf_add("\n0\nENDSEC\n0\nEOF\n")
68 e = MyEffect()
69 e.affect()