Statistics
| Revision:

root / trunk / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / DwgFile.java @ 9910

History | View | Annotate | Download (25.7 KB)

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

    
37
import java.awt.geom.Point2D;
38
import java.io.File;
39
import java.io.FileInputStream;
40
import java.io.IOException;
41
import java.nio.ByteBuffer;
42
import java.nio.channels.FileChannel;
43
import java.util.ArrayList;
44
import java.util.HashMap;
45
import java.util.LinkedList;
46
import java.util.List;
47
import java.util.Map;
48

    
49
import com.iver.cit.jdwglib.dwg.objects.DwgArc;
50
import com.iver.cit.jdwglib.dwg.objects.DwgAttdef;
51
import com.iver.cit.jdwglib.dwg.objects.DwgAttrib;
52
import com.iver.cit.jdwglib.dwg.objects.DwgBlock;
53
import com.iver.cit.jdwglib.dwg.objects.DwgBlockHeader;
54
import com.iver.cit.jdwglib.dwg.objects.DwgCircle;
55
import com.iver.cit.jdwglib.dwg.objects.DwgEllipse;
56
import com.iver.cit.jdwglib.dwg.objects.DwgEndblk;
57
import com.iver.cit.jdwglib.dwg.objects.DwgInsert;
58
import com.iver.cit.jdwglib.dwg.objects.DwgLayer;
59
import com.iver.cit.jdwglib.dwg.objects.DwgLine;
60
import com.iver.cit.jdwglib.dwg.objects.DwgLwPolyline;
61
import com.iver.cit.jdwglib.dwg.objects.DwgMText;
62
import com.iver.cit.jdwglib.dwg.objects.DwgPoint;
63
import com.iver.cit.jdwglib.dwg.objects.DwgPolyline2D;
64
import com.iver.cit.jdwglib.dwg.objects.DwgPolyline3D;
65
import com.iver.cit.jdwglib.dwg.objects.DwgSeqend;
66
import com.iver.cit.jdwglib.dwg.objects.DwgSolid;
67
import com.iver.cit.jdwglib.dwg.objects.DwgText;
68
import com.iver.cit.jdwglib.dwg.objects.DwgVertex2D;
69
import com.iver.cit.jdwglib.dwg.readers.IDwgFileReader;
70
import com.iver.cit.jdwglib.dwg.readers.DwgFileV15Reader;
71

    
72
/**
73
 * The DwgFile class provides a revision-neutral interface for reading and handling
74
 * DWG files
75
 * Reading methods are useful for reading DWG files, and handling methods like
76
 * calculateDwgPolylines() are useful for handling more complex
77
 * objects in the DWG file
78
 * 
79
 * @author jmorell
80
 * @author azabala
81
 */
