d2e645ea1dec9a44babd97633cd30c7118749f7e
1 #!/usr/bin/env python
2 '''
3 dxf_input.py - input a DXF file >= (AutoCAD Release 13 == AC1012)
5 Copyright (C) 2008, 2009 Alvin Penner, penner@vaxxine.com
6 Copyright (C) 2009 Christian Mayer, inkscape@christianmayer.de
7 - thanks to Aaron Spike for inkex.py and simplestyle.py
8 - without which this would not have been possible
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 '''
25 import inkex, simplestyle, math
26 from StringIO import StringIO
28 def export_MTEXT():
29 # mandatory group codes : (1, 10, 20) (text, x, y)
30 if vals[groups['1']] and vals[groups['10']] and vals[groups['20']]:
31 x = vals[groups['10']][0]
32 y = vals[groups['20']][0]
33 # optional group codes : (40, 50) (text height mm, text angle)
34 size = 12 # default fontsize in px
35 if vals[groups['40']]:
36 size = scale*vals[groups['40']][0]
37 attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %dpx; fill: %s' % (size, color)}
38 angle = 0 # default angle in degrees
39 if vals[groups['50']]:
40 angle = vals[groups['50']][0]
41 attribs.update({'transform': 'rotate (%f %f %f)' % (-angle, x, y)})
42 attribs.update({inkex.addNS('linespacing','sodipodi'): '125%'})
43 node = inkex.etree.SubElement(layer, 'text', attribs)
44 text = vals[groups['1']][0]
45 found = text.find('\P') # new line
46 while found > -1:
47 tspan = inkex.etree.SubElement(node , 'tspan', {inkex.addNS('role','sodipodi'): 'line'})
48 tspan.text = text[:found]
49 text = text[(found+2):]
50 found = text.find('\P')
51 tspan = inkex.etree.SubElement(node , 'tspan', {inkex.addNS('role','sodipodi'): 'line'})
52 tspan.text = text
54 def export_POINT():
55 # mandatory group codes : (10, 20) (x, y)
56 if vals[groups['10']] and vals[groups['20']]:
57 generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], w/2, 0.0, 1.0, 0.0, 0.0)
59 def export_LINE():
60 # mandatory group codes : (10, 11, 20, 21) (x1, x2, y1, y2)
61 if vals[groups['10']] and vals[groups['11']] and vals[groups['20']] and vals[groups['21']]:
62 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))
63 attribs = {'d': path, 'style': style}
64 inkex.etree.SubElement(layer, 'path', attribs)
66 def export_SPLINE():
67 # mandatory group codes : (10, 20, 70) (x, y, flags)
68 if vals[groups['10']] and vals[groups['20']] and vals[groups['70']]:
69 if not (vals[groups['70']][0] & 3) and len(vals[groups['10']]) == 4 and len(vals[groups['20']]) == 4:
70 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])
71 attribs = {'d': path, 'style': style}
72 inkex.etree.SubElement(layer, 'path', attribs)
73 if not (vals[groups['70']][0] & 3) and len(vals[groups['10']]) == 3 and len(vals[groups['20']]) == 3:
74 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])
75 attribs = {'d': path, 'style': style}
76 inkex.etree.SubElement(layer, 'path', attribs)
78 def export_CIRCLE():
79 # mandatory group codes : (10, 20, 40) (x, y, radius)
80 if vals[groups['10']] and vals[groups['20']] and vals[groups['40']]:
81 generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], scale*vals[groups['40']][0], 0.0, 1.0, 0.0, 0.0)
83 def export_ARC():
84 # mandatory group codes : (10, 20, 40, 50, 51) (x, y, radius, angle1, angle2)
85 if vals[groups['10']] and vals[groups['20']] and vals[groups['40']] and vals[groups['50']] and vals[groups['51']]:
86 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)
88 def export_ELLIPSE():
89 # mandatory group codes : (10, 11, 20, 21, 40, 41, 42) (xc, xm, yc, ym, width ratio, angle1, angle2)
90 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']]:
91 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])
93 def export_LEADER():
94 # mandatory group codes : (10, 20) (x, y)
95 if vals[groups['10']] and vals[groups['20']]:
96 if len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):
97 path = 'M %f,%f' % (vals[groups['10']][0], vals[groups['20']][0])
98 for i in range (1, len(vals[groups['10']])):
99 path += ' %f,%f' % (vals[groups['10']][i], vals[groups['20']][i])
100 attribs = {'d': path, 'style': style}
101 inkex.etree.SubElement(layer, 'path', attribs)
103 def export_LWPOLYLINE():
104 # mandatory group codes : (10, 20, 70) (x, y, flags)
105 if vals[groups['10']] and vals[groups['20']] and vals[groups['70']]:
106 if len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):
107 # optional group codes : (42) (bulge)
108 iseqs = 0
109 ibulge = 0
110 while seqs[iseqs] != '20':
111 iseqs += 1
112 path = 'M %f,%f' % (vals[groups['10']][0], vals[groups['20']][0])
113 xold = vals[groups['10']][0]
114 yold = vals[groups['20']][0]
115 for i in range (1, len(vals[groups['10']])):
116 bulge = 0
117 iseqs += 1
118 while seqs[iseqs] != '20':
119 if seqs[iseqs] == '42':
120 bulge = vals[groups['42']][ibulge]
121 ibulge += 1
122 iseqs += 1
123 if bulge:
124 sweep = 0 # sweep CCW
125 if bulge < 0:
126 sweep = 1 # sweep CW
127 bulge = -bulge
128 large = 0 # large-arc-flag
129 if bulge > 1:
130 large = 1
131 r = math.sqrt((vals[groups['10']][i] - xold)**2 + (vals[groups['20']][i] - yold)**2)
132 r = 0.25*r*(bulge + 1.0/bulge)
133 path += ' A %f,%f 0.0 %d %d %f,%f' % (r, r, large, sweep, vals[groups['10']][i], vals[groups['20']][i])
134 else:
135 path += ' L %f,%f' % (vals[groups['10']][i], vals[groups['20']][i])
136 xold = vals[groups['10']][i]
137 yold = vals[groups['20']][i]
138 if vals[groups['70']][0] == 1: # closed path
139 path += ' z'
140 attribs = {'d': path, 'style': style}
141 inkex.etree.SubElement(layer, 'path', attribs)
143 def export_HATCH():
144 # mandatory group codes : (10, 20, 72, 93) (x, y, Edge Type, Number of edges)
145 if vals[groups['10']] and vals[groups['20']] and vals[groups['72']] and vals[groups['93']]:
146 if len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):
147 # optional group codes : (11, 21, 40, 50, 51, 73) (x, y, r, angle1, angle2, CCW)
148 i10 = 1 # count start points
149 i11 = 0 # count line end points
150 i40 = 0 # count circles
151 i72 = 0 # count edge type flags
152 path = ''
153 for i in range (0, len(vals[groups['93']])):
154 xc = vals[groups['10']][i10]
155 yc = vals[groups['20']][i10]
156 if vals[groups['72']][i72] == 2: # arc
157 rm = scale*vals[groups['40']][i40]
158 a1 = vals[groups['50']][i40]
159 path += 'M %f,%f ' % (xc + rm*math.cos(a1*math.pi/180.0), yc + rm*math.sin(a1*math.pi/180.0))
160 else:
161 a1 = 0
162 path += 'M %f,%f ' % (xc, yc)
163 for j in range(0, vals[groups['93']][i]):
164 if vals[groups['72']][i72] == 2: # arc
165 xc = vals[groups['10']][i10]
166 yc = vals[groups['20']][i10]
167 rm = scale*vals[groups['40']][i40]
168 a2 = vals[groups['51']][i40]
169 diff = (a2 - a1 + 360) % (360)
170 sweep = 1 - vals[groups['73']][i40] # sweep CCW
171 large = 0 # large-arc-flag
172 if diff:
173 path += 'A %f,%f 0.0 %d %d %f,%f ' % (rm, rm, large, sweep, xc + rm*math.cos(a2*math.pi/180.0), yc + rm*math.sin(a2*math.pi/180.0))
174 else:
175 path += 'A %f,%f 0.0 %d %d %f,%f ' % (rm, rm, large, sweep, xc + rm*math.cos((a1+180.0)*math.pi/180.0), yc + rm*math.sin((a1+180.0)*math.pi/180.0))
176 path += 'A %f,%f 0.0 %d %d %f,%f ' % (rm, rm, large, sweep, xc + rm*math.cos(a1*math.pi/180.0), yc + rm*math.sin(a1*math.pi/180.0))
177 i40 += 1
178 i72 += 1
179 elif vals[groups['72']][i72] == 1: # line
180 path += 'L %f,%f ' % (scale*(vals[groups['11']][i11] - xmin), -scale*(vals[groups['21']][i11] - ymax))
181 i11 += 1
182 i72 += 1
183 elif vals[groups['72']][i72] == 0: # polyline
184 if j > 0:
185 path += 'L %f,%f ' % (vals[groups['10']][i10], vals[groups['20']][i10])
186 if j == vals[groups['93']][i] - 1:
187 i72 += 1
188 i10 += 1
189 path += "z "
190 style = simplestyle.formatStyle({'fill': '%s' % color})
191 attribs = {'d': path, 'style': style}
192 inkex.etree.SubElement(layer, 'path', attribs)
194 def export_DIMENSION():
195 # mandatory group codes : (10, 11, 13, 14, 20, 21, 23, 24) (x1..4, y1..4)
196 if vals[groups['10']] and vals[groups['11']] and vals[groups['13']] and vals[groups['14']] and vals[groups['20']] and vals[groups['21']] and vals[groups['23']] and vals[groups['24']]:
197 dx = abs(vals[groups['10']][0] - vals[groups['13']][0])
198 dy = abs(vals[groups['20']][0] - vals[groups['23']][0])
199 if (vals[groups['10']][0] == vals[groups['14']][0]) and dx > 0.00001:
200 d = dx/scale
201 dy = 0
202 path = 'M %f,%f %f,%f' % (vals[groups['10']][0], vals[groups['20']][0], vals[groups['13']][0], vals[groups['20']][0])
203 elif (vals[groups['20']][0] == vals[groups['24']][0]) and dy > 0.00001:
204 d = dy/scale
205 dx = 0
206 path = 'M %f,%f %f,%f' % (vals[groups['10']][0], vals[groups['20']][0], vals[groups['10']][0], vals[groups['23']][0])
207 else:
208 return
209 attribs = {'d': path, 'style': style + '; marker-start: url(#DistanceX); marker-end: url(#DistanceX)'}
210 inkex.etree.SubElement(layer, 'path', attribs)
211 x = scale*(vals[groups['11']][0] - xmin)
212 y = - scale*(vals[groups['21']][0] - ymax)
213 size = 3 # default fontsize in px
214 attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %dpx; fill: %s' % (size, color)}
215 if dx == 0:
216 attribs.update({'transform': 'rotate (%f %f %f)' % (-90, x, y)})
217 node = inkex.etree.SubElement(layer, 'text', attribs)
218 tspan = inkex.etree.SubElement(node , 'tspan', {inkex.addNS('role','sodipodi'): 'line'})
219 tspan.text = '%.2f' % d
221 def generate_ellipse(xc, yc, xm, ym, w, a1, a2):
222 rm = math.sqrt(xm*xm + ym*ym)
223 a = math.atan2(ym, xm)
224 diff = (a2 - a1 + 2*math.pi) % (2*math.pi)
225 if diff: # open arc
226 large = 0 # large-arc-flag
227 if diff > math.pi:
228 large = 1
229 xt = rm*math.cos(a1)
230 yt = w*rm*math.sin(a1)
231 x1 = xt*math.cos(a) - yt*math.sin(a)
232 y1 = xt*math.sin(a) + yt*math.cos(a)
233 xt = rm*math.cos(a2)
234 yt = w*rm*math.sin(a2)
235 x2 = xt*math.cos(a) - yt*math.sin(a)
236 y2 = xt*math.sin(a) + yt*math.cos(a)
237 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)
238 else: # closed arc
239 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)
240 attribs = {'d': path, 'style': style}
241 inkex.etree.SubElement(layer, 'path', attribs)
243 def get_line():
244 return (stream.readline().strip(), stream.readline().strip())
246 def get_group(group):
247 line = get_line()
248 if line[0] == group:
249 return float(line[1])
250 else:
251 return 0.0
253 # define DXF Entities and specify which Group Codes to monitor
255 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, 'HATCH': export_HATCH, 'DIMENSION': export_DIMENSION, 'ENDSEC': ''}
256 groups = {'1': 0, '8': 1, '10': 2, '11': 3, '13': 4, '14': 5, '20': 6, '21': 7, '23': 8, '24': 9, '40': 10, '41': 11, '42': 12, '50': 13, '51': 14, '62': 15, '70': 16, '72': 17, '73': 18, '93': 19, '370': 20}
257 colors = { 1: '#FF0000', 2: '#FFFF00', 3: '#00FF00', 4: '#00FFFF', 5: '#0000FF',
258 6: '#FF00FF', 8: '#414141', 9: '#808080', 30: '#FF7F00',
259 250: '#333333', 251: '#505050', 252: '#696969', 253: '#828282', 254: '#BEBEBE', 255: '#FFFFFF'}
261 doc = inkex.etree.parse(StringIO('<svg xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"></svg>'))
262 defs = inkex.etree.SubElement(doc.getroot(), 'defs', {} )
263 marker = inkex.etree.SubElement(defs, 'marker', {'id': 'DistanceX', 'orient': 'auto', 'refX': '0.0', 'refY': '0.0', 'style': 'overflow:visible'})
264 inkex.etree.SubElement(marker, 'path', {'d': 'M 3,-3 L -3,3 M 0,-5 L 0,5', 'style': 'stroke:#000000; stroke-width:0.5'})
265 stream = open(inkex.sys.argv[1], 'r')
266 xmax = xmin = 0.0
267 ymax = 297.0 # default A4 height in mm
268 line = get_line()
269 flag = 0
270 layer_colors = {} # store colors by layer
271 layer_nodes = {} # store nodes by layer
272 while line[0] and line[1] != 'ENTITIES':
273 line = get_line()
274 if line[1] == '$EXTMIN':
275 xmin = get_group('10')
276 if line[1] == '$EXTMAX':
277 xmax = get_group('10')
278 ymax = get_group('20')
279 if flag and line[0] == '2':
280 name = unicode(line[1], "iso-8859-1")
281 attribs = {inkex.addNS('groupmode','inkscape'): 'layer', inkex.addNS('label','inkscape'): '%s' % name}
282 layer_nodes[name] = inkex.etree.SubElement(doc.getroot(), 'g', attribs)
283 if line[0] == '2' and line[1] == 'LAYER':
284 flag = 1
285 if flag and line[0] == '62':
286 layer_colors[name] = int(line[1])
287 if line[0] == '0' and line[1] == 'ENDTAB':
288 flag = 0
290 scale = 90.0/25.4 # default convert from mm to pixels
291 if xmax > xmin:
292 scale *= 210.0/(xmax - xmin) # scale to A4 width
293 entity = ''
294 while line[0] and line[1] != 'ENDSEC':
295 line = get_line()
296 if entity and groups.has_key(line[0]):
297 seqs.append(line[0]) # list of group codes
298 if line[0] == '1' or line[0] == '8': # text value
299 val = line[1].replace('\~', ' ')
300 val = inkex.re.sub( '\\\\A.*;', '', val)
301 val = inkex.re.sub( '\\\\H.*;', '', val)
302 val = inkex.re.sub( '\\\\S.*;', '', val)
303 val = inkex.re.sub( '\\\\W.*;', '', val)
304 val = unicode(val, "iso-8859-1")
305 elif line[0] == '62' or line[0] == '70' or line[0] == '93':
306 val = int(line[1])
307 elif line[0] == '10' or line[0] == '13' or line[0] == '14': # scaled float x value
308 val = scale*(float(line[1]) - xmin)
309 elif line[0] == '20' or line[0] == '23' or line[0] == '24': # scaled float y value
310 val = - scale*(float(line[1]) - ymax)
311 else: # unscaled float value
312 val = float(line[1])
313 vals[groups[line[0]]].append(val)
314 elif entities.has_key(line[1]):
315 if entities.has_key(entity):
316 color = '#000000' # default color
317 if vals[groups['8']]: # Common Layer Name
318 layer = layer_nodes[vals[groups['8']][0]]
319 if layer_colors.has_key(vals[groups['8']][0]):
320 if colors.has_key(layer_colors[vals[groups['8']][0]]):
321 color = colors[layer_colors[vals[groups['8']][0]]]
322 if vals[groups['62']]: # Common Color Number
323 if colors.has_key(vals[groups['62']][0]):
324 color = colors[vals[groups['62']][0]]
325 style = simplestyle.formatStyle({'stroke': '%s' % color, 'fill': 'none'})
326 w = 0.5 # default lineweight for POINT
327 if vals[groups['370']]: # Common Lineweight
328 if vals[groups['370']][0] > 0:
329 w = 90.0/25.4*vals[groups['370']][0]/100.0
330 if w < 0.5:
331 w = 0.5
332 style = simplestyle.formatStyle({'stroke': '%s' % color, 'fill': 'none', 'stroke-width': '%.1f' % w})
333 entities[entity]()
334 entity = line[1]
335 vals = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
336 seqs = []
338 doc.write(inkex.sys.stdout)
340 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99