Statistics
| Revision:

root / trunk / extensions / extDwg / src / com / iver / cit / gvsig / drivers / dwg / DwgMemoryDriver.java @ 9825

History | View | Annotate | Download (32.9 KB)

1
/*
2
 * Created on 14-abr-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 * DwgMemoryDriver 0.2. Driver del formato DWG para gvSIG
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
/* CVS MESSAGES:
46
*
47
* $Id: DwgMemoryDriver.java 9825 2007-01-18 13:36:42Z azabala $
48
* $Log$
49
* Revision 1.3  2007-01-18 13:36:42  azabala
50
* Refactoring general para evitar dar tantas pasadas en la carga, y para incrementar
51
* la legibilidad del codigo (demasiados if-else-if en vez de usar polimorfismo)
52
*
53
* Revision 1.2  2007/01/12 19:57:44  azabala
54
* *** empty log message ***
55
*
56
* Revision 1.1  2007/01/11 20:31:05  azabala
57
* *** empty log message ***
58
*
59
*
60
*/
61
package com.iver.cit.gvsig.drivers.dwg;
62

    
63
import java.awt.Color;
64
import java.awt.Component;
65
import java.awt.Font;
66
import java.awt.geom.Point2D;
67
import java.awt.geom.Rectangle2D;
68
import java.io.File;
69
import java.io.IOException;
70
import java.util.ArrayList;
71

    
72
import com.hardcode.gdbms.engine.data.DataSourceFactory;
73
import com.hardcode.gdbms.engine.data.driver.DriverException;
74
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
75
import com.hardcode.gdbms.engine.data.edition.DataWare;
76
import com.hardcode.gdbms.engine.values.IntValue;
77
import com.hardcode.gdbms.engine.values.Value;
78
import com.hardcode.gdbms.engine.values.ValueFactory;
79
import com.iver.andami.PluginServices;
80
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
81
import com.iver.cit.gvsig.fmap.core.FPoint2D;
82
import com.iver.cit.gvsig.fmap.core.FPoint3D;
83
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
84
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
85
import com.iver.cit.gvsig.fmap.core.FPolyline3D;
86
import com.iver.cit.gvsig.fmap.core.FShape;
87
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
88
import com.iver.cit.gvsig.fmap.core.IGeometry;
89
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
90
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
91
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
92
import com.iver.cit.gvsig.fmap.drivers.AbstractCadMemoryDriver;
93
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
94
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
95
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
96
import com.iver.cit.gvsig.fmap.rendering.Legend;
97
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
98
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
99
import com.iver.cit.jdwglib.dwg.DwgFile;
100
import com.iver.cit.jdwglib.dwg.DwgObject;
101
import com.iver.cit.jdwglib.dwg.DwgVersionNotSupportedException;
102
import com.iver.cit.jdwglib.dwg.IDwg2FMap;
103
import com.iver.cit.jdwglib.dwg.IDwg3DTestable;
104
import com.iver.cit.jdwglib.dwg.objects.DwgArc;
105
import com.iver.cit.jdwglib.dwg.objects.DwgAttdef;
106
import com.iver.cit.jdwglib.dwg.objects.DwgAttrib;
107
import com.iver.cit.jdwglib.dwg.objects.DwgBlock;
108
import com.iver.cit.jdwglib.dwg.objects.DwgBlockControl;
109
import com.iver.cit.jdwglib.dwg.objects.DwgBlockHeader;
110
import com.iver.cit.jdwglib.dwg.objects.DwgCircle;
111
import com.iver.cit.jdwglib.dwg.objects.DwgEllipse;
112
import com.iver.cit.jdwglib.dwg.objects.DwgEndblk;
113
import com.iver.cit.jdwglib.dwg.objects.DwgInsert;
114
import com.iver.cit.jdwglib.dwg.objects.DwgLine;
115
import com.iver.cit.jdwglib.dwg.objects.DwgLwPolyline;
116
import com.iver.cit.jdwglib.dwg.objects.DwgMText;
117
import com.iver.cit.jdwglib.dwg.objects.DwgPoint;
118
import com.iver.cit.jdwglib.dwg.objects.DwgPolyline2D;
119
import com.iver.cit.jdwglib.dwg.objects.DwgPolyline3D;
120
import com.iver.cit.jdwglib.dwg.objects.DwgSolid;
121
import com.iver.cit.jdwglib.dwg.objects.DwgText;
122
import com.iver.cit.jdwglib.util.AcadColor;
123
import com.iver.cit.jdwglib.util.GisModelCurveCalculator;
124

    
125
/**
126
 * Driver that allows gvSIG to read files in DWG format Using this driver, a
127
 * gvSIG user can manipulate part of the information contained in a DWG file
128
 * This driver load the Dwg file in memory This driver uses jdwglib
129
 * 
130
 * @author jmorell
131
 */
