Code

b46477de16bf581366e880e4caa08cb948d7f7de
[inkscape.git] / share / extensions / dxf_input.py
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
27 from urllib import quote
29 def export_MTEXT():
30     # mandatory group codes : (1 or 3, 10, 20) (text, x, y)
31     if (vals[groups['1']] or vals[groups['3']]) and vals[groups['10']] and vals[groups['20']]:
32         x = vals[groups['10']][0]
33         y = vals[groups['20']][0]
34         # optional group codes : (21, 40, 50) (direction, text height mm, text angle)
35         size = 12                       # default fontsize in px
36         if vals[groups['40']]:
37             size = scale*vals[groups['40']][0]
38         attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %.1fpx; fill: %s; font-family: %s' % (size, color, options.font)}
39         angle = 0                       # default angle in degrees
40         if vals[groups['50']]:
41             angle = vals[groups['50']][0]
42             attribs.update({'transform': 'rotate (%f %f %f)' % (-angle, x, y)})
43         elif vals[groups['21']]:
44             if vals[groups['21']][0] == 1.0:
45                 attribs.update({'transform': 'rotate (%f %f %f)' % (-90, x, y)})
46             elif vals[groups['21']][0] == -1.0:
47                 attribs.update({'transform': 'rotate (%f %f %f)' % (90, x, y)})
48         attribs.update({inkex.addNS('linespacing','sodipodi'): '125%'})
49         node = inkex.etree.SubElement(layer, 'text', attribs)
50         text = ''
51         if vals[groups['3']]:
52             for i in range (0, len(vals[groups['3']])):
53                 text += vals[groups['3']][i]
54         if vals[groups['1']]:
55             text += vals[groups['1']][0]
56         found = text.find('\P')         # new line
57         while found > -1:
58             tspan = inkex.etree.SubElement(node , 'tspan', {inkex.addNS('role','sodipodi'): 'line'})
59             tspan.text = text[:found]
60             text = text[(found+2):]
61             found = text.find('\P')
62         tspan = inkex.etree.SubElement(node , 'tspan', {inkex.addNS('role','sodipodi'): 'line'})
63         tspan.text = text
65 def export_POINT():
66     # mandatory group codes : (10, 20) (x, y)
67     if vals[groups['10']] and vals[groups['20']]:
68         if options.gcodetoolspoints:
69             generate_gcodetools_point(vals[groups['10']][0], vals[groups['20']][0])
70         else:
71             generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], w/2, 0.0, 1.0, 0.0, 0.0)
73 def export_LINE():
74     # mandatory group codes : (10, 11, 20, 21) (x1, x2, y1, y2)
75     if vals[groups['10']] and vals[groups['11']] and vals[groups['20']] and vals[groups['21']]:
76         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))
77         attribs = {'d': path, 'style': style}
78         inkex.etree.SubElement(layer, 'path', attribs)
80 def export_SPLINE():
81     # mandatory group codes : (10, 20, 70) (x, y, flags)
82     if vals[groups['10']] and vals[groups['20']] and vals[groups['70']]:
83         if not (vals[groups['70']][0] & 3) and len(vals[groups['10']]) == 4 and len(vals[groups['20']]) == 4:
84             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])
85             attribs = {'d': path, 'style': style}
86             inkex.etree.SubElement(layer, 'path', attribs)
87         if not (vals[groups['70']][0] & 3) and len(vals[groups['10']]) == 3 and len(vals[groups['20']]) == 3:
88             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])
89             attribs = {'d': path, 'style': style}
90             inkex.etree.SubElement(layer, 'path', attribs)
92 def export_CIRCLE():
93     # mandatory group codes : (10, 20, 40) (x, y, radius)
94     if vals[groups['10']] and vals[groups['20']] and vals[groups['40']]:
95         generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], scale*vals[groups['40']][0], 0.0, 1.0, 0.0, 0.0)
97 def export_ARC():
98     # mandatory group codes : (10, 20, 40, 50, 51) (x, y, radius, angle1, angle2)
99     if vals[groups['10']] and vals[groups['20']] and vals[groups['40']] and vals[groups['50']] and vals[groups['51']]:
100         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)
102 def export_ELLIPSE():
103     # mandatory group codes : (10, 11, 20, 21, 40, 41, 42) (xc, xm, yc, ym, width ratio, angle1, angle2)
104     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']]:
105         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])
107 def export_LEADER():
108     # mandatory group codes : (10, 20) (x, y)
109     if vals[groups['10']] and vals[groups['20']]:
110         if len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):
111             path = 'M %f,%f' % (vals[groups['10']][0], vals[groups['20']][0])
112             for i in range (1, len(vals[groups['10']])):
113                 path += ' %f,%f' % (vals[groups['10']][i], vals[groups['20']][i])
114             attribs = {'d': path, 'style': style}
115             inkex.etree.SubElement(layer, 'path', attribs)
117 def export_LWPOLYLINE():
118     # mandatory group codes : (10, 20, 70) (x, y, flags)
119     if vals[groups['10']] and vals[groups['20']] and vals[groups['70']]:
120         if len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):
121             # optional group codes : (42) (bulge)
122             iseqs = 0
123             ibulge = 0
124             if vals[groups['70']][0]:           # closed path
125                 seqs.append('20')
126                 vals[groups['10']].append(vals[groups['10']][0])
127                 vals[groups['20']].append(vals[groups['20']][0])
128             while seqs[iseqs] != '20':
129                 iseqs += 1
130             path = 'M %f,%f' % (vals[groups['10']][0], vals[groups['20']][0])
131             xold = vals[groups['10']][0]
132             yold = vals[groups['20']][0]
133             for i in range (1, len(vals[groups['10']])):
134                 bulge = 0
135                 iseqs += 1
136                 while seqs[iseqs] != '20':
137                     if seqs[iseqs] == '42':
138                         bulge = vals[groups['42']][ibulge]
139                         ibulge += 1
140                     iseqs += 1
141                 if bulge:
142                     sweep = 0                   # sweep CCW
143                     if bulge < 0:
144                         sweep = 1               # sweep CW
145                         bulge = -bulge
146                     large = 0                   # large-arc-flag
147                     if bulge > 1:
148                         large = 1
149                     r = math.sqrt((vals[groups['10']][i] - xold)**2 + (vals[groups['20']][i] - yold)**2)
150                     r = 0.25*r*(bulge + 1.0/bulge)
151                     path += ' A %f,%f 0.0 %d %d %f,%f' % (r, r, large, sweep, vals[groups['10']][i], vals[groups['20']][i])
152                 else:
153                     path += ' L %f,%f' % (vals[groups['10']][i], vals[groups['20']][i])
154                 xold = vals[groups['10']][i]
155                 yold = vals[groups['20']][i]
156             if vals[groups['70']][0]:           # closed path
157                 path += ' z'
158             attribs = {'d': path, 'style': style}
159             inkex.etree.SubElement(layer, 'path', attribs)
161 def export_HATCH():
162     # mandatory group codes : (10, 20, 70, 72, 92, 93) (x, y, fill, Edge Type, Path Type, Number of edges)
163     if vals[groups['10']] and vals[groups['20']] and vals[groups['70']] and vals[groups['72']] and vals[groups['92']] and vals[groups['93']]:
164         if len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):
165             # optional group codes : (11, 21, 40, 50, 51, 73) (x, y, r, angle1, angle2, CCW)
166             i10 = 1    # count start points
167             i11 = 0    # count line end points
168             i40 = 0    # count circles
169             i72 = 0    # count edge type flags
170             path = ''
171             for i in range (0, len(vals[groups['93']])):
172                 xc = vals[groups['10']][i10]
173                 yc = vals[groups['20']][i10]
174                 if vals[groups['72']][i72] == 2:            # arc
175                     rm = scale*vals[groups['40']][i40]
176                     a1 = vals[groups['50']][i40]
177                     path += 'M %f,%f ' % (xc + rm*math.cos(a1*math.pi/180.0), yc + rm*math.sin(a1*math.pi/180.0))
178                 else:
179                     a1 = 0
180                     path += 'M %f,%f ' % (xc, yc)
181                 for j in range(0, vals[groups['93']][i]):
182                     if vals[groups['92']][i] & 2:           # polyline
183                         if j > 0:
184                             path += 'L %f,%f ' % (vals[groups['10']][i10], vals[groups['20']][i10])
185                         if j == vals[groups['93']][i] - 1:
186                             i72 += 1
187                     elif vals[groups['72']][i72] == 2:      # arc
188                         xc = vals[groups['10']][i10]
189                         yc = vals[groups['20']][i10]
190                         rm = scale*vals[groups['40']][i40]
191                         a2 = vals[groups['51']][i40]
192                         diff = (a2 - a1 + 360) % (360)
193                         sweep = 1 - vals[groups['73']][i40] # sweep CCW
194                         large = 0                           # large-arc-flag
195                         if diff:
196                             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))
197                         else:
198                             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))
199                             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))
200                         i40 += 1
201                         i72 += 1
202                     elif vals[groups['72']][i72] == 1:      # line
203                         path += 'L %f,%f ' % (scale*(vals[groups['11']][i11] - xmin), -scale*(vals[groups['21']][i11] - ymax))
204                         i11 += 1
205                         i72 += 1
206                     i10 += 1
207                 path += "z "
208             if vals[groups['70']][0]:
209                 style = simplestyle.formatStyle({'fill': '%s' % color})
210             else:
211                 style = simplestyle.formatStyle({'fill': 'url(#Hatch)', 'fill-opacity': '1.0'})
212             attribs = {'d': path, 'style': style}
213             inkex.etree.SubElement(layer, 'path', attribs)
215 def export_DIMENSION():
216     # mandatory group codes : (10, 11, 13, 14, 20, 21, 23, 24) (x1..4, y1..4)
217     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']]:
218         dx = abs(vals[groups['10']][0] - vals[groups['13']][0])
219         dy = abs(vals[groups['20']][0] - vals[groups['23']][0])
220         if (vals[groups['10']][0] == vals[groups['14']][0]) and dx > 0.00001:
221             d = dx/scale
222             dy = 0
223             path = 'M %f,%f %f,%f' % (vals[groups['10']][0], vals[groups['20']][0], vals[groups['13']][0], vals[groups['20']][0])
224         elif (vals[groups['20']][0] == vals[groups['24']][0]) and dy > 0.00001:
225             d = dy/scale
226             dx = 0
227             path = 'M %f,%f %f,%f' % (vals[groups['10']][0], vals[groups['20']][0], vals[groups['10']][0], vals[groups['23']][0])
228         else:
229             return
230         attribs = {'d': path, 'style': style + '; marker-start: url(#DistanceX); marker-end: url(#DistanceX); stroke-width: 0.25px'}
231         inkex.etree.SubElement(layer, 'path', attribs)
232         x = scale*(vals[groups['11']][0] - xmin)
233         y = - scale*(vals[groups['21']][0] - ymax)
234         size = 12                   # default fontsize in px
235         if vals[groups['3']]:
236             if DIMTXT.has_key(vals[groups['3']][0]):
237                 size = scale*DIMTXT[vals[groups['3']][0]]
238                 if size < 2:
239                     size = 2
240         attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %.1fpx; fill: %s; font-family: %s; text-anchor: middle; text-align: center' % (size, color, options.font)}
241         if dx == 0:
242             attribs.update({'transform': 'rotate (%f %f %f)' % (-90, x, y)})
243         node = inkex.etree.SubElement(layer, 'text', attribs)
244         tspan = inkex.etree.SubElement(node , 'tspan', {inkex.addNS('role','sodipodi'): 'line'})
245         tspan.text = str(float('%.2f' % d))
247 def export_INSERT():
248     # mandatory group codes : (2, 10, 20) (block name, x, y)
249     if vals[groups['2']] and vals[groups['10']] and vals[groups['20']]:
250         x = vals[groups['10']][0]
251         y = vals[groups['20']][0] - scale*ymax
252         attribs = {'x': '%f' % x, 'y': '%f' % y, inkex.addNS('href','xlink'): '#' + quote(vals[groups['2']][0].encode("utf-8"))}
253         inkex.etree.SubElement(layer, 'use', attribs)
255 def export_BLOCK():
256     # mandatory group codes : (2) (block name)
257     if vals[groups['2']]:
258         global block
259         block = inkex.etree.SubElement(defs, 'symbol', {'id': vals[groups['2']][0]})
261 def export_ENDBLK():
262     global block
263     block = defs                                    # initiallize with dummy
265 def export_ATTDEF():
266     # mandatory group codes : (1, 2) (default, tag)
267     if vals[groups['1']] and vals[groups['2']]:
268         vals[groups['1']][0] = vals[groups['2']][0]
269         export_MTEXT()
271 def generate_ellipse(xc, yc, xm, ym, w, a1, a2):
272     rm = math.sqrt(xm*xm + ym*ym)
273     a = math.atan2(ym, xm)
274     diff = (a2 - a1 + 2*math.pi) % (2*math.pi)
275     if abs(diff) > 0.0000001 and abs(diff - 2*math.pi) > 0.0000001: # open arc
276         large = 0                   # large-arc-flag
277         if diff > math.pi:
278             large = 1
279         xt = rm*math.cos(a1)
280         yt = w*rm*math.sin(a1)
281         x1 = xt*math.cos(a) - yt*math.sin(a)
282         y1 = xt*math.sin(a) + yt*math.cos(a)
283         xt = rm*math.cos(a2)
284         yt = w*rm*math.sin(a2)
285         x2 = xt*math.cos(a) - yt*math.sin(a)
286         y2 = xt*math.sin(a) + yt*math.cos(a)
287         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)
288     else:                           # closed arc
289         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)
290     attribs = {'d': path, 'style': style}
291     inkex.etree.SubElement(layer, 'path', attribs)
293 def generate_gcodetools_point(xc, yc):
294     path= 'm %s,%s 2.9375,-6.34375 0.8125,1.90625 6.84375,-6.84375 0,0 0.6875,0.6875 -6.84375,6.84375 1.90625,0.8125 z' % (xc,yc)
295     attribs = {'d': path, inkex.addNS('dxfpoint','inkscape'):'1', 'style': 'stroke:#ff0000;fill:#ff0000'}
296     inkex.etree.SubElement(layer, 'path', attribs)
298 def get_line():
299     return (stream.readline().strip(), stream.readline().strip())
301 def get_group(group):
302     line = get_line()
303     if line[0] == group:
304         return float(line[1])
305     else:
306         return 0.0
308 #   define DXF Entities and specify which Group Codes to monitor
310 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, 'INSERT': export_INSERT, 'BLOCK': export_BLOCK, 'ENDBLK': export_ENDBLK, 'ATTDEF': export_ATTDEF, 'VIEWPORT': False, 'DICTIONARY': False}
311 groups = {'1': 0, '2': 1, '3': 2, '6': 3, '8': 4, '10': 5, '11': 6, '13': 7, '14': 8, '20': 9, '21': 10, '23': 11, '24': 12, '40': 13, '41': 14, '42': 15, '50': 16, '51': 17, '62': 18, '70': 19, '72': 20, '73': 21, '92': 22, '93': 23, '370': 24}
312 colors = {  1: '#FF0000',   2: '#FFFF00',   3: '#00FF00',   4: '#00FFFF',   5: '#0000FF',
313             6: '#FF00FF',   8: '#414141',   9: '#808080',  12: '#BD0000',  30: '#FF7F00',
314           250: '#333333', 251: '#505050', 252: '#696969', 253: '#828282', 254: '#BEBEBE', 255: '#FFFFFF'}
316 parser = inkex.optparse.OptionParser(usage="usage: %prog [options] SVGfile", option_class=inkex.InkOption)
317 parser.add_option("--auto", action="store", type="inkbool", dest="auto", default=True)
318 parser.add_option("--scale", action="store", type="string", dest="scale", default="1.0")
319 parser.add_option("--gcodetoolspoints", action="store", type="inkbool", dest="gcodetoolspoints", default=True)
320 parser.add_option("--encoding", action="store", type="string", dest="input_encode", default="latin_1")
321 parser.add_option("--font", action="store", type="string", dest="font", default="Arial")
322 parser.add_option("--tab", action="store", type="string", dest="tab", default="Options")
323 parser.add_option("--inputhelp", action="store", type="string", dest="inputhelp", default="")
324 (options, args) = parser.parse_args(inkex.sys.argv[1:])
325 doc = inkex.etree.parse(StringIO('<svg xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"></svg>'))
326 desc = inkex.etree.SubElement(doc.getroot(), 'desc', {})
327 defs = inkex.etree.SubElement(doc.getroot(), 'defs', {})
328 marker = inkex.etree.SubElement(defs, 'marker', {'id': 'DistanceX', 'orient': 'auto', 'refX': '0.0', 'refY': '0.0', 'style': 'overflow:visible'})
329 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'})
330 pattern = inkex.etree.SubElement(defs, 'pattern', {'id': 'Hatch', 'patternUnits': 'userSpaceOnUse', 'width': '8', 'height': '8', 'x': '0', 'y': '0'})
331 inkex.etree.SubElement(pattern, 'path', {'d': 'M8 4 l-4,4', 'stroke': '#000000', 'stroke-width': '0.25', 'linecap': 'square'})
332 inkex.etree.SubElement(pattern, 'path', {'d': 'M6 2 l-4,4', 'stroke': '#000000', 'stroke-width': '0.25', 'linecap': 'square'})
333 inkex.etree.SubElement(pattern, 'path', {'d': 'M4 0 l-4,4', 'stroke': '#000000', 'stroke-width': '0.25', 'linecap': 'square'})
334 stream = open(args[0], 'r')
335 xmax = xmin = 0.0
336 ymax = 297.0                                        # default A4 height in mm
337 line = get_line()
338 flag = 0                                            # (0, 1, 2, 3) = (none, LAYER, LTYPE, DIMTXT)
339 layer_colors = {}                                   # store colors by layer
340 layer_nodes = {}                                    # store nodes by layer
341 linetypes = {}                                      # store linetypes by name
342 DIMTXT = {}                                         # store DIMENSION text sizes
344 while line[0] and line[1] != 'BLOCKS':
345     line = get_line()
346     if options.auto:
347         if line[1] == '$EXTMIN':
348             xmin = get_group('10')
349         if line[1] == '$EXTMAX':
350             xmax = get_group('10')
351             ymax = get_group('20')
352     if flag == 1 and line[0] == '2':
353         layername = unicode(line[1], options.input_encode)
354         attribs = {inkex.addNS('groupmode','inkscape'): 'layer', inkex.addNS('label','inkscape'): '%s' % layername}
355         layer_nodes[layername] = inkex.etree.SubElement(doc.getroot(), 'g', attribs)
356     if flag == 2 and line[0] == '2':
357         linename = unicode(line[1], options.input_encode)
358         linetypes[linename] = []
359     if flag == 3 and line[0] == '2':
360         stylename = unicode(line[1], options.input_encode)
361     if line[0] == '2' and line[1] == 'LAYER':
362         flag = 1
363     if line[0] == '2' and line[1] == 'LTYPE':
364         flag = 2
365     if line[0] == '2' and line[1] == 'DIMSTYLE':
366         flag = 3
367     if flag == 1 and line[0] == '62':
368         layer_colors[layername] = int(line[1])
369     if flag == 2 and line[0] == '49':
370         linetypes[linename].append(float(line[1]))
371     if flag == 3 and line[0] == '140':
372         DIMTXT[stylename] = float(line[1])
373     if line[0] == '0' and line[1] == 'ENDTAB':
374         flag = 0
376 if options.auto:
377     scale = 1.0
378     if xmax > xmin:
379         scale = 210.0/(xmax - xmin)                 # scale to A4 width
380 else:
381     scale = float(options.scale)                    # manual scale factor
382 desc.text = '%s - scale = %f' % (unicode(args[0], options.input_encode), scale)
383 scale *= 90.0/25.4                                  # convert from mm to pixels
385 if not layer_nodes:
386     attribs = {inkex.addNS('groupmode','inkscape'): 'layer', inkex.addNS('label','inkscape'): '0'}
387     layer_nodes['0'] = inkex.etree.SubElement(doc.getroot(), 'g', attribs)
388     layer_colors['0'] = 7
390 for linename in linetypes.keys():                   # scale the dashed lines
391     linetype = ''
392     for length in linetypes[linename]:
393         if length == 0:                             # test for dot
394             linetype += ' 0.5,'
395         else:
396             linetype += '%.4f,' % math.fabs(length*scale)
397     if linetype == '':
398         linetypes[linename] = 'stroke-linecap: round'
399     else:
400         linetypes[linename] = 'stroke-dasharray:' + linetype
402 entity = ''
403 block = defs                                        # initiallize with dummy
404 while line[0] and line[1] != 'DICTIONARY':
405     line = get_line()
406     if entity and groups.has_key(line[0]):
407         seqs.append(line[0])                        # list of group codes
408         if line[0] == '1' or line[0] == '2' or line[0] == '3' or line[0] == '6' or line[0] == '8':  # text value
409             val = line[1].replace('\~', ' ')
410             val = inkex.re.sub( '\\\\A.*;', '', val)
411             val = inkex.re.sub( '\\\\H.*;', '', val)
412             val = inkex.re.sub( '\\^I', '', val)
413             val = inkex.re.sub( '{\\\\L', '', val)
414             val = inkex.re.sub( '}', '', val)
415             val = inkex.re.sub( '\\\\S.*;', '', val)
416             val = inkex.re.sub( '\\\\W.*;', '', val)
417             val = unicode(val, options.input_encode)
418             val = val.encode('unicode_escape')
419             val = inkex.re.sub( '\\\\\\\\U\+([0-9A-Fa-f]{4})', '\\u\\1', val)
420             val = val.decode('unicode_escape')
421         elif line[0] == '62' or line[0] == '70' or line[0] == '92' or line[0] == '93':
422             val = int(line[1])
423         elif line[0] == '10' or line[0] == '13' or line[0] == '14': # scaled float x value
424             val = scale*(float(line[1]) - xmin)
425         elif line[0] == '20' or line[0] == '23' or line[0] == '24': # scaled float y value
426             val = - scale*(float(line[1]) - ymax)
427         else:                                       # unscaled float value
428             val = float(line[1])
429         vals[groups[line[0]]].append(val)
430     elif entities.has_key(line[1]):
431         if entities.has_key(entity):
432             if block != defs:                       # in a BLOCK
433                 layer = block
434             elif vals[groups['8']]:                 # use Common Layer Name
435                 layer = layer_nodes[vals[groups['8']][0]]
436             color = '#000000'                       # default color
437             if vals[groups['8']]:
438                 if layer_colors.has_key(vals[groups['8']][0]):
439                     if colors.has_key(layer_colors[vals[groups['8']][0]]):
440                         color = colors[layer_colors[vals[groups['8']][0]]]
441             if vals[groups['62']]:                  # Common Color Number
442                 if colors.has_key(vals[groups['62']][0]):
443                     color = colors[vals[groups['62']][0]]
444             style = simplestyle.formatStyle({'stroke': '%s' % color, 'fill': 'none'})
445             w = 0.5                                 # default lineweight for POINT
446             if vals[groups['370']]:                 # Common Lineweight
447                 if vals[groups['370']][0] > 0:
448                     w = 90.0/25.4*vals[groups['370']][0]/100.0
449                     if w < 0.5:
450                         w = 0.5
451                     style = simplestyle.formatStyle({'stroke': '%s' % color, 'fill': 'none', 'stroke-width': '%.1f' % w})
452             if vals[groups['6']]:                   # Common Linetype
453                 if linetypes.has_key(vals[groups['6']][0]):
454                     style += ';' + linetypes[vals[groups['6']][0]]
455             if entities[entity]:
456                 entities[entity]()
457         entity = line[1]
458         vals = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
459         seqs = []
461 doc.write(inkex.sys.stdout)
463 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99