Statistics
| Revision:

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

History | View | Annotate | Download (13.7 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 10628 2007-03-06 17:27:42Z caballero $
48
* $Log$
49
* Revision 1.8  2007-03-06 17:27:31  caballero
50
* Exceptions
51
*
52
* Revision 1.7  2007/02/27 17:18:03  azabala
53
* solved bug with text orientation
54
*
55
* Revision 1.6  2007/02/14 16:17:22  azabala
56
* *** empty log message ***
57
*
58
* Revision 1.5  2007/01/20 18:30:37  azabala
59
* refactoring of blocks
60
*
61
* Revision 1.4  2007/01/18 19:59:10  azabala
62
* refactoring to optimize and simplify the code
63
*
64
* Revision 1.3  2007/01/18 13:36:42  azabala
65
* Refactoring general para evitar dar tantas pasadas en la carga, y para incrementar
66
* la legibilidad del codigo (demasiados if-else-if en vez de usar polimorfismo)
67
*
68
* Revision 1.2  2007/01/12 19:57:44  azabala
69
* *** empty log message ***
70
*
71
* Revision 1.1  2007/01/11 20:31:05  azabala
72
* *** empty log message ***
73
*
74
*
75
*/
76
package com.iver.cit.gvsig.drivers.dwg;
77

    
78
import java.awt.Color;
79
import java.awt.Font;
80
import java.awt.geom.Point2D;
81
import java.io.File;
82
import java.io.IOException;
83
import java.util.ArrayList;
84
import java.util.List;
85

    
86
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
87
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
88
import com.hardcode.gdbms.driver.exceptions.UnsupportedVersionDriverException;
89
import com.hardcode.gdbms.engine.data.DataSourceFactory;
90
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
91
import com.hardcode.gdbms.engine.data.edition.DataWare;
92
import com.hardcode.gdbms.engine.values.IntValue;
93
import com.hardcode.gdbms.engine.values.Value;
94
import com.hardcode.gdbms.engine.values.ValueFactory;
95
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
96
import com.iver.cit.gvsig.fmap.core.FShape;
97
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
98
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
99
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
100
import com.iver.cit.gvsig.fmap.drivers.AbstractCadMemoryDriver;
101
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
102
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
103
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
104
import com.iver.cit.gvsig.fmap.rendering.Legend;
105
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
106
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
107
import com.iver.cit.jdwglib.dwg.DwgFile;
108
import com.iver.cit.jdwglib.dwg.DwgObject;
109
import com.iver.cit.jdwglib.dwg.DwgVersionNotSupportedException;
110
import com.iver.cit.jdwglib.dwg.IDwg2FMap;
111
import com.iver.cit.jdwglib.dwg.IDwg3DTestable;
112
import com.iver.cit.jdwglib.dwg.objects.DwgMText;
113
import com.iver.cit.jdwglib.dwg.objects.DwgText;
114
import com.iver.cit.jdwglib.util.AcadColor;
115

    
116
/**
117
 * Driver that allows gvSIG to read files in DWG format Using this driver, a
118
 * gvSIG user can manipulate part of the information contained in a DWG file
119
 * This driver load the Dwg file in memory This driver uses jdwglib
120
 *
121
 * @author jmorell
122
 */
123
public class DwgMemoryDriver extends AbstractCadMemoryDriver implements
124
                VectorialFileDriver, WithDefaultLegend {
125

    
126
        VectorialUniqueValueLegend defaultLegend;
127

    
128
        private File m_Fich;
129

    
130
        private DwgFile dwg;
131

    
132
        private List dwgObjects;
133

    
134
        private DriverAttributes attr = new DriverAttributes();
135

    
136
        private boolean isInitialized = false;
137

    
138
        /*
139
         * (non-Javadoc)
140
         *
141
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#open(java.io.File)
142
         */
143
        public void open(File f) {
144
                m_Fich = f;
145
        }
146

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

    
178
                attr.setLoadedInMemory(true);
179
                dwg = new DwgFile(m_Fich.getAbsolutePath());
180

    
181
                try {
182
                        dwg.read();
183
                } catch (DwgVersionNotSupportedException e1) {
184

    
185
                        String autodeskUrl = "<a href=\"http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=7024151\">"
186
                                        + "http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=7024151</a>";
187
//                        throw new InitializeDriverException(
188
//                                        formatString(
189
//                                                        "La version (%s) del fichero DWG '%s' no esta soportada por gvSIG.\n%s\n%s\n%s",
190
//                                                        new String[] { e1.getDwgVersion().toString(),
191
//                                                                        m_Fich.getName(),
192
//                                                                        Messages.getText("Probe_to_convert"),
193
//                                                                        Messages.getText("Autodesk_converter"),
194
//                                                                        autodeskUrl }));
195
                        String description=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
                        throw new UnsupportedVersionDriverException(getName(),e1,description);
203
                } catch (IOException e) {
204
                        throw new InitializeDriverException(getName(),e);
205
                }
206

    
207
                // dwg.initializeLayerTable();
208
                dwg.calculateGisModelDwgPolylines();
209
                dwg.blockManagement2();
210
                dwgObjects = dwg.getDwgObjects();
211

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

    
227
                boolean is3dFile = dwg.isDwg3DFile();
228
                
229
                for (int i = 0; i < dwgObjects.size(); i++) {
230
                        DwgObject entity = (DwgObject) dwgObjects.get(i);
231

    
232
                        if(entity instanceof IDwg2FMap){
233

    
234
                                IDwg2FMap dwgEnt = (IDwg2FMap)entity;
235
                                //TODO Ojo, revisar las fshape a null
236
                                FShape fshape = dwgEnt.toFMapGeometry(is3dFile);
237
                                
238
                                String fmapStr = dwgEnt.toFMapString(is3dFile);
239
                                //nombre del tipo de geometria fmap
240
                                auxRow[ID_FIELD_FSHAPE] = ValueFactory.createValue(fmapStr);
241
                                //indice del registro
242
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
243
                                //nombre de entidad dwg
244
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(dwgEnt.toString());
245
                                String layerName = dwg.getLayerName(entity);
246
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
247
                                int color = entity.getColor();
248
                                int colorByLayer = dwg.getColorByLayer(entity);
249

    
250
                                // TODO: if (color==0) color ByBlock
251
                                if (color == 256)
252
                                        color = colorByLayer;
253
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
254

    
255
                                auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0.0);
256
                                auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(0.0);
257
                                auxRow[ID_FIELD_TEXT] = ValueFactory.createNullValue();
258
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
259

    
260
                                if(entity instanceof IDwg3DTestable){
261
                                        auxRow[ID_FIELD_ELEVATION] =
262
                                                ValueFactory.createValue(((IDwg3DTestable)entity).getZ());
263
                                }
264

    
265
                                if(entity instanceof DwgMText){
266
                                        DwgMText mtext = (DwgMText) entity;
267
                                        auxRow[ID_FIELD_TEXT] = ValueFactory.
268
                                                                createValue(mtext.getText());
269
                                        auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.
270
                                                                createValue(mtext.getHeight());
271
                                }else if(entity instanceof DwgText){
272
                                        DwgText text = (DwgText) entity;
273
                                        auxRow[ID_FIELD_TEXT] = ValueFactory.
274
                                                        createValue(text.getText());
275
                                        auxRow[ID_FIELD_THICKNESS] = ValueFactory.
276
                                                        createValue(text.getThickness());
277
                                        auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.
278
                                                        createValue(text.getHeight());
279
                                        auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.
280
                                                        createValue(Math.toDegrees(text.getRotationAngle()));
281
                                }//if-else
282
                                addShape(fshape, auxRow);
283

    
284
                        }//if instanceof
285
                        else{
286
                                
287
                        }
288
                }//for
289

    
290
                FSymbol myDefaultSymbol = new FSymbol(getShapeType());
291
                myDefaultSymbol.setShapeVisible(false);
292
                myDefaultSymbol.setFontSizeInPixels(false);
293
                myDefaultSymbol.setFont(new Font("SansSerif", Font.PLAIN, 9));
294
                myDefaultSymbol.setFontColor(Color.BLACK);
295
                myDefaultSymbol.setFontSize(heightText);
296
                myDefaultSymbol.setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
297
                myDefaultSymbol.setSize(3);
298
                myDefaultSymbol.setSizeInPixels(true);
299

    
300
                defaultLegend = LegendFactory
301
                                .createVectorialUniqueValueLegend(getShapeType());
302
                defaultLegend.setFieldName("Color");
303
                defaultLegend.setLabelField("Text");
304
                defaultLegend.setDefaultSymbol(myDefaultSymbol);
305
                defaultLegend.setLabelHeightField("HeightText");
306
                defaultLegend.setLabelRotationField("RotationText");
307

    
308
                ObjectDriver rs = this;
309
                IntValue clave;
310
                FSymbol theSymbol = null;
311
                try {
312
                        for (long j = 0; j < rs.getRowCount(); j++) {
313
                                clave = (IntValue) rs.getFieldValue(j, ID_FIELD_COLOR);
314
                                if (defaultLegend.getSymbolByValue(clave) == null) {
315
                                        theSymbol = new FSymbol(getShapeType());
316
                                        theSymbol.setDescription(clave.toString());
317
                                        theSymbol.setColor(AcadColor.getColor(clave.getValue()));
318
                                        // theSymbol.setFill(fillProv);
319
                                        // 050202, jmorell: Asigna los colores de Autocad a los
320
                                        // bordes
321
                                        // de los pol?gonos.
322
                                        theSymbol.setOutlineColor(AcadColor.getColor(clave
323
                                                        .getValue()));
324

    
325
                                        theSymbol.setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
326
                                        theSymbol.setSize(3);
327
                                        theSymbol.setSizeInPixels(true);
328
                                        defaultLegend.addSymbol(clave, theSymbol);
329
                                }//if
330
                        } // for
331
                } catch (ReadDriverException e) {
332
                        throw new InitializeDriverException(getName(),e);
333
                }
334
        }
