Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / dxf / DXFMemoryDriver.java @ 5595

History | View | Annotate | Download (31.9 KB)

1
/*
2
 * @(#)DXFMemoryDriver    29-dic-2004
3
 *
4
 * @author jmorell (jose.morell@gmail.com)
5
 */
6
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
7
 *
8
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License
12
 * as published by the Free Software Foundation; either version 2
13
 * of the License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
23
 *
24
 * For more information, contact:
25
 *
26
 *  Generalitat Valenciana
27
 *   Conselleria d'Infraestructures i Transport
28
 *   Av. Blasco Ib??ez, 50
29
 *   46010 VALENCIA
30
 *   SPAIN
31
 *
32
 *      +34 963862235
33
 *   gvsig@gva.es
34
 *      www.gvsig.gva.es
35
 *
36
 *    or
37
 *
38
 *   IVER T.I. S.A
39
 *   Salamanca 50
40
 *   46005 Valencia
41
 *   Spain
42
 *
43
 *   +34 963163400
44
 *   dac@iver.es
45
 */
46
package com.iver.cit.gvsig.fmap.drivers.dxf;
47

    
48
import java.awt.Color;
49
import java.awt.Font;
50
import java.awt.geom.Point2D;
51
import java.io.File;
52
import java.io.FileInputStream;
53
import java.io.FileNotFoundException;
54
import java.io.FileOutputStream;
55
import java.io.IOException;
56
import java.nio.channels.FileChannel;
57
import java.util.ArrayList;
58
import java.util.Properties;
59

    
60
import org.cresques.cts.IProjection;
61
import org.cresques.cts.ProjectionPool;
62
import org.cresques.geo.Point3D;
63
import org.cresques.io.DxfFile;
64
import org.cresques.px.IObjList;
65
import org.cresques.px.dxf.AcadColor;
66
import org.cresques.px.dxf.DxfFeatureMaker;
67
import org.cresques.px.dxf.DxfHeaderManager;
68
import org.cresques.px.gml.Feature;
69
import org.cresques.px.gml.LineString;
70
import org.cresques.px.gml.LineString3D;
71
import org.cresques.px.gml.Point;
72
import org.cresques.px.gml.Polygon;
73
import org.cresques.px.gml.Polygon3D;
74

    
75
import com.hardcode.gdbms.driver.DriverUtilities;
76
import com.hardcode.gdbms.engine.data.driver.DriverException;
77
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
78
import com.hardcode.gdbms.engine.data.edition.DataWare;
79
import com.hardcode.gdbms.engine.values.IntValue;
80
import com.hardcode.gdbms.engine.values.Value;
81
import com.hardcode.gdbms.engine.values.ValueFactory;
82
import com.iver.cit.gvsig.fmap.core.FPoint2D;
83
import com.iver.cit.gvsig.fmap.core.FPoint3D;
84
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
85
import com.iver.cit.gvsig.fmap.core.FPolygon3D;
86
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
87
import com.iver.cit.gvsig.fmap.core.FPolyline3D;
88
import com.iver.cit.gvsig.fmap.core.FShape;
89
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
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.DriverAttributes;
93
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
94
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
95
import com.iver.cit.gvsig.fmap.drivers.MemoryDriver;
96
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
97
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
98
import com.iver.cit.gvsig.fmap.edition.EditionException;
99
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
100
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
101
import com.iver.cit.gvsig.fmap.edition.IWriteable;
102
import com.iver.cit.gvsig.fmap.edition.IWriter;
103
import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfFieldsMapping;
104
import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfWriter;
105
import com.iver.cit.gvsig.fmap.layers.FLayer;
106
import com.iver.cit.gvsig.fmap.rendering.Legend;
107
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
108
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
109

    
110
/**
111
 * @author jmorell (jose.morell@gmail.com)
112
 * @version 29-dic-2004
113
 */