82
public class DwgFile {
83
        /**
84
         * It has all known DWG version's header.
85
         * Extracted from Autodesk web.
86
         */
87
        private static HashMap acadVersions = new HashMap();
88
        static{
89
                acadVersions.put("AC1004", "Autocad R9");
90
                acadVersions.put("AC1006", "Autocad R10");
91
                acadVersions.put("AC1007", "Autocad pre-R11");
92
                acadVersions.put("AC1007", "Autocad pre-R11");
93
                acadVersions.put("AC1008", "Autocad pre-R11b");
94
                acadVersions.put("AC1009", "Autocad R12");
95
                acadVersions.put("AC1010", "Autocad pre-R13 a");
96
                acadVersions.put("AC1011", "Autocad pre-R13 b");
97
                acadVersions.put("AC1012", "Autocad R13");
98
                acadVersions.put("AC1013", "Autocad pre-R14");
99
                acadVersions.put("AC1014", "Autocad R14");
100
                acadVersions.put("AC1500", "Autocad pre-2000");
101
                acadVersions.put("AC1015", "Autocad R2000, R2000i, R2002");
102
                acadVersions.put("AC402a", "Autocad pre-2004a");
103
                acadVersions.put("AC402b", "Autocad pre-2004b");
104
                acadVersions.put("AC1018", "Autocad R2004, R2005, R2006");
105
                acadVersions.put("AC1021", "Autocad R2007");
106
        
107
        }
108

    
109
        private String fileName;
110
        private String dwgVersion;
111
        
112
        /**
113
         * Offsets to the DWG sections
114
         */
115
        private ArrayList dwgSectionOffsets;
116
        /**
117
         * Header vars readed from the HEADER section of the DWG file
118
         * */
119
        private Map headerVars;
120
        
121
        /**
122
         * This list contains what in OpenDWG specification is called
123
         * the object map ( a collection of entries where each entry contains
124
         * the seek of each object, and its size)
125
         * */
126
        private ArrayList dwgObjectOffsets;
127
        
128
        /**
129
         * For each entry in dwgObjectOffsets, we have an instance of
130
         * DwgObject in the dwgObjects collection
131
         * 
132
         * */
133
        private LinkedList dwgObjects;
134
        private HashMap handle_objects;
135
        
136

    
137
        private ArrayList dwgClasses;
138
        
139
        
140
        /**
141
         * hash map that indexes all DwgLayer objects
142
         * by its handle property
143
         * */
144
        private HashMap layerTable; 
145
        
146
        /**
147
         * Specific reader of the DWG file version (12, 13, 14, 2000, etc., each
148
         * version will have an specific reader)
149
         * */
150
        private IDwgFileReader dwgReader;
151
        private boolean dwg3DFile;
152
        /**
153
         * Memory mapped byte buffer of the whole DWG file
154
         * */
155
        private ByteBuffer bb;
156
        
157
        /*
158
         * Optimizaciones para evitar leer el fichero completo en cada
159
         * pasada.
160
         * */
161
        /**
162
         * Contains all IDwgPolyline implementations
163
         * */
164
        private List dwgPolylines;
165
        /**
166
         * map of blocks dwg entities indexed by their names.
167
         */
168
        private Map names_blocks;
169
        
170
        
171
        
172
        
173
        /**
174
         * Constructor.
175
         * @param fileName an absolute path to the DWG file
176
         */
177
        public DwgFile(String fileName) {
178
                this.fileName = fileName;
179
                
180
                dwgSectionOffsets = new ArrayList();
181
                dwgObjectOffsets = new ArrayList();
182
                headerVars = new HashMap();
183
                
184
                //TODO Si finalmente no se leen las clases, quitar
185
                //dwgClasses
186
                dwgClasses = new ArrayList();
187
                
188
                dwgObjects = new LinkedList();
189
                handle_objects = new HashMap();
190
                
191
                layerTable = new HashMap();
192
                
193
                dwgPolylines = new ArrayList();
194
                
195
                names_blocks = new HashMap();
196
        }
197
        
198
        public String getDwgVersion() {
199
                return dwgVersion;
200
        }
201
        
202
        /**
203
         * Reads a DWG file and put its objects in the dwgObjects Vector
204
         * This method is version independent
205
         * 
206
         * @throws IOException If the file location is wrong
207
         */
208
        public void read() throws IOException, 
209
                                DwgVersionNotSupportedException
210
        {
211
                setDwgVersion();
212
                if (dwgVersion.equalsIgnoreCase("Autocad R2000, R2000i, R2002")) {
213
                        dwgReader = new DwgFileV15Reader();
214
                        dwgReader.read(this, bb);
215
                } else {
216
                        DwgVersionNotSupportedException exception = 
217
                                new DwgVersionNotSupportedException("Version de DWG no soportada");
218
                        exception.setDwgVersion(dwgVersion);
219
                        throw exception;
220
                }
221
                
222
        }
223
        
224
        private void setDwgVersion() throws IOException {
225
                File file = new File(fileName);
226
                FileInputStream fileInputStream = new FileInputStream(file);
227
                FileChannel fileChannel = fileInputStream.getChannel();
228
                long channelSize = fileChannel.size();
229
                bb = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, channelSize);
230
                byte[] versionBytes = {bb.get(0), 
231
                                                                bb.get(1), 
232
                                                                bb.get(2),
233
                                                                bb.get(3),
234
                                                                bb.get(4), 
235
                                                                bb.get(5)};
236
                ByteBuffer versionByteBuffer = ByteBuffer.wrap(versionBytes);
237
                String[] bs = new String[versionByteBuffer.capacity()];
238
                String versionString = "";
239
                for (int i=0; i<versionByteBuffer.capacity(); i++) {
240
                        bs[i] = new String(new byte[]{(byte)(versionByteBuffer.get(i))});
241
                        versionString = versionString + bs[i];
242
                }
243
                String version = (String) acadVersions.get(versionString);
244
                if(version == null)
245
                        version = "Unknown Dwg format";
246
                this.dwgVersion = version;    
247
        }
