Statistics
| Revision:

root / trunk / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / DwgFileV15Reader.java @ 7178

History | View | Annotate | Download (16.5 KB)

1
/* jdwglib. Java Library for reading Dwg files.
2
 * 
3
 * Author: Jose Morell Rama (jose.morell@gmail.com).
4
 * Port from the Pythoncad Dwg library by Art Haas.
5
 *
6
 * Copyright (C) 2005 Jose Morell, IVER TI S.A. and Generalitat Valenciana
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 * Jose Morell (jose.morell@gmail.com)
25
 * 
26
 * or
27
 *
28
 * IVER TI S.A.
29
 *  C/Salamanca, 50
30
 *  46005 Valencia
31
 *  Spain
32
 *  +34 963163400
33
 *  dac@iver.es
34
 */
35
package com.iver.cit.jdwglib.dwg;
36

    
37
import java.io.File;
38
import java.io.FileInputStream;
39
import java.io.IOException;
40
import java.nio.ByteBuffer;
41
import java.nio.ByteOrder;
42
import java.nio.channels.FileChannel;
43
import java.util.ArrayList;
44

    
45
import com.iver.cit.jdwglib.dwg.objects.DwgArc;
46
import com.iver.cit.jdwglib.dwg.objects.DwgAttdef;
47
import com.iver.cit.jdwglib.dwg.objects.DwgAttrib;
48
import com.iver.cit.jdwglib.dwg.objects.DwgBlock;
49
import com.iver.cit.jdwglib.dwg.objects.DwgBlockControl;
50
import com.iver.cit.jdwglib.dwg.objects.DwgBlockHeader;
51
import com.iver.cit.jdwglib.dwg.objects.DwgCircle;
52
import com.iver.cit.jdwglib.dwg.objects.DwgEllipse;
53
import com.iver.cit.jdwglib.dwg.objects.DwgEndblk;
54
import com.iver.cit.jdwglib.dwg.objects.DwgInsert;
55
import com.iver.cit.jdwglib.dwg.objects.DwgLayer;
56
import com.iver.cit.jdwglib.dwg.objects.DwgLayerControl;
57
import com.iver.cit.jdwglib.dwg.objects.DwgLine;
58
import com.iver.cit.jdwglib.dwg.objects.DwgLinearDimension;
59
import com.iver.cit.jdwglib.dwg.objects.DwgLwPolyline;
60
import com.iver.cit.jdwglib.dwg.objects.DwgMText;
61
import com.iver.cit.jdwglib.dwg.objects.DwgPoint;
62
import com.iver.cit.jdwglib.dwg.objects.DwgPolyline2D;
63
import com.iver.cit.jdwglib.dwg.objects.DwgPolyline3D;
64
import com.iver.cit.jdwglib.dwg.objects.DwgSeqend;
65
import com.iver.cit.jdwglib.dwg.objects.DwgSolid;
66
import com.iver.cit.jdwglib.dwg.objects.DwgSpline;
67
import com.iver.cit.jdwglib.dwg.objects.DwgText;
68
import com.iver.cit.jdwglib.dwg.objects.DwgVertex2D;
69
import com.iver.cit.jdwglib.dwg.objects.DwgVertex3D;
70

    
71
import com.iver.cit.gvsig.fmap.drivers.dgn.ByteUtils;
72

    
73
import freenet.support.HexUtil;
74

    
75
/**
76
 * The DwgFileV15Reader reads the DWG version 15 format
77
 * 
78
 * @author jmorell
79
 */