114
public class DXFMemoryDriver extends MemoryDriver implements
115
                VectorialFileDriver, WithDefaultLegend, ISpatialWriter, IWriteable {
116
        private static String tempDirectoryPath = System
117
                        .getProperty("java.io.tmpdir");
118

    
119
        private final int ID_FIELD_ID = 0;
120

    
121
        private final int ID_FIELD_FSHAPE = 1;
122

    
123
        private final int ID_FIELD_ENTITY = 2;
124

    
125
        private final int ID_FIELD_LAYER = 3;
126

    
127
        private final int ID_FIELD_COLOR = 4;
128

    
129
        private final int ID_FIELD_ELEVATION = 5;
130

    
131
        private final int ID_FIELD_THICKNESS = 6;
132

    
133
        private final int ID_FIELD_TEXT = 7;
134

    
135
        private final int ID_FIELD_HEIGHTTEXT = 8;
136

    
137
        private final int ID_FIELD_ROTATIONTEXT = 9;
138

    
139
        private DxfWriter dxfWriter = new DxfWriter();
140

    
141
        private File fTemp;
142

    
143
        VectorialUniqueValueLegend defaultLegend;
144

    
145
        private String path;
146

    
147
        private File m_Fich;
148

    
149
        private DxfFile.EntityFactory featureMaker;
150

    
151
        private DxfFile dxfFeatureFile;
152

    
153
        private IObjList.vector features;
154

    
155
        private DriverAttributes attr = new DriverAttributes();
156

    
157
        /**
158
         * Habilita la utilizaci?n del lector del HEADER del DXF.
159
         */
160
        private DxfFile.VarSettings headerManager;
161

    
162
        /*
163
         * (non-Javadoc)
164
         *
165
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#open(java.io.File)
166
         */
167
        public void open(File f) throws IOException {
168
                m_Fich = f;
169
        }
170

    
171
        public void initialize() throws IOException {
172
                float heightText = 10;
173

    
174
                attr.setLoadedInMemory(true);
175

    
176
                IProjection proj = ProjectionPool.get("EPSG:23030");
177
                featureMaker = new DxfFeatureMaker(proj);
178
                headerManager = new DxfHeaderManager();
179
                dxfFeatureFile = new DxfFile(proj, m_Fich.getAbsolutePath(),
180
                                featureMaker, headerManager);
181
                dxfFeatureFile.load();
182
                features = (IObjList.vector) ((DxfFeatureMaker) featureMaker)
183
                                .getObjects();
184
                String acadVersion = (String) ((DxfHeaderManager) headerManager)
185
                                .getAcadVersion();
186
                System.out.println("initialize(): acadVersion = " + acadVersion);
187
                if (!featureMaker.isDxf3DFile() && !headerManager.isWritedDxf3D()) { // y no
188
                                                                                                                                                                // est?n
189
                                                                                                                                                                // todos
190
                                                                                                                                                                // a
191
                                                                                                                                                                // 9999
192
                        Feature[] features2D = new Feature[features.size()];
193
                        for (int i = 0; i < features.size(); i++) {
194
                                Feature fea = (Feature) features.get(i);
195
                                if (fea.getGeometry() instanceof org.cresques.px.gml.Point3D) {
196
                                        Point point = (Point) fea.getGeometry();
197
                                        Point point2 = new Point();
198
                                        for (int j = 0; j < point.pointNr(); j++) {
199
                                                point2.add(point.get(j));
200
                                        }
201
                                        point2.setTextPoint(point.isTextPoint());
202
                                        fea.setGeometry(point2);
203
                                        features2D[i] = fea;
204
                                        // } else if (fea.getGeometry() instanceof InsPoint3D) {
205
                                        // InsPoint insPoint = (InsPoint)fea.getGeometry();
206
                                        // InsPoint insPoint2 = new InsPoint();
207
                                        // for (int j=0;j<insPoint.pointNr();j++) {
208
                                        // insPoint2.add(insPoint.get(j));
209
                                        // }
210
                                        // fea.setGeometry(insPoint2);
211
                                        // features2D[i] = fea;
212
                                } else if (fea.getGeometry() instanceof LineString3D) {
213
                                        LineString lineString = (LineString) fea.getGeometry();
214
                                        LineString lineString2 = new LineString();
215
                                        for (int j = 0; j < lineString.pointNr(); j++) {
216
                                                lineString2.add(lineString.get(j));
217
                                        }
218
                                        fea.setGeometry(lineString2);
219
                                        features2D[i] = fea;
220
                                } else if (fea.getGeometry() instanceof Polygon3D) {
221
                                        Polygon polygon = (Polygon) fea.getGeometry();
222
                                        Polygon polygon2 = new Polygon();
223
                                        for (int j = 0; j < polygon.pointNr(); j++) {
224
                                                polygon2.add(polygon.get(j));
225
                                        }
226
                                        fea.setGeometry(polygon2);
227
                                        features2D[i] = fea;
228
                                }
229
                        }
230
                        features.clear();
231
                        for (int i = 0; i < features2D.length; i++) {
232
                                features.add(features2D[i]);
233
                        }
234
                }
235
                // String acadVersion =
236
                // (String)((DxfHeaderManager)headerManager).getAcadVersion();
237
                // System.out.println("initialize(): acadVersion = " + acadVersion);
238

    
239
                int nAtt = featureMaker.getAttributes().size();
240

    
241
                // Campos de las MemoryLayer:
242
                Value[] auxRow = new Value[10 + nAtt];
243
                ArrayList arrayFields = new ArrayList();
244
                arrayFields.add("ID");
245
                arrayFields.add("FShape");
246
                arrayFields.add("Entity");
247
                arrayFields.add("Layer");
248
                arrayFields.add("Color");
249
                arrayFields.add("Elevation");
250
                arrayFields.add("Thickness");
251
                arrayFields.add("Text");
252
                arrayFields.add("HeightText");
253
                arrayFields.add("RotationText");
254
                for (int i = 0; i < nAtt; i++) {
255
                        String att[] = new String[2];
256
                        att = (String[]) featureMaker.getAttributes().get(i);
257
                        arrayFields.add(att[0]);
258
                }
259

    
260
                getTableModel().setColumnIdentifiers(arrayFields.toArray());
261

    
262
                for (int i = 0; i < features.size(); i++) {
263

    
264
                        auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0.0);
265
                        auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(0.0);
266
                        auxRow[ID_FIELD_TEXT] = ValueFactory.createNullValue();
267

    
268
                        Feature fea = (Feature) features.get(i);
269
                        if (fea.getGeometry() instanceof Point
270
                                        && !(fea.getGeometry() instanceof org.cresques.px.gml.Point3D)) {
271
                                Point point = (Point) fea.getGeometry();
272
                                Point2D pto = new Point2D.Double();
273
                                pto = (Point2D) point.get(0);
274
                                FShape nuevoShp;
275
                                if (point.isTextPoint()) {
276
                                        auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
277
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
278
                                                        .createValue(new String("FPoint2D"));
279
                                        auxRow[ID_FIELD_ENTITY] = ValueFactory
280
                                                        .createValue(new String(fea.getProp("dxfEntity")));
281
                                        auxRow[ID_FIELD_LAYER] = ValueFactory
282
                                                        .createValue(new String(fea.getProp("layer")));
283
                                        int auxInt = Integer.parseInt(fea.getProp("color"));
284
                                        auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
285
                                        auxRow[ID_FIELD_TEXT] = ValueFactory
286
                                                        .createValue(new String(fea.getProp("text")));
287
                                        heightText = Float.parseFloat(fea.getProp("textHeight"));
288
                                        auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory
289
                                                        .createValue(heightText);
290
                                        double auxR = Double.parseDouble(fea
291
                                                        .getProp("textRotation"));
292
                                        auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory
293
                                                        .createValue(auxR);
294
                                        double auxE = Double.parseDouble(fea.getProp("elevation"));
295
                                        auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
296
                                        double auxT = Double.parseDouble(fea.getProp("thickness"));
297
                                        auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
298
                                        // Attributes
299
                                        for (int j = 0; j < nAtt; j++) {
300
                                                String[] attributes = new String[2];
301
                                                attributes = (String[]) featureMaker.getAttributes()
302
                                                                .get(j);
303
                                                auxRow[10 + j] = ValueFactory.createValue(new String(
304
                                                                (String) attributes[1]));
305
                                                if (!fea.getProp(attributes[0]).equals(attributes[1])) {
306
                                                        auxRow[10 + j] = ValueFactory
307
                                                                        .createValue(new String(fea
308
                                                                                        .getProp(attributes[0])));
309
                                                }
310
                                        }
311
                                        nuevoShp = new FPoint2D(pto.getX(), pto.getY());
312
                                        addShape(nuevoShp, auxRow);
313
                                } else {
314
                                        auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
315
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
316
                                                        .createValue(new String("FPoint2D"));
317
                                        auxRow[ID_FIELD_ENTITY] = ValueFactory
318
                                                        .createValue(new String(fea.getProp("dxfEntity")));
319
                                        auxRow[ID_FIELD_LAYER] = ValueFactory
320
                                                        .createValue(new String(fea.getProp("layer")));
321
                                        int auxInt = Integer.parseInt(fea.getProp("color"));
322
                                        auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
323
                                        double auxE = Double.parseDouble(fea.getProp("elevation"));
324
                                        auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
325
                                        double auxT = Double.parseDouble(fea.getProp("thickness"));
326
                                        auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
327
                                        // Attributes
328
                                        for (int j = 0; j < nAtt; j++) {
329
                                                String[] attributes = new String[2];
330
                                                attributes = (String[]) featureMaker.getAttributes()
331
                                                                .get(j);
332
                                                auxRow[10 + j] = ValueFactory.createValue(new String(
333
                                                                (String) attributes[1]));
334
                                                if (!fea.getProp(attributes[0]).equals(attributes[1])) {
335
                                                        auxRow[10 + j] = ValueFactory
336
                                                                        .createValue(new String(fea
337
                                                                                        .getProp(attributes[0])));
338
                                                }
339
                                        }
340
                                        nuevoShp = new FPoint2D(pto.getX(), pto.getY());
341
                                        addShape(nuevoShp, auxRow);
342
                                }
343
                        } else if (fea.getGeometry() instanceof org.cresques.px.gml.Point3D) {
344
                                org.cresques.px.gml.Point3D point = (org.cresques.px.gml.Point3D) fea
345
                                                .getGeometry();
346
                                Point3D pto = new Point3D();
347
                                pto = (Point3D) point.getPoint3D(0);
348
                                FShape nuevoShp;
349
                                if (point.isTextPoint()) {
350
                                        auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
351
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
352
                                                        .createValue(new String("FPoint3D"));
353
                                        auxRow[ID_FIELD_ENTITY] = ValueFactory
354
                                                        .createValue(new String(fea.getProp("dxfEntity")));
355
                                        auxRow[ID_FIELD_LAYER] = ValueFactory
356
                                                        .createValue(new String(fea.getProp("layer")));
357
                                        int auxInt = Integer.parseInt(fea.getProp("color"));
358
                                        auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
359
                                        auxRow[ID_FIELD_TEXT] = ValueFactory
360
                                                        .createValue(new String(fea.getProp("text")));
361
                                        heightText = Float.parseFloat(fea.getProp("textHeight"));
362
                                        auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory
363
                                                        .createValue(heightText);
364
                                        double auxR = Double.parseDouble(fea
365
                                                        .getProp("textRotation"));
366
                                        auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory
367
                                                        .createValue(auxR);
368
                                        double auxE = Double.parseDouble(fea.getProp("elevation"));
369
                                        auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
370
                                        double auxT = Double.parseDouble(fea.getProp("thickness"));
371
                                        auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
372
                                        // Attributes
373
                                        for (int j = 0; j < nAtt; j++) {
374
                                                String[] attributes = new String[2];
375
                                                attributes = (String[]) featureMaker.getAttributes()
376
                                                                .get(j);
377
                                                auxRow[10 + j] = ValueFactory.createValue(new String(
378
                                                                (String) attributes[1]));
379
                                                if (!fea.getProp(attributes[0]).equals(attributes[1])) {
380
                                                        auxRow[10 + j] = ValueFactory
381
                                                                        .createValue(new String(fea
382
                                                                                        .getProp(attributes[0])));
383
                                                }
384
                                        }
385
                                        nuevoShp = new FPoint3D(pto.getX(), pto.getY(), pto.getZ());
386
                                        addShape(nuevoShp, auxRow);
387
                                } else {
388
                                        auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
389
                                        auxRow[ID_FIELD_FSHAPE] = ValueFactory
390
                                                        .createValue(new String("FPoint3D"));
391
                                        auxRow[ID_FIELD_ENTITY] = ValueFactory
392
                                                        .createValue(new String(fea.getProp("dxfEntity")));
393
                                        auxRow[ID_FIELD_LAYER] = ValueFactory
394
                                                        .createValue(new String(fea.getProp("layer")));
395
                                        int auxInt = Integer.parseInt(fea.getProp("color"));
396
                                        auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
397
                                        double auxE = Double.parseDouble(fea.getProp("elevation"));
398
                                        auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
399
                                        double auxT = Double.parseDouble(fea.getProp("thickness"));
400
                                        auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
401
                                        // Attributes
402
                                        for (int j = 0; j < nAtt; j++) {
403
                                                String[] attributes = new String[2];
404
                                                attributes = (String[]) featureMaker.getAttributes()
405
                                                                .get(j);
406
                                                auxRow[10 + j] = ValueFactory.createValue(new String(
407
                                                                (String) attributes[1]));
408
                                                if (!fea.getProp(attributes[0]).equals(attributes[1])) {
409
                                                        auxRow[10 + j] = ValueFactory
410
                                                                        .createValue(new String(fea
411
                                                                                        .getProp(attributes[0])));
412
                                                }
413
                                        }
414
                                        nuevoShp = new FPoint3D(pto.getX(), pto.getY(), pto.getZ());
415
                                        addShape(nuevoShp, auxRow);
416
                                }
417
                                /*
418
                                 * } else if (fea.getGeometry() instanceof InsPoint &&
419
                                 * !(fea.getGeometry() instanceof InsPoint3D)) { InsPoint
420
                                 * insPoint = (InsPoint)fea.getGeometry(); Point2D pto = new
421
                                 * Point2D.Double(); pto = (Point2D)insPoint.get(0);
422
                                 * auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
423
                                 * auxRow[ID_FIELD_FSHAPE] = ValueFactory.createValue(new
424
                                 * String("PointZ")); auxRow[ID_FIELD_ENTITY] =
425
                                 * ValueFactory.createValue(new
426
                                 * String(fea.getProp("dxfEntity"))); auxRow[ID_FIELD_LAYER] =
427
                                 * ValueFactory.createValue(new String(fea.getProp("layer")));
428
                                 * int auxInt = Integer.parseInt(fea.getProp("color"));
429
                                 * auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
430
                                 * double auxE = Double.parseDouble(fea.getProp("elevation"));
431
                                 * auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE); //
432
                                 * Attributes for (int j=0;j<nAtt;j++) { String[] attributes =
433
                                 * new 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]))); } } FShape nuevoShp =
439
                                 * new FPoint2D(pto.getX(),pto.getY()); addShape(nuevoShp,
440
                                 * auxRow); } else if (fea.getGeometry() instanceof InsPoint3D) {
441
                                 * InsPoint3D insPoint = (InsPoint3D)fea.getGeometry(); Point3D
442
                                 * pto = new Point3D(); pto = (Point3D)insPoint.getPoint3D(0);
443
                                 * auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
444
                                 * auxRow[ID_FIELD_FSHAPE] = ValueFactory.createValue(new
445
                                 * String("PointZ")); auxRow[ID_FIELD_ENTITY] =
446
                                 * ValueFactory.createValue(new
447
                                 * String(fea.getProp("dxfEntity"))); auxRow[ID_FIELD_LAYER] =
448
                                 * ValueFactory.createValue(new String(fea.getProp("layer")));
449
                                 * int auxInt = Integer.parseInt(fea.getProp("color"));
450
                                 * auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
451
                                 * double auxE = Double.parseDouble(fea.getProp("elevation"));
452
                                 * auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE); //
453
                                 * Attributes for (int j=0;j<nAtt;j++) { String[] attributes =
454
                                 * new String[2]; attributes =
455
                                 * (String[])featureMaker.getAttributes().get(j); auxRow[9+j] =
456
                                 * ValueFactory.createValue(new String((String)attributes[1]));
457
                                 * if (!fea.getProp(attributes[0]).equals(attributes[1])) {
458
                                 * auxRow[9+j] = ValueFactory.createValue(new
459
                                 * String(fea.getProp(attributes[0]))); } } FShape nuevoShp =
460
                                 * new FPoint3D(pto.getX(),pto.getY(),pto.getZ());
461
                                 * addShape(nuevoShp, auxRow);
462
                                 */
463
                        } else if (fea.getGeometry() instanceof LineString
464
                                        && !(fea.getGeometry() instanceof LineString3D)) {
465
                                GeneralPathX genPathX = new GeneralPathX();
466
                                Point2D[] pts = new Point2D[fea.getGeometry().pointNr()];
467
                                for (int j = 0; j < fea.getGeometry().pointNr(); j++) {
468
                                        pts[j] = (Point2D) fea.getGeometry().get(j);
469
                                }
470
                                genPathX.moveTo(pts[0].getX(), pts[0].getY());
471
                                for (int j = 1; j < pts.length; j++) {
472
                                        genPathX.lineTo(pts[j].getX(), pts[j].getY());
473
                                }
474
                                // double[] elevations = new double[pts.length];
475
                                // for (int j=0;j<pts.length;j++) {
476
                                // elevations[j]=pts[j].getZ();
477
                                // }
478
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
479
                                auxRow[ID_FIELD_FSHAPE] = ValueFactory.createValue(new String(
480
                                                "FPolyline2D"));
481
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(
482
                                                fea.getProp("dxfEntity")));
483
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String(
484
                                                fea.getProp("layer")));
485
                                int auxInt = Integer.parseInt(fea.getProp("color"));
486
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
487
                                double auxE = Double.parseDouble(fea.getProp("elevation"));
488
                                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
489
                                double auxT = Double.parseDouble(fea.getProp("thickness"));
490
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
491
                                // Attributes
492
                                for (int j = 0; j < nAtt; j++) {
493
                                        String[] attributes = new String[2];
494
                                        attributes = (String[]) featureMaker.getAttributes().get(j);
495
                                        auxRow[10 + j] = ValueFactory.createValue(new String(
496
                                                        (String) attributes[1]));
497
                                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
498
                                                auxRow[10 + j] = ValueFactory.createValue(new String(
499
                                                                fea.getProp(attributes[0])));
500
                                        }
501
                                }