248
        
249
        public void setHeader(String key, Object value){
250
                headerVars.put(key, value);
251
        }
252
        
253
        
254
        
255
        /**
256
         * Initialize a new Vector that contains the DWG file layers. 
257
         * Each layer have three parameters. 
258
         * These parameters are handle, name and color
259
         */
260
//        public void initializeLayerTable() {
261
//                layerTable = new ArrayList();
262
//                for (int i=0;i<dwgObjects.size();i++) {
263
//                        DwgObject obj = (DwgObject)dwgObjects.get(i);
264
//                        if (obj instanceof DwgLayer) {
265
//                                ArrayList layerTableRecord = new ArrayList();
266
//                                layerTableRecord.add(new Integer(obj.getHandle()));
267
//                                layerTableRecord.add(((DwgLayer)obj).getName());
268
//                                layerTableRecord.add(new Integer(((DwgLayer)obj).getColor()));
269
//                                layerTable.add(layerTableRecord);
270
//                        }
271
//                }
272
//        }
273
        
274
        /*
275
         * A DwgLayer is a DWG drawing entity, so all DwgLayer objects are
276
         * saved in dwgObjects collection.
277
         * Instead of iterate over the full collection, is faster check every object
278
         * and save all the DwgLayer instances in their own collection.
279
         *
280
         */
281
        public void addDwgLayer(DwgLayer dwgLayer){
282
                layerTable.put(new Integer(dwgLayer.getHandle()), 
283
                                dwgLayer);
284
        }
285
        
286
        public DwgLayer getDwgLayer(DwgObject entity){
287
                
288
                int handleCode = entity.getLayerHandleCode();
289
                int layerHandle = -1;
290
                
291
                int entityRecord;
292
                DwgObject object;
293
                
294
//                switch(handleCode){
295
//                        case 0x2:
296
//                        case 0x3:
297
//                        case 0x4:
298
//                        case 0x5:
299
//                                layerHandle = entity.getLayerHandle();
300
//                        break;
301
//                        
302
//                        case 0x6:
303
//                                entityRecord = entity.getIndex();
304
//                                object = this.getDwgObject(entityRecord + 1);
305
//                                layerHandle = object.getLayerHandle();
306
//                        break;
307
//                        case 0x8:
308
//                                entityRecord = entity.getIndex();
309
//                                object = this.getDwgObject(entityRecord - 1);
310
//                                layerHandle = object.getLayerHandle();
311
//                                
312
//                        break;
313
//                        case 0xA:
314
//                                //reference handle plus offset of layer handle??
315
//                                int handle = entity.getHandle();
316
//                                int offset = entity.getLayerHandle();
317
//                                layerHandle = handle + offset;
318
//                                
319
//                                
320
//                        break;
321
//                        case 0xC:
322
//                            //reference handle minus offset of layer handle??
323
//                                handle = entity.getHandle();
324
//                                offset = entity.getLayerHandle();
325
//                                layerHandle = handle + offset;
326
//                                
327
//                        break;
328
//                        default:
329
//                                System.out.println("Unknown layer handle code:"+handleCode);        
330
//                        return null;
331
//                }
332
                
333
                
334
                
335
                /*
336
                 * OpenDWG spec, v3, page 11. HANDLE REFERENCES
337
                 * 
338
                 * A handle is formed by this fields:
339
                 * a) CODE (4 bits)
340
                 * b) COUNTER (4 bits)
341
                 * c) OFFSET of the object (handle itself).
342
                 * 
343
                 * CODE could take these values:
344
                 * 1) 0x2, 0x3, 0x4, 0x5 -> offset is the handle of the layer
345
                 * 2) 0x6 -> offset is the next object handle
346
                 * 3) 0x8 -> offset is the previous object handle
347
                 * 4) 0xA -> result is reference handle plus offset
348
                  * 5) 0xC -> result is reference handle minus offset
349
                 * 
350
                 * */
351
                
352
//                Iterator lyrIterator = layerTable.values().iterator();
353
//                while(lyrIterator.hasNext()){
354
//                        DwgLayer lyr = (DwgLayer) lyrIterator.next();
355
//                        int lyrHdl = lyr.getHandle();
356
//                        int entityHdlCode = entity.getLayerHandleCode();
357
//                        int entityHdl = entity.getLayerHandle();
358
//                        int newLyrHdl = layerHandle;
359
//                        System.currentTimeMillis();
360
//                }
361
                
362
                return (DwgLayer) layerTable.
363
                                get(new Integer(layerHandle));
364
        }
