Statistics
| Revision:

root / trunk / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / readers / DwgFileV12Reader.java @ 10632

History | View | Annotate | Download (54.2 KB)

1
/*
2
 * Created on 09-ene-2007
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. 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
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package com.iver.cit.jdwglib.dwg.readers;
45

    
46
import java.awt.geom.Point2D;
47
import java.io.IOException;
48
import java.nio.ByteBuffer;
49
import java.nio.ByteOrder;
50
import java.util.ArrayList;
51
import java.util.List;
52

    
53
import com.iver.cit.jdwglib.dwg.CorruptedDwgEntityException;
54
import com.iver.cit.jdwglib.dwg.DwgFile;
55
import com.iver.cit.jdwglib.dwg.DwgHandleReference;
56
import com.iver.cit.jdwglib.dwg.DwgObject;
57
import com.iver.cit.jdwglib.dwg.DwgObjectFactory;
58
import com.iver.cit.jdwglib.dwg.IDwgBlockMember;
59
import com.iver.cit.jdwglib.dwg.IDwgPolyline;
60
import com.iver.cit.jdwglib.dwg.IDwgVertex;
61
import com.iver.cit.jdwglib.dwg.objects.DwgArc;
62
import com.iver.cit.jdwglib.dwg.objects.DwgBlockHeader;
63
import com.iver.cit.jdwglib.dwg.objects.DwgCircle;
64
import com.iver.cit.jdwglib.dwg.objects.DwgEndblk;
65
import com.iver.cit.jdwglib.dwg.objects.DwgInsert;
66
import com.iver.cit.jdwglib.dwg.objects.DwgLayer;
67
import com.iver.cit.jdwglib.dwg.objects.DwgLine;
68
import com.iver.cit.jdwglib.dwg.objects.DwgMeshPolyline;
69
import com.iver.cit.jdwglib.dwg.objects.DwgPFacePolyline;
70
import com.iver.cit.jdwglib.dwg.objects.DwgPoint;
71
import com.iver.cit.jdwglib.dwg.objects.DwgPolyline2D;
72
import com.iver.cit.jdwglib.dwg.objects.DwgPolyline3D;
73
import com.iver.cit.jdwglib.dwg.objects.DwgSeqend;
74
import com.iver.cit.jdwglib.dwg.objects.DwgSolid;
75
import com.iver.cit.jdwglib.dwg.objects.DwgText;
76
import com.iver.cit.jdwglib.dwg.objects.DwgVertex2D;
77
import com.iver.cit.jdwglib.dwg.objects.DwgVertex3D;
78
import com.iver.cit.jdwglib.dwg.objects.DwgVertexPFace;
79
import com.iver.cit.jdwglib.dwg.objects.DwgVertexPFaceFace;
80

    
81
/**
82
 * Reads version 12 dwg files.
83
 * 
84
 * DWG 13 and DWG 12 are very different formats (nor do DWG 13-14 and 2000).
85
 * Thats the reason why this IDwgFileReader is very different from the rest. 
86
 * 
87
 * Documentation of reverse engineering of the format:
88
 *         http://www.iwriteiam.nl/DWG12.html
89
 * 
90
 * @author azabala
91
 * 
92
 */