502
                                FShape nuevoShp = new FPolyline2D(genPathX);
503
                                addShape(nuevoShp, auxRow);
504
                        } else if (fea.getGeometry() instanceof LineString3D) {
505
                                GeneralPathX genPathX = new GeneralPathX();
506
                                Point3D[] pts = new Point3D[fea.getGeometry().pointNr()];
507
                                for (int j = 0; j < fea.getGeometry().pointNr(); j++) {
508
                                        pts[j] = ((LineString3D) fea.getGeometry()).getPoint3D(j);
509
                                }
510
                                genPathX.moveTo(pts[0].getX(), pts[0].getY());
511
                                for (int j = 1; j < pts.length; j++) {
512
                                        genPathX.lineTo(pts[j].getX(), pts[j].getY());
513
                                }
514
                                double[] elevations = new double[pts.length];
515
                                for (int j = 0; j < pts.length; j++) {
516
                                        elevations[j] = pts[j].getZ();
517
                                }
518
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
519
                                auxRow[ID_FIELD_FSHAPE] = ValueFactory.createValue(new String(
520
                                                "FPolyline3D"));
521
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(
522
                                                fea.getProp("dxfEntity")));
523
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String(
524
                                                fea.getProp("layer")));
525
                                int auxInt = Integer.parseInt(fea.getProp("color"));
