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