93
public class DwgFileV12Reader implements IDwgFileReader{
94

    
95
        private DwgFile dwgFile;
96
        private ByteBuffer bb;
97
        ArrayList readers = new ArrayList();
98
        /*
99
         * En Python:
100
         *  if _version != 'AC1009':
101
        if _version == 'AC1010':
102
            r13 = True
103
        else:
104
            print "unknown version"
105
         * 
106
         * */
107
        private boolean r13 = false;
108
        int index = 0;
109
        
110
        ArrayList blocks = new ArrayList();
111
        
112
        
113
        /**
114
         * While DwgBlock is a DWG entity, readed from the DWG entities
115
         * section, a Block is an entry in the BLOCK Section of DWG 12 format.
116
         * 
117
         * Block has block name attribute (like DwgBlock), but its most
118
         * important attribute is its order in the Block section.
119
         * 
120
         * Inserts has a short number that we think is the order of its
121
         * referred block in the block table (the only approach to fech
122
         * the block of an insert)
123
         * */
124
        class Block {
125
                /*
126
        |----------|--------------------------------------------|
127
        | 1        | This is an anonymous Block generated by    |
128
        |          | hatching, associative dimensioning, other  |
129
        |          | internal operations, or an application     |
130
        |----------|--------------------------------------------|
131
        | 2        | This Block has Attributes                  |
132
        |----------|--------------------------------------------|
133
        | 4        | This Block is an external reference (Xref) |
134
        |----------|--------------------------------------------|
135
        | 8        | not used                                   |
136
        |----------|--------------------------------------------|
137
        | 16       | This Block is externally dependent         |
138
        |----------|--------------------------------------------|
139
        | 32       | This is a resolved external reference, or  |
140
        |          | dependent of an external reference         |
141
        |----------|--------------------------------------------|
142
        | 64       | This definition is referenced              |
143
        +-------------------------------------------------------+
144
        */
145
                byte flag;
146
                String name;
147
                short used;
148
                
149
                byte b1;
150
                short w1;
151
                byte b2;
152
                short w3;
153
                short crc;
154
        }
155
        
156
        public DwgFileV12Reader(boolean isR13){
157
                r13 = isR13;
158
                readers.add(null);//0
159
                readers.add(new LineReader());//1
160
                readers.add(new PointReader());//2
161
                readers.add(new CircleReader());//3
162
                readers.add(new ShapeReader());//4
163
                readers.add(null);//5
164
                readers.add(null);//6
165
                readers.add(new TextReader());//7
166
                readers.add(new ArcReader());//8
167
                readers.add(new TraceReader());//9
168
                readers.add(null);//10
169
                readers.add(new SolidReader());//11
170
                readers.add(new BlkReader());//12
171
                readers.add(new EndBlkReader());//13
172
                readers.add(new InsertReader());//14
173
                readers.add(new AttDefReader());//15
174
                readers.add(new AttribReader());//16
175
                readers.add(new SbEndReader());//17
176
                readers.add(null);//18
177
                readers.add(new PlineReader());//19
178
                readers.add(new VertexReader());//20
179
                readers.add(null);//21
180
                readers.add(new Face3DReader());//22
181
                readers.add(new DimReader());//23
182
                readers.add(new VPortReader());//24
183
        }
184
        
185
        
186

    
187
        /* (non-Javadoc)
188
         * @see com.iver.cit.jdwglib.dwg.readers.IDwgFileReader#read(com.iver.cit.jdwglib.dwg.DwgFile, java.nio.ByteBuffer)
189
         */
190
        public void read(DwgFile dwgFile, ByteBuffer bb) throws IOException {
191
                this.dwgFile = dwgFile;
192
                this.bb = bb;
193
                try {
194
////                        handle.seek(6, 1) # skip rest of version
195
//                        bb.position(12);
196
//                        bb.position(bb.position() + 6);
197
                        bb.position(0);
198
                        byte[] header = new byte[12];
199
                        bb.get(header);
200
                        String headerStr = new String(header);
201
                        System.out.println(headerStr);
202
                        
203
//                    (_b1, _w1, _w2, _w3, _b2) = struct.unpack("<BhhhB", handle.read(8))
204
                        bb.order(ByteOrder.LITTLE_ENDIAN);
205
                        byte b1 = bb.get();
206
                        short w1 = bb.getShort();
207
                        short w2 = bb.getShort();
208
                        short w3 = bb.getShort();
209
                        byte b2 = bb.get();
210
                        
211
                        
212
                        //To translate Python to Java, we must see the number
213
                        //of bytes of the reading
214
                        
215
//                         _estart, _eend = struct.unpack("<ll", handle.read(8))
216
                        bb.order(ByteOrder.LITTLE_ENDIAN);
217
                        int eStart = bb.getInt();
218
                        int eEnd = bb.getInt();
219
                        
220
//                        _bsstart, _l1, _bsend, _l2 = struct.unpack("<llll", handle.read(16))
221
                        int bsStart = bb.getInt();
222
                        int l1 = bb.getInt();
223
                        
224
                        int bsEnd = bb.getInt();
225
                        int l2 = bb.getInt();
226
                        
227
                        Dwg12Table blockTable = getTable();
228
                        
229
                        Dwg12Table layerTable = getTable();
230
                        
231
                        Dwg12Table styleTable = getTable();
232
                        
233
                        Dwg12Table lineTypeTable = getTable();
234
                        
235
                        Dwg12Table viewTable = getTable();
236
                        
237
                        readHeader();
238
                        
239
                        Dwg12Table ucsTable = getTable();
240
                        
241
                        bb.position(0x500);
242
                        
243
                        Dwg12Table vportTable = getTable();
244
                        
245
                        bb.position(bb.position() + 8);
246
                        
247
                        Dwg12Table appidTable = getTable();
248
                        
249
                        bb.position(bb.position() + 6);
250
                        
251
                        Dwg12Table dimStyleTable = getTable();
252
                        bb.position(0x69f);
253
                        
254
                        Dwg12Table p13table = getTable();
255
                        
256
                        bb.position(bb.position() + 38);
257
                        
258
                        int currentPosition = bb.position();
259
                        if(currentPosition != eStart){
260
                                //Se supone que deber?amos estar en eStart
261
                                throw new RuntimeException("Error: no se ha llegado al principio de las entidades de dibujo");
262
                        }
263
                        
264
                        //lee primero entidades normales?
265
                        readEntities(eStart, eEnd);
266
                        
267
                        bb.position(bb.position() + 19);
268
                        
269
                        readBlockTable(blockTable);
270
                        readLayerTable(layerTable);
271
                        
272
//de momento nos saltamos estas tablas
273
//                        readStyleTable(styleTable);
274
//                        readLTypeTable(lineTypeTable);
275
//                        readViewTable(viewTable);
276
//                        readUcsTable(ucsTable);
277
//                        readVportTable(vportTable);
278
//                        readAppidTable(appidTable);
279
//                        readDimStyleTable(dimStyleTable);
280
//                        readP13Table(p13table);
281
                        
282
                        //luego lee entidades de bloque
283
                        readEntities(bsStart, bsEnd);
284
                        
285
                        bb.position(bb.position() + 36);
286
                        
287
                        bb.order(ByteOrder.LITTLE_ENDIAN);
288
                        
289
                        int peStart, peEnd, pbStart, pbEnd;
290
                        
291
                        peStart = bb.getInt();
292
                        peEnd = bb.getInt();
293
                        pbStart = bb.getInt();
294
                        pbEnd = bb.getInt();
295
                        
296
                        if(peStart != eStart)
297
                                System.out.println("peStart="+peStart+" eStart="+eStart);
298
                        
299
                        if(peEnd != eEnd)
300
                                System.out.println("peStart="+peEnd+" eStart="+eEnd);
301
                        
302
                        if(pbStart != bsStart)
303
                                System.out.println("peStart="+pbStart+" eStart="+bsStart);
304
                        
305
                        if(bsEnd != pbEnd)
306
                                System.out.println("peStart="+pbStart+" eStart="+bsStart);
307
                   
308
                        bb.position(bb.position() + 12);
309
                        
310
                        Dwg12TableTest bts = getTableTest();
311
                        
312
                        Dwg12TableTest lyrTs = getTableTest();
313
                        
314
                        Dwg12TableTest sts = getTableTest();
315
                        
316
                        Dwg12TableTest ltts = getTableTest();
317
                        
318
                        Dwg12TableTest vts = getTableTest();
319
                        
320
                        Dwg12TableTest uts = getTableTest();
321
                        
322
                        Dwg12TableTest vpts = getTableTest();
323
                        
324
                        Dwg12TableTest ats = getTableTest();
325
                        
326
                        Dwg12TableTest dts = getTableTest();
327
                        
328
                        Dwg12TableTest pts = getTableTest();
329
                        
330
                        
331
                        
332
                        
333
                } catch (Exception e) {
334
                        e.printStackTrace();
335
//                        logger.error(e);
336
                }
337
        }
338
        
339
        
340
        private void readP13Table(Dwg12Table p13table) {
341
                // TODO Auto-generated method stub
342
        }
343

    
344

    
345

    
346
        private void readDimStyleTable(Dwg12Table dimStyleTable) {
347
                short size = dimStyleTable.s1;
348
                int numRecs = dimStyleTable.i1;
349
                int start = dimStyleTable.i2;
350
                int end = -1;
351
                bb.position(start);
352
                for(int i = 0; i < numRecs; i++){
353
                        end = bb.position() + size;
354
                        
355
                        byte flag = bb.get();
356
                        byte[] nameBytes = new byte[32];
357
                        bb.get(nameBytes);
358
                        String name = new String(nameBytes);
359
                        
360
                        bb.order(ByteOrder.LITTLE_ENDIAN);
361
                        short word = bb.getShort();
362
                        
363
                        double[] d4048 = new double[9];
364
                        for(int j = 0; j < 9; j++){
365
                                d4048[j] = bb.getDouble();
366
                        }
367
                        
368
                        double[] d40145 = new double[6];
369
                        for(int j = 0; j < 6; j++){
370
                                d40145[j] = bb.getDouble();
371
                        }
372
                        
373
                        byte[] b7078 = new byte[7];
374
                        for(int j = 0; j < 7; j++){
375
                                b7078[j] = bb.get();
376
                        }
377
                        
378
                        byte[] b170175 = new byte[6];
379
                        for(int j = 0; j < 6; j++){
380
                                b170175[j] = bb.get();
381
                        }
382
                        
383
                        bb.order(ByteOrder.nativeOrder());
384
                        byte[] s3 = new byte[16];
385
                        bb.get(s3);
386
                        
387
                        byte[] s4 = new byte[16];
388
                        bb.get(s4);
389
                        
390
                        byte[] s5 = new byte[32];
391
                        bb.get(s5);
392
                        
393
                        byte[] s6 = new byte[32];
394
                        bb.get(s6);
395
                        
396
                        byte[] s7 = new byte[32];
397
                        bb.get(s6);
398
                        
399
                        bb.position(bb.position() + 3);
400
                        
401
                        bb.order(ByteOrder.LITTLE_ENDIAN);
402
                        short w176 = bb.getShort();
403
                        short w177 = bb.getShort();
404
                        short w178 = bb.getShort();
405
                        
406
                        double d146 = bb.getDouble();
407
                        double d147 = bb.getDouble();
408
                        
409
                        
410
                        int offset = end - bb.position();
411
                        if(offset > 0)
412
                                bb.position(bb.position() + offset);
413
                        
414
                        bb.order(ByteOrder.LITTLE_ENDIAN);
415
                        short crc = bb.getShort();
416
                }
417
                byte[] crc32 = new byte[32];
418
                bb.get(crc32);
419
        }
420

    
421

    
422

    
423
        private void readAppidTable(Dwg12Table appidTable) {
424
                short size = appidTable.s1;
425
                int numRecs = appidTable.i1;
426
                int start = appidTable.i2;
427
                int end = -1;
428
                bb.position(start);
429
                for(int i = 0; i < numRecs; i++){
430
                        end = bb.position() + size;
431
                        
432
                        byte flag = bb.get();
433
                        byte[] nameBytes = new byte[32];
434
                        bb.get(nameBytes);
435
                        String name = new String(nameBytes);
436
                        
437
                        bb.order(ByteOrder.LITTLE_ENDIAN);
438
                        short word = bb.getShort();
439
                        
440
                        int offset = end - bb.position();
441
                        if(offset > 0)
442
                                bb.position(bb.position() + offset);
443
                        
444
                        bb.order(ByteOrder.LITTLE_ENDIAN);
445
                        short crc = bb.getShort();
446
                }
447
                byte[] crc32 = new byte[32];
448
                bb.get(crc32);
449
        }
450

    
451

    
452

    
453
        private void readVportTable(Dwg12Table vportTable) {
454
                short size = vportTable.s1;
455
                int numRecs = vportTable.i1;
456
                int start = vportTable.i2;
457
                int end = -1;
458
                bb.position(start);
459
                for(int i = 0; i < numRecs; i++){
460
                        end = bb.position() + size;
461
                        
462
                        byte flag = bb.get();
463
                        byte[] nameBytes = new byte[32];
464
                        bb.get(nameBytes);
465
                        String name = new String(nameBytes);
466
                        
467
                        bb.order(ByteOrder.LITTLE_ENDIAN);
468
                        short used = bb.getShort();
469
                        
470
                        double[] pt10 = getPoint(false);
471
                        double[] pt11 = getPoint(false);
472
                        double[] pt17 = getPoint(false);
473
                        double[] pt16 = getPoint(true);
474
                        
475
                        bb.order(ByteOrder.LITTLE_ENDIAN);
476
                        
477
                        double d50 = bb.getDouble();
478
                        double d40 = bb.getDouble();
479
                        
480
                        double[] pt12 = getPoint(false);
481
                        
482
                        bb.order(ByteOrder.LITTLE_ENDIAN);
483
                        double d41 = bb.getDouble();
484
                        double d42 = bb.getDouble();
485
                        double d43 = bb.getDouble();
486
                        double d44 = bb.getDouble();
487
                        
488
                        short[] w7178 = new short[8];
489
                        for(int j = 0; j < 8; j++){
490
                                w7178[j] = bb.getShort();
491
                        }
492
                        bb.order(ByteOrder.LITTLE_ENDIAN);
493
                        double d51 = bb.getDouble();
494
                        
495
                        double[] pt13 = getPoint(false);
496
                        double[] pt14 = getPoint(false);
497
                        double[] pt15 = getPoint(false);
498
                        
499
                        int offset = end - bb.position();
500
                        if(offset > 0)
501
                                bb.position(bb.position() + offset);
502
                        
503
                        bb.order(ByteOrder.LITTLE_ENDIAN);
504
                        short crc = bb.getShort();
505
                }
506
                byte[] crc32 = new byte[32];
507
                bb.get(crc32);
508
        }
509

    
510

    
511

    
512
        private void readUcsTable(Dwg12Table ucsTable) {
513
                short size = ucsTable.s1;
514
                int numRecs = ucsTable.i1;
515
                int start = ucsTable.i2;
516
                int end = -1;
517
                bb.position(start);
518
                for(int i = 0; i < numRecs; i++){
519
                        end = bb.position() + size;
520
                        
521
                        byte flag = bb.get();
522
                        byte[] nameBytes = new byte[32];
523
                        bb.get(nameBytes);
524
                        String name = new String(nameBytes);
525
                        
526
                        bb.order(ByteOrder.LITTLE_ENDIAN);
527
                        short used = bb.getShort();
528
                        
529
                        double[] pt10 = getPoint(true);
530
                        double[] pt11 = getPoint(true);
531
                        double[] pt12 = getPoint(true);
532
                        
533
                        
534
                        
535
                        int offset = end - bb.position();
536
                        if(offset > 0)
537
                                bb.position(bb.position() + offset);
538
                        
539
                        bb.order(ByteOrder.LITTLE_ENDIAN);
540
                        short crc = bb.getShort();
541
                }
542
                byte[] crc32 = new byte[32];
543
                bb.get(crc32);
544
                
545
                
546
        }
547

    
548

    
549

    
550
        private void readViewTable(Dwg12Table viewTable) {
551
                short size = viewTable.s1;
552
                int numRecs = viewTable.i1;
553
                int start = viewTable.i2;
554
                int end = -1;
555
                bb.position(start);
556
                for(int i = 0; i < numRecs; i++){
557
                        end = bb.position() + size;
558
                        
559
                        byte flag = bb.get();
560
                        byte[] nameBytes = new byte[32];
561
                        bb.get(nameBytes);
562
                        String name = new String(nameBytes);
563
                        
564
                        bb.order(ByteOrder.LITTLE_ENDIAN);
565
                        short used = bb.getShort();
566
                        double db40 = bb.getDouble();
567
                        
568
                        double[] pt10 = getPoint(false);
569
                        
570
                        bb.order(ByteOrder.LITTLE_ENDIAN);
571
                        double db41 = bb.getDouble();
572
                        
573
                        double[] pt11 = getPoint(true);
574
                        
575
                        double[] pt12 = getPoint(true);
576
                        
577
                        
578
                        bb.order(ByteOrder.LITTLE_ENDIAN);
579
                        short w71 = bb.getShort();
580
                        double db42 = bb.getDouble();
581
                        double db43 = bb.getDouble();
582
                        double db44 = bb.getDouble();
583
                        double db50 = bb.getDouble();
584
                        
585
                        int offset = end - bb.position();
586
                        if(offset > 0)
587
                                bb.position(bb.position() + offset);
588
                        
589
                        bb.order(ByteOrder.LITTLE_ENDIAN);
590
                        short crc = bb.getShort();
591
                        
592
                        
593
                }
594
                byte[] crc32 = new byte[32];
595
                bb.get(crc32);
596
        }
597

    
598

    
599

    
600
        private void readLTypeTable(Dwg12Table lineTypeTable) {
601
                short size = lineTypeTable.s1;
602
                int numRecs = lineTypeTable.i1;
603
                int start = lineTypeTable.i2;
604
                int end = -1;
605
                bb.position(start);
606
                for(int i = 0; i < numRecs; i++){
607
                        end = bb.position() + size;
608
                        byte flag = bb.get();
609
                        byte[] nameBytes = new byte[32];
610
                        bb.get(nameBytes);
611
                        String name = new String(nameBytes);
612
                        bb.order(ByteOrder.LITTLE_ENDIAN);
613
                        short w1 = bb.getShort();
614
                        
615
                        byte[] s1 = new byte[48];
616
                        bb.get(s1);
617
                        String s1name = new String(s1);
618
                        
619
                        bb.order(ByteOrder.nativeOrder());
620
                        byte b1 = bb.get();
621
                        byte b2 = bb.get();
622
                        
623
                        bb.order(ByteOrder.LITTLE_ENDIAN);
624
                        double[] doubles = new double[13];
625
                        for(int j = 0; j < 13; j++){
626
                                doubles[j] = bb.getDouble();
627
                        }
628
                        
629
                        short crc = bb.getShort();
630
                        
631
                        int offset = end - bb.position();
632
                        if(offset > 0)
633
                                bb.position(bb.position() + offset);
634
                }
635
                byte[] crc32 = new byte[32];
636
                bb.get(crc32);
637
        }
638

    
639

    
640

    
641
        /**
642
         * @param styleTable
643
         */
644
        private void readStyleTable(Dwg12Table styleTable) {
645
                short size = styleTable.s1;
646
                int numRecs = styleTable.i1;
647
                int start = styleTable.i2;
648
                int end = -1;
649
                bb.position(start);
650
                for(int i = 0; i < numRecs; i++){
651
                        end = bb.position() + size;
652
                        byte flag = bb.get();
653
                        byte[] nameBytes = new byte[32];
654
                        bb.get(nameBytes);
655
                        String name = new String(nameBytes);
656
                        bb.order(ByteOrder.LITTLE_ENDIAN);
657
                        short w1 = bb.getShort();
658
                        double d1 = bb.getDouble();
659
                        double d2 = bb.getDouble();
660
                        double d3 = bb.getDouble();
661
                        byte b1 = bb.get();
662
                        double d4 = bb.getDouble();
663
                        
664
                        byte[] s1 = new byte[128];
665
                        bb.get(s1);
666
                        
667
                        short crc = bb.getShort();
668
                        int offset = end - bb.position();
669
                        if(offset > 0)
670
                                bb.position(bb.position() + offset);
671
                }
672
                byte[] crc32 = new byte[32];
673
                bb.get(crc32);
674
        }
675

    
676

    
677

    
678
        /**
679
         * @param layerTable
680
         */
681
        private void readLayerTable(Dwg12Table layerTable) {
682
                short size = layerTable.s1;
683
                int numR = layerTable.i1;
684
                int start = layerTable.i2;
685
                
686
                int begin = -1;
687
                int end = -1;
688
                
689
                DwgLayer layer = null;
690
                for(int i = 0; i < numR; i++){
691
                        begin = start + i * size;
692
                        bb.position(begin);
693
                        end = begin + size - 2;
694
                        
695
                        layer = new DwgLayer(index);
696
                        index++;
697
                        
698
                        byte flag = bb.get();
699
                        /*
700
                         +=====================================================+
701
          | Flag bit | Meaning                                  |
702
          | value    |                                          |
703
          |----------|------------------------------------------|
704
          | 1        | If set, layer is frozen                  |
705
          |----------|------------------------------------------|
706
          | 2        | If set, layer is frozen by default in    |
707
          |          | new Viewports                            |
708
          |----------|------------------------------------------|
709
          | 4        | If set, layer is locked                  |
710
          +-----------------------------------------------------+
711
                         * */
712
                        if((flag & 0x1) > 0){
713
                                layer.setFrozen(true);
714
                        }
715
                        if((flag & 0x2) > 0){
716
                                layer.setFrozenInNew(true);
717
                        }
718
                        if((flag & 0x4) > 0){
719
                                layer.setLocked(true);
720
                        }
721
                        
722
                        byte[] nameByte = new byte[32];
723
                        bb.get(nameByte);
724
                        String name = new String(nameByte).trim();
725
                        layer.setName(name);
726
                        
727
                        bb.order(ByteOrder.LITTLE_ENDIAN);
728
                        short used = bb.getShort();
729
                        short color = bb.getShort();
730
                        layer.setColor(color);
731
                        short style = bb.getShort();
732
                        short crc = bb.getShort();
733

    
734
                        //TODO Vemos si el indice que ocupa la layer en la tabla
735
                        //de layers se corresponde con el valor short de las entidades
736
                        //graficas
737
                        DwgHandleReference handle = new DwgHandleReference(0x5, i);
738
                        layer.setHandle(handle);
739
                        dwgFile.addDwgObject(layer);
740
                        
741
                        
742
                        int offset = end - bb.position();
743
                        if(offset > 0)
744
                                bb.position(bb.position() + offset);
745
                        
746
                }
747
                byte[] crc32 = new byte[32];
748
                bb.get(crc32);
749
        }
750

    
751

    
752
        interface EntityReader{
753
                void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj);
754
        }