526
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
527
                                if (fea.getProp("elevation") != null) {
528
                                        double auxE = Double.parseDouble(fea.getProp("elevation"));
529
                                        auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
530
                                }
531
                                double auxT = Double.parseDouble(fea.getProp("thickness"));
532
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
533
                                // Attributes
534
                                for (int j = 0; j < nAtt; j++) {
535
                                        String[] attributes = new String[2];
536
                                        attributes = (String[]) featureMaker.getAttributes().get(j);
537
                                        auxRow[10 + j] = ValueFactory.createValue(new String(
538
                                                        (String) attributes[1]));
539
                                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
540
                                                auxRow[10 + j] = ValueFactory.createValue(new String(
541
                                                                fea.getProp(attributes[0])));
542
                                        }
543
                                }
544
                                FShape nuevoShp = new FPolyline3D(genPathX, elevations);
545
                                addShape(nuevoShp, auxRow);
546
                        } else if (fea.getGeometry() instanceof Polygon
547
                                        && !(fea.getGeometry() instanceof Polygon3D)) {
548
                                GeneralPathX genPathX = new GeneralPathX();
549
                                // 050112: A?ado una posici?n m?s para el punto que cierra y
550
                                // creo el objeto firstPt.
551
                                Point2D firstPt = new Point2D.Double();
552
                                firstPt = (Point2D) fea.getGeometry().get(0);
553
                                Point2D[] pts = new Point2D[fea.getGeometry().pointNr() + 1];
554
                                for (int j = 0; j < fea.getGeometry().pointNr(); j++) {
555
                                        pts[j] = (Point2D) fea.getGeometry().get(j);
556
                                }
557
                                // 050112: A?ado el primer punto al final para cerrar los
558
                                // pol?gonos.
559
                                pts[fea.getGeometry().pointNr()] = firstPt;
560
                                genPathX.moveTo(pts[0].getX(), pts[0].getY());
561
                                for (int j = 1; j < pts.length; j++) {
562
                                        genPathX.lineTo(pts[j].getX(), pts[j].getY());
563
                                }
564
                                // double[] elevations = new double[pts.length];
565
                                // for (int j=0;j<pts.length;j++) {
566
                                // elevations[j]=pts[j].getZ();
567
                                // }
568
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
569
                                auxRow[ID_FIELD_FSHAPE] = ValueFactory.createValue(new String(
570
                                                "FPolygon2D"));
571
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(
572
                                                fea.getProp("dxfEntity")));