80
public class DwgFileV15Reader extends DwgFileReader {
81
        protected DwgFile dwgFile;
82
        
83
        /**
84
         * Reads the DWG version 15 format
85
         * 
86
         * @param dwgFile Represents the DWG file that we want to read
87
     * @throws IOException When DWG file path is wrong
88
         */
89
        public void read(DwgFile dwgFile) throws IOException {
90
//                System.out.println("DwgFileV15Reader.read() executed ...");
91
                this.dwgFile = dwgFile;
92
                File f = new File(dwgFile.getFileName());
93
                FileInputStream fis = new FileInputStream(f);
94
                FileChannel fc = fis.getChannel();
95
                long s = fc.size();
96
                ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, s);
97
                readDwgSectionOffsets(bb);
98
                try {
99
                        long t0 = System.currentTimeMillis();
100
                        readDwgObjectOffsets(bb);
101
                        long t1 = System.currentTimeMillis();
102
System.out.println((t1-t0)+" en leer los objects offsets");                        
103
                        //readDwgClasses(bb);
104
                } catch (Exception e) {
105
//                    System.out.println("Error leyendo offsets y classes. Posible corrupci�n en" +
106
//                                    "el DWG file ...");
107
                }
108
                long t2 = System.currentTimeMillis();
109
                readDwgObjects(bb);
110
                long t3 = System.currentTimeMillis();
111
System.out.println((t3-t2)+" en leer los dwg objects");                
112
        }
113
        
114
        protected void readDwgSectionOffsets(ByteBuffer bb) {
115
                bb.position(19);
116
                bb.order(ByteOrder.LITTLE_ENDIAN);
117
                short codePage = bb.getShort();
118
                int count = bb.getInt();
119
                for (int i=0; i<count; i++) {
120
                        byte rec = bb.get();
121
                        int seek = bb.getInt();
122
                        int size = bb.getInt();
123
                        if (rec==0) {
124
                                dwgFile.addDwgSectionOffset("HEADERS", seek, size);
125
                        } else if (rec==1) {
126
                                dwgFile.addDwgSectionOffset("CLASSES", seek, size);
127
                        } else if (rec==2) {
128
                                dwgFile.addDwgSectionOffset("OBJECTS", seek, size);
129
                        } else if (rec==3) {
130
                                dwgFile.addDwgSectionOffset("UNKNOWN", seek, size);
131
                        } else if (rec==4) {
132
                                dwgFile.addDwgSectionOffset("R14DATA", seek, size);
133
                        } else if (rec==5) {
134
                                dwgFile.addDwgSectionOffset("R14REC5", seek, size);
135
                        } else {
136
//                                System.out.println("ERROR: C�digo de n�mero de registro no soportado: " + rec);
137
                        }
138
                }
139
        }
140
        protected void readDwgObjectOffsets(ByteBuffer bb) throws Exception {
141
                int offset = dwgFile.getDwgSectionOffset("OBJECTS");
142
                bb.position(offset);
143
                while (true) {
144
                        bb.order(ByteOrder.BIG_ENDIAN);
145
                        short size = bb.getShort();
146
                        if (size==2) break;
147
                        bb.order(ByteOrder.LITTLE_ENDIAN);
148
                        byte[] dataBytes = new byte[size];
149
                        for (int i=0; i<dataBytes.length; i++) {
150
                                dataBytes[i] = bb.get();
151
                        }
152
                        int[] data = DwgUtil.bytesToMachineBytes(dataBytes);
153
                        int lastHandle=0;
154
                        int lastLoc=0;
155
                        int bitPos=0;
156
                        int bitMax=(size-2)*8;
157
                        while (bitPos<bitMax) {
158
                                ArrayList v = DwgUtil.getModularChar(data, bitPos);
159
                                bitPos = ((Integer)v.get(0)).intValue();
160
                                lastHandle = lastHandle + ((Integer)v.get(1)).intValue();
161
                                v = DwgUtil.getModularChar(data, bitPos);
162
                                bitPos = ((Integer)v.get(0)).intValue();
163
                                lastLoc = lastLoc + ((Integer)v.get(1)).intValue();
164
                                dwgFile.addDwgObjectOffset(lastHandle, lastLoc);
165
                        }
166
                }
167
        }