365
        
366
   
367
        public String getLayerName(DwgObject entity) {                
368
                DwgLayer dwgLayer = getDwgLayer(entity);
369
                if(dwgLayer == null){//TODO Puede haber entidades sin layer???
370
                        return "";
371
                }else
372
                        return dwgLayer.getName();
373
        }
374
        
375
        /**
376
     * Returns the color of the layer of a DWG object 
377
         * 
378
     * @param entity DWG object which we want to know its layer color
379
         * @return int Layer color of the DWG object in the Autocad color code
380
         */
381
        public int getColorByLayer(DwgObject entity) {
382
                int colorByLayer;
383
                DwgLayer dwgLyr = getDwgLayer(entity);
384
                if(dwgLyr == null)
385
                        colorByLayer = 0;
386
                else
387
                        colorByLayer = dwgLyr.getColor();
388
                return colorByLayer;
389
        }
390
        
391

    
392
        //azabala: esto ahora se hace conforme se van creando los objetos
393
        //(y no al final en una segunda pasada
394
//    public void applyExtrusions() {
395
//        for (int i=0;i<dwgObjects.size();i++) {
396
//            DwgObject dwgObject = (DwgObject)dwgObjects.get(i);
397
//            if(dwgObject instanceof IDwgExtrusionable){
398
//                    ((IDwgExtrusionable)dwgObject).applyExtrussion();
399
//            }
400
//            // Seems that Autocad don't apply the extrusion to Ellipses
401
//            /*} else if (dwgObject instanceof DwgEllipse) {
402
//                double[] ellipseCenter = ((DwgEllipse)dwgObject).getCenter();
403
//                double[] ellipseExt = ((DwgEllipse)dwgObject).getExtrusion();
404
//                ellipseCenter = AcadExtrusionCalculator.CalculateAcadExtrusion(ellipseCenter, ellipseExt);
405
//                ((DwgEllipse)dwgObject).setCenter(ellipseCenter);*/
406
//        }//for
407
//    }
408
        
409
        /**
410
         * Configure the geometry of the polylines in a DWG file from the vertex list in
411
         * this DWG file. This geometry is given by an array of Points.
412
         * Besides, manage closed polylines and polylines with bulges in a GIS Data model.
413
     * It means that the arcs of the polylines will be done through a set of points and
414
     * a distance between these points.
415
         */
416
        public void calculateGisModelDwgPolylines() {
417
                /*
418
                 * Ahora mismo esto se est? haciendo al final (en una segunda pasada)
419
                 * porque posiblemente una Linea requiera de vertices que vengan
420
                 * despues, lo que complicar?a bastante todo....
421
                 * */
422
                
423
//                for (int i=0;i<dwgObjects.size();i++) {
424
                for (int i = 0; i < dwgPolylines.size(); i++){
425
//                        DwgObject pol = (DwgObject)dwgObjects.get(i);
426
                        DwgObject pol = (DwgObject)dwgPolylines.get(i);
427
                        if (pol instanceof IDwgPolyline) {
428
                                ((IDwgPolyline)pol).calculateGisModel(dwgObjects);
429
                        } 
430
                }
431
        }
432
        
433
    /**
434
     * Configure the geometry of the polylines in a DWG file from the vertex list in
435
     * this DWG file. This geometry is given by an array of Points
436
     * Besides, manage closed polylines and polylines with bulges in a GIS Data model.
437
     * It means that the arcs of the polylines will be done through a curvature
438
     * parameter called bulge associated with the points of the polyline.
439
     */
440
        //TODO Refactorizar para que solo se aplique a las Polilineas