573
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String(
574
                                                fea.getProp("layer")));
575
                                int auxInt = Integer.parseInt(fea.getProp("color"));
576
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
577
                                double auxE = Double.parseDouble(fea.getProp("elevation"));
578
                                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
579
                                double auxT = Double.parseDouble(fea.getProp("thickness"));
580
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
581
                                // Attributes
582
                                for (int j = 0; j < nAtt; j++) {
583
                                        String[] attributes = new String[2];
584
                                        attributes = (String[]) featureMaker.getAttributes().get(j);
585
                                        auxRow[10 + j] = ValueFactory.createValue(new String(
586
                                                        (String) attributes[1]));
587
                                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
588
                                                auxRow[10 + j] = ValueFactory.createValue(new String(
589
                                                                fea.getProp(attributes[0])));
590
                                        }
591
                                }
592
                                FShape nuevoShp = new FPolygon2D(genPathX);
593
                                addShape(nuevoShp, auxRow);
594
                        } else if (fea.getGeometry() instanceof Polygon3D) {
595
                                GeneralPathX genPathX = new GeneralPathX();
596
                                // 050112: A?ado una posici?n m?s para el punto que cierra y
597
                                // creo el objeto firstPt.
598
                                Point3D firstPt = new Point3D();
599
                                firstPt = (Point3D) ((Polygon3D) fea.getGeometry())
600
                                                .getPoint3D(0);
601
                                Point3D[] pts = new Point3D[fea.getGeometry().pointNr() + 1];
602
                                for (int j = 0; j < fea.getGeometry().pointNr(); j++) {
603
                                        pts[j] = (Point3D) ((Polygon3D) fea.getGeometry())
604
                                                        .getPoint3D(j);
605
                                }
606
                                // 050112: A?ado el primer punto al final para cerrar los
607
                                // pol?gonos.
608
                                pts[fea.getGeometry().pointNr()] = firstPt;
609
                                genPathX.moveTo(pts[0].getX(), pts[0].getY());
610
                                for (int j = 1; j < pts.length; j++) {
611
                                        genPathX.lineTo(pts[j].getX(), pts[j].getY());
612
                                }
613
                                double[] elevations = new double[pts.length];
614
                                for (int j = 0; j < pts.length; j++) {
615
                                        elevations[j] = pts[j].getZ();
616
                                }
617
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
618
                                auxRow[ID_FIELD_FSHAPE] = ValueFactory.createValue(new String(
619
                                                "FPolygon3D"));
620
                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(
621
                                                fea.getProp("dxfEntity")));
