From e23297675ca3cc0247403cbc142aa2710f9ad469 Mon Sep 17 00:00:00 2001 From: alvinpenner Date: Mon, 24 Nov 2008 00:40:43 +0000 Subject: [PATCH] some color support, multi-line text, automatic scaling --- share/extensions/dxf_input.py | 63 ++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/share/extensions/dxf_input.py b/share/extensions/dxf_input.py index 7114d3447..603cc94ab 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; stroke: %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)}) + attribs.update({'sodipodi:linespacing': '125%'}) node = inkex.etree.SubElement(doc.getroot(), 'text', attribs) - node.text = vals[groups['1']][0] + 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,7 +58,7 @@ 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) @@ -151,46 +162,66 @@ def generate_ellipse(xc, yc, xm, ym, w, a1, a2): 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, '10': 1, '11': 2, '20': 3, '21': 4, '40': 5, '41': 6, '42': 7, '50': 8, '51': 9, '62': 10, '70': 11, '370': 12} +colors = {1: '#FF0000', 2: '#FFFF00', 3: '#00FF00', 4: '#00FFFF', 5: '#0000FF', 6: '#FF00FF', 7: '#FFFFFF', 8: '#414141', 9: '#808080'} -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() 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') + +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 val = line[1].replace('\~', ' ') - elif line[0] == '70': # unscaled integer value + 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['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}) + 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) -- 2.30.2