441
        public void calculateCadModelDwgPolylines() {
442
                for (int i=0;i<dwgObjects.size();i++) {
443
                        DwgObject pol = (DwgObject)dwgObjects.get(i);
444
                        if (pol instanceof DwgPolyline2D) {
445
                                int flags = ((DwgPolyline2D)pol).getFlags();
446
                                int firstHandle = ((DwgPolyline2D)pol).getFirstVertexHandle();
447
                                int lastHandle = ((DwgPolyline2D)pol).getLastVertexHandle();
448
                                ArrayList pts = new ArrayList();
449
                                ArrayList bulges = new ArrayList();
450
                                double[] pt = new double[3];
451
                                for (int j=0;j<dwgObjects.size();j++) {
452
                                        DwgObject firstVertex = (DwgObject)dwgObjects.get(j);
453
                                        if (firstVertex instanceof DwgVertex2D) {
454
                                                int vertexHandle = firstVertex.getHandle();
455
                                                if (vertexHandle==firstHandle) {
456
                                                        int k=0;
457
                                                        while (true) {
458
                                                                DwgObject vertex = (DwgObject)dwgObjects.get(j+k);
459
                                                                int vHandle = vertex.getHandle();
460
                                                                if (vertex instanceof DwgVertex2D) {
461
                                                                        pt = ((DwgVertex2D)vertex).getPoint();
462
                                                                        pts.add(new Point2D.Double(pt[0], pt[1]));
463
                                                                        double bulge = ((DwgVertex2D)vertex).getBulge();
464
                                                                        bulges.add(new Double(bulge));
465
                                                                        k++;
466
                                                                        if (vHandle==lastHandle && vertex instanceof DwgVertex2D) {
467
                                                                                break;
468
                                                                        }
469
                                                                } else if (vertex instanceof DwgSeqend) {
470
                                                                        break;
471
                                                                }
472
                                                        }
473
                                                }
474
                                        }
475
                                }
476
                                if (pts.size()>0) {
477
                                        /*Point2D[] newPts = new Point2D[pts.size()];
478
                                        if ((flags & 0x1)==0x1) {
479
                                                newPts = new Point2D[pts.size()+1];
480
                                                for (int j=0;j<pts.size();j++) {
481
                                                        newPts[j] = (Point2D)pts.get(j);
482
                                                }
483
                                                newPts[pts.size()] = (Point2D)pts.get(0);
484
                                                bulges.add(new Double(0));
485
                                        } else {
486
                                                for (int j=0;j<pts.size();j++) {
487
                                                        newPts[j] = (Point2D)pts.get(j);
488
                                                }
489
                                        }*/
490
                                        double[] bs = new double[bulges.size()];
491
                                        for (int j=0;j<bulges.size();j++) {
492
                                                bs[j] = ((Double)bulges.get(j)).doubleValue();
493
                                        }
494
                                        ((DwgPolyline2D)pol).setBulges(bs);
495
                                        //Point2D[] points = GisModelCurveCalculator.calculateGisModelBulge(newPts, bs);
496
                                        Point2D[] points = new Point2D[pts.size()];
497
                                        for (int j=0;j<pts.size();j++) {
498
                                            points[j] = (Point2D)pts.get(j);
499
                                        }
500
                                        ((DwgPolyline2D)pol).setPts(points);
501
                                } else {
502
//                                        System.out.println("Encontrada polil?nea sin puntos ...");
503
                                        // TODO: No se debe mandar nunca una polil?nea sin puntos, si esto
504
                                        // ocurre es porque existe un error que hay que corregir ...
505
                                }
506
                        } else if (pol instanceof DwgPolyline3D) {
507
                        } else if (pol instanceof DwgLwPolyline && ((DwgLwPolyline)pol).getVertices()!=null) {
508
                        }
509
                }
510
        }
511
        
512
    /**
513
     * Modify the geometry of the objects contained in the blocks of a DWG file and
514
     * add these objects to the DWG object list.
515
     */
