~centralelyon2010/inkscape/imagelinks2

« back to all changes in this revision

Viewing changes to share/extensions/dxf_input.py

  • Committer: JazzyNico
  • Date: 2011-08-29 20:25:30 UTC
  • Revision ID: nicoduf@yahoo.fr-20110829202530-6deuoz11q90usldv
Code refactoring and merging with trunk (revision 10599).

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
        size = 12                       # default fontsize in px
36
36
        if vals[groups['40']]:
37
37
            size = scale*vals[groups['40']][0]
38
 
        attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %.1fpx; fill: %s' % (size, color)}
 
38
        attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %.1fpx; fill: %s; font-family: %s' % (size, color, options.font)}
39
39
        angle = 0                       # default angle in degrees
40
40
        if vals[groups['50']]:
41
41
            angle = vals[groups['50']][0]
65
65
def export_POINT():
66
66
    # mandatory group codes : (10, 20) (x, y)
67
67
    if vals[groups['10']] and vals[groups['20']]:
68
 
        generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], w/2, 0.0, 1.0, 0.0, 0.0)
 
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)
69
72
 
70
73
def export_LINE():
71
74
    # mandatory group codes : (10, 11, 20, 21) (x1, x2, y1, y2)
72
75
    if vals[groups['10']] and vals[groups['11']] and vals[groups['20']] and vals[groups['21']]:
73
 
        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))
 
76
        path = 'M %f,%f %f,%f' % (vals[groups['10']][0], vals[groups['20']][0], scale*(vals[groups['11']][0] - xmin), height - scale*(vals[groups['21']][0] - ymin))
74
77
        attribs = {'d': path, 'style': style}
75
78
        inkex.etree.SubElement(layer, 'path', attribs)
76
79
 
77
80
def export_SPLINE():
78
 
    # mandatory group codes : (10, 20, 70) (x, y, flags)
79
 
    if vals[groups['10']] and vals[groups['20']] and vals[groups['70']]:
80
 
        if not (vals[groups['70']][0] & 3) and len(vals[groups['10']]) == 4 and len(vals[groups['20']]) == 4:
81
 
            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])
 
81
    # see : http://www.mactech.com/articles/develop/issue_25/schneider.html
 
82
    # mandatory group codes : (10, 20, 40, 70) (x[], y[], knots[], flags)
 
83
    if vals[groups['70']] and not (vals[groups['70']][0] & 3) and len(vals[groups['10']]) == len(vals[groups['20']]) and vals[groups['10']] and vals[groups['20']] and vals[groups['40']]:
 
84
        knots = len(vals[groups['40']])
 
85
        ctrls = len(vals[groups['10']])
 
86
        if ctrls > 3 and knots == ctrls + 4:    # cubic
 
87
            if ctrls > 4:
 
88
                for i in range (knots - 5, 3, -1):
 
89
                    a0 = (vals[groups['40']][i] - vals[groups['40']][i-2])/(vals[groups['40']][i+1] - vals[groups['40']][i-2])
 
90
                    a1 = (vals[groups['40']][i] - vals[groups['40']][i-1])/(vals[groups['40']][i+2] - vals[groups['40']][i-1])
 
91
                    vals[groups['10']].insert(i-1, (1.0 - a1)*vals[groups['10']][i-2] + a1*vals[groups['10']][i-1])
 
92
                    vals[groups['20']].insert(i-1, (1.0 - a1)*vals[groups['20']][i-2] + a1*vals[groups['20']][i-1])
 
93
                    vals[groups['10']][i-2] = (1.0 - a0)*vals[groups['10']][i-3] + a0*vals[groups['10']][i-2]
 
94
                    vals[groups['20']][i-2] = (1.0 - a0)*vals[groups['20']][i-3] + a0*vals[groups['20']][i-2]
 
95
                    vals[groups['40']].insert(i, vals[groups['40']][i])
 
96
                knots = len(vals[groups['40']])
 
97
                for i in range (knots - 6, 3, -2):
 
98
                    a1 = (vals[groups['40']][i] - vals[groups['40']][i-1])/(vals[groups['40']][i+2] - vals[groups['40']][i-1])
 
99
                    vals[groups['10']].insert(i-1, (1.0 - a1)*vals[groups['10']][i-2] + a1*vals[groups['10']][i-1])
 
100
                    vals[groups['20']].insert(i-1, (1.0 - a1)*vals[groups['20']][i-2] + a1*vals[groups['20']][i-1])
 
101
            ctrls = len(vals[groups['10']])
 