755
        
756
        /**
757
         * In DWG 12 version, some kind of objects (Polylines and Vertex) cannot be
758
         * instanciated until the EntityReader has readed some stuff (flags and so)
759
         * from the file.
760
         * 
761
         * DefferedEntityReader has the responsability to create DwgObject instances,
762
         * and it has the precondition that DwgObject parameter of its method read must
763
         * be null.
764
         * */
765
        abstract class DefferedEntityReader implements EntityReader{
766
                abstract DwgObject getDwgObject(int index);
767
                
768
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj){
769
                        if(dwgObj != null)
770
                                throw new RuntimeException("DefferedEntityReader es el encargado de construir los objetos, debe recibirlos a null");
771
                }
772
                
773
        }
774
        
775
        class LineReader implements EntityReader{
776
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
777
                        if(! (dwgObj instanceof DwgLine))
778
                                throw new RuntimeException("No es entidad LINE");
779
                        DwgLine line = (DwgLine) dwgObj;
780
                        
781
                        boolean zflag = true;
782
                        if( (flags & 0x4) > 0)
783
                                zflag = false;
784
                        
785
                        double[] pt10 = getPoint(zflag);
786
                        double[] pt11 = getPoint(zflag);
787
                        
788
                        line.setP1(pt10);
789
                        line.setP2(pt11);
790
                        
791
                        if((opts & 0x1) > 0){
792
                                double[] pt210 = getPoint(true);
793
                                line.setExtrusion(pt210);
794
                        }
795
                        
796
                        if((opts & 0x2) > 0){
797
                                /*
798
                                 * This field is only found in DWG12 prev
799
                                 * files. Since DWG 12, elevation is saved
800
                                 * in third coordinate of points.
801
                                 * 
802
                                 * */
803
                                bb.order(ByteOrder.LITTLE_ENDIAN);
804
                                double db38 = bb.getDouble();
805
                                line.getP1()[2] = db38;
806
                                line.getP2()[2] = db38;
807
                        }
808
                }
809
        }
810
        
811
        
812
        
813
        class PointReader implements EntityReader{
814
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
815
                        if(! (dwgObj instanceof DwgPoint))
816
                                throw new RuntimeException("No es entidad POINT");
817
                        DwgPoint point = (DwgPoint) dwgObj;
818
                        boolean zflag = true;
819
                        if((flags & 0x4) > 0)
820
                                zflag = false;
821
                        double[] pt10 = getPoint(zflag);
822
                        point.setPoint(pt10);
823
                        
824
                        if((opts & 0x1) > 0){
825
                                double[] pt210 = getPoint(true);
826
                                point.setExtrusion(pt210);
827
                        }
828
                        
829
                        if((opts & 0x2) > 0){
830
                                bb.order(ByteOrder.LITTLE_ENDIAN);
831
                                double db38 = bb.getDouble();
832
                                point.getPoint()[2] = db38;
833
                        }
834
                }
835
        }
836
        
837
        
