Code

Merge from fe-moved
[inkscape.git] / share / extensions / dxf_input.py
1 #!/usr/bin/env python\r
2 '''\r
3 dxf_input.py - input a DXF file >= (AutoCAD Release 13 == AC1012)\r
4 \r
5 Copyright (C) 2008 Alvin Penner, penner@vaxxine.com\r
6 \r
7 This program is free software; you can redistribute it and/or modify\r
8 it under the terms of the GNU General Public License as published by\r
9 the Free Software Foundation; either version 2 of the License, or\r
10 (at your option) any later version.\r
11 \r
12 This program is distributed in the hope that it will be useful,\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 GNU General Public License for more details.\r
16 \r
17 You should have received a copy of the GNU General Public License\r
18 along with this program; if not, write to the Free Software\r
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
20 '''\r
21 \r
22 import inkex, simplestyle, math\r
23 from StringIO import StringIO\r
24 \r
25 def export_MTEXT():\r
26     # mandatory group codes : (1, 10, 20) (text, x, y)\r
27     if vals[groups['1']] and vals[groups['10']] and vals[groups['20']]:\r
28         x = vals[groups['10']][0]\r
29         y = vals[groups['20']][0]\r
30         # optional group codes : (40, 50) (text height mm, text angle)\r
31         size = 12                       # default fontsize in px\r
32         if vals[groups['40']]:\r
33             size = scale*vals[groups['40']][0]\r
34         attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %dpx' % size}\r
35         angle = 0                       # default angle in degrees\r
36         if vals[groups['50']]:\r
37             angle = vals[groups['50']][0]\r
38             attribs.update({'transform': 'rotate (%f %f %f)' % (-angle, x, y)})\r
39         node = inkex.etree.SubElement(doc.getroot(), 'text', attribs)\r
40         node.text = vals[groups['1']][0]\r
41 \r
42 def export_POINT():\r
43     # mandatory group codes : (10, 20) (x, y)\r
44     if vals[groups['10']] and vals[groups['20']]:\r
45         generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], w/2, 0.0, 1.0, 0.0, 0.0)\r
46 \r
47 def export_LINE():\r
48     # mandatory group codes : (10, 11, 20, 21) (x1, x2, y1, y2)\r
49     if vals[groups['10']] and vals[groups['11']] and vals[groups['20']] and vals[groups['21']]:\r
50         path = 'M %f,%f %f,%f' % (vals[groups['10']][0], vals[groups['20']][0], scale*vals[groups['11']][0], height - scale*vals[groups['21']][0])\r
51         attribs = {'d': path, 'style': style}\r
52         inkex.etree.SubElement(doc.getroot(), 'path', attribs)\r
53 \r
54 def export_SPLINE():\r
55     # mandatory group codes : (10, 20, 70) (x, y, flags)\r
56     if vals[groups['10']] and vals[groups['20']] and vals[groups['70']]:\r
57         if vals[groups['70']][0] == 8 and len(vals[groups['10']]) == 4 and len(vals[groups['20']]) == 4:\r
58             path = 'M %f,%f C %f,%f %f,%f %f,%f' % (vals[groups['10']][0], vals[groups['20']][0], vals[groups['10']][1], vals[groups['20']][1], vals[groups['10']][2], vals[groups['20']][2], vals[groups['10']][3], vals[groups['20']][3])\r
59             attribs = {'d': path, 'style': style}\r
60             inkex.etree.SubElement(doc.getroot(), 'path', attribs)\r
61 \r
62 def export_CIRCLE():\r
63     # mandatory group codes : (10, 20, 40) (x, y, radius)\r
64     if vals[groups['10']] and vals[groups['20']] and vals[groups['40']]:\r
65         generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], scale*vals[groups['40']][0], 0.0, 1.0, 0.0, 0.0)\r
66 \r
67 def export_ARC():\r
68     # mandatory group codes : (10, 20, 40, 50, 51) (x, y, radius, angle1, angle2)\r
69     if vals[groups['10']] and vals[groups['20']] and vals[groups['40']] and vals[groups['50']] and vals[groups['51']]:\r
70         generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], scale*vals[groups['40']][0], 0.0, 1.0, vals[groups['50']][0]*math.pi/180.0, vals[groups['51']][0]*math.pi/180.0)\r
71 \r
72 def export_ELLIPSE():\r
73     # mandatory group codes : (10, 11, 20, 21, 40, 41, 42) (xc, xm, yc, ym, width ratio, angle1, angle2)\r
74     if vals[groups['10']] and vals[groups['11']] and vals[groups['20']] and vals[groups['21']] and vals[groups['40']] and vals[groups['41']] and vals[groups['42']]:\r
75         generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], scale*vals[groups['11']][0], scale*vals[groups['21']][0], vals[groups['40']][0], vals[groups['41']][0], vals[groups['42']][0])\r
76 \r
77 def export_LEADER():\r
78     # mandatory group codes : (10, 20) (x, y)\r
79     if vals[groups['10']] and vals[groups['20']]:\r
80         if len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):\r
81             path = 'M %f,%f' % (vals[groups['10']][0], vals[groups['20']][0])\r
82             for i in range (1, len(vals[groups['10']])):\r
83                 path += ' %f,%f' % (vals[groups['10']][i], vals[groups['20']][i])\r
84             attribs = {'d': path, 'style': style}\r
85             inkex.etree.SubElement(doc.getroot(), 'path', attribs)\r
86 \r
87 def export_LWPOLYLINE():\r
88     # mandatory group codes : (10, 20, 70) (x, y, flags)\r
89     if vals[groups['10']] and vals[groups['20']] and vals[groups['70']]:\r
90         if len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):\r
91             # optional group codes : (42) (bulge)\r
92             iseqs = 0\r
93             ibulge = 0\r
94             while seqs[iseqs] != '20':\r
95                 iseqs += 1\r
96             path = 'M %f,%f' % (vals[groups['10']][0], vals[groups['20']][0])\r
97             xold = vals[groups['10']][0]\r
98             yold = vals[groups['20']][0]\r
99             for i in range (1, len(vals[groups['10']])):\r
100                 bulge = 0\r
101                 iseqs += 1\r
102                 while seqs[iseqs] != '20':\r
103                     if seqs[iseqs] == '42':\r
104                         bulge = vals[groups['42']][ibulge]\r
105                         ibulge += 1\r
106                     iseqs += 1\r
107                 if bulge:\r
108                     sweep = 0                   # sweep CCW\r
109                     if bulge < 0:\r
110                         sweep = 1               # sweep CW\r
111                         bulge = -bulge\r
112                     large = 0                   # large-arc-flag\r
113                     if bulge > 1:\r
114                         large = 1\r
115                     r = math.sqrt((vals[groups['10']][i] - xold)**2 + (vals[groups['20']][i] - yold)**2)\r
116                     r = 0.25*r*(bulge + 1.0/bulge)\r
117                     path += ' A %f,%f 0.0 %d %d %f,%f' % (r, r, large, sweep, vals[groups['10']][i], vals[groups['20']][i])\r
118                 else:\r
119                     path += ' L %f,%f' % (vals[groups['10']][i], vals[groups['20']][i])\r
120                 xold = vals[groups['10']][i]\r
121                 yold = vals[groups['20']][i]\r
122             if vals[groups['70']][0] == 1:      # closed path\r
123                 path += ' z'\r
124             attribs = {'d': path, 'style': style}\r
125             inkex.etree.SubElement(doc.getroot(), 'path', attribs)\r
126 \r
127 def generate_ellipse(xc, yc, xm, ym, w, a1, a2):\r
128     rm = math.sqrt(xm*xm + ym*ym)\r
129     a = math.atan(ym/xm)\r
130     if xm < 0:\r
131         a += math.pi\r
132     diff = (a2 - a1 + 2*math.pi) % (2*math.pi)\r
133     if diff:                        # open arc\r
134         large = 0                   # large-arc-flag\r
135         if diff > math.pi:\r
136             large = 1\r
137         xt = rm*math.cos(a1)\r
138         yt = w*rm*math.sin(a1)\r
139         x1 = xt*math.cos(a) - yt*math.sin(a)\r
140         y1 = xt*math.sin(a) + yt*math.cos(a)\r
141         xt = rm*math.cos(a2)\r
142         yt = w*rm*math.sin(a2)\r
143         x2 = xt*math.cos(a) - yt*math.sin(a)\r
144         y2 = xt*math.sin(a) + yt*math.cos(a)\r
145         path = 'M %f,%f A %f,%f %f %d 0 %f,%f' % (xc+x1, yc-y1, rm, w*rm, -180.0*a/math.pi, large, xc+x2, yc-y2)\r
146     else:                           # closed arc\r
147         path = 'M %f,%f A %f,%f %f 1 0 %f,%f %f,%f %f 1 0 %f,%f z' % (xc+xm, yc-ym, rm, w*rm, -180.0*a/math.pi, xc-xm, yc+ym, rm, w*rm, -180.0*a/math.pi, xc+xm, yc-ym)\r
148     attribs = {'d': path, 'style': style}\r
149     inkex.etree.SubElement(doc.getroot(), 'path', attribs)\r
150 \r
151 def get_line():\r
152     return (stream.readline().strip(), stream.readline().strip())\r
153 \r
154 #   define DXF Entities and specify which Group Codes to monitor\r
155 \r
156 entities = {'MTEXT': export_MTEXT, 'POINT': export_POINT, 'LINE': export_LINE, 'SPLINE': export_SPLINE, 'CIRCLE': export_CIRCLE, 'ARC': export_ARC, 'ELLIPSE': export_ELLIPSE, 'LEADER': export_LEADER, 'LWPOLYLINE': export_LWPOLYLINE, 'ENDSEC': ''}\r
157 groups = {'1': 0, '10': 1, '11': 2, '20': 3, '21': 4, '40': 5, '41': 6, '42': 7, '50': 8, '51': 9, '70': 10, '370': 11}\r
158 scale = 90.0/25.4                                   # convert from mm to pixels\r
159 height = 90.0*11.0                                  # 11 inch height in pixels\r
160 entity = ''\r
161 \r
162 doc = inkex.etree.parse(StringIO('<svg></svg>'))\r
163 stream = open(inkex.sys.argv[1], 'r')\r
164 line = get_line()\r
165 while line[0] and line[1] != 'ENTITIES':\r
166     line = get_line()\r
167 \r
168 while line[0] and line[1] != 'ENDSEC':\r
169     line = get_line()\r
170     if entity and groups.has_key(line[0]):\r
171         seqs.append(line[0])                        # list of group codes\r
172         if line[0] == '1':                          # text value\r
173             val = line[1].replace('\~', ' ')\r
174         elif line[0] == '70':                       # unscaled integer value\r
175             val = int(line[1])\r
176         elif line[0] == '10':                       # scaled float x value\r
177             val = scale*float(line[1])\r
178         elif line[0] == '20':                       # scaled float y value\r
179             val = height - scale*float(line[1])\r
180         else:                                       # unscaled float value\r
181             val = float(line[1])\r
182         vals[groups[line[0]]].append(val)\r
183     elif entities.has_key(line[1]):\r
184         if entities.has_key(entity):\r
185             style = simplestyle.formatStyle({'stroke': '#000000', 'fill': 'none'})\r
186             w = 0.5                                 # default lineweight for POINT\r
187             if vals[groups['370']]:                 # Common Lineweight\r
188                 if vals[groups['370']][0] > 0:\r
189                     w = scale*vals[groups['370']][0]/100.0\r
190                     style = simplestyle.formatStyle({'stroke': '#000000', 'fill': 'none', 'stroke-width': '%.1f' % w})\r
191             entities[entity]()\r
192         entity = line[1]\r
193         vals = [[],[],[],[],[],[],[],[],[],[],[],[]]\r
194         seqs = []\r
195 \r
196 doc.write(inkex.sys.stdout)\r
197 \r
198 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99\r