132
public class DwgMemoryDriver extends AbstractCadMemoryDriver implements
133
                VectorialFileDriver, WithDefaultLegend {
134

    
135
        VectorialUniqueValueLegend defaultLegend;
136

    
137
        private File m_Fich;
138

    
139
        private DwgFile dwg;
140

    
141
        private ArrayList dwgObjects;
142

    
143
        private DriverAttributes attr = new DriverAttributes();
144

    
145
        private boolean isInitialized = false;
146

    
147
        /*
148
         * (non-Javadoc)
149
         * 
150
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#open(java.io.File)
151
         */
152
        public void open(File f) throws IOException {
153
                m_Fich = f;
154
        }
155
        
156
        /**
157
         * Allows recovering of the DWG Drawing entity
158
         * associated to the FMap feature whose position 
159
         * is index
160
         * @param index position of the fmap feature whose
161
         * dwg entity is required
162
         * 
163
         * */
164
        public Object getCadSource(int index){
165
                return dwg.getDwgObject(index);
166
        }
167
        /**
168
         * This method load the DWG file in memory. First, it will be necessary to
169
         * create a DwgFile object with the DWG file path as the argument. Second,
170
         * the method read of DwgFile allows to read the objects inside the DWG
171
         * file. Third, it will be necessary to process some DWG objects like Layers
172
         * or Polylines Fourth, applyExtrusions() can change the location of the DWG
173
         * objects through the extrusion parameters. Fifth, the testDwg3D method
174
         * test if this DWG has elevation informacion. Sixth, we can extract the
175
         * objects contained inside the blocks through the blockManagement method.
176
         * And finally, we can read the objects Vector, and convert this objects to
177
         * the FMap object model.
178
         */
179
        public void initialize() throws IOException {
180
                if (isInitialized)
181
                        return;
182
                else
183
                        isInitialized = true;
184
                float heightText = 10;
185

    
186
                attr.setLoadedInMemory(true);
187
                dwg = new DwgFile(m_Fich.getAbsolutePath());
188

    
189
                try {
190
                        dwg.read();
191
                } catch (DwgVersionNotSupportedException e1) {
192
                        String autodeskUrl = "<a href=\"http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=7024151\">"
193
                                        + "http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=7024151</a>";
194
                        throw new IOException(
195
                                        formatString(
196
                                                        "La version (%s) del fichero DWG '%s' no esta soportada por gvSIG.\n%s\n%s\n%s",
197
                                                        new String[] { e1.getDwgVersion().toString(),
198
                                                                        m_Fich.getName(),
199
                                                                        Messages.getText("Probe_to_convert"),
200
                                                                        Messages.getText("Autodesk_converter"),
201
                                                                        autodeskUrl }));
202
                }// catch
203

    
204
                // dwg.initializeLayerTable();
205
                dwg.calculateGisModelDwgPolylines();
206
//                dwg.applyExtrusions();
207
//                dwg.testDwg3D();
208
                dwg.blockManagement();
209
                dwgObjects = dwg.getDwgObjects();
210

    
211
                // Campos de las MemoryLayer:
212
                Value[] auxRow = new Value[10];
213
                ArrayList arrayFields = new ArrayList();
214
                arrayFields.add("ID");
215
                arrayFields.add("FShape");
216
                arrayFields.add("Entity");
217
                arrayFields.add("Layer");
218
                arrayFields.add("Color");
219
                arrayFields.add("Elevation");
220
                arrayFields.add("Thickness");
221
                arrayFields.add("Text");
222
                arrayFields.add("HeightText");
223
                arrayFields.add("RotationText");
224
                getTableModel().setColumnIdentifiers(arrayFields.toArray());
225

    
226
                boolean is3dFile = dwg.isDwg3DFile();
227
                for (int i = 0; i < dwgObjects.size(); i++) {
228
                        DwgObject entity = (DwgObject) dwgObjects.get(i);
229
                        if(entity instanceof IDwg2FMap){
230
                                IDwg2FMap dwgEnt = (IDwg2FMap)entity;
231
                                //TODO Ojo, revisar las fshape a null
232
                                FShape fshape = dwgEnt.toFMapGeometry(is3dFile);
233
                                String fmapStr = dwgEnt.toFMapString(is3dFile);
234
                                //nombre del tipo de geometria fmap
235
                                auxRow[ID_FIELD_FSHAPE] = ValueFactory.createValue(fmapStr);
236
                                //indice del registro
237
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
238
                                //nombre de entidad dwg
239
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(dwgEnt.toString());
240
                                String layerName = dwg.getLayerName(entity);
241
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
242
                                int color = entity.getColor();
243
                                int colorByLayer = dwg.getColorByLayer(entity);
244
                                // TODO: if (color==0) color ByBlock
245
                                if (color == 256)
246
                                        color = colorByLayer;
247
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
248
                                auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0.0);
249
                                auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(0.0);
250
                                auxRow[ID_FIELD_TEXT] = ValueFactory.createNullValue();
251
                                if(entity instanceof IDwg3DTestable){
252
                                        auxRow[ID_FIELD_ELEVATION] = 
253
                                                ValueFactory.createValue(((IDwg3DTestable)entity).getZ());        
254
                                }
255
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
256
                                addShape(fshape, auxRow);
257
                        }//if instanceof
258
                        
259
                
260
                        
261
                        else if (entity instanceof DwgLwPolyline) {// &&
262
                                                                                                                        // !addingToBlock) {
263
                                // System.out.println("Encuentra una Lwpolyline ...");
264
                                Point2D[] pts = ((DwgLwPolyline) entity).getVertices();
265
                                double elev = ((DwgLwPolyline) entity).getElevation();
266
                                if (pts != null) {
267
                                        FPolyline2D lwpline;
268
                                        if (dwg.isDwg3DFile()) {
269
                                                double[][] pline3D = new double[pts.length][3];
270
                                                for (int j = 0; j < pts.length; j++) {
271
                                                        pline3D[j][0] = pts[j].getX();
272
                                                        pline3D[j][1] = pts[j].getY();
273
                                                        pline3D[j][2] = elev;
274
                                                }
275
                                                lwpline = points3DToFPolyline3D(pline3D);
276
                                                auxRow[ID_FIELD_FSHAPE] = ValueFactory
277
                                                                .createValue(new String("FPolyline3D"));
278
                                        } else {
279
                                                lwpline = points2DToFPolyline2D(pts);
280
                                                auxRow[ID_FIELD_FSHAPE] = ValueFactory
281
                                                                .createValue(new String("FPolyline2D"));
282
                                        }
283
                                        auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
284
                                        auxRow[ID_FIELD_ENTITY] = ValueFactory
285
                                                        .createValue(new String("LwPolyline"));
286
                                        String layerName = dwg.getLayerName(entity);
287
                                        auxRow[ID_FIELD_LAYER] = ValueFactory
288
                                                        .createValue(layerName);
289
                                        int color = entity.getColor();
290
                                        int colorByLayer = dwg.getColorByLayer(entity);
291
                                        // if (color==0) color ByBlock
292
                                        if (color == 256)
293
                                                color = colorByLayer;
294
                                        auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
295
                                        auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(elev);
296
                                        auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
297
                                        // Attributes
298
                                        /*
299
                                         * for (int j=0;j<nAtt;j++) { String[] attributes = new
300
                                         * String[2]; attributes =
301
                                         * (String[])featureMaker.getAttributes().get(j);
302
                                         * auxRow[9+j] = ValueFactory.createValue(new
303
                                         * String((String)attributes[1])); if
304
                                         * (!fea.getProp(attributes[0]).equals(attributes[1])) {
305
                                         * auxRow[9+j] = ValueFactory.createValue(new
306
                                         * String(fea.getProp(attributes[0]))); } }
307
                                         */
308
                                        addShape(lwpline, auxRow);
309
                                }
310
                        } 
311
                        
312
                        
313
                        
314
                        
315
                        else if (entity instanceof DwgSolid) {// && !addingToBlock) {
316
                                // System.out.println("Encuentra un Solid ...");
317
                                double[] p1 = ((DwgSolid) entity).getCorner1();
318
                                double[] p2 = ((DwgSolid) entity).getCorner2();
319
                                double[] p3 = ((DwgSolid) entity).getCorner3();
320
                                double[] p4 = ((DwgSolid) entity).getCorner4();
321
                                double elev = ((DwgSolid) entity).getElevation();
322
                                Point2D[] pts = new Point2D[] {
323
                                                new Point2D.Double(p1[0], p1[1]),
324
                                                new Point2D.Double(p2[0], p2[1]),
325
                                                new Point2D.Double(p4[0], p4[1]),
326
                                                new Point2D.Double(p3[0], p3[1]),
327
                                                new Point2D.Double(p1[0], p1[1]) };
328
                                FPolyline2D solid;
329
                                if (dwg.isDwg3DFile()) {
330
                                        double[][] pline3D = new double[pts.length][3];
331
                                        for (int j = 0; j < pts.length; j++) {
332
                                                pline3D[j][0] = pts[j].getX();
333
                                                pline3D[j][1] = pts[j].getY();
334
                                                pline3D[j][2] = elev;
335
                                        }
336
                                        solid = points3DToFPolyline3D(pline3D);
337
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
338
                                                        .createValue(new String("FPolyline3D"));
339
                                } else {
340
                                        solid = points2DToFPolyline2D(pts);
341
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
342
                                                        .createValue(new String("FPolyline2D"));
343
                                }
344
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
345
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(
346
                                                "Solid"));
347
                                String layerName = dwg.getLayerName(entity);
348
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
349
                                int color = entity.getColor();
350
                                int colorByLayer = dwg.getColorByLayer(entity);
351
                                // if (color==0) color ByBlock
352
                                if (color == 256)
353
                                        color = colorByLayer;
354
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
355
                                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(elev);
356
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
357
                                // Attributes
358
                                /*
359
                                 * for (int j=0;j<nAtt;j++) { String[] attributes = new
360
                                 * String[2]; attributes =
361
                                 * (String[])featureMaker.getAttributes().get(j); auxRow[9+j] =
362
                                 * ValueFactory.createValue(new String((String)attributes[1]));
363
                                 * if (!fea.getProp(attributes[0]).equals(attributes[1])) {
364
                                 * auxRow[9+j] = ValueFactory.createValue(new
365
                                 * String(fea.getProp(attributes[0]))); } }
366
                                 */
367
                                addShape(solid, auxRow);
368
                        } 
369
                        
370
                        
371
                        
372
                        
373
                        else if (entity instanceof DwgLine) {// && !addingToBlock) {
374
                                // System.out.println("Encuentra una Line ...");
375
                                double[] p1 = ((DwgLine) entity).getP1();
376
                                Point2D point1 = new Point2D.Double(p1[0], p1[1]);
377
                                double[] p2 = ((DwgLine) entity).getP2();
378
                                Point2D point2 = new Point2D.Double(p2[0], p2[1]);
379
                                Point2D[] lin = new Point2D[] { point1, point2 };
380
                                FPolyline2D line;
381
                                if (dwg.isDwg3DFile() && ((DwgLine) entity).isZflag()) {
382
                                        double[][] lin3D = new double[2][3];
383
                                        lin3D[0][0] = lin[0].getX();
384
                                        lin3D[0][1] = lin[0].getY();
385
                                        lin3D[0][2] = p1[2];
386
                                        lin3D[1][0] = lin[1].getX();
387
                                        lin3D[1][1] = lin[1].getY();
388
                                        lin3D[1][2] = p2[2];
389
                                        line = points3DToFPolyline3D(lin3D);
390
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
391
                                                        .createValue(new String("FPolyline3D"));
392
                                } else if (dwg.isDwg3DFile() && !((DwgLine) entity).isZflag()) {
393
                                        double[][] lin3D = new double[2][3];
394
                                        lin3D[0][0] = lin[0].getX();
395
                                        lin3D[0][1] = lin[0].getY();
396
                                        lin3D[0][2] = 0.0;
397
                                        lin3D[1][0] = lin[1].getX();
398
                                        lin3D[1][1] = lin[1].getY();
399
                                        lin3D[1][2] = 0.0;
400
                                        line = points3DToFPolyline3D(lin3D);
401
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
402
                                                        .createValue(new String("FPolyline3D"));
403
                                } else {
404
                                        line = points2DToFPolyline2D(lin);
405
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
406
                                                        .createValue(new String("FPolyline2D"));
407
                                }
408
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
409
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(
410
                                                "Line"));
411
                                String layerName = dwg.getLayerName(entity);
412
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
413
                                int color = entity.getColor();
414
                                int colorByLayer = dwg.getColorByLayer(entity);
415
                                // if (color==0) color ByBlock
416
                                if (color == 256)
417
                                        color = colorByLayer;
418
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
419
                                if (((DwgLine) entity).isZflag()) {
420
                                        if (p1[2] == p2[2])
421
                                                auxRow[ID_FIELD_ELEVATION] = ValueFactory
422
                                                                .createValue(p1[2]);
423
                                        else
424
                                                auxRow[ID_FIELD_ELEVATION] = ValueFactory
425
                                                                .createValue(0.0);
426
                                } else {
427
                                        auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
428
                                }
429
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
430
                                // Attributes
431
                                /*
432
                                 * for (int j=0;j<nAtt;j++) { String[] attributes = new
433
                                 * String[2]; attributes =
434
                                 * (String[])featureMaker.getAttributes().get(j); auxRow[9+j] =
435
                                 * ValueFactory.createValue(new String((String)attributes[1]));
436
                                 * if (!fea.getProp(attributes[0]).equals(attributes[1])) {
437
                                 * auxRow[9+j] = ValueFactory.createValue(new
438
                                 * String(fea.getProp(attributes[0]))); } }
439
                                 */
440
                                addShape(line, auxRow);
441
                        } 