838
        class CircleReader implements EntityReader{
839
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
840
                        if(! (dwgObj instanceof DwgCircle))
841
                                throw new RuntimeException("No es entidad CIRCLE");
842
                        DwgCircle circle = (DwgCircle) dwgObj;
843
                        
844
                        boolean zflag = true;
845
                        if((flags & 0x4) > 0)
846
                                zflag = false;
847
                        double[] pt10 = getPoint(zflag);
848
                        circle.setCenter(pt10);
849
                        
850
                        bb.order(ByteOrder.LITTLE_ENDIAN);
851
                        double d40 = bb.getDouble();
852
                        circle.setRadius(d40);
853
                        
854
                        double[] pt210 = null;
855
                        if((opts & 0x1) > 0){
856
                                pt210 = getPoint(true);
857
                                
858
                        }else{
859
                                pt210 = new double[]{0,0,1};
860
                        }
861
                        circle.setExtrusion(pt210);
862
                        if((opts & 0x2) > 0){
863
                                bb.order(ByteOrder.LITTLE_ENDIAN);
864
                                double db38 = bb.getDouble();
865
                                circle.getCenter()[2] = db38;
866
                        }
867
                }
868
        }
869
        
870
        
871
        
872
        class ShapeReader implements EntityReader{
873
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
874
                        
875
                        //TODO POR IMPLEMENTAR DwgShape (referencia a un
876
                        //fichero de texto)
877
                        
878
                        double[] pt10 = getPoint(false);
879
                        bb.order(ByteOrder.LITTLE_ENDIAN);
880
                        short w2 = bb.getShort();
881
                        if((opts & 0x1) > 0){
882
                                double[] pt210 = getPoint(true);
883
                        }
884
                        if((opts & 0x2) > 0){
885
                                bb.order(ByteOrder.LITTLE_ENDIAN);
886
                                double db38 = bb.getDouble();
887
                        }
888
                }
889
        }
890
        
891
        public String getString(){
892
                bb.order(ByteOrder.LITTLE_ENDIAN);
893
                short len = bb.getShort();
894
                byte[] bytes = new byte[len];
895
                bb.order(ByteOrder.nativeOrder());
896
                bb.get(bytes);
897
                return new String(bytes);
898
        }
899
        
900
        
901
        class TextReader implements EntityReader{
902
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
903
                        
904
                        if(! (dwgObj instanceof DwgText))
905
                                throw new RuntimeException("No es entidad TEXT");
906
                        DwgText txt = (DwgText) dwgObj;
907
                        
908
                        
909
                        double[] pt10 = getPoint(false);
910
                        txt.setInsertionPoint(new Point2D.Double(pt10[0], pt10[1]));
911
                        
912
                        
913
                        bb.order(ByteOrder.LITTLE_ENDIAN);
914
                        double db40 = bb.getDouble();
915
                        txt.setHeight(db40);
916
                        
917
                        
918
                        
919
                        String text = getString();
920
                        txt.setText(text);
921
                        if((opts & 0x1) > 0){
922
                                bb.order(ByteOrder.LITTLE_ENDIAN);
923
                                double db50 = bb.getDouble();
924
                                txt.setRotationAngle(db50);
925
                        }
926
                        if((opts & 0x2) > 0){
927
                                double db41 = bb.getDouble();
928
                                txt.setWidthFactor(db41);//TODO 41 es width factor seguro?
929
                        }
930
                        if((opts & 0x4) > 0){
931
                                double db51 = bb.getDouble();
932
                                txt.setObliqueAngle(db51);
933
                        }
934
                        if((opts & 0x8) > 0){
935
                                byte b7 = bb.get();
936
                        }
937
                        if((opts & 0x10) > 0){
938
                                byte b71 = bb.get();
939
                                //parametros de mirroring
940
                                txt.setGeneration(b71);
941
                        }
942
                        if((opts & 0x20) > 0){
943
                                byte b72 = bb.get();
944
                                txt.setHalign(b72);
945
                        }
946
                        if((opts & 0x40) > 0){
947
                                double[] pt11 = getPoint(false);
948
                                txt.setAlignmentPoint(new Point2D.Double(pt11[0], pt11[1]));
949
                        }
950
                        if((opts & 0x100) > 0){
951
                                byte b73 = bb.get();
952
                                txt.setValign(b73);
953
                        }
954
                        
955
                        //TODO La especificaci?n DXF 12 dice que la extrusion 
956
                        //codigos DXF 210, 220, 230 se aplica sobre:
957
                        /*
958
                         Line, Point, Circle, Shape, Text, Arc, Trace,
959
                         Solid, Block Reference, Polyline, 
960
                          Dimension, Attribute, and Attribute Definition entity
961
                          
962
                          Pero aqu? no aparece por ningun lado la extrusion
963
                         * */
964
                        double[] ext = new double[]{0, 0, 1};
965
                        txt.setExtrusion(ext);
966
                }
967
        }
968
        
969
        
970
        class ArcReader implements EntityReader{
971
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
972
                        if(! (dwgObj instanceof DwgArc))
973
                                throw new RuntimeException("No es entidad ARC");
974
                        DwgArc arc = (DwgArc) dwgObj;
975
                        
976
                        double[] pt10 = getPoint(false);
977
                        arc.setCenter(pt10);
978
                        
979
                        bb.order(ByteOrder.LITTLE_ENDIAN);
980
                        double d40 = bb.getDouble();
981
                        arc.setRadius(d40);
982
                        
983
                        double d50 = bb.getDouble();
984
                        arc.setInitAngle(d50);
985
                        
986
                        double d51 = bb.getDouble();
987
                        arc.setEndAngle(51);
988
                        
989
                        double[] pt210 = null;
990
                        if((opts & 0x1) > 0){
991
                                pt210 = getPoint(true);
992
                                arc.setExtrusion(pt210);
993
                        }else{
994
                                pt210 = new double[]{1, 0, 0};
995
                        }
996
                        if((opts & 0x2) > 0){
997
                                bb.order(ByteOrder.LITTLE_ENDIAN);
998
                                double db38 = bb.getDouble();
999
                                arc.getCenter()[2] = db38;
1000
                        }
1001
                }
1002
        }
1003
        
1004
        
1005
        
1006
        class TraceReader implements EntityReader{
1007
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
1008
                        //TODO Implementar DwgTrace
1009
                        double[] pt10 = getPoint(false);
1010
                        double[] pt11 = getPoint(false);
1011
                        double[] pt12 = getPoint(false);
1012
                        double[] pt13 = getPoint(false);
1013
                        if((opts & 0x1) > 0){
1014
                                double[] pt210 = getPoint(true);
1015
                        }
1016
                        if((opts & 0x2) > 0){
1017
                                bb.order(ByteOrder.LITTLE_ENDIAN);
1018
                                double db38 = bb.getDouble();
1019
                        }
1020
                }
1021
        }
1022
        
1023
        class SolidReader implements EntityReader{
1024
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
1025
                        if(! (dwgObj instanceof DwgSolid))
1026
                                throw new RuntimeException("No es entidad SOLID");
1027
                        DwgSolid solid = (DwgSolid) dwgObj;
1028
                        
1029
                        
1030
                        
1031
                        double[] pt11 = getPoint(false);
1032
                        solid.setCorner1(pt11);
1033
                        
1034
                        double[] pt12 = getPoint(false);
1035
                        solid.setCorner2(pt12);
1036
                        
1037
                        double[] pt13 = getPoint(false);
1038
                        solid.setCorner3(pt13);
1039
                        
1040
                        double[] pt14 = getPoint(false);
1041
                        solid.setCorner4(pt14);
1042
                        
1043
                        
1044
                        if((opts & 0x1) > 0){
1045
                                double[] pt210 = getPoint(true);
1046
                                solid.setExtrusion(pt210);
1047
                        }
1048
                        if((opts & 0x2) > 0){
1049
                                bb.order(ByteOrder.LITTLE_ENDIAN);
1050
                                double db38 = bb.getDouble();
1051
                                solid.getCorner1()[2] = db38;
1052
                                solid.getCorner2()[2] = db38;
1053
                                solid.getCorner3()[2] = db38;
1054
                                solid.getCorner4()[2] = db38;
1055
                        }
1056
                        
1057
                        solid.setExtrusion(new double[]{0, 0, 1});
1058
                }
1059
        }
1060
        
1061
        
1062
        private Block getBlock(String blockName){
1063
                for(int i = 0; i < blocks.size(); i++){
1064
                        Block block = (Block) blocks.get(i);
1065
                        if(block.name.equalsIgnoreCase(blockName)){
1066
                                return block;
1067
                        }
1068
                }
1069
                return null;
1070
        }
1071
        
1072
        
1073
        
1074
        class BlkReader implements EntityReader{
1075
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
1076
                        if(! (dwgObj instanceof DwgBlockHeader))
1077
                                throw new RuntimeException("No es entidad BLOCK");
1078
                        DwgBlockHeader blk = (DwgBlockHeader) dwgObj;
1079
                        
1080
                        
1081
                        double[] pt10 = getPoint(false);
1082
                        blk.setBasePoint(pt10);
1083
                        String blockName = getString();
1084
                        
1085
                        Block block = getBlock(blockName);
1086
                        if(block == null){
1087
                                System.out.println(blockName+" no encontrado en la tabla de bloques");
1088
                        }
1089
                        
1090
                        //TODO En DWG 12 no se distingue Block de BlockHeader, en ulteriores
1091
                        //versiones si
1092
                        //REVISAR EL CASO DE LOS BLOQUES
1093
                        
1094
                        
1095
                        if((opts & 0x2) > 0)
1096
                        {
1097
                                //puede ser que se trate de referencias
1098
                                //externas??
1099
                                String s3 = getString();
1100
                        }
1101
                        
1102
                        if((flags & 1) > 0){
1103
                                blk.setAnonymous(true);
1104
                        }
1105
                        
1106
                        if((flags & 2) > 0){
1107
                                blk.setHasAttrs(true);
1108
                        }
1109
                        
1110
                        if((flags & 4) > 0){
1111
                                blk.setBlkIsXRef(true);
1112
                        }
1113
                        
1114
                        if((flags & 16) > 0){
1115
                                blk.setXdep(true);
1116
                        }
1117
                        
1118
                        if((flags & 32) > 0){
1119
                                blk.setXRefOverLaid(true);
1120
                        }
1121
                        
1122
                        if((flags & 64) > 0){
1123
                                blk.setLoaded(true);
1124
                        }
1125
                        
1126
                        
1127
                        /*
1128
                        
1129
                           1        | This is an anonymous Block generated by    |
1130
                  |          | hatching, associative dimensioning, other  |
1131
                  |          | internal operations, or an application     |
1132
                  |----------|--------------------------------------------|
1133
                  | 2        | This Block has Attributes                  |
1134
                  |----------|--------------------------------------------|
1135
                  | 4        | This Block is an external reference (Xref) |
1136
                  |----------|--------------------------------------------|
1137
                  | 8        | not used                                   |
1138
                  |----------|--------------------------------------------|
1139
                  | 16       | This Block is externally dependent         |
1140
                  |----------|--------------------------------------------|
1141
                  | 32       | This is a resolved external reference, or  |
1142
                  |          | dependent of an external reference         |
1143
                  |----------|--------------------------------------------|
1144
                  | 64       | This definition is referenced       
1145
                        
1146
                        */
1147
                
1148
                }