622
                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String(
623
                                                fea.getProp("layer")));
624
                                int auxInt = Integer.parseInt(fea.getProp("color"));
625
                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
626
                                if (fea.getProp("elevation") != null) {
627
                                        double auxE = Double.parseDouble(fea.getProp("elevation"));
628
                                        auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
629
                                }
630
                                double auxT = Double.parseDouble(fea.getProp("thickness"));
631
                                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
632
                                // Attributes
633
                                for (int j = 0; j < nAtt; j++) {
634
                                        String[] attributes = new String[2];
635
                                        attributes = (String[]) featureMaker.getAttributes().get(j);
636
                                        auxRow[10 + j] = ValueFactory.createValue(new String(
637
                                                        (String) attributes[1]));
638
                                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
639
                                                auxRow[10 + j] = ValueFactory.createValue(new String(
640
                                                                fea.getProp(attributes[0])));
641
                                        }
642
                                }
643
                                FShape nuevoShp = new FPolygon3D(genPathX, elevations);
644
                                addShape(nuevoShp, auxRow);
645
                        } else {
646
                                // System.out.println("Detectado feature desconocido");
647
                        }
648
                }
649

    
650
                defaultLegend = LegendFactory
651
                                .createVectorialUniqueValueLegend(getShapeType());
652
                defaultLegend.setFieldName("Color");