516
        public void blockManagement() {
517
                LinkedList dwgObjectsWithoutBlocks = new LinkedList();
518
            boolean addingToBlock = false;
519
                for (int i=0; i < dwgObjects.size(); i++) {
520
                        DwgObject entity = (DwgObject)dwgObjects.get(i);
521
                        if (entity instanceof DwgArc && !addingToBlock) {
522
                                dwgObjectsWithoutBlocks.add(entity);
523
                        } else if (entity instanceof DwgEllipse && !addingToBlock) {
524
                                dwgObjectsWithoutBlocks.add(entity);
525
                        } else if (entity instanceof DwgCircle && !addingToBlock) {
526
                                dwgObjectsWithoutBlocks.add(entity);
527
                        } else if (entity instanceof DwgPolyline2D && !addingToBlock) {
528
                                dwgObjectsWithoutBlocks.add(entity);
529
                        } else if (entity instanceof DwgPolyline3D && !addingToBlock) {
530
                                dwgObjectsWithoutBlocks.add(entity);
531
                        } else if (entity instanceof DwgLwPolyline && !addingToBlock) {
532
                                dwgObjectsWithoutBlocks.add(entity);
533
                        } else if (entity instanceof DwgSolid && !addingToBlock) {
534
                                dwgObjectsWithoutBlocks.add(entity);
535
                        } else if (entity instanceof DwgLine && !addingToBlock) {
536
                                dwgObjectsWithoutBlocks.add(entity);
537
                        } else if (entity instanceof DwgPoint && !addingToBlock) {
538
                                dwgObjectsWithoutBlocks.add(entity);
539
                        } else if (entity instanceof DwgMText && !addingToBlock) {
540
                                dwgObjectsWithoutBlocks.add(entity);
541
                        } else if (entity instanceof DwgText && !addingToBlock) {
542
                                dwgObjectsWithoutBlocks.add(entity);
543
                        } else if (entity instanceof DwgAttrib && !addingToBlock) {
544
                                dwgObjectsWithoutBlocks.add(entity);
545
                        } else if (entity instanceof DwgAttdef && !addingToBlock) {
546
                                dwgObjectsWithoutBlocks.add(entity);
547
                        } else if (entity instanceof DwgBlock) {
548
                                addingToBlock = true;
549
                        } else if (entity instanceof DwgEndblk) {
550
                                addingToBlock = false;
551
                        } else if (entity instanceof DwgBlockHeader) {
552
                                addingToBlock = true;
553
                        } 
554
                        else if (entity instanceof DwgInsert && !addingToBlock) {
555
                                double[] p = ((DwgInsert)entity).getInsertionPoint();
556
                                Point2D point = new Point2D.Double(p[0], p[1]);
557
                                double[] scale = ((DwgInsert)entity).getScale();
558
                                double rot = ((DwgInsert)entity).getRotation();
559
                                int blockHandle = ((DwgInsert)entity).getBlockHeaderHandle();
560
                                manageInsert(point, scale, rot, blockHandle, dwgObjectsWithoutBlocks);
561
                        } else {
562
//                                System.out.println("Detectado dwgObject pendiente de implementar");
563
//                                System.out.println(entity.toString());
564
                        }
565
                }
566
                dwgObjects = dwgObjectsWithoutBlocks;
567
        }
568
        
569
    /**
570
     * Manages an INSERT of a DWG file. This object is the insertion point of a DWG
571
     * block. 
572
     * 
573
     * @param insPoint, coordinates of the insertion point.
574
     * @param scale, scale of the elements of the block that will be inserted.
575
     * @param rot, rotation angle of the elements of the block.
576
     * 
577
     * @param bHandle, offset for the coordinates of the elements of the block.
578
     * @param id, count that serves as a id.
579
     * @param dwgObjectsWithoutBlocks, a object list with the elements extracted from
580
     * the blocks.
581
     */
582
        //Hecho publico para manejar INSERT dentro de INSERT 