1149
        }
1150
        class EndBlkReader implements EntityReader{
1151
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
1152
                        System.out.println("endblk");
1153
                }
1154
        }
1155
        
1156
        class InsertReader implements EntityReader{
1157
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
1158
                        if(! (dwgObj instanceof DwgInsert))
1159
                                throw new RuntimeException("No es entidad INSERT");
1160
                        DwgInsert insert = (DwgInsert) dwgObj;
1161
                        
1162
                        bb.order(ByteOrder.nativeOrder());
1163
                        short w1 = bb.getShort();//Puede ser este el identificador de bloque???
1164
                        
1165
                        double[] pt10 = getPoint(false);
1166
                        insert.setInsertionPoint(pt10);
1167
                        
1168
                        
1169
                        bb.order(ByteOrder.LITTLE_ENDIAN);
1170
                        
1171
                        double x = 0d, y = 0d, z = 0d;
1172
                        
1173
                        if((opts & 0x1) > 0){
1174
                                double db41 = bb.getDouble();
1175
                                x = db41;
1176
                        }
1177
                        
1178
                        if((opts & 0x2) > 0){
1179
                                double db42 = bb.getDouble();
1180
                                y = db42;
1181
                        }
1182
                        
1183
                        if((opts & 0x4) > 0){
1184
                                double db43 = bb.getDouble();
1185
                                z = db43;
1186
                        }
1187
                        
1188
                        insert.setScale(new double[]{x, y, z});
1189
                        
1190
                        if((opts & 0x8) > 0){
1191
                                double db50 = bb.getDouble();
1192
                                insert.setRotation(db50);
1193
                        }
1194
                        
1195
                        if((opts & 0x10) > 0){
1196
                                short w70 = bb.getShort();
1197
                                //column counts
1198
                        }
1199
                        //creo que esto est? mal, y que debe poner 0x20
1200
                        if((opts & 0x10) > 0){
1201
                                short w71 = bb.getShort();
1202
                                //row counts
1203
                        }
1204
                        
1205
                        if((opts & 0x40) > 0){
1206
                                double db44 = bb.getDouble();
1207
                                //column spacing
1208
                        }
1209
                        
1210
                        if((opts & 0x80) > 0){
1211
                                double db45 = bb.getDouble();
1212
                                //row spacing
1213
                        }
1214
                        insert.setExtrusion(new double[]{0, 0, 1});
1215
                }
1216
        }
1217
        
1218
        class AttDefReader implements EntityReader{
1219
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
1220
                        double[] p10 = getPoint(false);
1221
                        String s1 = getString();
1222
                        String s3 = getString();
1223
                        String s2 = getString();
1224
                        bb.order(ByteOrder.LITTLE_ENDIAN);
1225
                        byte b70 = bb.get();
1226
                        
1227
                        if((opts & 0x1) > 0){
1228
                                byte b73 = bb.get();
1229
                        }
1230
                        
1231
                        if((opts & 0x2) > 0){
1232
                                double d50 = bb.getDouble();
1233
                        }
1234
                        
1235
                        if((opts & 0x4) > 0){
1236
                                double d41 = bb.getDouble();
1237
                        }
1238
                        
1239
                        if((opts & 0x8) > 0){
1240
                                double d42 = bb.getDouble();
1241
                        }
1242
                        
1243
                        if((opts & 0x10) > 0){
1244
                                byte b7 = bb.get();
1245
                        }
1246
                        
1247
                        if((opts & 0x20) > 0){
1248
                                byte b71 = bb.get();
1249
                        }
1250
                        
1251
                        if((opts & 0x40) > 0){
1252
                                byte b72 = bb.get();
1253
                        }
1254
                        
1255
                        if((opts & 0x80) > 0){
1256
                                double[] p11 = getPoint(false);
1257
                        }
1258
                        
1259
                        if((opts & 0x100) > 0){
1260
                                double[] p210 = getPoint(true);
1261
                        }
1262
                        
1263
                        if((opts & 0x200) > 0){
1264
                                bb.order(ByteOrder.LITTLE_ENDIAN);
1265
                                double d38 = bb.getDouble();
1266
                        }
1267
                }
1268
        }
1269
        
1270
        class AttribReader implements EntityReader{
1271
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
1272
                        double[] p10 = getPoint(false);
1273
                        
1274
                        bb.order(ByteOrder.LITTLE_ENDIAN);
1275
                        double d40 = bb.getDouble();
1276
                        
1277
                        String s1 = getString();
1278
                        String s2 = getString();
1279
                        
1280
                        if((opts & 0x1) > 0){
1281
                                byte b73 = bb.get();
1282
                        }
1283
                        
1284
                        bb.order(ByteOrder.LITTLE_ENDIAN);
1285
                        if((opts & 0x2) > 0){
1286
                                double d50 = bb.getDouble();        
1287
                        }
1288
                        
1289
                        if((opts & 0x4) > 0){
1290
                                double d41 = bb.getDouble();        
1291
                        }
1292
                        
1293
                        if((opts & 0x8) > 0){
1294
                                double d42 = bb.getDouble();        
1295
                        }
1296
                        
1297
                        if((opts & 0x10) > 0){
1298
                                byte b7 = bb.get();
1299
                        }
1300
                        
1301
                        if((opts & 0x20) > 0){
1302
                                byte b71 = bb.get();
1303
                        }
1304
                        
1305
                        if((opts & 0x40) > 0){
1306
                                byte b72 = bb.get();
1307
                        }
1308
                        
1309
                        if((opts & 0x80) > 0){
1310
                                double[] p11 = getPoint(false);
1311
                        }
1312
                        double[] p210 = null;
1313
                        if((opts & 0x100) > 0){
1314
                                p210 = getPoint(true);
1315
                        }else{
1316
                                p210 = new double[]{0, 0, 1};
1317
                        }
1318
                        bb.order(ByteOrder.LITTLE_ENDIAN);
1319
                        if((opts & 0x200) > 0){
1320
                                double d38 = bb.getDouble();
1321
                        }
1322
                }
1323
        }
1324
        
1325
        
1326
        class SbEndReader implements EntityReader{
1327
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
1328
                        bb.order(ByteOrder.LITTLE_ENDIAN);
1329
                        int l = bb.getInt();
1330
                }
1331
        }
1332
        
1333
        class PlineReader extends DefferedEntityReader{
1334
                
1335
                /*
1336
                 * CURVE TYPE CONSTANTS
1337
                 * */
1338
                final static int CLOSED       = 0x01;//if line is a mesh, curve closed in M direction
1339
                final static int CURVE_FIT    = 0x02;
1340
                final static int SPLINE_FIT     = 0x04;
1341
                final static int PLINE3D    = 0x08;
1342
                final static int MESH          = 0x10;
1343
                final static int MESH_CLOSED   = 0x20;//curve closed in N direction
1344
                final static int ANY_MESH = 0x30;  //MESH | MESH_CLOSED
1345
                final static int PFACE          = 0x40;
1346
                final static int CONT_LTYPE   = 0x80;
1347
                
1348
                /*
1349
                 * polyline attributes
1350
                 * */
1351
                /**
1352
                 * flag: curve type (CLOSED, MESH,PFACE, etc.)
1353
                 * */
1354
                byte flag70;
1355
                /**
1356
                 * Number of points in M direction for a Mesh,
1357
                 * number of vertices for a PFace polyline
1358
                 * */
1359
                byte meshM71;
1360
                /**
1361
                 * Number of points in N direction for a Mesh,
1362
                 * number of faces for a PFace polyline
1363
                 * */
1364
                byte meshN72;
1365
                double startW40;
1366
                byte smoothM73;
1367
                byte smoothN74;
1368
                byte curveType75;
1369
                
1370
                
1371
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
1372
                        super.read(bb, flags, opts, dwgObj);
1373
                        
1374
                        //TODO El byte 70 es el que determina el tipo de Polyline
1375
                        if( (opts & 0x1) > 0){
1376
                                flag70 = bb.get();
1377
                        }
1378
                        bb.order(ByteOrder.LITTLE_ENDIAN);
1379
                        if( (opts & 0x2) > 0){
1380
                                startW40 = bb.getDouble();
1381
                        }
1382
                        
1383
                        if( (opts & 0x4) > 0){
1384
                                meshM71 = bb.get();
1385
                        }
1386
                        
1387
                        if( (opts & 0x8) > 0){
1388
                                meshN72 = bb.get();
1389
                        }
1390
                        
1391
                        if( (opts & 0x10) > 0){
1392
                                smoothM73 = bb.get();
1393
                        }
1394
                        
1395
                        if( (opts & 0x20) > 0){
1396
                                smoothN74 = bb.get();
1397
                        }
1398
                        
1399
                        if( (opts & 0x40) > 0){
1400
                                curveType75 = bb.get();
1401
                        }
1402
                }
