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