442
                        
443
                        
444
                        
445
                        
446
                        else if (entity instanceof DwgPoint) {// && !addingToBlock) {
447
                                // System.out.println("Encuentra un Point ...");
448
                                double[] p = ((DwgPoint) entity).getPoint();
449
                                FPoint2D point;
450
                                if (dwg.isDwg3DFile()) {
451
                                        point = new FPoint3D(p[0], p[1], p[2]);
452
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
453
                                                        .createValue(new String("FPoint3D"));
454
                                } else {
455
                                        point = new FPoint2D(p[0], p[1]);
456
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
457
                                                        .createValue(new String("FPoint2D"));
458
                                }
459
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
460
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(
461
                                                "Point"));
462
                                String layerName = dwg.getLayerName(entity);
463
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
464
                                int color = entity.getColor();
465
                                int colorByLayer = dwg.getColorByLayer(entity);
466
                                // if (color==0) color ByBlock
467
                                if (color == 256)
468
                                        color = colorByLayer;
469
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
470
                                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(p[2]);
471
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
472
                                // Attributes
473
                                /*
474
                                 * for (int j=0;j<nAtt;j++) { String[] attributes = new
475
                                 * String[2]; attributes =
476
                                 * (String[])featureMaker.getAttributes().get(j); auxRow[9+j] =
477
                                 * ValueFactory.createValue(new String((String)attributes[1]));
478
                                 * if (!fea.getProp(attributes[0]).equals(attributes[1])) {
479
                                 * auxRow[9+j] = ValueFactory.createValue(new
480
                                 * String(fea.getProp(attributes[0]))); } }
481
                                 */