1403

    
1404
                DwgObject getDwgObject(int index) {
1405
                        DwgObject solution = null;
1406
                        boolean isSpline = (flag70 & SPLINE_FIT) != 0 ? true: false;
1407
                        if( (flag70 & PFACE) != 0){
1408
                                //PFacePolyline
1409
                                solution = new DwgPFacePolyline(index);
1410
//        often made wrong, so we have to correct...TODO Revisar
1411
                                if(meshM71 <= 0)
1412
                                        meshM71 = 16;
1413
                                if(meshN72 <= 0)
1414
                                        meshN72 = 4;
1415
                                ((DwgPFacePolyline)solution).setVertexCount(meshM71);
1416
                                ((DwgPFacePolyline)solution).setFaceCount(meshN72);
1417
                        }else if((flag70 & ANY_MESH) != 0){
1418
                                //Mesh, closed or not
1419
                                solution = new DwgMeshPolyline(index);
1420
                                boolean isClosedM = (flag70 & CLOSED) != 0 ? true: false;
1421
                                boolean isClosedN = (flag70 & MESH_CLOSED) > 0 ? true: false;
1422
                                
1423
                                ((DwgMeshPolyline)solution).setClosedM(isClosedM);
1424
                                ((DwgMeshPolyline)solution).setClosedN(isClosedN);
1425
                                if(isSpline){
1426
                                        ((DwgMeshPolyline)solution).setMVerticies(smoothM73);
1427
                                        ((DwgMeshPolyline)solution).setNVerticies(smoothN74);
1428
                                }else{
1429
                                        ((DwgMeshPolyline)solution).setMVerticies(meshM71);
1430
                                        ((DwgMeshPolyline)solution).setNVerticies(meshN72);
1431
                                }
1432
                                ((DwgMeshPolyline)solution).setCurveType(curveType75);
1433
                        }else{
1434
                                //polyline
1435
                                boolean is3DPolyline = (flag70 & PLINE3D) != 0 ? true: false;
1436
                                if(is3DPolyline)
1437
                                {
1438
                                        solution = new DwgPolyline3D(index);
1439
                                        boolean isClosed = (flag70 & CLOSED) != 0 ? true: false;
1440
                                        //for compatibility with dwg 13 and later, we use a byte flag for
1441
                                        //closed and spline properties
1442
                                        ((DwgPolyline3D)solution).setClosedFlags(isClosed ? 0x1: 0x0);
1443
                                        ((DwgPolyline3D)solution).setSplineFlags(isSpline ? 0x1: 0x0);
1444
                                        
1445
                                }else{
1446
                                        solution = new DwgPolyline2D(index);
1447
                                        ((DwgPolyline2D)solution).setCurveType(curveType75);
1448
                                        ((DwgPolyline2D)solution).setInitWidth(startW40);
1449
                                }
1450
                        }
1451
                        
1452
                        
1453
                        return solution;
1454
                }
1455
        }
1456
        
1457
        class VertexReader extends DefferedEntityReader{
1458
                final static int FIT_POINT    = 0x01;
1459
                final static int TANGENT      = 0x02;
1460
                final static int SPLINE_LINE_FIT     = 0x08;
1461
            final static int SPLINE_CONTROL_POINT      = 0x10;
1462
            final static int POLYLINE_3D      = 0x20;
1463
                final static int MESH_3D       = 0x40;
1464
                final static int PFACE_MESH  = 0x80;
1465
                
1466
                /**
1467
                 * Coordinates of the vertex
1468
                 * */
1469
                double[] pt10;
1470
                
1471
                /**
1472
                 * Start width of the vertex. If 0, it will
1473
                 * use the start with of the owner polyline.
1474
                 * */
1475
                double startWidth40;
1476
                /**
1477
                 * End width of the vertex. If 0, it will
1478
                 * use the end with of the owner polyline.
1479
                 * */
1480
                double endWidth41;
1481
                
1482
                /**
1483
                 * Bulge of the vertex
1484
                 * */
1485
                double tangentDir50;
1486
                
1487
                /**
1488
                 * Flag that maks what kind of vertex is:
1489
          1        | Extra vertex created by curve-fitting       |
1490
          2        | Curve-fit tangent defined for this vertex.  |
1491
          |          | A curve-fit tangent direction of 0 may be   |
1492
          |          | omitted from the DXF output, but is         |
1493
          |          | significant if this bit is set              |
1494
          |----------|---------------------------------------------|
1495
          | 4        | Unused (never set in DXF files)             |
1496
          |----------|---------------------------------------------|
1497
          | 8        | Spline vertex created by spline-fitting     |
1498
          |----------|---------------------------------------------|
1499
          | 16       | Spline frame control point                  |
1500
          |----------|---------------------------------------------|
1501
          | 32       | 3D Polyline vertex                          |
1502
          |----------|---------------------------------------------|
1503
          | 64       | 3D polygon mesh vertex                      |
1504
          |----------|---------------------------------------------|
1505
          | 128      | Polyface mesh vertex                        |
1506
          +--------------------------------------------------------+
1507
                 * */
1508
                byte vertexFlag70;
1509
                
1510
                
1511
                
1512
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
1513
                        super.read(bb, flags, opts, dwgObj);
1514
                        pt10 = getPoint(false);
1515
                        bb.order(ByteOrder.LITTLE_ENDIAN);
1516
                        if((opts & 0x1) > 0){
1517
                                startWidth40 = bb.getDouble();
1518
                        }
1519
                        if((opts & 0x2) > 0){
1520
                                endWidth41 = bb.getDouble();
1521
                        }
1522
                        if((opts & 0x4) > 0){
1523
                                tangentDir50 = bb.getDouble();
1524
                        }
1525
                        if((opts & 0x8) > 0){
1526
                                vertexFlag70 = bb.get();
1527
                        }
1528
                }
1529

    
1530
                DwgObject getDwgObject(int index) {
1531
                        DwgObject solution = null;
1532
                        if( (vertexFlag70 & PFACE_MESH) != 0){
1533
                                //It is a PFace Mesh Vertex
1534
                                if( (vertexFlag70 & MESH_3D) != 0){
1535
                                        solution = new DwgVertexPFace(index);
1536
                                        ((DwgVertexPFace)solution).setPoint(pt10);
1537
                                        ((DwgVertexPFace)solution).setFlags(vertexFlag70);
1538
                                }else{
1539
                                        /*
1540
                                        azabala
1541
                                         DXF 12 spec says:
1542
                                                 "If the Vertex defines a face of the mesh, 
1543
                                                 its Vertex flags (70) group has the 128 bit 
1544
                                                 set but not the 64 bit. The 10, 
1545
                                                 20, and 30 (location) groups of the face entity are 
1546
                                                 irrelevant and are always written as zero in a DXF file. 
1547
                                                 The vertex indexes that define the mesh are given by 71, 
1548
                                                 72, 73, and 74 groups, the values of which are integers 
1549
                                                 specifying one of the previously defined vertices by 
1550
                                                 index. If the index is negative, the edge that begins with 
1551
                                                 that vertex is invisible. The first zero vertex marks the 
1552
                                                 end of the vertices of the face. Since the 71 through 74 
1553
                                                 groups are optional fields with default values of zero, 
1554
                                                 they are present in DXF only if nonzero."
1555
                                         But DWG 12 spec doesnt say anything about 71,72,73 and
1556
                                         74 fields for VERTEX.        
1557
                                         * */
1558
                                        solution = new DwgVertexPFaceFace(index);
1559
                                }
1560
                        }else if( (vertexFlag70 & POLYLINE_3D) != 0){
1561
                                solution = new DwgVertex3D(index);
1562
                                ((DwgVertex3D)solution).setPoint(pt10);
1563
                                ((DwgVertex3D)solution).setFlags(vertexFlag70);
1564
                        }else{
1565
                                solution = new DwgVertex2D(index);
1566
                                ((DwgVertex2D)solution).setPoint(pt10);
1567
                                ((DwgVertex2D)solution).setFlags(vertexFlag70);
1568
                        }
1569
                        return solution;
1570
                }
1571
        }
1572
        
1573
        class Face3DReader implements EntityReader{
1574
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
1575
                        boolean zflag = false;
1576
                        if((flags & 0x4) > 0){
1577
                                zflag = true;
1578
                        }
1579
                        double[] pt10 = getPoint(zflag);
1580
                        double[] pt11 = getPoint(zflag);
1581
                        double[] pt12 = getPoint(zflag);
1582
                        double[] pt13 = getPoint(zflag);
1583
                
1584
                }
1585
        }
1586
        
1587
        class DimReader implements EntityReader{
1588
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
1589
                        bb.order(ByteOrder.LITTLE_ENDIAN);
1590
                        short w1 = bb.getShort();
1591
                        double[] pt10 = getPoint(true);
1592
                        double[] pt11 = getPoint(false);
1593
                        
1594
                        if((opts & 0x2) > 0){
1595
                                byte b70 = bb.get();
1596
                        }
1597
                        
1598
                        if((opts & 0x1) > 0){
1599
                                double[] pt12 = getPoint(true);
1600
                        }
1601
                        
1602
                        if((opts & 0x4) > 0){
1603
                                String s1 = getString();
1604
                        }
1605
                        
1606
                        if((opts & 0x8) > 0){
1607
                                double[] pt13 = getPoint(true);
1608
                        }
1609
                        
1610
                        if((opts & 0x10) > 0){
1611
                                double[] pt13 = getPoint(true);
1612
                        }
1613
                        
1614
                        if((opts & 0x20) > 0){
1615
                                double[] pt15 = getPoint(true);
1616
                        }
1617
                        
1618
                        if((opts & 0x40) > 0){
1619
                                double[] pt16 = getPoint(true);
1620
                        }
1621
                        
1622
                        bb.order(ByteOrder.LITTLE_ENDIAN);
1623
                        if((opts & 0x80) > 0){
1624
                                double d40 = bb.getDouble();
1625
                        }
1626
                        
1627
                        if((opts & 0x100) > 0){
1628
                                double d50 = bb.getDouble();
1629
                        }
1630
                        
1631
                        if((opts & 0x200) > 0){
1632
                                double d51 = bb.getDouble();
1633
                        }
1634
                        if((opts & 0x400) > 0){
1635
                                double d52 = bb.getDouble();
1636
                        }
1637
                        if((opts & 0x800) > 0){
1638
                                double d53 = bb.getDouble();
1639
                        }
1640
                }