102
            path = 'M %f,%f' % (vals[groups['10']][0], vals[groups['20']][0])
 
103
            for i in range (0, (ctrls - 1)/3):
 
104
                path += ' C %f,%f %f,%f %f,%f' % (vals[groups['10']][3*i + 1], vals[groups['20']][3*i + 1], vals[groups['10']][3*i + 2], vals[groups['20']][3*i + 2], vals[groups['10']][3*i + 3], vals[groups['20']][3*i + 3])
82
105
            attribs = {'d': path, 'style': style}
83
106
            inkex.etree.SubElement(layer, 'path', attribs)
84
 
        if not (vals[groups['70']][0] & 3) and len(vals[groups['10']]) == 3 and len(vals[groups['20']]) == 3:
 
107
        if ctrls == 3 and knots == 6:           # quadratic
85
108
            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])
86
109
            attribs = {'d': path, 'style': style}
87
110
            inkex.etree.SubElement(layer, 'path', attribs)
 
111
        if ctrls == 5 and knots == 8:           # spliced quadratic
 
112
            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])
 
113
            attribs = {'d': path, 'style': style}
 
114
            inkex.etree.SubElement(layer, 'path', attribs)
88
115
 
89
116
def export_CIRCLE():
90
117
    # mandatory group codes : (10, 20, 40) (x, y, radius)
118
145
            # optional group codes : (42) (bulge)
119
146
            iseqs = 0
120
147
            ibulge = 0
 
148
            if vals[groups['70']][0]:           # closed path
 
149
                seqs.append('20')
 
150
                vals[groups['10']].append(vals[groups['10']][0])
 
151
                vals[groups['20']].append(vals[groups['20']][0])
121
152
            while seqs[iseqs] != '20':
122
153
                iseqs += 1
123
154
            path = 'M %f,%f' % (vals[groups['10']][0], vals[groups['20']][0])
146
177
                    path += ' L %f,%f' % (vals[groups['10']][i], vals[groups['20']][i])
147
178
                xold = vals[groups['10']][i]
148
179
                yold = vals[groups['20']][i]
149
 
            if vals[groups['70']][0] == 1:      # closed path
 
180
            if vals[groups['70']][0]:           # closed path
150
181
                path += ' z'
151
182
            attribs = {'d': path, 'style': style}
152
183
            inkex.etree.SubElement(layer, 'path', attribs)
154
185
def export_HATCH():
155
186
    # mandatory group codes : (10, 20, 70, 72, 92, 93) (x, y, fill, Edge Type, Path Type, Number of edges)
156
187
    if vals[groups['10']] and vals[groups['20']] and vals[groups['70']] and vals[groups['72']] and vals[groups['92']] and vals[groups['93']]:
157
 
        if vals[groups['70']][0] and len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):
 
188
        if len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):
158
189
            # optional group codes : (11, 21, 40, 50, 51, 73) (x, y, r, angle1, angle2, CCW)
159
190
            i10 = 1    # count start points
160
191
            i11 = 0    # count line end points
193
224
                        i40 += 1
194
225
                        i72 += 1
195
226
                    elif vals[groups['72']][i72] == 1:      # line
196
 
                        path += 'L %f,%f ' % (scale*(vals[groups['11']][i11] - xmin), -scale*(vals[groups['21']][i11] - ymax))
 
227
                        path += 'L %f,%f ' % (scale*(vals[groups['11']][i11] - xmin), height - scale*(vals[groups['21']][i11] - ymin))
197
228
                        i11 += 1
198
229
                        i72 += 1
199
230
                    i10 += 1
200
231
                path += "z "
201
 
            style = simplestyle.formatStyle({'fill': '%s' % color})
 
232
            if vals[groups['70']][0]:
 
233
                style = simplestyle.formatStyle({'fill': '%s' % color})
 
234
            else:
 
235
                style = simplestyle.formatStyle({'fill': 'url(#Hatch)', 'fill-opacity': '1.0'})
202
236
            attribs = {'d': path, 'style': style}
203
237
            inkex.etree.SubElement(layer, 'path', attribs)
204
238
 
217
251
            path = 'M %f,%f %f,%f' % (vals[groups['10']][0], vals[groups['20']][0], vals[groups['10']][0], vals[groups['23']][0])
218
252
        else:
219
253
            return
220
 
        attribs = {'d': path, 'style': style + '; marker-start: url(#DistanceX); marker-end: url(#DistanceX)'}
 
254
        attribs = {'d': path, 'style': style + '; marker-start: url(#DistanceX); marker-end: url(#DistanceX); stroke-width: 0.25px'}