583
        public void manageInsert(Point2D insPoint, double[] scale,
584
                                                        double rot, int bHandle, 
585
                                                        List dwgObjectsWithoutBlocks) {
586
                
587
                DwgObject object = (DwgObject) handle_objects.get(new Integer(bHandle));
588
                if(! (object instanceof DwgBlockHeader)){
589
                        //Hay un problema con la asignaci?n de handle
590
                        //Un INSERT tiene como handle de Block una entidad que no es block
591
                        return;
592
                }
593
                DwgBlockHeader blockHeader = (DwgBlockHeader)object;
594
                double[] bPoint = blockHeader.getBasePoint();
595
                String bname = blockHeader.getName();
596
                if (bname.startsWith("*")) 
597
                        return;
598
                int firstObjectHandle = blockHeader.getFirstEntityHandle();   
599
                int lastObjectHandle = blockHeader.getLastEntityHandle();
600
                
601
                //TODO Ver que hacia el metodo antiguo cuando llegaban handles a 0
602
                //como extremos de un bloque (sera bloque vacio, supongo)
603
                if(firstObjectHandle == 0 || lastObjectHandle == 0)
604
                        return;
605
                
606
                //Busca el bloque cuyo nombre sea bname
607
//                El bloque no se usa para nada DwgBlock block = (DwgBlock) names_blocks.get(bname);
608
                /*
609
                 * Ahora localiza la primera entidad del bloque, 
610
                 * y a partir de ah?, a?ade
611
                 * al bloque todas las entidades que encuentre hasta la 
612
                 * ?ltima entidad del bloque
613
                 * 
614
                 * */
615
                 DwgObject firstObj = (DwgObject) handle_objects.
616
                                                 get(new Integer(firstObjectHandle));
617
                 DwgObject lastObj =  (DwgObject) handle_objects.
618
                                                 get(new Integer(lastObjectHandle));
619
                 
620
                 int firstObjIdx = dwgObjects.indexOf(firstObj);
621
                 int lastObjIdx = dwgObjects.indexOf(lastObj);
622
                 if(firstObjIdx == -1 || lastObjIdx == -1){
623
                         System.out.println("1?="+firstObjIdx+",Ultimo="+lastObjIdx);
624
                 }
625
                 for(int i = firstObjIdx; i <= lastObjIdx; i++){
626
                         DwgObject obj = (DwgObject) dwgObjects.get(i);
627
                         manageBlockEntity(obj, bPoint, 
628
                                                           insPoint, scale, 
629
                                                           rot, dwgObjectsWithoutBlocks);
630
                 }//for
631
        }
632
        
633
    /**
634
     * Changes the location of an object extracted from a block. This location will be
635
     * obtained through the insertion parameters from the block and the corresponding
636
     * insert.
637
     * @param entity, the entity extracted from the block.
638
     * @param bPoint, offset for the coordinates of the entity.
639
     * @param insPoint, coordinates of the insertion point for the entity.
640
     * @param scale, scale for the entity.
641
     * @param rot, rotation angle for the entity.
642
     * @param id, a count as a id.
643
     * @param dwgObjectsWithoutBlocks, a object list with the elements extracted from
644
     * the blocks.
645
     */
646
        private void manageBlockEntity(DwgObject entity, 
647
                                                                        double[] bPoint, 
648
                                                                        Point2D insPoint, 
649
                                                                        double[] scale, 
650
                                                                        double rot, 
651
                                                                        List dwgObjectsWithoutBlocks) {
652
                
653
                if(entity instanceof IDwgBlockMember){
654
                        IDwgBlockMember blockMember = (IDwgBlockMember)entity;
655
                        blockMember.transform2Block(bPoint, insPoint, scale, rot, dwgObjectsWithoutBlocks, this);
656
                }
657
        }
658
        
659
        
660
        /*
661
         * azabala: Esto ahora se hace mientras las entidades dwg
662
         * se van creando, y no en una segunda pasada
663
         * */
664
        /*
665
        public void testDwg3D() {
666
                for (int i=0;i<dwgObjects.size();i++) {
667
                        DwgObject obj = (DwgObject)dwgObjects.get(i);
668
                        
669
                        if(obj instanceof IDwg3DTestable){
670
                                if(((IDwg3DTestable)obj).has3DData())
671
                                        dwg3DFile = true;
672
                                return;
673
                        }
674
                        
675
                        //} else if (obj instanceof DwgLinearDimension) {
676
                        //        z = ((DwgLinearDimension)obj).getElevation();
677
                        //        if (z!=0.0) dwg3DFile = true;         
678
                }//for
679
                dwg3DFile = false;
680
        }
681
        */
682
        
683
        /**
684
         * Add a DWG section offset to the dwgSectionOffsets vector
685
         * 
686
         * @param key Define the DWG section
687
         * @param seek Offset of the section
688
         * @param size Size of the section
689
         */
690
        public void addDwgSectionOffset(String key, int seek, int size) {
691
                DwgSectionOffset dso = new DwgSectionOffset(key, seek, size);
692
                dwgSectionOffsets.add(dso);
693
        }
694
        
695
        /**
696
     * Returns the offset of DWG section given by its key 
697
         * 
698
     * @param key Define the DWG section
699
         * @return int Offset of the section in the DWG file
700
         */