1641
        }
1642
        
1643
        class VPortReader implements EntityReader{
1644
                public void read(ByteBuffer bb, byte flags, short opts, DwgObject dwgObj) {
1645
                        double[] pt10 = getPoint(true);
1646
                        bb.order(ByteOrder.LITTLE_ENDIAN);
1647
                        double d40 = bb.getDouble();
1648
                        double d41 = bb.getDouble();
1649
                        short w68 = bb.getShort();
1650
                }
1651
        }
1652
        
1653
        /**
1654
         * @param start
1655
         * @param end
1656
         */
1657
        private void readEntities(int start, int end) {
1658
                bb.position(start);
1659
                int ant = bb.position();
1660
                int emax = readers.size();
1661
                EntityReader reader = null;
1662
                
1663
                /*
1664
                 * it isnt null until well finish to read the vertices
1665
                 * of a given polyline (seqend found)
1666
                 * 
1667
                 */
1668
                IDwgPolyline currentPolyline = null;
1669
                
1670
                /*
1671
                 * Not null when a DwgBlock is found, it will be null
1672
                 * when a DwgEndBlk found. While this, all entities readed
1673
                 * will be added to currentBlock
1674
                 * */
1675
                DwgBlockHeader currentBlock = null;
1676
                
1677
                while(ant < (end - 32)){
1678
                        bb.order(ByteOrder.LITTLE_ENDIAN);
1679
                        
1680
                        byte kind = bb.get();
1681
                        if(kind < emax){
1682
                                reader = (EntityReader) readers.get(kind);
1683
                        }//if
1684
                        
1685
                        
1686
                        //PROPERTIES COMMON TO ALL DWG 12 ENTITIES
1687
                        byte flag = bb.get();
1688
                        short lenght = bb.getShort();
1689
                        
1690
                        //segun esto los dos ultimos bytes son el CRC
1691
                        int crcpos = ant + (lenght - 2);
1692
                        
1693
                        
1694
                        short layer = bb.getShort();
1695
                        short opts = bb.getShort();
1696
                        
1697
                        byte color = 0;
1698
                        if ((flag & 0x1) > 0){
1699
                                color = bb.get();
1700
                        }
1701
                        
1702
                        byte extra = 0;
1703
                        if ((flag & 0x40) > 0)
1704
                                extra = bb.get();
1705
                        
1706
                        
1707
                        String xdata = null;
1708
                        if((extra & 0x2) > 0)
1709
                                xdata = readXdata();
1710
                        
1711
                        
1712
                        short type = 0;
1713
                        if((flag & 0x2) > 0)
1714
                                type = bb.getShort();
1715
                        
1716
                        
1717
                        double z = 0d;
1718
                        if( (flag & 0x4) > 0 && (kind > 2) && (kind != 22)){
1719
                                z = bb.getDouble();
1720
                        }
1721
                        
1722
                        double th = 0d;
1723
                        if((flag & 0x8) > 0){
1724
                                th = bb.getDouble();
1725
                        }
1726
                        
1727
                        byte[] handle = null;
1728
                        if((flag & 0x20) > 0)
1729
                                handle = getHandle();
1730
                        
1731
                        short paper = 0;
1732
                        if((extra & 0x4) > 0){
1733
                                paper = bb.getShort();
1734
                        }
1735
                        
1736
                        if(reader != null){
1737
                                DwgObject entity = null;
1738
                                
1739
                                entity = DwgObjectFactory.
1740
                                                        getInstance().
1741
                                                        create(kind, index);
1742
                                
1743
                                //TODO Idem con el espacio papel o el espacio modelo
1744
                                boolean paperSpace = false;
1745
                                if(paper != 0){
1746
                                        paperSpace = true;
1747
                                }
1748
                                reader.read(bb, flag, opts, entity);
1749
                                if(reader instanceof DefferedEntityReader){
1750
                                        entity = ((DefferedEntityReader)reader).getDwgObject(index);        
1751
                                }
1752
                                
1753
                                if(entity instanceof IDwgPolyline){
1754
                                        currentPolyline = (IDwgPolyline) entity;
1755
                                }
1756
                                else if(entity instanceof DwgSeqend){
1757
                                        currentPolyline = null;
1758
                                }else if(entity instanceof DwgBlockHeader){
1759
                                        currentBlock = (DwgBlockHeader) entity;
1760
                                }else if(entity instanceof DwgEndblk){
1761
                                        currentBlock = null;
1762
                                }
1763
                                
1764
                                DwgHandleReference hdl = null;
1765
                                /*
1766
                                In V12 format a handle has 2 bytes, while
1767
                                in later formats a hande has 4 bytes. To avoid
1768
                                rewriting a lot of code we are going to do some "tricks"
1769
                                with handles.
1770
                                */
1771
                                if(entity instanceof DwgBlockHeader){
1772
                                        /*
1773
                                         * If entity is a block, in a later step we'll match
1774
                                         * an insert with its block by handle.
1775
                                     * In dwg 12 format, association insert-blocks is not
1776
                                     * by handle (is from the order of the block in block table)
1777
                                     * So here we use a little trick to avoid rewriting of
1778
                                     * block and inserts management logic.
1779
                                         * */
1780
                                        int order = -1;
1781
                                        DwgBlockHeader blk = (DwgBlockHeader) entity;
1782
                                        String blockName = blk.getName();
1783
                                        Block block = getBlock(blockName);
1784
                                        if(block != null){
1785
                                                order = blocks.indexOf(block);
1786
                                                hdl = new DwgHandleReference(0, order);
1787
                                        }else{
1788
                                                System.out.println("BLOQUE "+blockName+" NO EST? EN LA TABLA DE BLOQUES");
1789
                                                bb.position(crcpos);
1790
                                                short crc = bb.getShort();
1791
                                                continue;
1792
                                        }
1793
                                }else{
1794
                                        if(handle != null){
1795
                                                int offset = handle.hashCode();
1796
                                                hdl = new DwgHandleReference(0, offset);
1797
                                        }else{
1798
                                                System.out.println("HANDLE A NULL");
1799
                                                bb.position(crcpos);
1800
                                                short crc = bb.getShort();
1801
                                                continue;
1802
                                        }
1803
                                }
1804
                                
1805
                                entity.setColor(color);
1806
                                entity.setType(type);//TODO Este type es el mismo que para DWG 13-14-2000?
1807
                                entity.setSizeInBits(lenght);//TODO Este size es el mismo que para DWG 13-14-2000?
1808
                                entity.setHandle(hdl);
1809
                                /*
1810
                                 * TODO
1811
                                 * Parece ser que DWG 12 no gestiona las layers al igual que las
1812
                                 * versiones sucesivas (hay que descifrar si 'layer' significa
1813
                                 * un orden en la tabla de layers
1814
                                 * 
1815
                                 * De ser as?, y si indexamos las layers de esta forma
1816
                                 * (con un HandleReference de code 0x5) ser? todo homogeneo
1817
                                 * 
1818
                                 * TODO Construir instancias de DwgLayer a partir de las entradas
1819
                                 * de la tabla de layers, e indexarlas con un handle igual a su orden
1820
                                 * */
1821
                                
1822
                                entity.setLayerHandle(new DwgHandleReference(0x5, layer));
1823
                                
1824
                                //TODO El valor de elevation en 12 es comun a todas, en 13 y ulteriores
1825
                                //no. ver que se hace
1826
//                                entity.setZ(z);
1827
                                
1828
                                List xdataAsList = new ArrayList();
1829
                                xdataAsList.add(xdata);
1830
                                entity.setExtendedData(xdataAsList);
1831
                                
1832
                                //TODO con thickness tenemos el mismo problema que con
1833
                                //elevation. En dwg 12 es comun a todos
1834
//                                entity.setThickness(th);
1835
                                
1836
                                ant = bb.position();
1837
//                                if(ant < crcpos){
1838
                                if(ant != crcpos){
1839
                                        bb.position(bb.position() + (crcpos - ant));
1840
                                }
1841
                                short crc = bb.getShort();
1842
                                
1843
                                if( (entity instanceof IDwgVertex) && (currentPolyline != null)){
1844
                                        currentPolyline.addVertex((IDwgVertex) entity);
1845
                                        
1846
                                }else if( (entity instanceof IDwgBlockMember) && (currentBlock != null)){
1847
                                        currentBlock.addObject(entity);
1848
                                } else{
1849
//                                        if(! paperSpace)//TODO solo a?adimos las entidades en espacio modelo????
1850
                                        dwgFile.addDwgObject(entity);
1851
                                }
1852
                        }//if reader
1853
                        else{
1854
                                System.out.println("Reader a NULL. DWG 12 MAL LEIDO");
1855
                                System.out.println("kind = " + kind);
1856
                                bb.position(crcpos);
1857
                                short crc = bb.getShort();
1858
                                continue;
1859
                                
1860
                        }
1861
                        ant = bb.position();
1862
//                        byte[] crc32 = new byte[32];
1863
//                        bb.get(crc32);
1864
                        index++;
1865
                }//while
1866
                byte[] crc32 = new byte[32];//TODO va dentro o fuera del while??
1867
                bb.get(crc32);
1868
        }
1869

    
1870

    
1871
        /**
1872
         * @return
1873
         */
1874
        private byte[] getHandle() {
1875
                 byte[] bytes = null;
1876
                 bb.order(ByteOrder.nativeOrder());
1877
                 byte len = bb.get();
1878
                 bytes = new byte[len];
1879
                 bb.get(bytes);
1880
                 return bytes;
1881
//Ver si los handles de DWG 12 concurdan con nuestra clase
1882
//DwgHandleReference
1883
//             return (5, _len) + tuple(_bytes)
1884
        }