482
                                addShape(point, auxRow);
483
                        } 
484
                        
485
                        
486
                        
487
                        
488
                        
489
                        else if (entity instanceof DwgInsert) {// && !addingToBlock) {
490
                                // System.out.println("Encuentra un Insert ...");
491
                                double[] p = ((DwgInsert) entity).getInsertionPoint();
492
                                Point2D point = new Point2D.Double(p[0], p[1]);
493
                                FPoint2D fPoint;
494
                                /*
495
                                 * double[] scale = ((DwgInsert)entity).getScale(); double rot =
496
                                 * ((DwgInsert)entity).getRotation(); int blockHandle =
497
                                 * ((DwgInsert)entity).getBlockHeaderHandle();
498
                                 */
499
                                // manageInsert(dwgObjects, point, scale, rot, blockHandle, i,
500
                                // auxRow);
501
                                if (dwg.isDwg3DFile()) {
502
                                        fPoint = new FPoint3D(p[0], p[1], p[2]);
503
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
504
                                                        .createValue(new String("FPoint3D"));
505
                                } else {
506
                                        fPoint = new FPoint2D(p[0], p[1]);
507
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
508
                                                        .createValue(new String("FPoint2D"));
509
                                }
510
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
511
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(
512
                                                "Insert"));
513
                                String layerName = dwg.getLayerName(entity);