221
255
        inkex.etree.SubElement(layer, 'path', attribs)
222
256
        x = scale*(vals[groups['11']][0] - xmin)
223
 
        y = - scale*(vals[groups['21']][0] - ymax)
 
257
        y = height - scale*(vals[groups['21']][0] - ymin)
224
258
        size = 12                   # default fontsize in px
225
259
        if vals[groups['3']]:
226
260
            if DIMTXT.has_key(vals[groups['3']][0]):
227
261
                size = scale*DIMTXT[vals[groups['3']][0]]
228
262
                if size < 2:
229
263
                    size = 2
230
 
        attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %.1fpx; fill: %s' % (size, color)}
 
264
        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)}
231
265
        if dx == 0:
232
266
            attribs.update({'transform': 'rotate (%f %f %f)' % (-90, x, y)})
233
267
        node = inkex.etree.SubElement(layer, 'text', attribs)
237
271
def export_INSERT():
238
272
    # mandatory group codes : (2, 10, 20) (block name, x, y)
239
273
    if vals[groups['2']] and vals[groups['10']] and vals[groups['20']]:
240
 
        x = vals[groups['10']][0]
241
 
        y = vals[groups['20']][0] - scale*ymax
 
274
        x = vals[groups['10']][0] + scale*xmin
 
275
        y = vals[groups['20']][0] - scale*ymin - height
242
276
        attribs = {'x': '%f' % x, 'y': '%f' % y, inkex.addNS('href','xlink'): '#' + quote(vals[groups['2']][0].encode("utf-8"))}
243
277
        inkex.etree.SubElement(layer, 'use', attribs)
244
278
 
280
314
    attribs = {'d': path, 'style': style}
281
315
    inkex.etree.SubElement(layer, 'path', attribs)
282
316
 
 
317
def generate_gcodetools_point(xc, yc):
 
318
    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)
 
319
    attribs = {'d': path, inkex.addNS('dxfpoint','inkscape'):'1', 'style': 'stroke:#ff0000;fill:#ff0000'}
 
320
    inkex.etree.SubElement(layer, 'path', attribs)
 
321
 
283
322
def get_line():
284
323
    return (stream.readline().strip(), stream.readline().strip())
285
324
 
292
331
 
293
332
#   define DXF Entities and specify which Group Codes to monitor
294
333
 
295
 
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, 'DICTIONARY': False}
 
334
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, 'ENDSEC': False}
296
335
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}
297
336
colors = {  1: '#FF0000',   2: '#FFFF00',   3: '#00FF00',   4: '#00FFFF',   5: '#0000FF',
298
337
            6: '#FF00FF',   8: '#414141',   9: '#808080',  12: '#BD0000',  30: '#FF7F00',
301
340
parser = inkex.optparse.OptionParser(usage="usage: %prog [options] SVGfile", option_class=inkex.InkOption)
302
341
parser.add_option("--auto", action="store", type="inkbool", dest="auto", default=True)
303
342
parser.add_option("--scale", action="store", type="string", dest="scale", default="1.0")
 
343
parser.add_option("--xmin", action="store", type="string", dest="xmin", default="0.0")
 
344
parser.add_option("--ymin", action="store", type="string", dest="ymin", default="0.0")
 
345
parser.add_option("--gcodetoolspoints", action="store", type="inkbool", dest="gcodetoolspoints", default=True)
304
346
parser.add_option("--encoding", action="store", type="string", dest="input_encode", default="latin_1")
 
347
parser.add_option("--font", action="store", type="string", dest="font", default="Arial")
305
348
parser.add_option("--tab", action="store", type="string", dest="tab", default="Options")
306
349
parser.add_option("--inputhelp", action="store", type="string", dest="inputhelp", default="")
307
350
(options, args) = parser.parse_args(inkex.sys.argv[1:])
308
 
doc = inkex.etree.parse(StringIO('<svg xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"></svg>'))
 
351
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)))
309
352
desc = inkex.etree.SubElement(doc.getroot(), 'desc', {})
310
353
defs = inkex.etree.SubElement(doc.getroot(), 'defs', {})
311
354
marker = inkex.etree.SubElement(defs, 'marker', {'id': 'DistanceX', 'orient': 'auto', 'refX': '0.0', 'refY': '0.0', 'style': 'overflow:visible'})
312
355
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'})
 
356
pattern = inkex.etree.SubElement(defs, 'pattern', {'id': 'Hatch', 'patternUnits': 'userSpaceOnUse', 'width': '8', 'height': '8', 'x': '0', 'y': '0'})
 