1885

    
1886

    
1887

    
1888
        /**
1889
         * @return
1890
         */
1891
        private String readXdata() {
1892
                String extData = "";
1893
                short xlen = bb.getShort();
1894
                while (xlen > 0){
1895
                        byte xval = bb.get();
1896
                        xlen--;
1897
                        if(xval == 0){
1898
                                if(xlen < 1)
1899
                                        break;
1900
                                byte val = bb.get();
1901
                                xlen--;
1902
                                if(r13){
1903
                                        if(xlen < 1)
1904
                                                break;
1905
                                        byte codePage = bb.get();
1906
                                        xlen--;
1907
                                }//if r13
1908
                                if(xlen < val)
1909
                                        break;
1910
                                byte[] strByte = new byte[val];
1911
                                bb.get(strByte);
1912
                                xlen -= val;
1913
                                extData += new String(strByte);
1914
                        }//if xval == 0
1915
                        else if(xval == 1 || xval == 3 || xval == 70){
1916
                                if(xlen < 2)
1917
                                        break;
1918
                                short val = bb.getShort();
1919
                                extData += val;
1920
                                xlen += 2;
1921
                        }else if(xval == 2){
1922
                                if(xlen < 1)
1923
                                        break;
1924
                                byte val = bb.get();
1925
                                if(val == 0)
1926
                                        extData += "{";
1927
                                else if(val == 1)
1928
                                        extData += "}";
1929
                                else
1930
                                        System.out.println("Byte no esperado:"+val);
1931
                        }else if(xval == 5){
1932
                                if(xlen < 8)
1933
                                        break;
1934
                                long val = bb.getLong();
1935
                                //probably it is an entity handle
1936
                                xlen -= 8;
1937
                        }else if (xval == 40 || xval == 41 || xval == 42){
1938
                                if (xlen < 8)
1939
                                        break;
1940
                                double val = bb.getDouble();
1941
                                xlen -= 8;
1942
                                extData += val;
1943
                                
1944
                        }else if (xval == 10 || xval == 11 || xval == 12 || xval == 13){
1945
                                //13 not in R12 spec, is in R13/R14
1946
                                if(xlen < 24)
1947
                                        break;
1948
                                Object point = getPoint(true);
1949
                                extData += point.toString();
1950
                                xlen -= 24;
1951
                        }else if(xval == 71){
1952
                                if (xlen < 4)
1953
                                        break;
1954
                                int val = bb.getInt();
1955
                                extData += val;
1956
                                xlen -= 4;
1957
                        }else
1958
                                xlen = 0;
1959
                        return extData;
1960
                }
1961
                /*
1962

1963
           
1964
       
1965
        elif _xval == 71:
1966
            if _xlen < 4: break
1967
            _val = struct.unpack('<l', handle.read(4))[0]
1968
            _data.append(_val)
1969
            _xlen = _xlen - 4
1970
        else:
1971
            _xlen = 0
1972
    return _data
1973
                 * */
1974
                return null;
1975
        }
1976

    
1977

    
1978

    
1979
        /**
1980
         * @param b
1981
         * @return
1982
         */
1983
        private double[] getPoint(boolean b) {
1984
                bb.order(ByteOrder.LITTLE_ENDIAN);
1985
                double x = bb.getDouble();
1986
                double y = bb.getDouble();
1987
                double z = 0d;
1988
                if(b){
1989
                        z = bb.getDouble();
1990
                }
1991
                return new double[]{x, y, z};
1992
        }
1993

    
1994

    
1995

    
1996
        //TODO Que se hacen con los registros que se leen???
1997
        private void readBlockTable(Dwg12Table blockTable) {
1998
                short size = blockTable.s1;
1999
                int numRecords =  blockTable.i1;
2000
                int start = blockTable.i2;
2001
                
2002
                int begin = -1;
2003
                int end = -1;
2004
                
2005
                for(int i = 0; i < numRecords; i++){
2006
                        
2007
                        begin = start + i * size;
2008
                        bb.position(begin);
2009
                        
2010
                        end = begin + size - 2;
2011
        
2012
                        byte flag = bb.get();
2013
                        byte[] nameByte = new byte[32];
2014
                        bb.get(nameByte);
2015
                        String name = new String(nameByte).trim();
2016
                        
2017
                        bb.order(ByteOrder.LITTLE_ENDIAN);
2018
                        
2019
                        short used = bb.getShort();
2020
                        byte b1 = bb.get();
2021
                        short w1 = bb.getShort();
2022
                        byte b2 = bb.get();
2023
                        
2024
                        short w3 = bb.getShort();
2025
                        short crc = bb.getShort();
2026
                
2027
                        int offset = end - bb.position();
2028
                        if(offset > 0)
2029
                                bb.position(bb.position() + end);
2030
                        
2031
                        Block block = new Block();
2032
                        block.flag = flag;
2033
                        block.name = name;
2034
                        block.used = used;
2035
                        block.b1 = b1;
2036
                        block.w1 = w1;
2037
                        block.b2 = b2;
2038
                        block.w3 = w3;
2039
                        block.crc = crc;
2040
                        blocks.add(block);
2041
                }//for
2042
                byte[] crc32 = new byte[32];
2043
                bb.get(crc32);
2044
        }
2045

    
2046
        public void readHeader(){
2047
                bb.order(ByteOrder.LITTLE_ENDIAN);
2048
                short w1 = bb.getShort();
2049
                double[] inBase = getPoint(true);
2050
                double[] extMin = getPoint(true);
2051
                double[] extMax = getPoint(true);
2052
                double[] limMin = getPoint(false);
2053
                double[] limMax = getPoint(false);
2054
                
2055
                bb.order(ByteOrder.LITTLE_ENDIAN);
2056
                double vcx = bb.getDouble();
2057
                double vcy = bb.getDouble();
2058
                double d3 = bb.getDouble();
2059
                double d4 = bb.getDouble();
2060
                
2061
                byte b1 = bb.get();
2062
                byte b2 = bb.get();
2063
                
2064
                double sx = bb.getDouble();
2065
                double sy = bb.getDouble();
2066
                
2067
                bb.order(ByteOrder.nativeOrder());
2068
                byte[] b56 = new byte[56];
2069
                bb.get(b56);
2070
                
2071
                bb.order(ByteOrder.LITTLE_ENDIAN);
2072
                double d7 = bb.getDouble();
2073
                double d8 = bb.getDouble();
2074
                double d9 = bb.getDouble();
2075
                
2076
                bb.order(ByteOrder.nativeOrder());
2077
                byte[] b18 = new byte[18];
2078
                bb.get(b18);
2079
                
2080
                bb.order(ByteOrder.LITTLE_ENDIAN);
2081
                double d10 = bb.getDouble();
2082
                
2083
                int at = bb.position();
2084
                
2085
                bb.order(ByteOrder.LITTLE_ENDIAN);
2086
                short w2 = bb.getShort();
2087
                short w3 = bb.getShort();
2088
                
2089
                bb.order(ByteOrder.nativeOrder());
2090
                byte[] b44 = new byte[44];
2091
                bb.get(b44);
2092
                
2093
                at = bb.position();
2094
                
2095
                bb.order(ByteOrder.nativeOrder());
2096
                byte[] b354 = new byte[354];
2097
                bb.get(b354);
2098
                
2099
                if( (b354[0] == 0x61) && (b354[1] == 0x63) && (b354[2] == 0x61 ) && (b354[2] == 0x64 ))
2100
                        System.out.println("Encontrada la cadena 'acad' al comienzo del bloque de 354 bytes");
2101
        
2102
                bb.position(0x3ef);
2103
        }
2104
        
2105
        
2106
        
2107
        
2108
        
2109
        class Dwg12Table{
2110
                short s1;
2111
                int i1;
2112
                int i2;
2113
        }
2114
        
2115
        public Dwg12Table getTable(){
2116
                
2117
                Dwg12Table solution = null;
2118
                bb.order(ByteOrder.LITTLE_ENDIAN);
2119
                
2120
                short s1 = bb.getShort();
2121
                int i1 = bb.getInt();
2122
                int i2 = bb.getInt();
2123
                
2124
                solution = new Dwg12Table();
2125
                solution.s1 = s1;
2126
                solution.i1 = i1;
2127
                solution.i2 = i2;
2128
                return solution;
2129
        }
2130
        
2131
        
2132
        class Dwg12TableTest{
2133
                short s1, s2, s3;
2134
                int i1;
2135
        }
2136
        public Dwg12TableTest getTableTest(){
2137
                Dwg12TableTest solution = null;
2138
                
2139
                
2140
                bb.order(ByteOrder.LITTLE_ENDIAN);
2141
                short s1 = bb.getShort();
2142
                short s2 = bb.getShort();
2143
                short s3 = bb.getShort();
2144
                int i1 = bb.getInt();
2145
                
2146
                solution = new Dwg12TableTest();
2147
                solution.s1 = s1;
2148
                solution.s2 = s2;
2149
                solution.s3 = s3;
2150
                solution.i1 = i1;
2151
                
2152
                return solution;
2153
        }
2154

    
2155
        /* (non-Javadoc)
2156
         * @see com.iver.cit.jdwglib.dwg.readers.IDwgFileReader#readObjectHeader(int[], int, com.iver.cit.jdwglib.dwg.DwgObject)
2157
         */
2158
        public int readObjectHeader(int[] data, int offset, DwgObject dwgObject) throws RuntimeException, CorruptedDwgEntityException {
2159
                return 0;
2160
        }
2161

    
2162
        /* (non-Javadoc)
2163
         * @see com.iver.cit.jdwglib.dwg.readers.IDwgFileReader#readObjectTailer(int[], int, com.iver.cit.jdwglib.dwg.DwgObject)
2164
         */
2165
        public int readObjectTailer(int[] data, int offset, DwgObject dwgObject) throws RuntimeException, CorruptedDwgEntityException {
2166
                return 0;
2167
        }
2168

    
2169
}