653
                defaultLegend.setLabelField("Text");
654
                defaultLegend.setDefaultSymbol(new FSymbol(getShapeType()));
655
                defaultLegend.getDefaultSymbol().setShapeVisible(false);
656
                defaultLegend.getDefaultSymbol().setFontSizeInPixels(false);
657
                defaultLegend.getDefaultSymbol().setFont(
658
                                new Font("SansSerif", Font.PLAIN, 9));
659
                defaultLegend.getDefaultSymbol().setFontColor(Color.BLACK);
660
                defaultLegend.getDefaultSymbol().setFontSize(heightText);
661
                defaultLegend.getDefaultSymbol().setStyle(
662
                                FConstant.SYMBOL_STYLE_DGNSPECIAL);
663
                defaultLegend.getDefaultSymbol().setSize(3);
664
                defaultLegend.getDefaultSymbol().setSizeInPixels(true);
665

    
666
                defaultLegend.setLabelHeightField("HeightText");
667
                defaultLegend.setLabelRotationField("RotationText");
668

    
669
                ObjectDriver rs = this;
670
                IntValue clave;
671
                FSymbol theSymbol = null;
672

    
673
                try {
674
                        // TODO: Provisional hasta que cambiemos los s?mbolos.
675
                        /*
676
                         * BufferedImage bi= new BufferedImage(5, 5,
677
                         * BufferedImage.TYPE_INT_ARGB); Graphics2D big =
678
                         * bi.createGraphics(); Color color=new Color(0,0,0,0);
679
                         * big.setBackground(color); big.clearRect(0, 0, 5, 5); Paint
680
                         * fillProv = null; Rectangle2D rProv = new Rectangle();
681
                         * rProv.setFrame(0, 0,5,5); fillProv = new TexturePaint(bi,rProv);
682
                         */
683

    
684
                        for (long j = 0; j < rs.getRowCount(); j++) {
685
                                clave = (IntValue) rs.getFieldValue(j, ID_FIELD_COLOR);
686
                                if (defaultLegend.getSymbolByValue(clave) == null) {
687
                                        theSymbol = new FSymbol(getShapeType());
688
                                        theSymbol.setDescription(clave.toString());
689
                                        theSymbol.setColor(AcadColor.getColor(clave.getValue()));
690
                                        // theSymbol.setFill(fillProv);
691
                                        // 050202, jmorell: Asigna los colores de Autocad a los
692
                                        // bordes
693
                                        // de los pol?gonos.
694
                                        theSymbol.setOutlineColor(AcadColor.getColor(clave
695
                                                        .getValue()));
696

    
697
                                        theSymbol.setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
698
                                        theSymbol.setSize(3);
699
                                        theSymbol.setSizeInPixels(true);
700

    
701
                                        defaultLegend.addSymbol(clave, theSymbol);
702
                                }
703
                        } // for
704
                } catch (DriverException e) {
705
                        e.printStackTrace();
706
                        throw new IOException(
707
                                        "Error al poner la leyenda por defecto en el DXF");
708
                }
709
        }
710

    
711
        /*
712
         * (non-Javadoc)
713
         *
714
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#accept(java.io.File)
715
         */
716
        public boolean accept(File f) {
717
                return f.getName().toUpperCase().endsWith("DXF");
718
        }
719

    
720
        /*
721
         * (non-Javadoc)
722
         *
723
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getShapeType()
724
         */
725
        public int getShapeType() {
726
                return FShape.MULTI;
727
        }