357
inkex.etree.SubElement(pattern, 'path', {'d': 'M8 4 l-4,4', 'stroke': '#000000', 'stroke-width': '0.25', 'linecap': 'square'})
 
358
inkex.etree.SubElement(pattern, 'path', {'d': 'M6 2 l-4,4', 'stroke': '#000000', 'stroke-width': '0.25', 'linecap': 'square'})
 
359
inkex.etree.SubElement(pattern, 'path', {'d': 'M4 0 l-4,4', 'stroke': '#000000', 'stroke-width': '0.25', 'linecap': 'square'})
313
360
stream = open(args[0], 'r')
314
 
xmax = xmin = 0.0
315
 
ymax = 297.0                                        # default A4 height in mm
 
361
xmax = xmin = ymin = 0.0
 
362
height = 297.0*90.0/25.4                            # default A4 height in pixels
316
363
line = get_line()
317
364
flag = 0                                            # (0, 1, 2, 3) = (none, LAYER, LTYPE, DIMTXT)
318
365
layer_colors = {}                                   # store colors by layer
325
372
    if options.auto:
326
373
        if line[1] == '$EXTMIN':
327
374
            xmin = get_group('10')
 
375
            ymin = get_group('20')
328
376
        if line[1] == '$EXTMAX':
329
377
            xmax = get_group('10')
330
 
            ymax = get_group('20')
331
378
    if flag == 1 and line[0] == '2':
332
379
        layername = unicode(line[1], options.input_encode)
333
380
        attribs = {inkex.addNS('groupmode','inkscape'): 'layer', inkex.addNS('label','inkscape'): '%s' % layername}
358
405
        scale = 210.0/(xmax - xmin)                 # scale to A4 width
359
406
else:
360
407
    scale = float(options.scale)                    # manual scale factor
 
408
    xmin = float(options.xmin)
 
409
    ymin = float(options.ymin)
361
410
desc.text = '%s - scale = %f' % (unicode(args[0], options.input_encode), scale)
362
411
scale *= 90.0/25.4                                  # convert from mm to pixels
363
412
 
369
418
for linename in linetypes.keys():                   # scale the dashed lines
370
419
    linetype = ''
371
420
    for length in linetypes[linename]:
372
 
        linetype += '%.4f,' % math.fabs(length*scale)
373
 
    linetypes[linename] = 'stroke-dasharray:' + linetype
 
421
        if length == 0:                             # test for dot
 
422
            linetype += ' 0.5,'
 
423
        else:
 
424
            linetype += '%.4f,' % math.fabs(length*scale)
 
425
    if linetype == '':
 
426
        linetypes[linename] = 'stroke-linecap: round'
 
427
    else:
 
428
        linetypes[linename] = 'stroke-dasharray:' + linetype
374
429
 
375
430
entity = ''
 
431
inENTITIES = False
376
432
block = defs                                        # initiallize with dummy
377
 
while line[0] and line[1] != 'DICTIONARY':
 
433
while line[0] and (line[1] != 'ENDSEC' or not inENTITIES):
378
434
    line = get_line()
 
435
    if line[1] == 'ENTITIES':
 
436
        inENTITIES = True
379
437
    if entity and groups.has_key(line[0]):
380
438
        seqs.append(line[0])                        # list of group codes
381
439
        if line[0] == '1' or line[0] == '2' or line[0] == '3' or line[0] == '6' or line[0] == '8':  # text value
396
454
        elif line[0] == '10' or line[0] == '13' or line[0] == '14': # scaled float x value
397
455
            val = scale*(float(line[1]) - xmin)
398
456
        elif line[0] == '20' or line[0] == '23' or line[0] == '24': # scaled float y value
399
 
            val = - scale*(float(line[1]) - ymax)
 
457
            val = height - scale*(float(line[1]) - ymin)
400
458
        else:                                       # unscaled float value
401
459
            val = float(line[1])
402
460
        vals[groups[line[0]]].append(val)
425
483
            if vals[groups['6']]:                   # Common Linetype
426
484
                if linetypes.has_key(vals[groups['6']][0]):
427
485
                    style += ';' + linetypes[vals[groups['6']][0]]
428
 
            entities[entity]()
 
486
            if entities[entity]:
 
487
                entities[entity]()
429
488
        entity = line[1]
430
489
        vals = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
431
490
        seqs = []
432
491
 
433
492
doc.write(inkex.sys.stdout)
434
493
 
435
 
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99
 
494
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99