Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / dxf / DXFMemoryDriver.java @ 18621

History | View | Annotate | Download (34.1 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.geom.Point2D;
50
import java.io.File;
51
import java.io.FileInputStream;
52
import java.io.FileNotFoundException;
53
import java.io.FileOutputStream;
54
import java.io.IOException;
55
import java.nio.channels.FileChannel;
56
import java.sql.Types;
57
import java.util.ArrayList;
58
import java.util.Properties;
59
import java.util.logging.Logger;
60

    
61
import javax.swing.table.DefaultTableModel;
62

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

    
77
import com.hardcode.gdbms.driver.DriverUtilities;
78
import com.hardcode.gdbms.driver.exceptions.FileNotFoundDriverException;
79
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
80
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException;
81
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
82
import com.hardcode.gdbms.driver.exceptions.ReloadDriverException;
83
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
84
import com.hardcode.gdbms.engine.data.edition.DataWare;
85
import com.hardcode.gdbms.engine.values.IntValue;
86
import com.hardcode.gdbms.engine.values.Value;
87
import com.hardcode.gdbms.engine.values.ValueFactory;
88
import com.iver.cit.gvsig.exceptions.visitors.ProcessWriterVisitorException;
89
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
90
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException;
91
import com.iver.cit.gvsig.fmap.core.FPoint2D;
92
import com.iver.cit.gvsig.fmap.core.FPoint3D;
93
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
94
import com.iver.cit.gvsig.fmap.core.FPolygon3D;
95
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
96
import com.iver.cit.gvsig.fmap.core.FPolyline3D;
97
import com.iver.cit.gvsig.fmap.core.FShape;
98
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
99
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
100
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
101
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
102
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
103
import com.iver.cit.gvsig.fmap.drivers.AbstractCadMemoryDriver;
104
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
105
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
106
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
107
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
108
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
109
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
110
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
111
import com.iver.cit.gvsig.fmap.edition.IWriteable;
112
import com.iver.cit.gvsig.fmap.edition.IWriter;
113
import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfFieldsMapping;
114
import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfWriter;
115
import com.iver.cit.gvsig.fmap.rendering.ILegend;
116
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
117
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
118
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.AttrInTableLabelingStrategy;
119
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.ILabelingStrategy;
120

    
121
/**
122
 * @author jmorell (jose.morell@gmail.com)
123
 * @author azabala
124
 * @version 29-dic-2004
125
 */
126
public class DXFMemoryDriver extends AbstractCadMemoryDriver implements
127
                VectorialFileDriver, WithDefaultLegend, ISpatialWriter, IWriteable {
128

    
129
        private static String tempDirectoryPath = System
130
                        .getProperty("java.io.tmpdir");
131

    
132

    
133
        private DxfWriter dxfWriter = new DxfWriter();
134
        private File fTemp;
135
        VectorialUniqueValueLegend defaultLegend;
136
        private File m_Fich;
137
        private DxfFile.EntityFactory featureMaker;
138
        private DxfFile dxfFeatureFile;
139
        private IObjList.vector features;
140
        private DriverAttributes attr = new DriverAttributes();
141

    
142
        /**
143
         * Habilita la utilizaci?n del lector del HEADER del DXF.
144
         */
145
        private DxfFile.VarSettings headerManager;
146
        private AttrInTableLabelingStrategy labeling;
147

    
148
        /*
149
         * (non-Javadoc)
150
         *
151
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#open(java.io.File)
152
         */
153
        public void open(File f) {
154
                m_Fich = f;
155
        }
156

    
157
        public void initialize() throws ReadDriverException {
158
                float heightText = 10;
159

    
160
                attr.setLoadedInMemory(true);
161

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

    
229
                int nAtt = featureMaker.getAttributes().size();
230

    
231
                // Campos de las MemoryLayer:
232
                Value[] auxRow = new Value[10 + nAtt];
233
                ArrayList arrayFields = new ArrayList();
234
                arrayFields.add("ID");
235
                arrayFields.add("FShape");
236
                arrayFields.add("Entity");
237
                arrayFields.add("Layer");
238
                arrayFields.add("Color");
239
                arrayFields.add("Elevation");
240
                arrayFields.add("Thickness");
241
                arrayFields.add("Text");
242
                arrayFields.add("HeightText");
243
                arrayFields.add("RotationText");
244
                for (int i = 0; i < nAtt; i++) {
245
                        String att[] = new String[2];
246
                        att = (String[]) featureMaker.getAttributes().get(i);
247
                        arrayFields.add(att[0]);
248
                }
249

    
250
                labeling = new AttrInTableLabelingStrategy();
251
                ((AttrInTableLabelingStrategy) labeling).setTextFieldId(arrayFields.indexOf("Text"));
252
                ((AttrInTableLabelingStrategy) labeling).setRotationFieldId(arrayFields.indexOf("RotationText"));
253
                ((AttrInTableLabelingStrategy) labeling).setHeightFieldId(arrayFields.indexOf("HeightText"));
254
                ((AttrInTableLabelingStrategy) labeling).setUnit(1); //MapContext.NAMES[1] (meters)
255

    
256
                getTableModel().setColumnIdentifiers(arrayFields.toArray());
257

    
258
                for (int i = 0; i < features.size(); i++) {
259

    
260
                        auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0.0);
261
                        auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(0.0);
262
                        auxRow[ID_FIELD_TEXT] = ValueFactory.createNullValue();
263

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

    
646
                defaultLegend = LegendFactory
647
                                .createVectorialUniqueValueLegend(getShapeType());
648
                defaultLegend.setClassifyingFieldNames(new String[] {"Color"});
649
                defaultLegend.setClassifyingFieldTypes(new int[]{Types.INTEGER,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.INTEGER,Types.DOUBLE,Types.DOUBLE});
650

    
651
                Logger.getAnonymousLogger().info("DXFMemoryDriver: should check if this is a text symbol");
652
                ISymbol myDefaultSymbol = SymbologyFactory.
653
                        createDefaultSymbolByShapeType(getShapeType(), Color.BLACK);
654

    
655
                defaultLegend.setDefaultSymbol(myDefaultSymbol);
656

    
657
                ObjectDriver rs = this;
658
                IntValue clave;
659
                ISymbol theSymbol = null;
660

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

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

686
                                        theSymbol.setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
687
                                        theSymbol.setSize(3);
688
                                        theSymbol.setSizeInPixels(true);
689
                                        */
690

    
691
                                        // jaume, moved to ISymbol
692
                                        theSymbol = SymbologyFactory.
693
                                                createDefaultSymbolByShapeType(
694
                                                                getShapeType(),
695
                                                                AcadColor.getColor(clave.getValue()));
696
                                        theSymbol.setDescription(clave.toString());
697
                                        // Asigna los colores de Autocad a los
698
                                        // bordes
699
                                        // de los pol?gonos.
700
                                        if (theSymbol instanceof IFillSymbol) {
701
                                                ((IFillSymbol) theSymbol).getOutline().setLineColor(AcadColor.getColor(clave
702
                                                                .getValue()));
703

    
704
                                        }
705
                                        defaultLegend.addSymbol(clave, theSymbol);
706
                                }
707
                        } // for
708
                } catch (ReadDriverException e) {
709
                        throw new InitializeDriverException(getName(),e);
710
                }
