Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extDwg / src / com / iver / cit / gvsig / drivers / dwg / DwgMemoryDriver.java @ 13893

History | View | Annotate | Download (22.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 13893 2007-09-20 07:15:33Z jaume $
48
 * $Log$
49
 * Revision 1.18  2007-09-20 07:15:33  jaume
50
 * ReadExpansionFileException removed from this context
51
 *
52
 * Revision 1.17  2007/07/18 07:07:44  caballero
53
 * id integer
54
 *
55
 * Revision 1.16  2007/06/27 15:20:37  azabala
56
 * changes in open method to allow reusing an instance to load multiple files
57
 *
58
 * Revision 1.15  2007/06/22 10:50:16  caballero
59
 * getFieldType
60
 *
61
 * Revision 1.14  2007/06/11 12:50:24  jaume
62
 * refactored to use ISymbol
63
 *
64
 * Revision 1.13  2007/05/28 14:16:47  jaume
65
 * *** empty log message ***
66
 *
67
 * Revision 1.12  2007/05/17 09:43:35  jaume
68
 * *** empty log message ***
69
 *
70
 * Revision 1.11  2007/04/04 17:49:06  azabala
71
 * Dependencies of libDwg with libFMap removed
72
 *
73
 * Revision 1.10  2007/03/22 19:56:12  azabala
74
 * changes in exceptinos api
75
 *
76
 * Revision 1.9  2007/03/20 20:21:19  azabala
77
 * added roi filtering, reordering of code
78
 *
79
 * Revision 1.7  2007/02/27 17:18:03  azabala
80
 * solved bug with text orientation
81
 *
82
 * Revision 1.6  2007/02/14 16:17:22  azabala
83
 * *** empty log message ***
84
 *
85
 * Revision 1.5  2007/01/20 18:30:37  azabala
86
 * refactoring of blocks
87
 *
88
 * Revision 1.4  2007/01/18 19:59:10  azabala
89
 * refactoring to optimize and simplify the code
90
 *
91
 * Revision 1.3  2007/01/18 13:36:42  azabala
92
 * Refactoring general para evitar dar tantas pasadas en la carga, y para incrementar
93
 * la legibilidad del codigo (demasiados if-else-if en vez de usar polimorfismo)
94
 *
95
 * Revision 1.2  2007/01/12 19:57:44  azabala
96
 * *** empty log message ***
97
 *
98
 * Revision 1.1  2007/01/11 20:31:05  azabala
99
 * *** empty log message ***
100
 *
101
 *
102
 */
103
package com.iver.cit.gvsig.drivers.dwg;
104

    
105
import java.awt.Color;
106
import java.awt.geom.Rectangle2D;
107
import java.io.File;
108
import java.io.IOException;
109
import java.sql.Types;
110
import java.util.ArrayList;
111
import java.util.HashMap;
112
import java.util.List;
113
import java.util.Map;
114

    
115
import javax.swing.table.DefaultTableModel;
116

    
117
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
118
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
119
import com.hardcode.gdbms.driver.exceptions.UnsupportedVersionDriverException;
120
import com.hardcode.gdbms.engine.data.DataSourceFactory;
121
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
122
import com.hardcode.gdbms.engine.data.edition.DataWare;
123
import com.hardcode.gdbms.engine.values.IntValue;
124
import com.hardcode.gdbms.engine.values.Value;
125
import com.hardcode.gdbms.engine.values.ValueFactory;
126
import com.iver.cit.gvsig.drivers.dwg.fmapconverters.DwgArc2FMapConverter;
127
import com.iver.cit.gvsig.drivers.dwg.fmapconverters.DwgCircle2FMapConverter;
128
import com.iver.cit.gvsig.drivers.dwg.fmapconverters.DwgEllipse2FMapConverter;
129
import com.iver.cit.gvsig.drivers.dwg.fmapconverters.DwgLine2FMapConverter;
130
import com.iver.cit.gvsig.drivers.dwg.fmapconverters.DwgLwPline2FMapConverter;
131
import com.iver.cit.gvsig.drivers.dwg.fmapconverters.DwgMText2FMapConverter;
132
import com.iver.cit.gvsig.drivers.dwg.fmapconverters.DwgPFacePline2FMapConverter;
133
import com.iver.cit.gvsig.drivers.dwg.fmapconverters.DwgPoint2FMapConverter;
134
import com.iver.cit.gvsig.drivers.dwg.fmapconverters.DwgPolyline2D2FMapConverter;
135
import com.iver.cit.gvsig.drivers.dwg.fmapconverters.DwgPolyline3D2FMapConverter;
136
import com.iver.cit.gvsig.drivers.dwg.fmapconverters.DwgSolid2FMapConverter;
137
import com.iver.cit.gvsig.drivers.dwg.fmapconverters.DwgText2FMapConverter;
138
import com.iver.cit.gvsig.drivers.dwg.fmapconverters.IDwg2FMap;
139
import com.iver.cit.gvsig.fmap.core.FShape;
140
import com.iver.cit.gvsig.fmap.core.IGeometry;
141
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
142
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
143
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
144
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
145
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
146
import com.iver.cit.gvsig.fmap.drivers.AbstractCadMemoryDriver;
147
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
148
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
149
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
150
import com.iver.cit.gvsig.fmap.rendering.ILegend;
151
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
152
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
153
import com.iver.cit.gvsig.fmap.rendering.styling.AttrInTableLabeling;
154
import com.iver.cit.gvsig.fmap.rendering.styling.ILabelingStrategy;
155
import com.iver.cit.jdwglib.dwg.DwgFile;
156
import com.iver.cit.jdwglib.dwg.DwgObject;
157
import com.iver.cit.jdwglib.dwg.DwgVersionNotSupportedException;
158
import com.iver.cit.jdwglib.dwg.IDwg3DTestable;
159
import com.iver.cit.jdwglib.dwg.objects.DwgArc;
160
import com.iver.cit.jdwglib.dwg.objects.DwgCircle;
161
import com.iver.cit.jdwglib.dwg.objects.DwgEllipse;
162
import com.iver.cit.jdwglib.dwg.objects.DwgLine;
163
import com.iver.cit.jdwglib.dwg.objects.DwgLwPolyline;
164
import com.iver.cit.jdwglib.dwg.objects.DwgMText;
165
import com.iver.cit.jdwglib.dwg.objects.DwgPFacePolyline;
166
import com.iver.cit.jdwglib.dwg.objects.DwgPoint;
167
import com.iver.cit.jdwglib.dwg.objects.DwgPolyline2D;
168
import com.iver.cit.jdwglib.dwg.objects.DwgPolyline3D;
169
import com.iver.cit.jdwglib.dwg.objects.DwgSolid;
170
import com.iver.cit.jdwglib.dwg.objects.DwgText;
171
import com.iver.cit.jdwglib.util.AcadColor;
172

    
173
/**
174
 * Driver that allows gvSIG to read files in DWG format Using this driver, a
175
 * gvSIG user can manipulate part of the information contained in a DWG file
176
 * This driver load the Dwg file in memory This driver uses jdwglib
177
 *
178
 * @author jmorell
179
 * @author azabala
180
 */
181
public class DwgMemoryDriver extends AbstractCadMemoryDriver implements
182
                VectorialFileDriver, WithDefaultLegend {
183

    
184
        static Map convertersRegistry = new HashMap();
185

    
186
        /*
187
         * this process of registration of the 2FMapConverters could be
188
         * automathic (by java reflection), but the number of dwg entities
189
         * is finited, and some converters could exists but dont work well
190
         * (so thats the reason we mantain a "manual" registering process.
191
         * */
192
        static{
193
                registerFMapConverter(DwgArc.class, new DwgArc2FMapConverter());
194
                registerFMapConverter(DwgCircle.class, new DwgCircle2FMapConverter());
195
                registerFMapConverter(DwgEllipse.class, new DwgEllipse2FMapConverter());
196
                registerFMapConverter(DwgLine.class, new DwgLine2FMapConverter());
197
                registerFMapConverter(DwgLwPolyline.class, new DwgLwPline2FMapConverter());
198
                registerFMapConverter(DwgMText.class, new DwgMText2FMapConverter());
199
                registerFMapConverter(DwgPFacePolyline.class, new DwgPFacePline2FMapConverter());
200
                registerFMapConverter(DwgPoint.class, new DwgPoint2FMapConverter());
201
                registerFMapConverter(DwgPolyline2D.class, new DwgPolyline2D2FMapConverter());
202
                registerFMapConverter(DwgPolyline3D.class, new DwgPolyline3D2FMapConverter());
203
                registerFMapConverter(DwgSolid.class, new DwgSolid2FMapConverter());
204
                registerFMapConverter(DwgText.class, new DwgText2FMapConverter());
205
                try {
206
                        Class.forName("com.iver.cit.gvsig.drivers.dwg.fmapconverters.FMapUtil");
207
                } catch (ClassNotFoundException e) {
208
                        e.printStackTrace();
209
                }
210
        }
211

    
212
        public static void registerFMapConverter (Class clazz, IDwg2FMap converter){
213
                if(! DwgObject.class.isAssignableFrom(clazz))
214
                        return;
215
                convertersRegistry.put(clazz, converter);
216
        }
217

    
218
        public static IDwg2FMap getConverter(Class clazz){
219
                if(! DwgObject.class.isAssignableFrom(clazz))
220
                        return null;
221
                return (IDwg2FMap) convertersRegistry.get(clazz);
222
        }
223

    
224
        VectorialUniqueValueLegend defaultLegend;
225
        private File m_Fich;
226
        private String fileVersion;
227
        private DriverAttributes attr = new DriverAttributes();
228
        private boolean isInitialized = false;
229
        float heightText = 10;
230

    
231
        //new WithDefaultLegend API
232
        private AttrInTableLabeling labeling;
233

    
234
        private boolean debug = false;
235
        /**
236
         * entities of the dwg file (once applied many transformation,
237
         * including block insertion) that are IDwgToFMap.
238
         * */
239
        ArrayList entities = new ArrayList();
240

    
241
        /**
242
         * Saves an original DWG entity associated to a FMap entity
243
         * by their index.
244
         * Only available in debug mode (if debug is false, doesnt save anything)
245
         * */
246
        private void saveEntity(DwgObject entity) {
247
                if(debug)
248
                        entities.add(entity);
249
        }
250

    
251
        public DwgObject getEntity(int i){
252
                if(debug)
253
                        return (DwgObject) entities.get(i);
254
                else
255
                        return null;
256
        }
257

    
258
        /*
259
         * (non-Javadoc)
260
         *
261
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#open(java.io.File)
262
         */
263
        public void open(File f) {
264
                m_Fich = f;
265
                isInitialized = false;
266
        }
267

    
268
        /**
269
         * Allows recovering of the DWG Drawing entity associated to the FMap
270
         * feature whose position is index.
271
         * If debug mode is false, it always returns null.
272
         *
273
         * @param index
274
         *            position of the fmap feature whose dwg entity is required
275
         *
276
         */
277
        public Object getCadSource(int index) {
278
                if(debug)
279
                        return getEntity(index);
280
                else
281
                        return null;
282
        }
283

    
284
        /**
285
         * This method load the DWG file in memory. First, it will be necessary to
286
         * create a DwgFile object with the DWG file path as the argument. Second,
287
         * the method read of DwgFile allows to read the objects inside the DWG
288
         * file. Third, it will be necessary to process some DWG objects like Layers
289
         * or Polylines Fourth, applyExtrusions() can change the location of the DWG
290
         * objects through the extrusion parameters. Fifth, the testDwg3D method
291
         * test if this DWG has elevation informacion. Sixth, we can extract the
292
         * objects contained inside the blocks through the blockManagement method.
293
         * And finally, we can read the objects Vector, and convert this objects to
294
         * the FMap object model.
295
         */
296
        public void initialize() throws InitializeDriverException {
297
                if (isInitialized)
298
                        return;
299
                else
300
                        isInitialized = true;
301
                attr.setLoadedInMemory(true);
302
                DwgFile dwg = new DwgFile(m_Fich.getAbsolutePath());
303

    
304
                try {
305
                        dwg.read();
306
                } catch (DwgVersionNotSupportedException e1) {
307

    
308
                        String autodeskUrl = "<a href=\"http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=7024151\">"
309
                                        + "http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=7024151</a>";
310
//                        throw new InitializeDriverException(
311
//                                        formatString(
312
//                                                        "La version (%s) del fichero DWG '%s' no esta soportada por gvSIG.\n%s\n%s\n%s",
313
//                                                        new String[] { e1.getDwgVersion().toString(),
314
//                                                                        m_Fich.getName(),
315
//                                                                        Messages.getText("Probe_to_convert"),
316
//                                                                        Messages.getText("Autodesk_converter"),
317
//                                                                        autodeskUrl }));
318
                        String description=formatString(
319
                                        "La version (%s) del fichero DWG '%s' no esta soportada por gvSIG.\n%s\n%s\n%s",
320
                                        new String[] { e1.getDwgVersion().toString(),
321
                                                        m_Fich.getName(),
322
                                                        Messages.getText("Probe_to_convert"),
323
                                                        Messages.getText("Autodesk_converter"),
324
                                                        autodeskUrl });
325
                        throw new UnsupportedVersionDriverException(getName(),e1,description);
326
                } catch (IOException e) {
327
                        throw new InitializeDriverException(getName(),e);
328
                }
329

    
330
                fileVersion = dwg.getDwgVersion();
331
                dwg.calculateGisModelDwgPolylines();
332
                dwg.blockManagement2();
333
                List dwgObjects = dwg.getDwgObjects();
334

    
335
                //Each dwg file has two headers vars, EXTMIN and EXTMAX
336
                //that marks bounds of the file.
337
                //Also, it could have spureus entities (deleted objects
338
                //dont removed from the DWG database)
339
                double[] extMin = (double[]) dwg.getHeader("MSPACE_EXTMIN");
340
                double[] extMax = (double[]) dwg.getHeader("MSPACE_EXTMAX");
341
                if(extMin != null && extMax != null){
342
                        if(extMin.length >= 2 && extMax.length >= 2){
343
                                double xmin = extMin[0];
344
                                double ymin = extMin[1];
345
                                double xmax = extMax[0];
346
                                double ymax = extMax[1];
347
                                Rectangle2D roi = new
348
                                        Rectangle2D.Double(xmin-100, ymin-100,
349
                                                        (xmax-xmin)+100, (ymax-ymin)+100);
350
                                addRegionOfInterest(roi);
351
                        }
352
                }
353
                // Campos de las MemoryLayer:
354
                Value[] auxRow = new Value[10];
355
                ArrayList arrayFields = new ArrayList();
356
                arrayFields.add("ID");
357
                arrayFields.add("FShape");
358
                arrayFields.add("Entity");
359
                arrayFields.add("Layer");
360
                arrayFields.add("Color");
361
                arrayFields.add("Elevation");
362
                arrayFields.add("Thickness");
363
                arrayFields.add("Text");
364
                arrayFields.add("HeightText");
365
                arrayFields.add("RotationText");
366
                getTableModel().setColumnIdentifiers(arrayFields.toArray());
367

    
368
                labeling = new AttrInTableLabeling();
369
                ((AttrInTableLabeling) labeling).setTextFieldId(arrayFields.indexOf("Text"));
370
                ((AttrInTableLabeling) labeling).setRotationFieldId(arrayFields.indexOf("RotationText"));
371
                ((AttrInTableLabeling) labeling).setHeightFieldId(arrayFields.indexOf("HeightText"));
372

    
373
                boolean is3dFile = dwg.isDwg3DFile();
374
                try {
375
                        for (int i = 0; i < dwgObjects.size(); i++) {
376
                                DwgObject entity = (DwgObject) dwgObjects.get(i);
377
                                Class clazz = entity.getClass();
378
                                IDwg2FMap converter = getConverter(clazz);
379
                                try {
380
                                        if(converter != null){
381
                                                IGeometry geometry = converter.toFMapGeometry(entity, is3dFile);
382
                                                if (geometry == null)
383
                                                        continue;
384
                                                //we check for Region of Interest of the CAD file
385
                                                if(! checkRois(geometry))
386
                                                        continue;
387

    
388
                                                String fmapStr = converter.toFMapString(is3dFile);
389
                                                // nombre del tipo de geometria fmap
390
                                                auxRow[ID_FIELD_FSHAPE] = ValueFactory.createValue(fmapStr);
391

    
392
                                                // indice del registro
393
                                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
394

    
395
                                                // nombre de entidad dwg
396
                                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(entity.toString());
397
                                                String layerName = dwg.getLayerName(entity);
398
                                                auxRow[ID_FIELD_LAYER] = ValueFactory
399
                                                                .createValue(layerName);
400
                                                int colorByLayer = dwg.getColorByLayer(entity);
401

    
402
                                                int color = entity.getColor();
403
                                                if(color < 0)
404
                                                        color = Math.abs(color);
405
                                                if(color > 255)
406
                                                        color = colorByLayer;
407
                                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
408

    
409
                                                auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0.0);
410
                                                auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory
411
                                                                .createValue(0.0);
412
                                                auxRow[ID_FIELD_TEXT] = ValueFactory.createNullValue();
413
                                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
414

    
415
                                                if (entity instanceof IDwg3DTestable) {
416
                                                        auxRow[ID_FIELD_ELEVATION] = ValueFactory
417
                                                                        .createValue(((IDwg3DTestable) entity).getZ());
418
                                                }
419

    
420
                                                if (entity instanceof DwgMText) {
421
                                                        DwgMText mtext = (DwgMText) entity;
422
                                                        auxRow[ID_FIELD_TEXT] = ValueFactory.createValue(mtext
423
                                                                        .getText());
424
                                                        auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory
425
                                                                        .createValue(mtext.getHeight());
426
                                                } else if (entity instanceof DwgText) {
427
                                                        DwgText text = (DwgText) entity;
428
                                                        auxRow[ID_FIELD_TEXT] = ValueFactory.createValue(text
429
                                                                        .getText());
430
                                                        auxRow[ID_FIELD_THICKNESS] = ValueFactory
431
                                                                        .createValue(text.getThickness());
432
                                                        auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory
433
                                                                        .createValue(text.getHeight());
434
                                                        auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory
435
                                                                        .createValue(Math.toDegrees(text
436
                                                                                        .getRotationAngle()));
437
                                                }// if-else
438
                                                addGeometry(geometry, auxRow);
439

    
440
                                                if(debug)
441
                                                        saveEntity(entity);
442

    
443
                                        }
444
                                } catch (Exception e) {
445
                                        // TODO Auto-generated catch block
446
                                        e.printStackTrace();
447
                                        continue;
448
                                }
449
                        }// for
450

    
451
                } catch (Throwable t) {
452
                        t.printStackTrace();
453
                }
454
                dwgObjects = null;
455
                dwg = null;
456
                System.gc();
457
                setSymbols();
458
        }
459

    
460
        /**
461
         * Sets a symbol for each dwg entity's derived feature based in
462
         * the DWG symbology info.
463
         * */
464
        private void setSymbols() throws InitializeDriverException{
465
                // JAUME REFACTORED TO ISymbol
466
//                FSymbol myDefaultSymbol = new FSymbol(getShapeType());
467
//                myDefaultSymbol.setShapeVisible(false);
468
//                myDefaultSymbol.setFontSizeInPixels(false);
469
//                myDefaultSymbol.setFont(new Font("SansSerif", Font.PLAIN, 9));
470
//                myDefaultSymbol.setFontColor(Color.BLACK);
471
//                myDefaultSymbol.setFontSize(heightText);
472
//                myDefaultSymbol.setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
473
//                myDefaultSymbol.setSize(3);
474
//                myDefaultSymbol.setSizeInPixels(true);
475

    
476
                defaultLegend = LegendFactory
477
                                .createVectorialUniqueValueLegend(getShapeType());
478
                defaultLegend.setClassifyingFieldNames(new String[] {"Color"});
479
                defaultLegend.setDefaultSymbol(SymbologyFactory.
480
                                createDefaultSymbolByShapeType(getShapeType(), Color.BLACK));
481

    
482
                ObjectDriver rs = this;
483
                IntValue clave;
484
                ISymbol theSymbol = null;
485
                try {
486
                        for (long j = 0; j < rs.getRowCount(); j++) {
487
                                clave = (IntValue) rs.getFieldValue(j, ID_FIELD_COLOR);
488
                                if (defaultLegend.getSymbolByValue(clave) == null) {
489
                                        // JAUME REFACTORED TO ISymbol
490
//                                        theSymbol = new FSymbol(getShapeType());
491
//                                        theSymbol.setDescription(clave.toString());
492
//                                        theSymbol.setColor(AcadColor.getColor(clave.getValue()));
493
//                                        // theSymbol.setFill(fillProv);
494
//                                        // 050202, jmorell: Asigna los colores de Autocad a los
495
//                                        // bordes
496
//                                        // de los pol?gonos.
497
//                                        theSymbol.setOutlineColor(AcadColor.getColor(clave
498
//                                                        .getValue()));
499
//
500
//                                        theSymbol.setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
501
//                                        theSymbol.setSize(3);
502
//                                        theSymbol.setSizeInPixels(true);
503
//                                        defaultLegend.addSymbol(clave, theSymbol);
504
                                        Color c = AcadColor.getColor(clave.getValue());
505
                                        theSymbol =        SymbologyFactory.
506
                                        createDefaultSymbolByShapeType(getShapeType(), c);
507
                                theSymbol.setDescription(clave.toString());
508

    
509
                                if (theSymbol instanceof IMarkerSymbol) {
510
                                        ((IMarkerSymbol) theSymbol).setSize(1);
511
                                }
512

    
513
                                if (theSymbol instanceof ILineSymbol) {
514
                                        ((ILineSymbol) theSymbol).setLineWidth(1);
515
                                }
516

    
517
                                if (theSymbol instanceof IFillSymbol) {
518
                                        ((IFillSymbol) theSymbol).getOutline().setLineColor(c);
519
                                        ((IFillSymbol) theSymbol).getOutline().setLineWidth(1);
520
                                        ((IFillSymbol) theSymbol).setFillColor(null);
521
                                }
522

    
523
                                // theSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_TRANSPARENT);
524
                                defaultLegend.addSymbol(clave, theSymbol);
525
                                }// if
526
                        } // for
527
                } catch (ReadDriverException e) {
528
                        throw new InitializeDriverException(getName(),e);
529
                }
530
        }
531

    
532
        /**
533
         * checks if the given geometry intersects or its
534
         * contained in one of the driver regions of interest.
535
         *
536
         * @param geometry feature geometry we want to check
537
         * against the drivers rois.
538
         *
539
         *  @return true if feature is contained in any ROI.
540
         *  false if not.
541
         * */
542
        private boolean checkRois(IGeometry geometry){
543
                Rectangle2D rect = geometry.getBounds2D();
544
                int numRois = this.getNumOfRois();
545
                for(int i = 0; i < numRois; i++){
546
                        Rectangle2D roi = getRegionOfInterest(i);
547
                        if( checkIntersect(rect.getMinX(), rect.getMaxX(), rect.getMinY(), rect.getMaxY(),
548
                                        roi.getMinX(), roi.getMaxX(), roi.getMinY(),roi.getMaxY()) ||
549
                                checkContains(rect.getMinX(), rect.getMaxX(), rect.getMinY(), rect.getMaxY(),
550
                                                roi.getMinX(), roi.getMaxX(), roi.getMinY(),roi.getMaxY())
551
                        ){
552
                                return true;
553
                        }
554
//                        if(roi.intersects(rect) || roi.contains(rect))
555
//                                return true;
556
                }
557
                return false;
558
        }
559

    
560
        private boolean checkContains(double x1, double x2, double y1, double y2,
561
                        double ax1, double ax2, double ay1, double ay2){
562
                 boolean solution = ( x1 >= ax1 &&
563
                x2 <= ax2 &&
564
                y1 >= ay1 &&
565
                y2 <= ay2);
566
                 return solution;
567
        }
568

    
569
        private boolean checkIntersect(double x1, double x2, double y1, double y2,
570
                                                                double ax1, double ax2, double ay1, double ay2){
571

    
572
                 return !(x1 > ax2 ||
573
                        x2 < ax1 ||
574
                        y1 > ay2 ||
575
                        y2 < ay1);
576

    
577
        }
578

    
579
        public String getFileVersion() {
580
                return fileVersion;
581
        }
582

    
583
        private String formatString(String fmt, String[] params) {
584
                String ret = fmt;
585
                for (int i = 0; i < params.length; i++) {
586
                        ret = ret.replaceFirst("%s", params[i]);
587
                }
588
                return ret;
589
        }
590

    
591

    
592
        /*
593
         * (non-Javadoc)
594
         *
595
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#accept(java.io.File)
596
         */
597
        public boolean accept(File f) {
598
                return f.getName().toUpperCase().endsWith("DWG");
599
        }
600

    
601
        /*
602
         * (non-Javadoc)
603
         *
604
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getShapeType()
605
         */
606
        public int getShapeType() {
607
                return FShape.MULTI;
608
        }
609

    
610
        /*
611
         * (non-Javadoc)
612
         *
613
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getName()
614
         */
615
        public String getName() {
616
                return "gvSIG DWG Memory Driver";
617
        }
618

    
619
        /*
620
         * (non-Javadoc)
621
         *
622
         * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
623
         */
624
        public ILegend getDefaultLegend() {
625
                return defaultLegend;
626
        }
627

    
628
        /*
629
         * (non-Javadoc)
630
         *
631
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
632
         */
633
        public DriverAttributes getDriverAttributes() {
634
                return attr;
635
        }
636

    
637
        /*
638
         * (non-Javadoc)
639
         *
640
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
641
         */
642
        public int[] getPrimaryKeys() {
643
                // TODO Auto-generated method stub
644
                return null;
645
        }
646

    
647
        /*
648
         * (non-Javadoc)
649
         *
650
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
651
         */
652
        public void write(DataWare arg0)  {
653
                // TODO Auto-generated method stub
654

    
655
        }
656

    
657
        public void setDataSourceFactory(DataSourceFactory arg0) {
658
                // TODO Auto-generated method stub
659

    
660
        }
661

    
662
        public void close()  {
663
                entities.clear();
664
                entities = null;
665
                System.gc();
666

    
667
        }
668

    
669
        public File getFile() {
670
                return m_Fich;
671
        }
672

    
673
        public boolean isWritable() {
674
                return m_Fich.canWrite();
675
        }
676

    
677

    
678
        public boolean isDebug() {
679
                return debug;
680
        }
681

    
682

    
683
        public void setDebug(boolean debug) {
684
                this.debug = debug;
685
        }
686

    
687
        public ILabelingStrategy getDefaultLabelingStrategy() {
688
                return labeling;
689
        }
690
        public int getFieldType(int i) {
691
            DefaultTableModel dtm=getTableModel();
692
                String columnName=dtm.getColumnName(i);
693
            if (columnName.equals("ID")){
694
                    return Types.INTEGER;
695
            } else if (columnName.equals("FShape")){
696
                    return Types.VARCHAR;
697
            } else if (columnName.equals("Entity")){
698
                            return Types.VARCHAR;
699
                } else if (columnName.equals("Layer")){
700
                    return Types.VARCHAR;
701
            } else if (columnName.equals("Color")){
702
                    return Types.INTEGER;
703
            } else if (columnName.equals("Elevation")){
704
                    return Types.DOUBLE;
705
            } else if (columnName.equals("Thickness")){
706
                    return Types.DOUBLE;
707
            } else if (columnName.equals("HeightText")){
708
                    return Types.DOUBLE;
709
            } else if (columnName.equals("RotationText")){
710
                    return Types.DOUBLE;
711
            } else if (columnName.equals("Text")){
712
                    return Types.VARCHAR;
713
            } else{
714
                    return Types.VARCHAR;
715
            }
716
        }
717
}
718

    
719

    
720

    
721

    
722

    
723