728

    
729
        /*
730
         * (non-Javadoc)
731
         *
732
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getName()
733
         */
734
        public String getName() {
735
                return "gvSIG DXF Memory Driver";
736
        }
737

    
738
        /*
739
         * (non-Javadoc)
740
         *
741
         * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
742
         */
743
        public Legend getDefaultLegend() {
744
                return defaultLegend;
745
        }
746

    
747
        /*
748
         * (non-Javadoc)
749
         *
750
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
751
         */
752
        public DriverAttributes getDriverAttributes() {
753
                return attr;
754
        }
755

    
756
        /**
757
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
758
         */
759
        public int[] getPrimaryKeys() throws DriverException {
760
                return null;
761
        }
762

    
763
        /**
764
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
765
         */
766
        public void write(DataWare arg0) throws DriverException {
767
                // TODO Auto-generated method stub
768

    
769
        }
770

    
771
        /*
772
         * (non-Javadoc)
773
         *
774
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#close()
775
         */
776
        public void close() throws IOException {
777
                // TODO Auto-generated method stub
778

    
779
        }
780

    
781
        public File getFile() {
782
                return m_Fich;
783
        }
784

    
785
        public boolean canWriteGeometry(int gvSIGgeometryType) {
786
                return dxfWriter.canWriteGeometry(gvSIGgeometryType);
787
        }
788

    
789
        public void initialize(ITableDefinition layerDef) throws EditionException {
790
                int aux = (int) (Math.random() * 1000);
791
                fTemp = new File(tempDirectoryPath + "/tmpDxf" + aux + ".dxf");
792
                dxfWriter.setFile(fTemp);
793

    
794
                dxfWriter.initialize(layerDef);
795
                /*
796
                 * arrayFields.add("ID"); arrayFields.add("FShape");
797
                 * arrayFields.add("Entity"); arrayFields.add("Layer");
798
                 * arrayFields.add("Color"); arrayFields.add("Elevation");
799
                 * arrayFields.add("Thickness"); arrayFields.add("Text");
800
                 * arrayFields.add("HeightText"); arrayFields.add("RotationText");
801
                 */
802

    
803
                DxfFieldsMapping fieldsMapping = new DxfFieldsMapping();
804
                fieldsMapping.setLayerField("Layer");
805
                fieldsMapping.setColorField("Color");
806
                fieldsMapping.setElevationField("Elevation");
807
                fieldsMapping.setThicknessField("Thickness");
808
                fieldsMapping.setTextField("Text");
809
                fieldsMapping.setHeightText("HeightText");
810
                fieldsMapping.setRotationText("Layer");
811
                dxfWriter.setFieldMapping(fieldsMapping);
812
                dxfWriter.setProjection(((ILayerDefinition)layerDef).getProjection());
813
        }
814

    
815
        public void preProcess() throws EditionException {
816
                dxfWriter.preProcess();
817
        }
818

    
819
        public void process(IRowEdited row) throws EditionException {
820
                dxfWriter.process(row);
821
        }
822

    
823
        public void postProcess() throws EditionException {
824
                dxfWriter.postProcess();
825
                try {
826

    
827
                        // close();
828

    
829
                        // Dxf
830
                        FileChannel fcinDxf = new FileInputStream(fTemp).getChannel();
831
                        FileChannel fcoutDxf = new FileOutputStream(m_Fich).getChannel();
832
                        DriverUtilities.copy(fcinDxf, fcoutDxf);
833

    
834
                        // Borramos los temporales
835
                        fTemp.delete();
836
                        reLoad();
837

    
838
                } catch (FileNotFoundException e) {
839
                        e.printStackTrace();
840
                        throw new EditionException(e);
841
                } catch (IOException e) {
842
                        e.printStackTrace();
843
                        throw new EditionException(e);
844
                }
845

    
846
        }
847

    
848
        public String getCapability(String capability) {
849
                return dxfWriter.getCapability(capability);
850
        }
851

    
852
        public void setCapabilities(Properties capabilities) {
853
                dxfWriter.setCapabilities(capabilities);
854

    
855
        }
856

    
857
        public boolean canWriteAttribute(int sqlType) {
858
                return dxfWriter.canWriteAttribute(sqlType);
859
        }
860

    
861
        /*
862
         * (non-Javadoc)
863
         *
864
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#reLoad()
865
         */
866
        public void reLoad() throws IOException {
867
                super.reLoad();
868
                try {
869
                        initialize();
870
                } catch (IOException e) {
871
                        e.printStackTrace();
872
                        throw new IOException("DXF: Error reloading " + path + " file.");
873
                }
874
        }
875

    
876
        public void setFlatness(double flatness) {
877
                // TODO Auto-generated method stub
878

    
879
        }
880

    
881
        public boolean isWritable() {
882
                return m_Fich.canWrite();
883
        }
884

    
885
        public IWriter getWriter() {
886
                return this;
887
        }
888

    
889
}