514
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
515
                                int color = entity.getColor();
516
                                int colorByLayer = dwg.getColorByLayer(entity);
517
                                // if (color==0) color ByBlock
518
                                if (color == 256)
519
                                        color = colorByLayer;
520
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
521
                                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(p[2]);
522
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
523
                                // Attributes
524
                                /*
525
                                 * for (int j=0;j<nAtt;j++) { String[] attributes = new
526
                                 * String[2]; attributes =
527
                                 * (String[])featureMaker.getAttributes().get(j); auxRow[9+j] =
528
                                 * ValueFactory.createValue(new String((String)attributes[1]));
529
                                 * if (!fea.getProp(attributes[0]).equals(attributes[1])) {
530
                                 * auxRow[9+j] = ValueFactory.createValue(new
531
                                 * String(fea.getProp(attributes[0]))); } }
532
                                 */
533
                                addShape(fPoint, auxRow);
534
                        } 
535
                        
536
                        
537
                        
538
                        
539
                        
540
                        else if (entity instanceof DwgMText) {// ) && !addingToBlock) {
541
                                // System.out.println("Encuentra un MText ...");
542
                                double[] p = ((DwgMText) entity).getInsertionPoint();
543
                                FPoint2D fPoint;
544
                                if (dwg.isDwg3DFile()) {
545
                                        fPoint = new FPoint3D(p[0], p[1], p[2]);
546
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
547
                                                        .createValue(new String("FPoint3D"));
548
                                } else {
549
                                        fPoint = new FPoint2D(p[0], p[1]);
550
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
551
                                                        .createValue(new String("FPoint2D"));
552
                                }
553
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
554
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(
555
                                                "MText"));
556
                                String layerName = dwg.getLayerName(entity);
557
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
558
                                int color = entity.getColor();
559
                                int colorByLayer = dwg.getColorByLayer(entity);
560
                                // if (color==0) color ByBlock
561
                                if (color == 256)
562
                                        color = colorByLayer;
563
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
564
                                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(p[2]);
565
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
566
                                // Attributes
567
                                /*
568
                                 * for (int j=0;j<nAtt;j++) { String[] attributes = new
569
                                 * String[2]; attributes =
570
                                 * (String[])featureMaker.getAttributes().get(j); auxRow[9+j] =
571
                                 * ValueFactory.createValue(new String((String)attributes[1]));
572
                                 * if (!fea.getProp(attributes[0]).equals(attributes[1])) {
573
                                 * auxRow[9+j] = ValueFactory.createValue(new
574
                                 * String(fea.getProp(attributes[0]))); } }
575
                                 */