335

    
336
        public String getFileVersion() {
337
                return dwg.getDwgVersion();
338
        }
339

    
340
        private String formatString(String fmt, String[] params) {
341
                String ret = fmt;
342
                for (int i = 0; i < params.length; i++) {
343
                        ret = ret.replaceFirst("%s", params[i]);
344
                }
345
                return ret;
346
        }
347

    
348

    
349

    
350

    
351

    
352

    
353
        /**
354
         * Method that changes a Point2D array to a FPolygon2D. Is useful to convert
355
         * a polygon given by it points to a FPolygon2D, a polygon in the FMap model
356
         * object
357
         *
358
         * @param pts
359
         *            Array of Point2D that defines the polygon that will be
360
         *            converted in a FPolygon2D
361
         * @return FPolygon2D This FPolygon2D is build using the array of Point2D
362
         *         that is the argument of the method
363
         */
364
        private FPolygon2D points2DToFPolygon2D(Point2D[] pts) {
365
                GeneralPathX genPathX = new GeneralPathX();
366
                genPathX.moveTo(pts[0].getX(), pts[0].getY());
367
                for (int i = 1; i < pts.length; i++) {
368
                        genPathX.lineTo(pts[i].getX(), pts[i].getY());
369
                }
370
                genPathX.closePath();
371
                return new FPolygon2D(genPathX);
372
        }
