Statistics
| Revision:

root / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / dxf / DXFMemoryDriver.java @ 23971

History | View | Annotate | Download (34.7 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.sql.Types;
58
import java.util.ArrayList;
59
import java.util.Date;
60
import java.util.Properties;
61
import java.util.regex.Matcher;
62
import java.util.regex.Pattern;
63

    
64
import javax.swing.table.DefaultTableModel;
65

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

    
80
import com.hardcode.gdbms.driver.DriverUtilities;
81
import com.hardcode.gdbms.engine.data.driver.DriverException;
82
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
83
import com.hardcode.gdbms.engine.data.edition.DataWare;
84
import com.hardcode.gdbms.engine.values.IntValue;
85
import com.hardcode.gdbms.engine.values.Value;
86
import com.hardcode.gdbms.engine.values.ValueFactory;
87
import com.iver.cit.gvsig.fmap.core.FPoint2D;
88
import com.iver.cit.gvsig.fmap.core.FPoint3D;
89
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
90
import com.iver.cit.gvsig.fmap.core.FPolygon3D;
91
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
92
import com.iver.cit.gvsig.fmap.core.FPolyline3D;
93
import com.iver.cit.gvsig.fmap.core.FShape;
94
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
95
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
96
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
97
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
98
import com.iver.cit.gvsig.fmap.drivers.AbstractCadMemoryDriver;
99
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
100
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
101
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
102
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
103
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
104
import com.iver.cit.gvsig.fmap.edition.EditionException;
105
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
106
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
107
import com.iver.cit.gvsig.fmap.edition.IWriteable;
108
import com.iver.cit.gvsig.fmap.edition.IWriter;
109
import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfFieldsMapping;
110
import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfWriter;
111
import com.iver.cit.gvsig.fmap.rendering.Legend;
112
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
113
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
114

    
115
/**
116
 * @author jmorell (jose.morell@gmail.com)
117
 * @author azabala
118
 * @version 29-dic-2004
119
 */
120
public class DXFMemoryDriver extends AbstractCadMemoryDriver implements
121
                VectorialFileDriver, WithDefaultLegend, ISpatialWriter, IWriteable {
122

    
123
        private static String tempDirectoryPath = System
124
                        .getProperty("java.io.tmpdir");
125

    
126

    
127
        private DxfWriter dxfWriter = new DxfWriter();
128

    
129
        private File fTemp;
130

    
131
        VectorialUniqueValueLegend defaultLegend;
132

    
133
        private String path;
134

    
135
        private File m_Fich;
136

    
137
        private DxfFile.EntityFactory featureMaker;
138

    
139
        private DxfFile dxfFeatureFile;
140

    
141
        private IObjList.vector features;
142

    
143
        private DriverAttributes attr = new DriverAttributes();
144

    
145
        /**
146
         * Habilita la utilizaci?n del lector del HEADER del DXF.
147
         */
148
        private DxfFile.VarSettings headerManager;
149

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

    
159
        public void initialize() throws IOException {
160
                float heightText = 10;
161

    
162
                attr.setLoadedInMemory(true);
163

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

    
227
                int nAtt = featureMaker.getAttributes().size();
228

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

    
248
                getTableModel().setColumnIdentifiers(arrayFields.toArray());
249

    
250
                for (int i = 0; i < features.size(); i++) {
251

    
252
                        auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0.0);
253
                        auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(0.0);
254
                        auxRow[ID_FIELD_TEXT] = ValueFactory.createNullValue();
255

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

    
638
                defaultLegend = LegendFactory
639
                                .createVectorialUniqueValueLegend(getShapeType());
640
                defaultLegend.setFieldName("Color");
641
                defaultLegend.setLabelField("Text");
642

    
643
                FSymbol myDefaultSymbol = new FSymbol(getShapeType());
644
                myDefaultSymbol.setShapeVisible(true);
645
                myDefaultSymbol.setFontSizeInPixels(false);
646
                myDefaultSymbol.setFont(new Font("SansSerif",Font.PLAIN, 9));
647
                myDefaultSymbol.setFontColor(Color.BLACK);
648
                myDefaultSymbol.setFontSize(heightText);
649
                myDefaultSymbol.setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
650
                myDefaultSymbol.setSize(3);
651
                myDefaultSymbol.setSizeInPixels(true);
652

    
653
                defaultLegend.setDefaultSymbol(myDefaultSymbol);
654

    
655
                defaultLegend.setLabelHeightField("HeightText");
656
                defaultLegend.setLabelRotationField("RotationText");
657

    
658
                ObjectDriver rs = this;
659
                IntValue clave;
660
                FSymbol theSymbol = null;
661

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

    
673
                        for (long j = 0; j < rs.getRowCount(); j++) {
674
                                clave = (IntValue) rs.getFieldValue(j, ID_FIELD_COLOR);
675
                                if (defaultLegend.getSymbolByValue(clave) == null) {
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
                                        defaultLegend.addSymbol(clave, theSymbol);
691
                                }
692
                        } // for
693
                } catch (DriverException e) {
694
                        e.printStackTrace();
695
                        throw new IOException(
696
                                        "Error al poner la leyenda por defecto en el DXF");
697
                }
698
        }
699

    
700
        /*
701
         * (non-Javadoc)
702
         *
703
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#accept(java.io.File)
704
         */
705
        public boolean accept(File f) {
706
                return f.getName().toUpperCase().endsWith("DXF");
707
        }
708

    
709
        /*
710
         * (non-Javadoc)
711
         *
712
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getShapeType()
713
         */
714
        public int getShapeType() {
715
                return FShape.MULTI;
716
        }
717

    
718
        /*
719
         * (non-Javadoc)
720
         *
721
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getName()
722
         */
723
        public String getName() {
724
                return "gvSIG DXF Memory Driver";
725
        }
726

    
727
        /*
728
         * (non-Javadoc)
729
         *
730
         * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
731
         */
732
        public Legend getDefaultLegend() {
733
                return defaultLegend;
734
        }
735

    
736
        /*
737
         * (non-Javadoc)
738
         *
739
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
740
         */
741
        public DriverAttributes getDriverAttributes() {
742
                return attr;
743
        }
744

    
745
        /**
746
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
747
         */
748
        public int[] getPrimaryKeys() throws DriverException {
749
                return null;
750
        }
751

    
752
        /**
753
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
754
         */
755
        public void write(DataWare arg0) throws DriverException {
756
                // TODO Auto-generated method stub
757

    
758
        }
759

    
760
        /*
761
         * (non-Javadoc)
762
         *
763
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#close()
764
         */
765
        public void close() throws IOException {
766
                // TODO Auto-generated method stub
767

    
768
        }
769

    
770
        public File getFile() {
771
                return m_Fich;
772
        }
773

    
774
        public boolean canWriteGeometry(int gvSIGgeometryType) {
775
                return dxfWriter.canWriteGeometry(gvSIGgeometryType);
776
        }
777

    
778
        public void initialize(ITableDefinition layerDef) throws EditionException {
779
                int aux = (int) (Math.random() * 1000);
780
                fTemp = new File(tempDirectoryPath + "/tmpDxf" + aux + ".dxf");
781
                dxfWriter.setFile(fTemp);
782

    
783
                dxfWriter.initialize(layerDef);
784
                /*
785
                 * arrayFields.add("ID"); arrayFields.add("FShape");
786
                 * arrayFields.add("Entity"); arrayFields.add("Layer");
787
                 * arrayFields.add("Color"); arrayFields.add("Elevation");
788
                 * arrayFields.add("Thickness"); arrayFields.add("Text");
789
                 * arrayFields.add("HeightText"); arrayFields.add("RotationText");
790
                 */
791

    
792
                DxfFieldsMapping fieldsMapping = new DxfFieldsMapping();
793
                fieldsMapping.setLayerField("Layer");
794
                fieldsMapping.setColorField("Color");
795
                fieldsMapping.setElevationField("Elevation");
796
                fieldsMapping.setThicknessField("Thickness");
797
                fieldsMapping.setTextField("Text");
798
                fieldsMapping.setHeightText("HeightText");
799
                fieldsMapping.setRotationText("Layer");
800
                dxfWriter.setFieldMapping(fieldsMapping);
801
                dxfWriter.setProjection(((ILayerDefinition)layerDef).getProjection());
802
        }
803

    
804
        public void preProcess() throws EditionException {
805
                dxfWriter.preProcess();
806
        }
807

    
808
        public void process(IRowEdited row) throws EditionException {
809
                dxfWriter.process(row);
810
        }
811

    
812
        public void postProcess() throws EditionException {
813
                dxfWriter.postProcess();
814
                try {
815

    
816
                        // close();
817

    
818
                        // Dxf
819
                        FileChannel fcinDxf = new FileInputStream(fTemp).getChannel();
820
                        FileChannel fcoutDxf = new FileOutputStream(m_Fich).getChannel();
821
                        DriverUtilities.copy(fcinDxf, fcoutDxf);
822

    
823
                        // Borramos los temporales
824
                        fTemp.delete();
825
//                        reload();
826

    
827
                } catch (FileNotFoundException e) {
828
                        e.printStackTrace();
829
                        throw new EditionException(e);
830
                } catch (IOException e) {
831
                        e.printStackTrace();
832
                        throw new EditionException(e);
833
                }
834

    
835
        }
836

    
837
        public String getCapability(String capability) {
838
                return dxfWriter.getCapability(capability);
839
        }
840

    
841
        public void setCapabilities(Properties capabilities) {
842
                dxfWriter.setCapabilities(capabilities);
843

    
844
        }
845

    
846
        public boolean canWriteAttribute(int sqlType) {
847
                return dxfWriter.canWriteAttribute(sqlType);
848
        }
849

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

    
865
//        public void setFlatness(double flatness) {
866
//                // TODO Auto-generated method stub
867
//
868
//        }
869

    
870
        public boolean isWritable() {
871
                return m_Fich.canWrite();
872
        }
873

    
874
        public IWriter getWriter() {
875
                return this;
876
        }
877

    
878
        public ITableDefinition getTableDefinition() {
879
                return dxfWriter.getTableDefinition();
880
        }
881

    
882
        public boolean canAlterTable() {
883
                return false;
884
        }
885

    
886
        public boolean canSaveEdits() {
887
                return dxfWriter.canSaveEdits();
888
        }
889

    
890
        public boolean isWriteAll() {
891
                return true;
892
        }
893
        public int getFieldType(int i) throws DriverException {
894
            DefaultTableModel dtm=getTableModel();
895
                String columnName=dtm.getColumnName(i);
896
            if (columnName.equals("ID")){
897
                    return Types.INTEGER;
898
            } else if (columnName.equals("FShape")){
899
                    return Types.VARCHAR;
900
            } else if (columnName.equals("Entity")){
901
                    return Types.VARCHAR;
902
                } else if (columnName.equals("Layer")){
903
                    return Types.VARCHAR;
904
            } else if (columnName.equals("Color")){
905
                    return Types.INTEGER;
906
            } else if (columnName.equals("Elevation")){
907
                    return Types.DOUBLE;
908
            } else if (columnName.equals("Thickness")){
909
                    return Types.DOUBLE;
910
            } else if (columnName.equals("HeightText")){
911
                    return Types.DOUBLE;
912
            } else if (columnName.equals("RotationText")){
913
                    return Types.DOUBLE;
914
            } else if (columnName.equals("Text")){
915
                    return Types.VARCHAR;
916
            } else{
917
                    return Types.VARCHAR;
918
            }
919
        }
920
        /**
921
         * Replace the Unicode characters formatted for AutoCAD
922
         * by the Unicode character corresponding
923
         *
924
         * @param s
925
         *         Multiline text formatted with Autocad.
926
         *
927
         * @return Text fixed
928
         */
929
        private String fixUnicode(String s){
930
                // Viene una cadena tal que as? CA\U+00D1O1
931
                // (una especie de formato Unicode de Autocad)
932
                // y hay que devolver CA?O1
933

    
934
                String s2 = "";
935
                String patron = "(\\\\[U][+])([0-9A-Fa-f]{4})";
936
                Pattern compiledPatron = Pattern.compile(patron);
937
                Matcher matcher = compiledPatron.matcher(s);
938

    
939
                int lastEnd = 0;
940
                while(matcher.find()){
941
                        int start = matcher.start();
942
                        int end = matcher.end();
943

    
944
                        String code = matcher.group(2);
945
                        String hexa = "0x"+code;
946
                        int caracter = Integer.decode(hexa).intValue();
947

    
948
                        s2 = s2 + s.substring(lastEnd, start) + (char)caracter;
949
                        lastEnd = end;
950
                }
951
                s2 = s2 + s.substring(lastEnd);
952
                return s2;
953
        }
954

    
955

    
956
        /**
957
         * Extracts the text of a multiline text formatted with Autocad
958
         *
959
         * @param mtext
960
         *            Multiline text formatted with Autocad.
961
         *            ACAD seems to add braces ({ }) and backslash-P's
962
         *            to indicate paragraphs, as well as fonts and / or sizes.
963
         *
964
         * @return Text extracted
965
         */
966
        private String getTextFromMtext(String mtext){
967
                String text = "";
968
                String patron = "([^{]*)([{][^;]*[;])([^}]*)([}])";
969
                Pattern compiledPatron = Pattern.compile(patron);
970
                Matcher matcher = compiledPatron.matcher(mtext);
971
                int lastEnd = 0;
972
                while(matcher.find()){
973
                        for (int i=1; i<=matcher.groupCount(); i++){
974
                                if (i==1){
975
                                        text = text + matcher.group(i);
976
                                }
977
                                if (i%4 == 3){
978
                                        String grupo = matcher.group(i).replace("\\P", " \n");
979
                                        if(grupo.contains(";")){
980
                                                String[] ss = grupo.split(";");
981
                                                text = text + ss[ss.length-1];
982
                                        } else {
983
                                                text = text + grupo;
984
                                        }
985
                                }
986
                        }
987
                        lastEnd = matcher.end();
988
                }
989
                text = text + mtext.substring(lastEnd);
990
                return text;
991
        }
992
}