576
                                addShape(fPoint, auxRow);
577
                        } 
578
                        
579
                        
580
                        else if (entity instanceof DwgAttrib) {// && !addingToBlock) {
581
                                // System.out.println("Encuentra un Attrib ...");
582
                        } 
583
                        
584
                        
585
                        
586
                        
587
                        
588
                        
589
                        
590
                        else if (entity instanceof DwgText) {// && !addingToBlock) {
591
                                // System.out.println("Encuentra un Text ...");
592
                                Point2D p = ((DwgText) entity).getInsertionPoint();
593
                                int dflag = ((DwgText) entity).getDataFlag();
594
                                double elev = 0.0;
595
                                if ((dflag & 0x1) == 0)
596
                                        elev = ((DwgText) entity).getElevation();
597
                                FPoint2D point;
598
                                if (dwg.isDwg3DFile()) {
599
                                        point = new FPoint3D(p.getX(), p.getY(), elev);
600
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
601
                                                        .createValue(new String("FPoint3D"));
602
                                } else {
603
                                        point = new FPoint2D(p.getX(), p.getY());
604
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
605
                                                        .createValue(new String("FPoint2D"));
606
                                }
607
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
608
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(
609
                                                "Text"));
610
                                String layerName = dwg.getLayerName(entity);
611
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
612
                                int color = entity.getColor();
613
                                int colorByLayer = dwg.getColorByLayer(entity);
614
                                // if (color==0) color ByBlock
615
                                if (color == 256)
616
                                        color = colorByLayer;
617
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
618
                                String text = ((DwgText) entity).getText();
619
                                auxRow[ID_FIELD_TEXT] = ValueFactory.createValue(new String(
620
                                                text));
621
                                heightText = (float) ((DwgText) entity).getHeight();
622
                                auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory
623
                                                .createValue(heightText);
624
                                if ((dflag & 0x8) == 0) {
625
                                        double textRot = ((DwgText) entity).getRotationAngle();
626
                                        auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory
627
                                                        .createValue(Math.toDegrees(textRot));
628
                                } else {
629
                                        auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory
630
                                                        .createValue(0.0);
631
                                }
632
                                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(elev);
633
                                double thickness = ((DwgText) entity).getThickness();
634
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory
635
                                                .createValue(thickness);
636
                                // Attributes
637
                                /*
638
                                 * for (int j=0;j<nAtt;j++) { String[] attributes = new
639
                                 * String[2]; attributes =
640
                                 * (String[])featureMaker.getAttributes().get(j); auxRow[9+j] =
641
                                 * ValueFactory.createValue(new String((String)attributes[1]));
642
                                 * if (!fea.getProp(attributes[0]).equals(attributes[1])) {
643
                                 * auxRow[9+j] = ValueFactory.createValue(new
644
                                 * String(fea.getProp(attributes[0]))); } }
645
                                 */
646
                                addShape(point, auxRow);
647
                        } 
648
                        
649
                        
650
                        
651
                        
652
                        
653
                        
654
                        else if (entity instanceof DwgBlock) {
655
                                // System.out.println("Encuentra un Block ...");
656
                                // addingToBlock = true;
657
                                String blockName = ((DwgBlock) entity).getName();
658
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
659
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(
660
                                                "Block"));
661
                                String layerName = dwg.getLayerName(entity);
662
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
663
                                int color = entity.getColor();
664
                                int colorByLayer = dwg.getColorByLayer(entity);
665
                                // if (color==0) color ByBlock
666
                                if (color == 256)
667
                                        color = colorByLayer;
668
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
669
                                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
670
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
671
                                // Attributes
672
                                /*
673
                                 * for (int j=0;j<nAtt;j++) { String[] attributes = new
674
                                 * String[2]; attributes =
675
                                 * (String[])featureMaker.getAttributes().get(j); auxRow[9+j] =
676
                                 * ValueFactory.createValue(new String((String)attributes[1]));
677
                                 * if (!fea.getProp(attributes[0]).equals(attributes[1])) {
678
                                 * auxRow[9+j] = ValueFactory.createValue(new
679
                                 * String(fea.getProp(attributes[0]))); } }
680
                                 */
681
                                // addShape(point, auxRow);
682
                        } 
683
                        
684
                        
685
                        
686
                        
687
                        
688
                        
689
                        
690
                        
691
                        
692
                        
693
                        
694
                        else if (entity instanceof DwgEndblk) {
695
                                // System.out.println("Encuentra un Endblk ...");
696
                                // addingToBlock = false;
697
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
698
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(
699
                                                "EndBlk"));
700
                                String layerName = dwg.getLayerName(entity);
