Code

support for color BYLAYER
[inkscape.git] / share / extensions / dxf_input.py
1 #!/usr/bin/env python\r
2 '''\r
3 dxf_input.py - input a DXF file >= (AutoCAD Release 13 == AC1012)\r
4 \r
5 Copyright (C) 2008 Alvin Penner, penner@vaxxine.com\r
6 - thanks to Aaron Spike for inkex.py and simplestyle.py\r
7 - without which this would not have been possible\r
8 \r
9 This program is free software; you can redistribute it and/or modify\r
10 it under the terms of the GNU General Public License as published by\r
11 the Free Software Foundation; either version 2 of the License, or\r
12 (at your option) any later version.\r
13 \r
14 This program is distributed in the hope that it will be useful,\r
15 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17 GNU General Public License for more details.\r
18 \r
19 You should have received a copy of the GNU General Public License\r
20 along with this program; if not, write to the Free Software\r
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
22 '''\r
23 \r
24 import inkex, simplestyle, math\r
25 from StringIO import StringIO\r
26 \r
27 def export_MTEXT():\r
28     # mandatory group codes : (1, 10, 20) (text, x, y)\r
29     if vals[groups['1']] and vals[groups['10']] and vals[groups['20']]:\r
30         x = vals[groups['10']][0]\r
31         y = vals[groups['20']][0]\r
32         # optional group codes : (40, 50) (text height mm, text angle)\r
33         size = 12                       # default fontsize in px\r
34         if vals[groups['40']]:\r
35             size = scale*vals[groups['40']][0]\r
36         attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %dpx; fill: %s' % (size, color)}\r
37         angle = 0                       # default angle in degrees\r
38         if vals[groups['50']]:\r
39             angle = vals[groups['50']][0]\r
40             attribs.update({'transform': 'rotate (%f %f %f)' % (-angle, x, y)})\r
41         attribs.update({'sodipodi:linespacing': '125%'})\r
42         node = inkex.etree.SubElement(doc.getroot(), 'text', attribs)\r
43         text = vals[groups['1']][0]\r
44         found = text.find('\P')         # new line\r
45         while found > -1:\r
46             tspan = inkex.etree.SubElement(node , 'tspan', {'sodipodi:role': 'line'})\r
47             tspan.text = text[:found]\r
48             text = text[(found+2):]\r
49             found = text.find('\P')\r
50         tspan = inkex.etree.SubElement(node , 'tspan', {'sodipodi:role': 'line'})\r
51         tspan.text = text\r
52 \r
53 def export_POINT():\r
54     # mandatory group codes : (10, 20) (x, y)\r
55     if vals[groups['10']] and vals[groups['20']]:\r
56         generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], w/2, 0.0, 1.0, 0.0, 0.0)\r
57 \r
58 def export_LINE():\r
59     # mandatory group codes : (10, 11, 20, 21) (x1, x2, y1, y2)\r
60     if vals[groups['10']] and vals[groups['11']] and vals[groups['20']] and vals[groups['21']]:\r
61         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
62         attribs = {'d': path, 'style': style}\r
63         inkex.etree.SubElement(doc.getroot(), 'path', attribs)\r
64 \r
65 def export_SPLINE():\r
66     # mandatory group codes : (10, 20, 70) (x, y, flags)\r
67     if vals[groups['10']] and vals[groups['20']] and vals[groups['70']]:\r
68         if vals[groups['70']][0] == 8 and len(vals[groups['10']]) == 4 and len(vals[groups['20']]) == 4:\r
69             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
70             attribs = {'d': path, 'style': style}\r
71             inkex.etree.SubElement(doc.getroot(), 'path', attribs)\r
72 \r
73 def export_CIRCLE():\r
74     # mandatory group codes : (10, 20, 40) (x, y, radius)\r
75     if vals[groups['10']] and vals[groups['20']] and vals[groups['40']]:\r
76         generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], scale*vals[groups['40']][0], 0.0, 1.0, 0.0, 0.0)\r
77 \r
78 def export_ARC():\r
79     # mandatory group codes : (10, 20, 40, 50, 51) (x, y, radius, angle1, angle2)\r
80     if vals[groups['10']] and vals[groups['20']] and vals[groups['40']] and vals[groups['50']] and vals[groups['51']]:\r
81         generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], scale*vals[groups['40']][0], 0.0, 1.0, vals[groups['50']][0]*math.pi/180.0, vals[groups['51']][0]*math.pi/180.0)\r
82 \r
83 def export_ELLIPSE():\r
84     # mandatory group codes : (10, 11, 20, 21, 40, 41, 42) (xc, xm, yc, ym, width ratio, angle1, angle2)\r
85     if vals[groups['10']] and vals[groups['11']] and vals[groups['20']] and vals[groups['21']] and vals[groups['40']] and vals[groups['41']] and vals[groups['42']]:\r
86         generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], scale*vals[groups['11']][0], scale*vals[groups['21']][0], vals[groups['40']][0], vals[groups['41']][0], vals[groups['42']][0])\r
87 \r
88 def export_LEADER():\r
89     # mandatory group codes : (10, 20) (x, y)\r
90     if vals[groups['10']] and vals[groups['20']]:\r
91         if len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):\r
92             path = 'M %f,%f' % (vals[groups['10']][0], vals[groups['20']][0])\r
93             for i in range (1, len(vals[groups['10']])):\r
94                 path += ' %f,%f' % (vals[groups['10']][i], vals[groups['20']][i])\r
95             attribs = {'d': path, 'style': style}\r
96             inkex.etree.SubElement(doc.getroot(), 'path', attribs)\r
97 \r
98 def export_LWPOLYLINE():\r
99     # mandatory group codes : (10, 20, 70) (x, y, flags)\r
100     if vals[groups['10']] and vals[groups['20']] and vals[groups['70']]:\r
101         if len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):\r
102             # optional group codes : (42) (bulge)\r
103             iseqs = 0\r
104             ibulge = 0\r
105             while seqs[iseqs] != '20':\r
106                 iseqs += 1\r
107             path = 'M %f,%f' % (vals[groups['10']][0], vals[groups['20']][0])\r
108             xold = vals[groups['10']][0]\r
109             yold = vals[groups['20']][0]\r
110             for i in range (1, len(vals[groups['10']])):\r
111                 bulge = 0\r
112                 iseqs += 1\r
113                 while seqs[iseqs] != '20':\r
114                     if seqs[iseqs] == '42':\r
115                         bulge = vals[groups['42']][ibulge]\r
116                         ibulge += 1\r
117                     iseqs += 1\r
118                 if bulge:\r
119                     sweep = 0                   # sweep CCW\r
120                     if bulge < 0:\r
121                         sweep = 1               # sweep CW\r
122                         bulge = -bulge\r
123                     large = 0                   # large-arc-flag\r
124                     if bulge > 1:\r
125                         large = 1\r
126                     r = math.sqrt((vals[groups['10']][i] - xold)**2 + (vals[groups['20']][i] - yold)**2)\r
127                     r = 0.25*r*(bulge + 1.0/bulge)\r
128                     path += ' A %f,%f 0.0 %d %d %f,%f' % (r, r, large, sweep, vals[groups['10']][i], vals[groups['20']][i])\r
129                 else:\r
130                     path += ' L %f,%f' % (vals[groups['10']][i], vals[groups['20']][i])\r
131                 xold = vals[groups['10']][i]\r
132                 yold = vals[groups['20']][i]\r
133             if vals[groups['70']][0] == 1:      # closed path\r
134                 path += ' z'\r
135             attribs = {'d': path, 'style': style}\r
136             inkex.etree.SubElement(doc.getroot(), 'path', attribs)\r
137 \r
138 def generate_ellipse(xc, yc, xm, ym, w, a1, a2):\r
139     rm = math.sqrt(xm*xm + ym*ym)\r
140     a = math.atan(ym/xm)\r
141     if xm < 0:\r
142         a += math.pi\r
143     diff = (a2 - a1 + 2*math.pi) % (2*math.pi)\r
144     if diff:                        # open arc\r
145         large = 0                   # large-arc-flag\r
146         if diff > math.pi:\r
147             large = 1\r
148         xt = rm*math.cos(a1)\r
149         yt = w*rm*math.sin(a1)\r
150         x1 = xt*math.cos(a) - yt*math.sin(a)\r
151         y1 = xt*math.sin(a) + yt*math.cos(a)\r
152         xt = rm*math.cos(a2)\r
153         yt = w*rm*math.sin(a2)\r
154         x2 = xt*math.cos(a) - yt*math.sin(a)\r
155         y2 = xt*math.sin(a) + yt*math.cos(a)\r
156         path = 'M %f,%f A %f,%f %f %d 0 %f,%f' % (xc+x1, yc-y1, rm, w*rm, -180.0*a/math.pi, large, xc+x2, yc-y2)\r
157     else:                           # closed arc\r
158         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
159     attribs = {'d': path, 'style': style}\r
160     inkex.etree.SubElement(doc.getroot(), 'path', attribs)\r
161 \r
162 def get_line():\r
163     return (stream.readline().strip(), stream.readline().strip())\r
164 \r
165 def get_group(group):\r
166     line = get_line()\r
167     if line[0] == group:\r
168         return float(line[1])\r
169     else:\r
170         return 0.0\r
171 \r
172 #   define DXF Entities and specify which Group Codes to monitor\r
173 \r
174 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
175 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
176 colors = {  1: '#FF0000',   2: '#FFFF00',   3: '#00FF00',   4: '#00FFFF',   5: '#0000FF',\r
177             6: '#FF00FF',   8: '#414141',   9: '#808080',  30: '#FF7F00',\r
178           250: '#333333', 251: '#505050', 252: '#696969', 253: '#828282', 254: '#BEBEBE', 255: '#FFFFFF'}\r
179 \r
180 doc = inkex.etree.parse(StringIO('<svg xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"></svg>'))\r
181 stream = open(inkex.sys.argv[1], 'r')\r
182 xmax = xmin = 0.0\r
183 ymax = 297.0                                        # default A4 height in mm\r
184 line = get_line()\r
185 flag = 0\r
186 layers = {}                                         # store colors by layer\r
187 while line[0] and line[1] != 'ENTITIES':\r
188     line = get_line()\r
189     if line[1] == '$EXTMIN':\r
190         xmin = get_group('10')\r
191     if line[1] == '$EXTMAX':\r
192         xmax = get_group('10')\r
193         ymax = get_group('20')\r
194     if line[0] == '2':\r
195         name = line[1]\r
196     if line[0] == '2' and line[1] == 'LAYER':\r
197         flag = 1\r
198     if flag and line[0] == '62':\r
199         layers[name] = int(line[1])\r
200     if line[0] == '0' and line[1] == 'ENDTAB':\r
201         flag = 0\r
202 \r
203 scale = 90.0/25.4                                   # default convert from mm to pixels\r
204 if xmax > xmin:\r
205     scale *= 210.0/(xmax - xmin)                    # scale to A4 width\r
206 entity = ''\r
207 while line[0] and line[1] != 'ENDSEC':\r
208     line = get_line()\r
209     if entity and groups.has_key(line[0]):\r
210         seqs.append(line[0])                        # list of group codes\r
211         if line[0] == '1' or line[0] == '8':        # text value\r
212             val = line[1].replace('\~', ' ')\r
213         elif line[0] == '62' or line[0] == '70':    # unscaled integer value\r
214             val = int(line[1])\r
215         elif line[0] == '10':                       # scaled float x value\r
216             val = scale*(float(line[1]) - xmin)\r
217         elif line[0] == '20':                       # scaled float y value\r
218             val = - scale*(float(line[1]) - ymax)\r
219         else:                                       # unscaled float value\r
220             val = float(line[1])\r
221         vals[groups[line[0]]].append(val)\r
222     elif entities.has_key(line[1]):\r
223         if entities.has_key(entity):\r
224             color = '#000000'                       # default color\r
225             if vals[groups['8']]:                   # Common Layer Name\r
226                 if layers.has_key(vals[groups['8']][0]):\r
227                     if colors.has_key(layers[vals[groups['8']][0]]):\r
228                         color = colors[layers[vals[groups['8']][0]]]\r
229             if vals[groups['62']]:                  # Common Color Number\r
230                 if colors.has_key(vals[groups['62']][0]):\r
231                     color = colors[vals[groups['62']][0]]\r
232             style = simplestyle.formatStyle({'stroke': '%s' % color, 'fill': 'none'})\r
233             w = 0.5                                 # default lineweight for POINT\r
234             if vals[groups['370']]:                 # Common Lineweight\r
235                 if vals[groups['370']][0] > 0:\r
236                     w = scale*vals[groups['370']][0]/100.0\r
237                     style = simplestyle.formatStyle({'stroke': '%s' % color, 'fill': 'none', 'stroke-width': '%.1f' % w})\r
238             entities[entity]()\r
239         entity = line[1]\r
240         vals = [[],[],[],[],[],[],[],[],[],[],[],[],[],[]]\r
241         seqs = []\r
242 \r
243 doc.write(inkex.sys.stdout)\r
244 \r
245 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99\r