373

    
374
        /*
375
         * (non-Javadoc)
376
         *
377
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#accept(java.io.File)
378
         */
379
        public boolean accept(File f) {
380
                return f.getName().toUpperCase().endsWith("DWG");
381
        }
382

    
383
        /*
384
         * (non-Javadoc)
385
         *
386
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getShapeType()
387
         */
388
        public int getShapeType() {
389
                return FShape.MULTI;
390
        }
391

    
392
        /*
393
         * (non-Javadoc)
394
         *
395
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getName()
396
         */
397
        public String getName() {
398
                return "gvSIG DWG Memory Driver";
399
        }
400

    
401
        /*
402
         * (non-Javadoc)
403
         *
404
         * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
405
         */
406
        public Legend getDefaultLegend() {
407
                return defaultLegend;
408
        }
409

    
410
        /*
411
         * (non-Javadoc)
412
         *
413
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
414
         */
415
        public DriverAttributes getDriverAttributes() {
416
                return attr;
417
        }
418

    
419
        /*
420
         * (non-Javadoc)
421
         *
422
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
423
         */
424
        public int[] getPrimaryKeys() {
425
                // TODO Auto-generated method stub
426
                return null;
427
        }
428

    
429
        /*
430
         * (non-Javadoc)
431
         *
432
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
433
         */
434
        public void write(DataWare arg0) {
435
                // TODO Auto-generated method stub
436

    
437
        }
438

    
439
        public void setDataSourceFactory(DataSourceFactory arg0) {
440
                // TODO Auto-generated method stub
441

    
442
        }
443

    
444
        public void close() {
445
                // TODO Auto-generated method stub
446

    
447
        }
448

    
449
        public File getFile() {
450
                return m_Fich;
451
        }
452

    
453
        public boolean isWritable() {
454
                return m_Fich.canWrite();
455
        }
456
}