168
        protected void readDwgClasses(ByteBuffer bb) throws Exception {
169
                int offset = dwgFile.getDwgSectionOffset("CLASSES");
170
                // Por ahora nos saltamos los 16 bytes de control
171
                bb.position(offset+16);
172
                bb.order(ByteOrder.LITTLE_ENDIAN);
173
                int size = bb.getInt();
174
                byte[] dataBytes = new byte[size];
175
                for (int i=0; i<dataBytes.length; i++) {
176
                        dataBytes[i] = bb.get();
177
                }
178
                int[] data = DwgUtil.bytesToMachineBytes(dataBytes);
179
                for (int i=0; i<data.length; i++) {
180
                        data[i] = (byte)ByteUtils.getUnsigned((byte)data[i]);
181
                }
182
                bb.position(bb.position()+2+16);
183
                int maxbit = size * 8;
184
                int bitPos = 0;
185
                while ((bitPos+8) < maxbit) {
186
                        ArrayList v = DwgUtil.getBitShort(data, bitPos);
187
                        bitPos = ((Integer)v.get(0)).intValue();
188
                        v = DwgUtil.getBitShort(data, bitPos);
189
                        bitPos = ((Integer)v.get(0)).intValue();
190
                        v = DwgUtil.getTextString(data, bitPos);
191
                        bitPos = ((Integer)v.get(0)).intValue();
192
                        v = DwgUtil.getTextString(data, bitPos);
193
                        bitPos = ((Integer)v.get(0)).intValue();
194
                        v = DwgUtil.getTextString(data, bitPos);
195
                        bitPos = ((Integer)v.get(0)).intValue();
196
                        v = DwgUtil.testBit(data, bitPos);
197
                        bitPos = ((Integer)v.get(0)).intValue();
198
                        v = DwgUtil.getBitShort(data, bitPos);
199
                        bitPos = ((Integer)v.get(0)).intValue();
200
                }
201
        }
202
        protected void readDwgObjects(ByteBuffer bb) {
203
                //System.out.println("DwgFileV15Reader.readDwgObjects() executed ...");
204
                for (int i=0; i<dwgFile.getDwgObjectOffsets().size(); i++) {
205
                        DwgObjectOffset doo = (DwgObjectOffset)dwgFile.getDwgObjectOffsets().get(i);
206
                        DwgObject obj = readDwgObject(bb, doo.getOffset());
207
                        if (obj!=null) {
208
                dwgFile.addDwgObject(obj);
209
            }
210
                }
211
        }