701
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
702
                                int color = entity.getColor();
703
                                int colorByLayer = dwg.getColorByLayer(entity);
704
                                // if (color==0) color ByBlock
705
                                if (color == 256)
706
                                        color = colorByLayer;
707
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
708
                                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
709
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
710
                                // Attributes
711
                                /*
712
                                 * for (int j=0;j<nAtt;j++) { String[] attributes = new
713
                                 * String[2]; attributes =
714
                                 * (String[])featureMaker.getAttributes().get(j); auxRow[9+j] =
715
                                 * ValueFactory.createValue(new String((String)attributes[1]));
716
                                 * if (!fea.getProp(attributes[0]).equals(attributes[1])) {
717
                                 * auxRow[9+j] = ValueFactory.createValue(new
718
                                 * String(fea.getProp(attributes[0]))); } }
719
                                 */
720
                                // addShape(point, auxRow);
721
                        } 
722
                        
723
                        
724
                        
725
                        
726
                        
727
                        
728
                        
729
                        
730
                        
731
                        
732
                        
733
                        
734
                        
735
                        
736
                        else if (entity instanceof DwgBlockControl) {
737
                                // System.out.println("Encuentra un Block Control Object ...");
738
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
739
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(
740
                                                "Block Control Object"));
741
                                String layerName = dwg.getLayerName(entity);
742
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
743
                                int color = entity.getColor();
744
                                int colorByLayer = dwg.getColorByLayer(entity);
745
                                // if (color==0) color ByBlock
746
                                if (color == 256)
747
                                        color = colorByLayer;
748
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
749
                                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
750
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
751
                                // Attributes
752
                                /*
753
                                 * for (int j=0;j<nAtt;j++) { String[] attributes = new
754
                                 * String[2]; attributes =
755
                                 * (String[])featureMaker.getAttributes().get(j); auxRow[9+j] =
756
                                 * ValueFactory.createValue(new String((String)attributes[1]));
757
                                 * if (!fea.getProp(attributes[0]).equals(attributes[1])) {
758
                                 * auxRow[9+j] = ValueFactory.createValue(new
759
                                 * String(fea.getProp(attributes[0]))); } }
760
                                 */
761
                                // addShape(point, auxRow);
762
                        } 
763
                        
764
                        
765
                        
766
                        
767
                        
768
                        
769
                        
770
                        
771
                        
772
                        
773
                        
774
                        else if (entity instanceof DwgBlockHeader) {
775
                                // System.out.println("Encuentra un Block Header ...");
776
                                // addingToBlock = true;
777
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
778
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(
779
                                                "Block Header"));
780
                                String layerName = dwg.getLayerName(entity);
781
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
782
                                int color = entity.getColor();
783
                                int colorByLayer = dwg.getColorByLayer(entity);
784
                                // if (color==0) color ByBlock
785
                                if (color == 256)
786
                                        color = colorByLayer;
787
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
788
                                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
789
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
790
                                // Attributes
791
                                /*
792
                                 * for (int j=0;j<nAtt;j++) { String[] attributes = new
793
                                 * String[2]; attributes =
794
                                 * (String[])featureMaker.getAttributes().get(j); auxRow[9+j] =
795
                                 * ValueFactory.createValue(new String((String)attributes[1]));
796
                                 * if (!fea.getProp(attributes[0]).equals(attributes[1])) {
797
                                 * auxRow[9+j] = ValueFactory.createValue(new
798
                                 * String(fea.getProp(attributes[0]))); } }
799
                                 */
800
                                // addShape(point, auxRow);
801
                        } 
802
                        
803
                        
804
                        
805
                        
806
                        else if (entity instanceof DwgAttdef) {// && !addingToBlock) {
807
                                // System.out.println("Encuentra un Attdef ...");
808
                        } 
809
                        
810
                        
811
                        
812
                        
813
                        else {
814
                                // System.out.println("Detectado dwgObject pendiente de
815
                                // implementar");
816
                        }
817
                }
818
                
819
                
820
                
821
                
822

    
823
                defaultLegend = LegendFactory
824
                                .createVectorialUniqueValueLegend(getShapeType());
825
                defaultLegend.setFieldName("Color");
826
                defaultLegend.setLabelField("Text");
827

    
828
                FSymbol myDefaultSymbol = new FSymbol(getShapeType());
829
                myDefaultSymbol.setShapeVisible(false);
830
                myDefaultSymbol.setFontSizeInPixels(false);
831
                myDefaultSymbol.setFont(new Font("SansSerif", Font.PLAIN, 9));
832
                myDefaultSymbol.setFontColor(Color.BLACK);
833
                myDefaultSymbol.setFontSize(heightText);
834
                myDefaultSymbol.setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
835
                myDefaultSymbol.setSize(3);
836
                myDefaultSymbol.setSizeInPixels(true);
