Code

some color support, multi-line text, automatic scaling
[inkscape.git] / share / extensions / dxf_input.py
index 7114d3447ba3b1f7ee54d518742cd36d0d39a134..603cc94abdc803bf84ef868e7783188c1b8d6968 100644 (file)
@@ -3,6 +3,8 @@
 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
@@ -31,13 +33,22 @@ def export_MTEXT():
         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; stroke: %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
+        attribs.update({'sodipodi:linespacing': '125%'})\r
         node = inkex.etree.SubElement(doc.getroot(), 'text', attribs)\r
-        node.text = vals[groups['1']][0]\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
@@ -47,7 +58,7 @@ def export_POINT():
 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
 \r
@@ -151,46 +162,66 @@ def generate_ellipse(xc, yc, xm, ym, w, a1, a2):
 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, '10': 1, '11': 2, '20': 3, '21': 4, '40': 5, '41': 6, '42': 7, '50': 8, '51': 9, '62': 10, '70': 11, '370': 12}\r
+colors = {1: '#FF0000', 2: '#FFFF00', 3: '#00FF00', 4: '#00FFFF', 5: '#0000FF', 6: '#FF00FF', 7: '#FFFFFF', 8: '#414141', 9: '#808080'}\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
 while line[0] and line[1] != 'ENTITIES':\r
     line = get_line()\r
-\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
+\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
             val = line[1].replace('\~', ' ')\r
-        elif line[0] == '70':                       # unscaled integer value\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['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
+                    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