212
        protected DwgObject readDwgObject(ByteBuffer bb, int offset) {
213
            try {
214
                        bb.position(offset);
215
                        int size = DwgUtil.getModularShort(bb);
216
                        bb.order(ByteOrder.LITTLE_ENDIAN);
217
                        byte[] dataBytes = new byte[size];
218
                        String[] dataMachValString = new String[size];
219
                        int[] data = new int[size];
220
                        // fromfile de python devuelve los bytes en valores nativos de la m�quina ...
221
                        for (int i=0; i<size; i++) {
222
                                dataBytes[i] = bb.get();
223
                                dataMachValString[i] = HexUtil.bytesToHex(new byte[]{dataBytes[i]});
224
                                Integer dataMachValShort = Integer.decode("0x" + dataMachValString[i]);
225
                                data[i] = dataMachValShort.byteValue();
226
                                // Hacerlos unsigned ...
227
                                data[i] = ByteUtils.getUnsigned((byte)data[i]);
228
                        }
229
                        int bitPos = 0;
230
                        ArrayList v = DwgUtil.getBitShort(data, bitPos);
231
                        bitPos = ((Integer)v.get(0)).intValue();
232
                        int type = ((Integer)v.get(1)).intValue();
233
                        //System.out.println("type = " + type);
234
                    DwgObject obj = new DwgObject();
235
                    if (type==0x11) {
236
                            obj = new DwgArc();
237
                            obj.setGraphicsFlag(true);
238
                        } else if (type==0x12) {
239
                                obj = new DwgCircle();
240
                            obj.setGraphicsFlag(true);
241
                        } else if (type==0x13) {
242
                                obj = new DwgLine();
243
                            obj.setGraphicsFlag(true);
244
                        } else if (type==0x1B) {
245
                            obj = new DwgPoint();
246
                            obj.setGraphicsFlag(true);
247
                        } else if (type==0x0F) {
248
                            obj = new DwgPolyline2D();
249
                            obj.setGraphicsFlag(true);
250
                        } else if (type==0x10) {
251
                            obj = new DwgPolyline3D();
252
                            obj.setGraphicsFlag(true);
253
                        } else if (type==0x0A) {
254
                            obj = new DwgVertex2D();
255
                            obj.setGraphicsFlag(true);
256
                        } else if (type==0x0B) {
257
                            obj = new DwgVertex3D();
258
                            obj.setGraphicsFlag(true);
259
                        } else if (type==0x6) {
260
                            obj = new DwgSeqend();
261
                            obj.setGraphicsFlag(true);
262
                        } else if (type==0x1) {
263
                            obj = new DwgText();
264
                            obj.setGraphicsFlag(true);
265
                        } else if (type==0x2) {
266
                            obj = new DwgAttrib();
267
                            obj.setGraphicsFlag(true);
268
                        } else if (type==0x3) {
269
                            obj = new DwgAttdef();
270
                            obj.setGraphicsFlag(true);
271
                        } else if (type==0x4) {
272
                            obj = new DwgBlock();
273
                //System.out.println("Creando objeto tipo DwgBlock");
274
                            obj.setGraphicsFlag(true);
275
                        } else if (type==0x5) {
276
                            obj = new DwgEndblk();
277
                            obj.setGraphicsFlag(true);
278
                        } else if (type==0x30) {
279
                            obj = new DwgBlockControl();
280
                            obj.setGraphicsFlag(false);
281
                        } else if (type==0x31) {
282
                            obj = new DwgBlockHeader();
283
                //System.out.println("Creando objeto tipo DwgBlockHeader");
284
                            obj.setGraphicsFlag(false);
285
                        } else if (type==0x32) {
286
                            obj = new DwgLayerControl();
287
                            obj.setGraphicsFlag(false);
288
                        } else if (type==0x33) {
289
                            obj = new DwgLayer();
290
                            obj.setGraphicsFlag(false);
291
                        } else if (type==0x7) {
292
                            obj = new DwgInsert();
293
                //System.out.println("Creando objeto tipo DwgInsert");
294
                            obj.setGraphicsFlag(true);
295
                        } else if (type==0x2C) {
296
                            obj = new DwgMText();
297
                            obj.setGraphicsFlag(true);
298
                        } else if (type==0x1F) {
299
                            obj = new DwgSolid();
300
                            obj.setGraphicsFlag(true);
301
                        } else if (type==0x23) {
302
                            obj = new DwgEllipse();
303
                            obj.setGraphicsFlag(true);
304
                        } else if (type==0x24) {
305
                            obj = new DwgSpline();
306
                            obj.setGraphicsFlag(true);
307
                        } else if (type==0x15) {
308
                            obj = new DwgLinearDimension();
309
                            obj.setGraphicsFlag(true);
310
                        } else if (type==0x4D) {
311
                            obj = new DwgLwPolyline();
312
                //System.out.println("Creando objeto tipo DwgLwPolyline");
313
                            obj.setGraphicsFlag(true);
314
                        } else if (type==0x4E) {
315
                            obj = new DwgLwPolyline();
316
                            obj.setGraphicsFlag(true);
317
                        } else if (type==0x4F) {
318
                            obj = new DwgLwPolyline();
319
                            obj.setGraphicsFlag(true);
320
                        } else if (type==0x50) {
321
                            obj = new DwgLwPolyline();
322
                            obj.setGraphicsFlag(true);
323
                        } else if (type==0x51) {
324
                            obj = new DwgLwPolyline();
325
                            obj.setGraphicsFlag(true);
326
                        } else if (type==0x52) {
327
                            obj = new DwgLwPolyline();
328
                            obj.setGraphicsFlag(true);
329
                        } else if (type==0x53) {
330
                            obj = new DwgLwPolyline();
331
                            obj.setGraphicsFlag(true);
332
                    } else {
333
                    }
334
                    obj.setType(type);
335
                    v = DwgUtil.getRawLong(data, bitPos);
336
                    bitPos = ((Integer)v.get(0)).intValue();
337
                    int objBSize = ((Integer)v.get(1)).intValue();
338
                    obj.setSizeInBits(objBSize);
339
                    ArrayList entityHandle = new ArrayList();
340
                    v = DwgUtil.getHandle(data, bitPos);
341
                    bitPos = ((Integer)v.get(0)).intValue();
342
                    for (int i=1;i<v.size();i++) {
343
                            entityHandle.add(v.get(i));
344
                    }
345
                    obj.setHandle(DwgUtil.handleBinToHandleInt(entityHandle));
346
                    v = DwgUtil.readExtendedData(data, bitPos);
347
                    bitPos = ((Integer)v.get(0)).intValue();
348
                    ArrayList extData = (ArrayList)v.get(1);
349
                    obj.setExtendedData(extData);
350
                    
351
                    boolean gflag = false;
352
                    gflag = obj.isGraphicsFlag();
353
                    if (gflag) {
354
                            v = DwgUtil.testBit(data, bitPos);
355
                            bitPos = ((Integer)v.get(0)).intValue();
356
                            boolean val = ((Boolean)v.get(1)).booleanValue();
357
                            if (val) {
358
                                    v = DwgUtil.getRawLong(data, bitPos);
359
                                    bitPos = ((Integer)v.get(0)).intValue();
360
                                    size = ((Integer)v.get(1)).intValue();
361
                                    int bgSize = size*8;
362
                                        Integer giData = (Integer)DwgUtil.getBits(data, bgSize, bitPos);
363
                                        obj.setGraphicData(giData.intValue());
364
                                        bitPos = bitPos + bgSize;
365
                            }
366
                    }
367
                        readSpecificObject(obj, data, bitPos);
368
                    return obj;
369
            } catch (Exception e) {
370
//                System.out.println("Exception capturada. Probablemente se ha encontrado un" +
371
//                                "objeto con type non fixed");
372
                //e.printStackTrace();
373
                return null;
374
            }
375
        }