837

    
838
                defaultLegend.setDefaultSymbol(myDefaultSymbol);
839
                defaultLegend.setLabelHeightField("HeightText");
840
                defaultLegend.setLabelRotationField("RotationText");
841

    
842
                ObjectDriver rs = this;
843
                IntValue clave;
844
                FSymbol theSymbol = null;
845

    
846
                try {
847
                        for (long j = 0; j < rs.getRowCount(); j++) {
848
                                clave = (IntValue) rs.getFieldValue(j, ID_FIELD_COLOR);
849
                                if (defaultLegend.getSymbolByValue(clave) == null) {
850
                                        theSymbol = new FSymbol(getShapeType());
851
                                        theSymbol.setDescription(clave.toString());
852
                                        theSymbol.setColor(AcadColor.getColor(clave.getValue()));
853
                                        // theSymbol.setFill(fillProv);
854
                                        // 050202, jmorell: Asigna los colores de Autocad a los
855
                                        // bordes
856
                                        // de los pol?gonos.
857
                                        theSymbol.setOutlineColor(AcadColor.getColor(clave
858
                                                        .getValue()));
859

    
860
                                        theSymbol.setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
861
                                        theSymbol.setSize(3);
862
                                        theSymbol.setSizeInPixels(true);
863

    
864
                                        defaultLegend.addSymbol(clave, theSymbol);
865
                                }
866
                        } // for
867
                } catch (DriverException e) {
868
                        e.printStackTrace();
869
                        throw new IOException(
870
                                        "Error al poner la leyenda por defecto en el Dwg");
871
                }
872
        }
873

    
874
        public String getFileVersion() {
875
                return dwg.getDwgVersion();
876
        }
877

    
878
        private String formatString(String fmt, String[] params) {
879
                String ret = fmt;
880
                for (int i = 0; i < params.length; i++) {
881
                        ret = ret.replaceFirst("%s", params[i]);
882
                }
883
                return ret;
884
        }
885

    
886
        
887
        
888

    
889
        
890

    
891
        /**
892
         * Method that changes a Point2D array to a FPolygon2D. Is useful to convert
893
         * a polygon given by it points to a FPolygon2D, a polygon in the FMap model
894
         * object
895
         * 
896
         * @param pts
897
         *            Array of Point2D that defines the polygon that will be
898
         *            converted in a FPolygon2D
899
         * @return FPolygon2D This FPolygon2D is build using the array of Point2D
900
         *         that is the argument of the method
901
         */
902
        private FPolygon2D points2DToFPolygon2D(Point2D[] pts) {
903
                GeneralPathX genPathX = new GeneralPathX();
904
                genPathX.moveTo(pts[0].getX(), pts[0].getY());
905
                for (int i = 1; i < pts.length; i++) {
906
                        genPathX.lineTo(pts[i].getX(), pts[i].getY());
907
                }
908
                genPathX.closePath();
909
                return new FPolygon2D(genPathX);
910
        }
911

    
912
        /*
913
         * (non-Javadoc)
914
         * 
915
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#accept(java.io.File)
916
         */
917
        public boolean accept(File f) {
918
                return f.getName().toUpperCase().endsWith("DWG");
919
        }
920

    
921
        /*
922
         * (non-Javadoc)
923
         * 
924
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getShapeType()
925
         */
926
        public int getShapeType() {
927
                return FShape.MULTI;
928
        }
929

    
930
        /*
931
         * (non-Javadoc)
932
         * 
933
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getName()
934
         */
935
        public String getName() {
936
                return "gvSIG DWG Memory Driver";
937
        }
938

    
939
        /*
940
         * (non-Javadoc)
941
         * 
942
         * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
943
         */
944
        public Legend getDefaultLegend() {
945
                return defaultLegend;
946
        }
947

    
948
        /*
949
         * (non-Javadoc)
950
         * 
951
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
952
         */
953
        public DriverAttributes getDriverAttributes() {
954
                return attr;
955
        }
956

    
957
        /*
958
         * (non-Javadoc)
959
         * 
960
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
961
         */
962
        public int[] getPrimaryKeys() throws DriverException {
963
                // TODO Auto-generated method stub
964
                return null;
965
        }
966

    
967
        /*
968
         * (non-Javadoc)
969
         * 
970
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
971
         */
972
        public void write(DataWare arg0) throws DriverException {
973
                // TODO Auto-generated method stub
974

    
975
        }
976

    
977
        public void setDataSourceFactory(DataSourceFactory arg0) {
978
                // TODO Auto-generated method stub
979

    
980
        }
981

    
982
        public void close() throws IOException {
983
                // TODO Auto-generated method stub
984

    
985
        }
986

    
987
        public File getFile() {
988
                return m_Fich;
989
        }
990

    
991
        public boolean isWritable() {
992
                return m_Fich.canWrite();
993
        }
994
}