b518969fe522b6c9cd6538c59b7556533579a6e5
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 - thanks to Aaron Spike for inkex.py and simplestyle.py\r
7 - without which this would not have been possible\r
8 \r
9 This program is free software; you can redistribute it and/or modify\r
10 it under the terms of the GNU General Public License as published by\r
11 the Free Software Foundation; either version 2 of the License, or\r
12 (at your option) any later version.\r
13 \r
14 This program is distributed in the hope that it will be useful,\r
15 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
17 GNU General Public License for more details.\r
18 \r
19 You should have received a copy of the GNU General Public License\r
20 along with this program; if not, write to the Free Software\r
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
22 '''\r
23 \r
24 import inkex, simplestyle, math\r
25 from StringIO import StringIO\r
26 \r
27 def export_MTEXT():\r
28 # mandatory group codes : (1, 10, 20) (text, x, y)\r
29 if vals[groups['1']] and vals[groups['10']] and vals[groups['20']]:\r
30 x = vals[groups['10']][0]\r
31 y = vals[groups['20']][0]\r
32 # optional group codes : (40, 50) (text height mm, text angle)\r
33 size = 12 # default fontsize in px\r
34 if vals[groups['40']]:\r
35 size = scale*vals[groups['40']][0]\r
36 attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %dpx; fill: %s' % (size, color)}\r
37 angle = 0 # default angle in degrees\r
38 if vals[groups['50']]:\r
39 angle = vals[groups['50']][0]\r
40 attribs.update({'transform': 'rotate (%f %f %f)' % (-angle, x, y)})\r
41 attribs.update({'sodipodi:linespacing': '125%'})\r
42 node = inkex.etree.SubElement(layer, 'text', attribs)\r
43 text = vals[groups['1']][0]\r
44 found = text.find('\P') # new line\r
45 while found > -1:\r
46 tspan = inkex.etree.SubElement(node , 'tspan', {'sodipodi:role': 'line'})\r
47 tspan.text = text[:found]\r
48 text = text[(found+2):]\r
49 found = text.find('\P')\r
50 tspan = inkex.etree.SubElement(node , 'tspan', {'sodipodi:role': 'line'})\r
51 tspan.text = text\r
52 \r
53 def export_POINT():\r
54 # mandatory group codes : (10, 20) (x, y)\r
55 if vals[groups['10']] and vals[groups['20']]:\r
56 generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], w/2, 0.0, 1.0, 0.0, 0.0)\r
57 \r
58 def export_LINE():\r
59 # mandatory group codes : (10, 11, 20, 21) (x1, x2, y1, y2)\r
60 if vals[groups['10']] and vals[groups['11']] and vals[groups['20']] and vals[groups['21']]:\r
61 path = 'M %f,%f %f,%f' % (vals[groups['10']][0], vals[groups['20']][0], scale*(vals[groups['11']][0] - xmin), - scale*(vals[groups['21']][0] - ymax))\r
62 attribs = {'d': path, 'style': style}\r
63 inkex.etree.SubElement(layer, 'path', attribs)\r
64 \r
65 def export_SPLINE():\r
66 # mandatory group codes : (10, 20, 70) (x, y, flags)\r
67 if vals[groups['10']] and vals[groups['20']] and vals[groups['70']]:\r
68 if not (vals[groups['70']][0] & 3) and len(vals[groups['10']]) == 4 and len(vals[groups['20']]) == 4:\r
69 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
70 attribs = {'d': path, 'style': style}\r
71 inkex.etree.SubElement(layer, 'path', attribs)\r
72 if not (vals[groups['70']][0] & 3) and len(vals[groups['10']]) == 3 and len(vals[groups['20']]) == 3:\r
73 path = 'M %f,%f Q %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])\r
74 attribs = {'d': path, 'style': style}\r
75 inkex.etree.SubElement(layer, 'path', attribs)\r
76 \r
77 def export_CIRCLE():\r
78 # mandatory group codes : (10, 20, 40) (x, y, radius)\r
79 if vals[groups['10']] and vals[groups['20']] and vals[groups['40']]:\r
80 generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], scale*vals[groups['40']][0], 0.0, 1.0, 0.0, 0.0)\r
81 \r
82 def export_ARC():\r
83 # mandatory group codes : (10, 20, 40, 50, 51) (x, y, radius, angle1, angle2)\r
84 if vals[groups['10']] and vals[groups['20']] and vals[groups['40']] and vals[groups['50']] and vals[groups['51']]:\r
85 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
86 \r
87 def export_ELLIPSE():\r
88 # mandatory group codes : (10, 11, 20, 21, 40, 41, 42) (xc, xm, yc, ym, width ratio, angle1, angle2)\r
89 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
90 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
91 \r
92 def export_LEADER():\r
93 # mandatory group codes : (10, 20) (x, y)\r
94 if vals[groups['10']] and vals[groups['20']]:\r
95 if len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):\r
96 path = 'M %f,%f' % (vals[groups['10']][0], vals[groups['20']][0])\r
97 for i in range (1, len(vals[groups['10']])):\r
98 path += ' %f,%f' % (vals[groups['10']][i], vals[groups['20']][i])\r
99 attribs = {'d': path, 'style': style}\r
100 inkex.etree.SubElement(layer, 'path', attribs)\r
101 \r
102 def export_LWPOLYLINE():\r
103 # mandatory group codes : (10, 20, 70) (x, y, flags)\r
104 if vals[groups['10']] and vals[groups['20']] and vals[groups['70']]:\r
105 if len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):\r
106 # optional group codes : (42) (bulge)\r
107 iseqs = 0\r
108 ibulge = 0\r
109 while seqs[iseqs] != '20':\r
110 iseqs += 1\r
111 path = 'M %f,%f' % (vals[groups['10']][0], vals[groups['20']][0])\r
112 xold = vals[groups['10']][0]\r
113 yold = vals[groups['20']][0]\r
114 for i in range (1, len(vals[groups['10']])):\r
115 bulge = 0\r
116 iseqs += 1\r
117 while seqs[iseqs] != '20':\r
118 if seqs[iseqs] == '42':\r
119 bulge = vals[groups['42']][ibulge]\r
120 ibulge += 1\r
121 iseqs += 1\r
122 if bulge:\r
123 sweep = 0 # sweep CCW\r
124 if bulge < 0:\r
125 sweep = 1 # sweep CW\r
126 bulge = -bulge\r
127 large = 0 # large-arc-flag\r
128 if bulge > 1:\r
129 large = 1\r
130 r = math.sqrt((vals[groups['10']][i] - xold)**2 + (vals[groups['20']][i] - yold)**2)\r
131 r = 0.25*r*(bulge + 1.0/bulge)\r
132 path += ' A %f,%f 0.0 %d %d %f,%f' % (r, r, large, sweep, vals[groups['10']][i], vals[groups['20']][i])\r
133 else:\r
134 path += ' L %f,%f' % (vals[groups['10']][i], vals[groups['20']][i])\r
135 xold = vals[groups['10']][i]\r
136 yold = vals[groups['20']][i]\r
137 if vals[groups['70']][0] == 1: # closed path\r
138 path += ' z'\r
139 attribs = {'d': path, 'style': style}\r
140 inkex.etree.SubElement(layer, 'path', attribs)\r
141 \r
142 def generate_ellipse(xc, yc, xm, ym, w, a1, a2):\r
143 rm = math.sqrt(xm*xm + ym*ym)\r
144 a = math.atan(ym/xm)\r
145 if xm < 0:\r
146 a += math.pi\r
147 diff = (a2 - a1 + 2*math.pi) % (2*math.pi)\r
148 if diff: # open arc\r
149 large = 0 # large-arc-flag\r
150 if diff > math.pi:\r
151 large = 1\r
152 xt = rm*math.cos(a1)\r
153 yt = w*rm*math.sin(a1)\r
154 x1 = xt*math.cos(a) - yt*math.sin(a)\r
155 y1 = xt*math.sin(a) + yt*math.cos(a)\r
156 xt = rm*math.cos(a2)\r
157 yt = w*rm*math.sin(a2)\r
158 x2 = xt*math.cos(a) - yt*math.sin(a)\r
159 y2 = xt*math.sin(a) + yt*math.cos(a)\r
160 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
161 else: # closed arc\r
162 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
163 attribs = {'d': path, 'style': style}\r
164 inkex.etree.SubElement(layer, 'path', attribs)\r
165 \r
166 def get_line():\r
167 return (stream.readline().strip(), stream.readline().strip())\r
168 \r
169 def get_group(group):\r
170 line = get_line()\r
171 if line[0] == group:\r
172 return float(line[1])\r
173 else:\r
174 return 0.0\r
175 \r
176 # define DXF Entities and specify which Group Codes to monitor\r
177 \r
178 entities = {'MTEXT': export_MTEXT, 'TEXT': 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
179 groups = {'1': 0, '8': 1, '10': 2, '11': 3, '20': 4, '21': 5, '40': 6, '41': 7, '42': 8, '50': 9, '51': 10, '62': 11, '70': 12, '370': 13}\r
180 colors = { 1: '#FF0000', 2: '#FFFF00', 3: '#00FF00', 4: '#00FFFF', 5: '#0000FF',\r
181 6: '#FF00FF', 8: '#414141', 9: '#808080', 30: '#FF7F00',\r
182 250: '#333333', 251: '#505050', 252: '#696969', 253: '#828282', 254: '#BEBEBE', 255: '#FFFFFF'}\r
183 \r
184 doc = inkex.etree.parse(StringIO('<svg xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"></svg>'))\r
185 stream = open(inkex.sys.argv[1], 'r')\r
186 xmax = xmin = 0.0\r
187 ymax = 297.0 # default A4 height in mm\r
188 line = get_line()\r
189 flag = 0\r
190 layer_colors = {} # store colors by layer\r
191 layer_nodes = {} # store nodes by layer\r
192 while line[0] and line[1] != 'ENTITIES':\r
193 line = get_line()\r
194 if line[1] == '$EXTMIN':\r
195 xmin = get_group('10')\r
196 if line[1] == '$EXTMAX':\r
197 xmax = get_group('10')\r
198 ymax = get_group('20')\r
199 if flag and line[0] == '2':\r
200 name = line[1]\r
201 attribs = {inkex.addNS('groupmode','inkscape'): 'layer', inkex.addNS('label','inkscape'): '%s' % name}\r
202 layer_nodes[name] = inkex.etree.SubElement(doc.getroot(), 'g', attribs)\r
203 if line[0] == '2' and line[1] == 'LAYER':\r
204 flag = 1\r
205 if flag and line[0] == '62':\r
206 layer_colors[name] = int(line[1])\r
207 if line[0] == '0' and line[1] == 'ENDTAB':\r
208 flag = 0\r
209 \r
210 scale = 90.0/25.4 # default convert from mm to pixels\r
211 if xmax > xmin:\r
212 scale *= 210.0/(xmax - xmin) # scale to A4 width\r
213 entity = ''\r
214 while line[0] and line[1] != 'ENDSEC':\r
215 line = get_line()\r
216 if entity and groups.has_key(line[0]):\r
217 seqs.append(line[0]) # list of group codes\r
218 if line[0] == '1' or line[0] == '8': # text value\r
219 val = line[1].replace('\~', ' ')\r
220 val = unicode(val, "iso-8859-1")\r
221 elif line[0] == '62' or line[0] == '70': # unscaled integer value\r
222 val = int(line[1])\r
223 elif line[0] == '10': # scaled float x value\r
224 val = scale*(float(line[1]) - xmin)\r
225 elif line[0] == '20': # scaled float y value\r
226 val = - scale*(float(line[1]) - ymax)\r
227 else: # unscaled float value\r
228 val = float(line[1])\r
229 vals[groups[line[0]]].append(val)\r
230 elif entities.has_key(line[1]):\r
231 if entities.has_key(entity):\r
232 color = '#000000' # default color\r
233 if vals[groups['8']]: # Common Layer Name\r
234 layer = layer_nodes[vals[groups['8']][0]]\r
235 if layer_colors.has_key(vals[groups['8']][0]):\r
236 if colors.has_key(layer_colors[vals[groups['8']][0]]):\r
237 color = colors[layer_colors[vals[groups['8']][0]]]\r
238 if vals[groups['62']]: # Common Color Number\r
239 if colors.has_key(vals[groups['62']][0]):\r
240 color = colors[vals[groups['62']][0]]\r
241 style = simplestyle.formatStyle({'stroke': '%s' % color, 'fill': 'none'})\r
242 w = 0.5 # default lineweight for POINT\r
243 if vals[groups['370']]: # Common Lineweight\r
244 if vals[groups['370']][0] > 0:\r
245 w = scale*vals[groups['370']][0]/100.0\r
246 if w < 0.5:\r
247 w = 0.5\r
248 style = simplestyle.formatStyle({'stroke': '%s' % color, 'fill': 'none', 'stroke-width': '%.1f' % w})\r
249 entities[entity]()\r
250 entity = line[1]\r
251 vals = [[],[],[],[],[],[],[],[],[],[],[],[],[],[]]\r
252 seqs = []\r
253 \r
254 doc.write(inkex.sys.stdout)\r
255 \r
256 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99\r