376
        protected void readSpecificObject(DwgObject obj, int[] data, int bitPos) throws Exception {
377
                if (obj.getType()==0x11) {
378
                        ((DwgArc)obj).readDwgArcV15(data, bitPos);
379
                } else if (obj.getType()==0x12) {
380
                        ((DwgCircle)obj).readDwgCircleV15(data, bitPos);
381
                } else if (obj.getType()==0x13) {
382
                        ((DwgLine)obj).readDwgLineV15(data, bitPos);
383
                } else if (obj.getType()==0x1B) {
384
                        ((DwgPoint)obj).readDwgPointV15(data, bitPos);
385
                } else if (obj.getType()==0x0F) {
386
                        ((DwgPolyline2D)obj).readDwgPolyline2DV15(data, bitPos);
387
                } else if (obj.getType()==0x10) {
388
                        ((DwgPolyline3D)obj).readDwgPolyline3DV15(data, bitPos);
389
                } else if (obj.getType()==0x0A) {
390
                        ((DwgVertex2D)obj).readDwgVertex2DV15(data, bitPos);
391
                } else if (obj.getType()==0x0B) {
392
                        ((DwgVertex3D)obj).readDwgVertex3DV15(data, bitPos);
393
                } else if (obj.getType()==0x6) {
394
                        ((DwgSeqend)obj).readDwgSeqendV15(data, bitPos);
395
                } else if (obj.getType()==0x1) {
396
                        ((DwgText)obj).readDwgTextV15(data, bitPos);
397
                } else if (obj.getType()==0x2) {
398
                        ((DwgAttrib)obj).readDwgAttribV15(data, bitPos);
399
                } else if (obj.getType()==0x3) {
400
                        ((DwgAttdef)obj).readDwgAttdefV15(data, bitPos);
401
                } else if (obj.getType()==0x4) {
402
                        ((DwgBlock)obj).readDwgBlockV15(data, bitPos);
403
                } else if (obj.getType()==0x5) {
404
                        ((DwgEndblk)obj).readDwgEndblkV15(data, bitPos);
405
                } else if (obj.getType()==0x30) {
406
                        ((DwgBlockControl)obj).readDwgBlockControlV15(data, bitPos);
407
                } else if (obj.getType()==0x31) {
408
                        ((DwgBlockHeader)obj).readDwgBlockHeaderV15(data, bitPos);
409
                } else if (obj.getType()==0x32) {
410
                        ((DwgLayerControl)obj).readDwgLayerControlV15(data, bitPos);
411
                } else if (obj.getType()==0x33) {
412
                        ((DwgLayer)obj).readDwgLayerV15(data, bitPos);
413
                } else if (obj.getType()==0x7) {
414
                        ((DwgInsert)obj).readDwgInsertV15(data, bitPos);
415
                } else if (obj.getType()==0x2C) {
416
                        ((DwgMText)obj).readDwgMTextV15(data, bitPos);
417
                } else if (obj.getType()==0x1F) {
418
                        ((DwgSolid)obj).readDwgSolidV15(data, bitPos);
419
                } else if (obj.getType()==0x23) {
420
                        ((DwgEllipse)obj).readDwgEllipseV15(data, bitPos);
421
                } else if (obj.getType()==0x24) {
422
                        ((DwgSpline)obj).readDwgSplineV15(data, bitPos);
423
                } else if (obj.getType()==0x14) {
424
                        //System.out.println("... detectado un dim del tipo 0x14 ...");
425
                } else if (obj.getType()==0x15) {
426
                        //System.out.println("... detectado un dim del tipo 0x15 ...");
427
                        //((DwgLinearDimension)obj).readDwgLinearDimensionV15(data, bitPos);
428
                } else if (obj.getType()==0x16) {
429
                        //System.out.println("... detectado un dim del tipo 0x16 ...");
430
                } else if (obj.getType()==0x17) {
431
                        //System.out.println("... detectado un dim del tipo 0x17 ...");
432
                } else if (obj.getType()==0x18) {
433
                        //System.out.println("... detectado un dim del tipo 0x18 ...");
434
                } else if (obj.getType()==0x19) {
435
                        //System.out.println("... detectado un dim del tipo 0x19 ...");
436
                } else if (obj.getType()==0x1A) {
437
                        //System.out.println("... detectado un dim del tipo 0x1A ...");
438
                } else if (obj.getType()==0x4D) {
439
                        ((DwgLwPolyline)obj).readDwgLwPolylineV15(data, bitPos);
440
                } else if (obj.getType()==0x4E) {
441
                        ((DwgLwPolyline)obj).readDwgLwPolylineV15(data, bitPos);
442
                } else if (obj.getType()==0x4F) {
443
                        ((DwgLwPolyline)obj).readDwgLwPolylineV15(data, bitPos);
444
                } else if (obj.getType()==0x50) {
445
                        ((DwgLwPolyline)obj).readDwgLwPolylineV15(data, bitPos);
446
                } else if (obj.getType()==0x51) {
447
                        ((DwgLwPolyline)obj).readDwgLwPolylineV15(data, bitPos);
448
                } else if (obj.getType()==0x52) {
449
                        ((DwgLwPolyline)obj).readDwgLwPolylineV15(data, bitPos);
450
                } else if (obj.getType()==0x53) {
451
                        ((DwgLwPolyline)obj).readDwgLwPolylineV15(data, bitPos);
452
                } else {
453
                        //System.out.println("Tipo de objeto pendiente de implementaci�n");
454
                }
455
        }
456
}