701
        public int getDwgSectionOffset(String key) {
702
                int offset = 0;
703
                for (int i=0; i<dwgSectionOffsets.size(); i++) {
704
                        DwgSectionOffset dso = (DwgSectionOffset)dwgSectionOffsets.get(i);
705
                        String ikey = dso.getKey();
706
                        if (key.equals(ikey)) {
707
                                offset = dso.getSeek();
708
                                break;
709
                        }
710
                }
711
                return offset;
712
        }
713
        
714
        /**
715
         * Add a DWG object offset to the dwgObjectOffsets vector
716
         * 
717
         * @param handle Object handle
718
         * @param offset Offset of the object data in the DWG file
719
         */
720
        public void addDwgObjectOffset(int handle, int offset) {
721
                DwgObjectOffset doo = new DwgObjectOffset(handle, offset);
722
                dwgObjectOffsets.add(doo);
723
        }
724
        
725
        /**
726
         * 
727
         * Add a DWG object to the dwgObject vector
728
         * 
729
         * @param dwgObject DWG object
730
         */
731
        public void addDwgObject(DwgObject dwgObject){
732
                //TODO Ver si puedo inicializar las listas especificas
733
                //(IDwgPolyline, etc) aqu? 
734
                dwgObjects.add(dwgObject);
735
                handle_objects.put(new Integer(dwgObject.getHandle()), dwgObject);
736
                /*
737
                 * TODO Quitar todos estos if-then y sustituirlos por un metodo callbackj
738
                 * 
739
                 * 
740
                 * (dwgObject.init(this), y que cada objeto haga lo que tenga que hacer
741
                 * */
742
                if(dwgObject instanceof DwgLayer){
743
                        this.addDwgLayer((DwgLayer) dwgObject);
744
                }
745
                if(dwgObject instanceof IDwgExtrusionable){
746
                        ((IDwgExtrusionable)dwgObject).applyExtrussion();
747
                }
748
                if(dwgObject instanceof IDwgPolyline){
749
                        dwgPolylines.add(dwgObject);
750
                }
751
                if(dwgObject instanceof IDwg3DTestable){
752
                        if(! dwg3DFile){//if its true, we dont check again
753
                                dwg3DFile = ((IDwg3DTestable)dwgObject).has3DData();
754
                        }
755
                }
756
                
757
                if(dwgObject instanceof DwgBlock){
758
                        DwgBlock block = (DwgBlock) dwgObject;
759
                        names_blocks.put(block.getName(), block);
760
                }
761
                
762
                
763
        }
764
        
765
        /**
766
         * Returns dwgObjects from its insertion order (position
767
         * in the dwg file)
768
         * 
769
         * @param index order in the dwg file
770
         * @return position
771
         * */
772
        public DwgObject getDwgObject(int index){
773
                return (DwgObject) dwgObjects.get(index);
774
        }
775
        
776
        public DwgObject getDwgObjectFromHandle(int handle){
777
                return (DwgObject) handle_objects.get(new Integer(handle));
778
        }
779
        
780
        /**
781
         * Add a DWG class to the dwgClasses vector
782
         * 
783
         * @param dwgClass DWG class
784
         */
785
        public void addDwgClass(DwgClass dwgClass){
786
                dwgClasses.add(dwgClass);
787
        }
788
        
789
        public List getDwgClasses(){
790
                return dwgClasses;
791
        }
792
        
793
    /**
794
     * @return Returns the dwgObjectOffsets.
795
     */
796
    public ArrayList getDwgObjectOffsets() {
797
        return dwgObjectOffsets;
798
    }
799
    /**
800
     * @return Returns the dwgObjects.
801
     */
802
    public LinkedList getDwgObjects() {
803
        return dwgObjects;
804
    }
805
    /**
806
     * @return Returns the fileName.
807
     */
808
    public String getFileName() {
809
        return fileName;
810
    }
811
    /**
812
     * @return Returns the dwg3DFile.
813
     */
814
    public boolean isDwg3DFile() {
815
        return dwg3DFile;
816
    }
817
    /**
818
     * @param dwg3DFile The dwg3DFile to set.
819
     */
820
    public void setDwg3DFile(boolean dwg3DFile) {
821
        this.dwg3DFile = dwg3DFile;
822
    }
823
}