Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / dxf / DXFMemoryDriver.java @ 10717

History | View | Annotate | Download (31.8 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.geo.Point3D;
62
import org.cresques.io.DxfFile;
63
import org.cresques.px.IObjList;
64
import org.cresques.px.dxf.AcadColor;
65
import org.cresques.px.dxf.DxfFeatureMaker;
66
import org.cresques.px.dxf.DxfHeaderManager;
67
import org.cresques.px.gml.Feature;
68
import org.cresques.px.gml.LineString;
69
import org.cresques.px.gml.LineString3D;
70
import org.cresques.px.gml.Point;
71
import org.cresques.px.gml.Polygon;
72
import org.cresques.px.gml.Polygon3D;
73

    
74
import com.hardcode.gdbms.driver.DriverUtilities;
75
import com.hardcode.gdbms.engine.data.driver.DriverException;
76
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
77
import com.hardcode.gdbms.engine.data.edition.DataWare;
78
import com.hardcode.gdbms.engine.values.IntValue;
79
import com.hardcode.gdbms.engine.values.Value;
80
import com.hardcode.gdbms.engine.values.ValueFactory;
81
import com.iver.cit.gvsig.fmap.core.FPoint2D;
82
import com.iver.cit.gvsig.fmap.core.FPoint3D;
83
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
84
import com.iver.cit.gvsig.fmap.core.FPolygon3D;
85
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
86
import com.iver.cit.gvsig.fmap.core.FPolyline3D;
87
import com.iver.cit.gvsig.fmap.core.FShape;
88
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
89
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
90
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
91
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
92
import com.iver.cit.gvsig.fmap.drivers.AbstractCadMemoryDriver;
93
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
94
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
95
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
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.rendering.Legend;
106
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
107
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
108

    
109
/**
110
 * @author jmorell (jose.morell@gmail.com)
111
 * @author azabala
112
 * @version 29-dic-2004
113
 */
114
public class DXFMemoryDriver extends AbstractCadMemoryDriver implements
115
                VectorialFileDriver, WithDefaultLegend, ISpatialWriter, IWriteable {
116

    
117
        private static String tempDirectoryPath = System
118
                        .getProperty("java.io.tmpdir");
119

    
120

    
121
        private DxfWriter dxfWriter = new DxfWriter();
122

    
123
        private File fTemp;
124

    
125
        VectorialUniqueValueLegend defaultLegend;
126

    
127
        private String path;
128

    
129
        private File m_Fich;
130

    
131
        private DxfFile.EntityFactory featureMaker;
132

    
133
        private DxfFile dxfFeatureFile;
134

    
135
        private IObjList.vector features;
136

    
137
        private DriverAttributes attr = new DriverAttributes();
138

    
139
        /**
140
         * Habilita la utilizaci?n del lector del HEADER del DXF.
141
         */
142
        private DxfFile.VarSettings headerManager;
143

    
144
        /*
145
         * (non-Javadoc)
146
         *
147
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#open(java.io.File)
148
         */
149
        public void open(File f) throws IOException {
150
                m_Fich = f;
151
        }
152

    
153
        public void initialize() throws IOException {
154
                float heightText = 10;
155

    
156
                attr.setLoadedInMemory(true);
157

    
158
                IProjection proj = CRSFactory.getCRS("EPSG:23030");
159
                featureMaker = new DxfFeatureMaker(proj);
160
                headerManager = new DxfHeaderManager();
161
                dxfFeatureFile = new DxfFile(proj, m_Fich.getAbsolutePath(),
162
                                featureMaker, headerManager);
163
                dxfFeatureFile.load();
164
                features = (IObjList.vector) ((DxfFeatureMaker) featureMaker)
165
                                .getObjects();
166
                String acadVersion = (String) ((DxfHeaderManager) headerManager)
167
                                .getAcadVersion();
168
                System.out.println("initialize(): acadVersion = " + acadVersion);
169
                if (!featureMaker.isDxf3DFile() && !headerManager.isWritedDxf3D()) { // y no
170
                                                                                                                                                                // est?n
171
                                                                                                                                                                // todos
172
                                                                                                                                                                // a
173
                                                                                                                                                                // 9999
174
                        Feature[] features2D = new Feature[features.size()];
175
                        for (int i = 0; i < features.size(); i++) {
176
                                Feature fea = (Feature) features.get(i);
177
                                if (fea.getGeometry() instanceof org.cresques.px.gml.Point3D) {
178
                                        Point point = (Point) fea.getGeometry();
179
                                        Point point2 = new Point();
180
                                        for (int j = 0; j < point.pointNr(); j++) {
181
                                                point2.add(point.get(j));
182
                                        }
183
                                        point2.setTextPoint(point.isTextPoint());
184
                                        fea.setGeometry(point2);
185
                                        features2D[i] = fea;
186
                                        // } else if (fea.getGeometry() instanceof InsPoint3D) {
187
                                        // InsPoint insPoint = (InsPoint)fea.getGeometry();
188
                                        // InsPoint insPoint2 = new InsPoint();
189
                                        // for (int j=0;j<insPoint.pointNr();j++) {
190
                                        // insPoint2.add(insPoint.get(j));
191
                                        // }
192
                                        // fea.setGeometry(insPoint2);
193
                                        // features2D[i] = fea;
194
                                } else if (fea.getGeometry() instanceof LineString3D) {
195
                                        LineString lineString = (LineString) fea.getGeometry();
196
                                        LineString lineString2 = new LineString();
197
                                        for (int j = 0; j < lineString.pointNr(); j++) {
198
                                                lineString2.add(lineString.get(j));
199
                                        }
200
                                        fea.setGeometry(lineString2);
201
                                        features2D[i] = fea;
202
                                } else if (fea.getGeometry() instanceof Polygon3D) {
203
                                        Polygon polygon = (Polygon) fea.getGeometry();
204
                                        Polygon polygon2 = new Polygon();
205
                                        for (int j = 0; j < polygon.pointNr(); j++) {
206
                                                polygon2.add(polygon.get(j));
207
                                        }
208
                                        fea.setGeometry(polygon2);
209
                                        features2D[i] = fea;
210
                                }
211
                        }
212
                        features.clear();
213
                        for (int i = 0; i < features2D.length; i++) {
214
                                features.add(features2D[i]);
215
                        }
216
                }
217
                // String acadVersion =
218
                // (String)((DxfHeaderManager)headerManager).getAcadVersion();
219
                // System.out.println("initialize(): acadVersion = " + acadVersion);
220

    
221
                int nAtt = featureMaker.getAttributes().size();
222

    
223
                // Campos de las MemoryLayer:
224
                Value[] auxRow = new Value[10 + nAtt];
225
                ArrayList arrayFields = new ArrayList();
226
                arrayFields.add("ID");
227
                arrayFields.add("FShape");
228
                arrayFields.add("Entity");
229
                arrayFields.add("Layer");
230
                arrayFields.add("Color");
231
                arrayFields.add("Elevation");
232
                arrayFields.add("Thickness");
233
                arrayFields.add("Text");
234
                arrayFields.add("HeightText");
235
                arrayFields.add("RotationText");
236
                for (int i = 0; i < nAtt; i++) {
237
                        String att[] = new String[2];
238
                        att = (String[]) featureMaker.getAttributes().get(i);
239
                        arrayFields.add(att[0]);
240
                }
241

    
242
                getTableModel().setColumnIdentifiers(arrayFields.toArray());
243

    
244
                for (int i = 0; i < features.size(); i++) {
245

    
246
                        auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0.0);
247
                        auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(0.0);
248
                        auxRow[ID_FIELD_TEXT] = ValueFactory.createNullValue();
249

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

    
632
                defaultLegend = LegendFactory
633
                                .createVectorialUniqueValueLegend(getShapeType());
634
                defaultLegend.setFieldName("Color");
635
                defaultLegend.setLabelField("Text");
636

    
637
                FSymbol myDefaultSymbol = new FSymbol(getShapeType());
638
                myDefaultSymbol.setShapeVisible(true);
639
                myDefaultSymbol.setFontSizeInPixels(false);
640
                myDefaultSymbol.setFont(new Font("SansSerif",Font.PLAIN, 9));
641
                myDefaultSymbol.setFontColor(Color.BLACK);
642
                myDefaultSymbol.setFontSize(heightText);
643
                myDefaultSymbol.setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
644
                myDefaultSymbol.setSize(3);
645
                myDefaultSymbol.setSizeInPixels(true);
646

    
647
                defaultLegend.setDefaultSymbol(myDefaultSymbol);
648

    
649
                defaultLegend.setLabelHeightField("HeightText");
650
                defaultLegend.setLabelRotationField("RotationText");
651

    
652
                ObjectDriver rs = this;
653
                IntValue clave;
654
                FSymbol theSymbol = null;
655

    
656
                try {
657
                        // TODO: Provisional hasta que cambiemos los s?mbolos.
658
                        /*
659
                         * BufferedImage bi= new BufferedImage(5, 5,
660
                         * BufferedImage.TYPE_INT_ARGB); Graphics2D big =
661
                         * bi.createGraphics(); Color color=new Color(0,0,0,0);
662
                         * big.setBackground(color); big.clearRect(0, 0, 5, 5); Paint
663
                         * fillProv = null; Rectangle2D rProv = new Rectangle();
664
                         * rProv.setFrame(0, 0,5,5); fillProv = new TexturePaint(bi,rProv);
665
                         */
666

    
667
                        for (long j = 0; j < rs.getRowCount(); j++) {
668
                                clave = (IntValue) rs.getFieldValue(j, ID_FIELD_COLOR);
669
                                if (defaultLegend.getSymbolByValue(clave) == null) {
670
                                        theSymbol = new FSymbol(getShapeType());
671
                                        theSymbol.setDescription(clave.toString());
672
                                        theSymbol.setColor(AcadColor.getColor(clave.getValue()));
673
                                        // theSymbol.setFill(fillProv);
674
                                        // 050202, jmorell: Asigna los colores de Autocad a los
675
                                        // bordes
676
                                        // de los pol?gonos.
677
                                        theSymbol.setOutlineColor(AcadColor.getColor(clave
678
                                                        .getValue()));
679

    
680
                                        theSymbol.setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
681
                                        theSymbol.setSize(3);
682
                                        theSymbol.setSizeInPixels(true);
683

    
684
                                        defaultLegend.addSymbol(clave, theSymbol);
685
                                }
686
                        } // for
687
                } catch (DriverException e) {
688
                        e.printStackTrace();
689
                        throw new IOException(
690
                                        "Error al poner la leyenda por defecto en el DXF");
691
                }
692
        }
693

    
694
        /*
695
         * (non-Javadoc)
696
         *
697
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#accept(java.io.File)
698
         */
699
        public boolean accept(File f) {
700
                return f.getName().toUpperCase().endsWith("DXF");
701
        }
702

    
703
        /*
704
         * (non-Javadoc)
705
         *
706
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getShapeType()
707
         */
708
        public int getShapeType() {
709
                return FShape.MULTI;
710
        }
711

    
712
        /*
713
         * (non-Javadoc)
714
         *
715
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getName()
716
         */
717
        public String getName() {
718
                return "gvSIG DXF Memory Driver";
719
        }
720

    
721
        /*
722
         * (non-Javadoc)
723
         *
724
         * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
725
         */
726
        public Legend getDefaultLegend() {
727
                return defaultLegend;
728
        }
729

    
730
        /*
731
         * (non-Javadoc)
732
         *
733
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
734
         */
735
        public DriverAttributes getDriverAttributes() {
736
                return attr;
737
        }
738

    
739
        /**
740
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
741
         */
742
        public int[] getPrimaryKeys() throws DriverException {
743
                return null;
744
        }
745

    
746
        /**
747
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
748
         */
749
        public void write(DataWare arg0) throws DriverException {
750
                // TODO Auto-generated method stub
751

    
752
        }
753

    
754
        /*
755
         * (non-Javadoc)
756
         *
757
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#close()
758
         */
759
        public void close() throws IOException {
760
                // TODO Auto-generated method stub
761

    
762
        }
763

    
764
        public File getFile() {
765
                return m_Fich;
766
        }
767

    
768
        public boolean canWriteGeometry(int gvSIGgeometryType) {
769
                return dxfWriter.canWriteGeometry(gvSIGgeometryType);
770
        }
771

    
772
        public void initialize(ITableDefinition layerDef) throws EditionException {
773
                int aux = (int) (Math.random() * 1000);
774
                fTemp = new File(tempDirectoryPath + "/tmpDxf" + aux + ".dxf");
775
                dxfWriter.setFile(fTemp);
776

    
777
                dxfWriter.initialize(layerDef);
778
                /*
779
                 * arrayFields.add("ID"); arrayFields.add("FShape");
780
                 * arrayFields.add("Entity"); arrayFields.add("Layer");
781
                 * arrayFields.add("Color"); arrayFields.add("Elevation");
782
                 * arrayFields.add("Thickness"); arrayFields.add("Text");
783
                 * arrayFields.add("HeightText"); arrayFields.add("RotationText");
784
                 */
785

    
786
                DxfFieldsMapping fieldsMapping = new DxfFieldsMapping();
787
                fieldsMapping.setLayerField("Layer");
788
                fieldsMapping.setColorField("Color");
789
                fieldsMapping.setElevationField("Elevation");
790
                fieldsMapping.setThicknessField("Thickness");
791
                fieldsMapping.setTextField("Text");
792
                fieldsMapping.setHeightText("HeightText");
793
                fieldsMapping.setRotationText("Layer");
794
                dxfWriter.setFieldMapping(fieldsMapping);
795
                dxfWriter.setProjection(((ILayerDefinition)layerDef).getProjection());
796
        }
797

    
798
        public void preProcess() throws EditionException {
799
                dxfWriter.preProcess();
800
        }
801

    
802
        public void process(IRowEdited row) throws EditionException {
803
                dxfWriter.process(row);
804
        }
805

    
806
        public void postProcess() throws EditionException {
807
                dxfWriter.postProcess();
808
                try {
809

    
810
                        // close();
811

    
812
                        // Dxf
813
                        FileChannel fcinDxf = new FileInputStream(fTemp).getChannel();
814
                        FileChannel fcoutDxf = new FileOutputStream(m_Fich).getChannel();
815
                        DriverUtilities.copy(fcinDxf, fcoutDxf);
816

    
817
                        // Borramos los temporales
818
                        fTemp.delete();
819
                        reload();
820

    
821
                } catch (FileNotFoundException e) {
822
                        e.printStackTrace();
823
                        throw new EditionException(e);
824
                } catch (IOException e) {
825
                        e.printStackTrace();
826
                        throw new EditionException(e);
827
                } catch (DriverException e) {
828
                        e.printStackTrace();
829
                        throw new EditionException(e);
830
                }
831

    
832
        }
833

    
834
        public String getCapability(String capability) {
835
                return dxfWriter.getCapability(capability);
836
        }
837

    
838
        public void setCapabilities(Properties capabilities) {
839
                dxfWriter.setCapabilities(capabilities);
840

    
841
        }
842

    
843
        public boolean canWriteAttribute(int sqlType) {
844
                return dxfWriter.canWriteAttribute(sqlType);
845
        }
846

    
847
        /*
848
         * (non-Javadoc)
849
         *
850
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#reLoad()
851
         */
852
        public void reload() throws IOException, DriverException {
853
                super.reload();
854
                try {
855
                        initialize();
856
                } catch (IOException e) {
857
                        e.printStackTrace();
858
                        throw new IOException("DXF: Error reloading " + path + " file.");
859
                }
860
        }
861

    
862
//        public void setFlatness(double flatness) {
863
//                // TODO Auto-generated method stub
864
//
865
//        }
866

    
867
        public boolean isWritable() {
868
                return m_Fich.canWrite();
869
        }
870

    
871
        public IWriter getWriter() {
872
                return this;
873
        }
874

    
875
        public ITableDefinition getTableDefinition() {
876
                return dxfWriter.getTableDefinition();
877
        }
878

    
879
        public boolean canAlterTable() {
880
                return false;
881
        }
882

    
883
        public boolean canSaveEdits() {
884
                return dxfWriter.canSaveEdits();
885
        }
886

    
887
        public boolean isWriteAll() {
888
                return true;
889
        }
890

    
891
}