X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=share%2Fextensions%2Fdxf_input.py;h=b518969fe522b6c9cd6538c59b7556533579a6e5;hb=785664a2cf66bbb874bc72d3a3301ff26b1213fd;hp=7114d3447ba3b1f7ee54d518742cd36d0d39a134;hpb=6c3e745a94ef6b25a4ef9f018d350a7535aa45af;p=inkscape.git diff --git a/share/extensions/dxf_input.py b/share/extensions/dxf_input.py index 7114d3447..b518969fe 100644 --- a/share/extensions/dxf_input.py +++ b/share/extensions/dxf_input.py @@ -3,6 +3,8 @@ dxf_input.py - input a DXF file >= (AutoCAD Release 13 == AC1012) Copyright (C) 2008 Alvin Penner, penner@vaxxine.com +- thanks to Aaron Spike for inkex.py and simplestyle.py +- without which this would not have been possible This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -31,13 +33,22 @@ def export_MTEXT(): size = 12 # default fontsize in px if vals[groups['40']]: size = scale*vals[groups['40']][0] - attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %dpx' % size} + attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %dpx; fill: %s' % (size, color)} angle = 0 # default angle in degrees if vals[groups['50']]: angle = vals[groups['50']][0] attribs.update({'transform': 'rotate (%f %f %f)' % (-angle, x, y)}) - node = inkex.etree.SubElement(doc.getroot(), 'text', attribs) - node.text = vals[groups['1']][0] + attribs.update({'sodipodi:linespacing': '125%'}) + node = inkex.etree.SubElement(layer, 'text', attribs) + text = vals[groups['1']][0] + found = text.find('\P') # new line + while found > -1: + tspan = inkex.etree.SubElement(node , 'tspan', {'sodipodi:role': 'line'}) + tspan.text = text[:found] + text = text[(found+2):] + found = text.find('\P') + tspan = inkex.etree.SubElement(node , 'tspan', {'sodipodi:role': 'line'}) + tspan.text = text def export_POINT(): # mandatory group codes : (10, 20) (x, y) @@ -47,17 +58,21 @@ def export_POINT(): def export_LINE(): # mandatory group codes : (10, 11, 20, 21) (x1, x2, y1, y2) if vals[groups['10']] and vals[groups['11']] and vals[groups['20']] and vals[groups['21']]: - path = 'M %f,%f %f,%f' % (vals[groups['10']][0], vals[groups['20']][0], scale*vals[groups['11']][0], height - scale*vals[groups['21']][0]) + 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)) attribs = {'d': path, 'style': style} - inkex.etree.SubElement(doc.getroot(), 'path', attribs) + inkex.etree.SubElement(layer, 'path', attribs) def export_SPLINE(): # mandatory group codes : (10, 20, 70) (x, y, flags) if vals[groups['10']] and vals[groups['20']] and vals[groups['70']]: - if vals[groups['70']][0] == 8 and len(vals[groups['10']]) == 4 and len(vals[groups['20']]) == 4: + if not (vals[groups['70']][0] & 3) and len(vals[groups['10']]) == 4 and len(vals[groups['20']]) == 4: 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]) attribs = {'d': path, 'style': style} - inkex.etree.SubElement(doc.getroot(), 'path', attribs) + inkex.etree.SubElement(layer, 'path', attribs) + if not (vals[groups['70']][0] & 3) and len(vals[groups['10']]) == 3 and len(vals[groups['20']]) == 3: + 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]) + attribs = {'d': path, 'style': style} + inkex.etree.SubElement(layer, 'path', attribs) def export_CIRCLE(): # mandatory group codes : (10, 20, 40) (x, y, radius) @@ -82,7 +97,7 @@ def export_LEADER(): for i in range (1, len(vals[groups['10']])): path += ' %f,%f' % (vals[groups['10']][i], vals[groups['20']][i]) attribs = {'d': path, 'style': style} - inkex.etree.SubElement(doc.getroot(), 'path', attribs) + inkex.etree.SubElement(layer, 'path', attribs) def export_LWPOLYLINE(): # mandatory group codes : (10, 20, 70) (x, y, flags) @@ -122,7 +137,7 @@ def export_LWPOLYLINE(): if vals[groups['70']][0] == 1: # closed path path += ' z' attribs = {'d': path, 'style': style} - inkex.etree.SubElement(doc.getroot(), 'path', attribs) + inkex.etree.SubElement(layer, 'path', attribs) def generate_ellipse(xc, yc, xm, ym, w, a1, a2): rm = math.sqrt(xm*xm + ym*ym) @@ -146,51 +161,94 @@ def generate_ellipse(xc, yc, xm, ym, w, a1, a2): else: # closed arc 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) attribs = {'d': path, 'style': style} - inkex.etree.SubElement(doc.getroot(), 'path', attribs) + inkex.etree.SubElement(layer, 'path', attribs) def get_line(): return (stream.readline().strip(), stream.readline().strip()) +def get_group(group): + line = get_line() + if line[0] == group: + return float(line[1]) + else: + return 0.0 + # define DXF Entities and specify which Group Codes to monitor -entities = {'MTEXT': 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': ''} -groups = {'1': 0, '10': 1, '11': 2, '20': 3, '21': 4, '40': 5, '41': 6, '42': 7, '50': 8, '51': 9, '70': 10, '370': 11} -scale = 90.0/25.4 # convert from mm to pixels -height = 90.0*11.0 # 11 inch height in pixels -entity = '' +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': ''} +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} +colors = { 1: '#FF0000', 2: '#FFFF00', 3: '#00FF00', 4: '#00FFFF', 5: '#0000FF', + 6: '#FF00FF', 8: '#414141', 9: '#808080', 30: '#FF7F00', + 250: '#333333', 251: '#505050', 252: '#696969', 253: '#828282', 254: '#BEBEBE', 255: '#FFFFFF'} -doc = inkex.etree.parse(StringIO('')) +doc = inkex.etree.parse(StringIO('')) stream = open(inkex.sys.argv[1], 'r') +xmax = xmin = 0.0 +ymax = 297.0 # default A4 height in mm line = get_line() +flag = 0 +layer_colors = {} # store colors by layer +layer_nodes = {} # store nodes by layer while line[0] and line[1] != 'ENTITIES': line = get_line() + if line[1] == '$EXTMIN': + xmin = get_group('10') + if line[1] == '$EXTMAX': + xmax = get_group('10') + ymax = get_group('20') + if flag and line[0] == '2': + name = line[1] + attribs = {inkex.addNS('groupmode','inkscape'): 'layer', inkex.addNS('label','inkscape'): '%s' % name} + layer_nodes[name] = inkex.etree.SubElement(doc.getroot(), 'g', attribs) + if line[0] == '2' and line[1] == 'LAYER': + flag = 1 + if flag and line[0] == '62': + layer_colors[name] = int(line[1]) + if line[0] == '0' and line[1] == 'ENDTAB': + flag = 0 +scale = 90.0/25.4 # default convert from mm to pixels +if xmax > xmin: + scale *= 210.0/(xmax - xmin) # scale to A4 width +entity = '' while line[0] and line[1] != 'ENDSEC': line = get_line() if entity and groups.has_key(line[0]): seqs.append(line[0]) # list of group codes - if line[0] == '1': # text value + if line[0] == '1' or line[0] == '8': # text value val = line[1].replace('\~', ' ') - elif line[0] == '70': # unscaled integer value + val = unicode(val, "iso-8859-1") + elif line[0] == '62' or line[0] == '70': # unscaled integer value val = int(line[1]) elif line[0] == '10': # scaled float x value - val = scale*float(line[1]) + val = scale*(float(line[1]) - xmin) elif line[0] == '20': # scaled float y value - val = height - scale*float(line[1]) + val = - scale*(float(line[1]) - ymax) else: # unscaled float value val = float(line[1]) vals[groups[line[0]]].append(val) elif entities.has_key(line[1]): if entities.has_key(entity): - style = simplestyle.formatStyle({'stroke': '#000000', 'fill': 'none'}) + color = '#000000' # default color + if vals[groups['8']]: # Common Layer Name + layer = layer_nodes[vals[groups['8']][0]] + if layer_colors.has_key(vals[groups['8']][0]): + if colors.has_key(layer_colors[vals[groups['8']][0]]): + color = colors[layer_colors[vals[groups['8']][0]]] + if vals[groups['62']]: # Common Color Number + if colors.has_key(vals[groups['62']][0]): + color = colors[vals[groups['62']][0]] + style = simplestyle.formatStyle({'stroke': '%s' % color, 'fill': 'none'}) w = 0.5 # default lineweight for POINT if vals[groups['370']]: # Common Lineweight if vals[groups['370']][0] > 0: w = scale*vals[groups['370']][0]/100.0 - style = simplestyle.formatStyle({'stroke': '#000000', 'fill': 'none', 'stroke-width': '%.1f' % w}) + if w < 0.5: + w = 0.5 + style = simplestyle.formatStyle({'stroke': '%s' % color, 'fill': 'none', 'stroke-width': '%.1f' % w}) entities[entity]() entity = line[1] - vals = [[],[],[],[],[],[],[],[],[],[],[],[]] + vals = [[],[],[],[],[],[],[],[],[],[],[],[],[],[]] seqs = [] doc.write(inkex.sys.stdout)