711
        }
712

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

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

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

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

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

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

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

    
771
        }
772

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

    
781
        }
782

    
783
        public File getFile() {
784
                return m_Fich;
785
        }
786

    
787
        public boolean canWriteGeometry(int gvSIGgeometryType) {
788
                return dxfWriter.canWriteGeometry(gvSIGgeometryType);
789
        }
790

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

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

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

    
817
        public void preProcess() throws StartWriterVisitorException {
818
                dxfWriter.preProcess();
819
        }
820

    
821
        public void process(IRowEdited row) throws ProcessWriterVisitorException {
822
                dxfWriter.process(row);
823
        }
824

    
825
        public void postProcess() throws StopWriterVisitorException {
826
                dxfWriter.postProcess();
827
                try {
828

    
829
                        // close();
830

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

    
836
                        // Borramos los temporales
837
                        fTemp.delete();
838
//                        reload();
839

    
840
                } catch (FileNotFoundException e) {
841
                        throw new StopWriterVisitorException(getName(),e);
842
                } catch (IOException e) {
843
                        throw new StopWriterVisitorException(getName(),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
        public void reload() throws ReloadDriverException {
863
                super.reload();
864
                try {
865
                        initialize();
866
                } catch (InitializeDriverException e) {
867
                        throw new ReloadDriverException(getName(),e);
868
                } catch (ReadDriverException e) {
869
                        throw new ReloadDriverException(getName(),e);
870
                }
871
        }
872

    
873
        public boolean isWritable() {
874
                return m_Fich.canWrite();
875
        }
876

    
877
        public IWriter getWriter() {
878
                return this;
879
        }
880

    
881
        public ITableDefinition getTableDefinition() {
882
                return dxfWriter.getTableDefinition();
883
        }
884

    
885
        public boolean canAlterTable() {
886
                return false;
887
        }
888

    
889
        public boolean canSaveEdits() {
890
                return dxfWriter.canSaveEdits();
891
        }
892

    
893
        /**
894
         * Returns the drawing entity associated to an FMap feature
895
         * */
896
        public Object getCadSource(int index) {
897
                IObjList.vector vector = (IObjList.vector) ((DxfFeatureMaker) featureMaker)
898
                .getObjects();
899
                return vector.get(index);
900
        }
901

    
902
        public boolean isWriteAll() {
903
                return true;
904
        }
905

    
906
        public ILabelingStrategy getDefaultLabelingStrategy() {
907
                return labeling;
908
        }
909
        public int getFieldType(int i) {
910
            DefaultTableModel dtm=getTableModel();
911
                String columnName=dtm.getColumnName(i);
912
            if (columnName.equals("ID")){
913
                    return Types.INTEGER;
914
            } else if (columnName.equals("FShape")){
915
                    return Types.VARCHAR;
916
            } else if (columnName.equals("Entity")){
917
                    return Types.VARCHAR;
918
                } else if (columnName.equals("Layer")){
919
                    return Types.VARCHAR;
920
            } else if (columnName.equals("Color")){
921
                    return Types.INTEGER;
922
            } else if (columnName.equals("Elevation")){
923
                    return Types.DOUBLE;
924
            } else if (columnName.equals("Thickness")){
925
                    return Types.DOUBLE;
926
            } else if (columnName.equals("HeightText")){
927
                    return Types.DOUBLE;
928
            } else if (columnName.equals("RotationText")){
929
                    return Types.DOUBLE;
930
            } else if (columnName.equals("Text")){
931
                    return Types.VARCHAR;
932
            } else{
933
                    return Types.VARCHAR;
934
            }
935
        }
936
}