Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / writers / dxf / DxfWriter.java @ 5136

History | View | Annotate | Download (32.1 KB)

1
package com.iver.cit.gvsig.fmap.edition.writers.dxf;
2

    
3
import java.awt.geom.AffineTransform;
4
import java.awt.geom.PathIterator;
5
import java.awt.geom.Point2D;
6
import java.io.File;
7
import java.io.IOException;
8
import java.util.Vector;
9

    
10
import org.cresques.cts.IProjection;
11
import org.cresques.geo.Point3D;
12
import org.cresques.io.DxfFile;
13
import org.cresques.io.DxfGroup;
14
import org.cresques.io.DxfGroupVector;
15
import org.cresques.px.dxf.DxfCircle;
16
import org.cresques.px.dxf.DxfEntityMaker;
17
import org.cresques.px.dxf.DxfLayer;
18

    
19
import com.iver.cit.gvsig.fmap.DriverException;
20
import com.iver.cit.gvsig.fmap.core.FArc2D;
21
import com.iver.cit.gvsig.fmap.core.FCircle2D;
22
import com.iver.cit.gvsig.fmap.core.FEllipse2D;
23
import com.iver.cit.gvsig.fmap.core.FGeometry;
24
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
25
import com.iver.cit.gvsig.fmap.core.FPoint2D;
26
import com.iver.cit.gvsig.fmap.core.FPoint3D;
27
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
28
import com.iver.cit.gvsig.fmap.core.FShape;
29
import com.iver.cit.gvsig.fmap.core.IFeature;
30
import com.iver.cit.gvsig.fmap.core.IGeometry;
31
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
32
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
33
import com.iver.cit.gvsig.fmap.edition.EditionException;
34
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
35
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
36
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
37
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter;
38
import com.iver.cit.gvsig.fmap.layers.FLayer;
39
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
40
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
41

    
42
public class DxfWriter extends AbstractWriter implements ISpatialWriter {
43

    
44
        private DxfFieldsMapping fieldMapping = null;
45

    
46
        private File file;
47

    
48
        private FieldDescription[] fieldsDesc = null;
49

    
50
        private DxfFile.EntityFactory entityMaker;
51

    
52
        private IProjection proj = null;
53

    
54
        private DxfFile dxfFile;
55

    
56
        int handle = 40; // Revisar porqu? es 40.
57

    
58
        int k = 0;
59

    
60
        boolean dxf3DFile = false;
61

    
62
        public void setFile(File f) {
63
                file = f;
64
        }
65

    
66
        public boolean canWriteAttribute(int sqlType) {
67
                return false;
68
        }
69

    
70
        public boolean canWriteGeometry(int gvSIGgeometryType) {
71
                return true; // I guess all geometries can be here...
72
        }
73

    
74
        public void initialize(FLayer layer) throws EditionException {
75
                try {
76
                        SelectableDataSource sds = ((FLyrVect) layer).getRecordset();
77
                        // Aqu? hay que revisar los campos de sds y compararlos con los
78
                        // que podemos escribir. (Layer, Color, etc).
79
                        fieldsDesc = sds.getFieldsDescription();
80
                } catch (DriverException e) {
81
                        e.printStackTrace();
82
                        throw new EditionException(e);
83
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
84
                        e.printStackTrace();
85
                        throw new EditionException(e);
86
                }
87

    
88
        }
89

    
90
        /**
91
         * Useful to create a layer from scratch Call setFile before using this
92
         * function
93
         * 
94
         * @param lyrDef
95
         * @throws EditionException
96
         */
97
        public void initialize(ILayerDefinition lyrDef) throws EditionException {
98
                fieldsDesc = lyrDef.getFieldsDesc();
99
        }
100

    
101
        public void preProcess() throws EditionException {
102
                if (fieldMapping == null) {
103
                        throw new EditionException(
104
                                        "DXFWriter: You MUST specify the fieldMapping first!!.");
105
                }
106
                // NOTA: La proyecci?n no se usa absolutamente para nada (al menos
107
                // por ahora). Las entidades se escribir?n con las coordenadas con
108
                // las que se crean.
109
                if (proj == null) {
110
                        throw new EditionException(
111
                                        "DXFWriter: You MUST specify the projection first!!.");
112
                }
113

    
114
                entityMaker = new DxfEntityMaker(proj);
115

    
116
        }
117

    
118
public void process(IRowEdited row) throws EditionException {
119
                if (row.getStatus() == IRowEdited.STATUS_DELETED) return;
120
                
121
                try 
122
                {
123
                        IFeature feat = (IFeature) row.getLinkedRow();
124
                    IGeometry geom = feat.getGeometry();
125
                    // TODO: Tratamiento de los campos
126
                    // y modificar los createXXX para que acepten como par?metro
127
                    // los datos de LAYER, COLOR, ELEVATION, THICKNESS, TEXT
128
                    // HEIGHTTEXT, ROTATIONTEXT y el resto que puedan hacer
129
                    // falta.
130
                    
131
                    
132
                    // ////////////////
133
                if (geom.getGeometryType()==FShape.POINT) {
134
                    createPoint2D(handle, k, geom);
135
                    k++;
136
                } else if (geom.getGeometryType()==(FShape.POINT | FShape.Z)) {
137
                    dxf3DFile = true;
138
                    createPoint3D(handle, k, geom);
139
                    k++;
140
                } else if (geom.getGeometryType()==FShape.LINE) {
141
                    createLwPolyline2D(handle, k, geom, false);
142
                    k++;
143
                } else if (geom.getGeometryType()==(FShape.LINE | FShape.Z)) {
144
                    dxf3DFile = true;
145
                    k = createPolyline3D(handle, k, geom);
146
                } else if (geom.getGeometryType()==FShape.POLYGON) {
147
                    // createPolygon2D(handle, k, geom);
148
                    createLwPolyline2D(handle, k, geom, true);                    
149
                    k++;
150
                } else if (geom.getGeometryType()==(FShape.POLYGON | FShape.Z)) {
151
                    dxf3DFile = true;
152
                    k = createPolyline3D(handle, k, geom);
153
                    // k = createPolygon3D(handle, k, geom);
154
                } else if (geom.getGeometryType()==FShape.CIRCLE) {
155
                        FCircle2D circle = (FCircle2D) geom.getInternalShape();
156
                        createCircle2D(handle, k, circle);
157
                    k++;
158
                } else if (geom.getGeometryType()==FShape.ARC) {
159
                        FArc2D arc = (FArc2D) geom.getInternalShape();
160
                        createArc2D(arc);
161
                    k++;
162
                } else if (geom.getGeometryType()==FShape.ELLIPSE) {
163
                        FEllipse2D ellipse = (FEllipse2D) geom.getInternalShape();
164
                        createEllipse2D(ellipse);
165
                    k++;
166
                        /* } else if (geom instanceof FGeometryCollection) {
167
                                // System.out.println("Polil?nea encontrada (Soluci?n
168
                                // provisional).");
169
                                FGeometryCollection gc = (FGeometryCollection)geom;
170
                                IGeometry[] geoms = gc.getGeometries();
171
                                // double[] lineCoords = new double[6];
172
                                // GeneralPathXIterator polylineIt =
173
                                // geoms[i].getGeneralPathXIterator();
174
                                DxfGroupVector plv = new DxfGroupVector();
175
                                DxfGroup polylineLayer = new DxfGroup(8, "default");
176
                                DxfGroup vNum = new DxfGroup();
177
                                vNum.setCode(90);
178
                                vNum.setData(new Integer(fShapes.length+1));
179
                                plv.add(polylineLayer);
180
                                plv.add(vNum);
181
                                Point2D first = new Point2D.Double();
182
                                Point2D last = new Point2D.Double();
183
                                for (int j=0;j<geoms.length;j++) {
184
                                        if (geom.getInternalShape() instanceof FPolyline2D && !(geom.getInternalShape() instanceof FArc2D)) {
185
                                                // System.out.println("L?nea encontrada dentro de la
186
                                                // polil?nea.");
187
                                                FPolyline2D fLine = (FPolyline2D)geom.getInternalShape();
188
                                                double[] lineCoords = new double[6];
189
                                                PathIterator lineIt = fLine.getPathIterator(new AffineTransform());
190
                                                int k = 0;
191
                                                Point2D[] pts = new Point2D[2];
192
                                                while (!lineIt.isDone()) {
193
                                                        int type = lineIt.currentSegment(lineCoords);
194
                                                        pts[k] = new Point2D.Double(lineCoords[0], lineCoords[1]);
195
                                                        k++;
196
                                                        lineIt.next();
197
                                                }
198
                                                DxfGroup vx = new DxfGroup();
199
                                                DxfGroup vy = new DxfGroup();
200
                                                vx.setCode(10);
201
                                                vx.setData(new Double(pts[0].getX()));
202
                                                vy.setCode(20);
203
                                                vy.setData(new Double(pts[0].getY()));
204
                                                plv.add(vx);
205
                                                plv.add(vy);
206
                                                if (j==0) {
207
                                                        first = new Point2D.Double(pts[0].getX(), pts[0].getY());
208
                                                }
209
                                                if (j==fShapes.length-1) {
210
                                                        last = new Point2D.Double(pts[1].getX(), pts[1].getY());
211
                                                        if (first.getX()==last.getX() && first.getY()==last.getY()) {
212
                                                                // Polil?nea cerrada.
213
                                                        } else {
214
                                                                DxfGroup vxf = new DxfGroup();
215
                                                                DxfGroup vyf = new DxfGroup();
216
                                                                vxf.setCode(10);
217
                                                                vxf.setData(new Double(pts[1].getX()));
218
                                                                vyf.setCode(20);
219
                                                                vyf.setData(new Double(pts[1].getY()));
220
                                                                plv.add(vxf);
221
                                                                plv.add(vyf);
222
                                                        }
223
                                                }
224
                                        } else if (fShapes[j] instanceof FArc2D) {
225
                                                // System.out.println("Arco encontrada dentro de la
226
                                                // polil?nea.");
227
                                                FArc2D fArc = (FArc2D)fShapes[j];
228
                                                double[] lineCoords = new double[6];
229
                                                Point2D[] pts = new Point2D[3];
230
                                                pts[0] = fArc.getInit();
231
                                                pts[1] = fArc.getMid();
232
                                                pts[2] = fArc.getEnd();
233
                                                Point2D center = fArc.getCenter(); // TrigonometricalFunctions.getCenter(pts[0],
234
                                                                                                                        // pts[1], pts[2]);
235
                                                // System.out.println("pts[0] = " + pts[0]);
236
                                                // System.out.println("pts[1] = " + pts[1]);
237
                                                // System.out.println("center = " + center);
238
                                                // System.out.println("pts[2] = " + pts[2]);
239
                                                double initAngRad = TrigonometricalFunctions.getAngle(center, pts[0]);
240
                                                double endAngRad = TrigonometricalFunctions.getAngle(center, pts[2]);
241
                                                double angleRad = endAngRad-initAngRad;
242
                                                if (angleRad<0) angleRad = angleRad+2*Math.PI;
243
                                                //
244
                                                // boolean bulgeIsNegative = true;
245
                                                double bulge = 0;
246
                                                if (TrigonometricalFunctions.isCCW(pts[0], pts[1], pts[2])) {
247
                                                        double angleRad2 = angleRad/4.0;
248
                                                        bulge = Math.tan(angleRad2);
249
                                                } else {
250
                                                        angleRad = 2*Math.PI-angleRad;
251
                                                        double angleRad2 = angleRad/4.0;
252
                                                        bulge = -1*Math.tan(angleRad2);
253
                                                }
254
                                                DxfGroup vx = new DxfGroup();
255
                                                DxfGroup vy = new DxfGroup();
256
                                                DxfGroup vb = new DxfGroup();
257
                                                vx.setCode(10);
258
                                                vx.setData(new Double(pts[0].getX()));
259
                                                vy.setCode(20);
260
                                                vy.setData(new Double(pts[0].getY()));
261
                                                vb.setCode(42);
262
                                                vb.setData(new Double(bulge));
263
                                                plv.add(vx);
264
                                                plv.add(vy);
265
                                                plv.add(vb);
266
                                                if (j==0) {
267
                                                        first = new Point2D.Double(pts[0].getX(), pts[0].getY());
268
                                                }
269
                                                if (j==fShapes.length-1) {
270
                                                        last = new Point2D.Double(pts[2].getX(), pts[2].getY());
271
                                                        if (first.getX()==last.getX() && first.getY()==last.getY()) {
272
                                                                // Polil?nea cerrada.
273
                                                        } else {
274
                                                                DxfGroup vxf = new DxfGroup();
275
                                                                DxfGroup vyf = new DxfGroup();
276
                                                                vxf.setCode(10);
277
                                                                vxf.setData(new Double(pts[2].getX()));
278
                                                                vyf.setCode(20);
279
                                                                vyf.setData(new Double(pts[2].getY()));
280
                                                                plv.add(vxf);
281
                                                                plv.add(vyf);
282
                                                        }
283
                                                }                    
284
                                        }
285
                                } */
286
                } else {
287
                    System.out.println("IGeometry not supported yet");
288
                    k++;
289
                }
290
                }
291
                catch(Exception e)
292
                {
293
                        throw new EditionException(e);
294
                }
295

    
296
                
297
        }        private void createArc2D(FArc2D fArc) throws Exception {
298
                Point2D[] pts = new Point2D[3];
299
                pts[0] = fArc.getInit();
300
                pts[1] = fArc.getMid();
301
                pts[2] = fArc.getEnd();
302
                Point2D center = fArc.getCenter();
303
                double radius = center.distance(pts[0]);
304
                double initAngle = UtilFunctions.getAngle(center, pts[0]);
305
                initAngle = Math.toDegrees(initAngle);
306
                // System.out.println("initAngle = " + initAngle);
307
                double midAngle = UtilFunctions.getAngle(center, pts[1]);
308
                midAngle = Math.toDegrees(midAngle);
309
                // System.out.println("midAngle = " + midAngle);
310
                double endAngle = UtilFunctions.getAngle(center, pts[2]);
311
                endAngle = Math.toDegrees(endAngle);
312
                // System.out.println("endAngle = " + endAngle);
313

    
314
                // 050307, jmorell: Resoluci?n de un bug sobre el sentido de
315
                // los arcos.
316
                /*
317
                 * if (!TrigonometricalFunctions.isCCW(pts[0],pts[1],pts[2])){ double
318
                 * aux=initAngle; initAngle=endAngle; endAngle=aux; }
319
                 */
320

    
321
                /*
322
                 * FArc2D arc = (FArc2D)(shapes[0]); Point2D center = arc.getCenter();
323
                 * Point2D init = arc.getInit(); Point2D end = arc.getEnd(); // C?lculo
324
                 * del radio: double radius =
325
                 * Math.sqrt(Math.pow(init.getX()-center.getX(),2)+Math.pow(init.getY()-center.getY(),2)); //
326
                 * double initAngle=TrigonometricalFunctions.getAngle(center, init);
327
                 * initAngle = Math.toDegrees(initAngle); double
328
                 * endAngle=TrigonometricalFunctions.getAngle(center, end); endAngle =
329
                 * Math.toDegrees(endAngle);
330
                 */
331
                DxfGroup arcLayer = new DxfGroup(8, "default");
332
                DxfGroup ax = new DxfGroup();
333
                DxfGroup ay = new DxfGroup();
334
                DxfGroup ac = new DxfGroup();
335
                DxfGroup ai = new DxfGroup();
336
                DxfGroup ae = new DxfGroup();
337
                ax.setCode(10);
338
                ax.setData(new Double(center.getX()));
339
                ay.setCode(20);
340
                ay.setData(new Double(center.getY()));
341
                ac.setCode(40);
342
                ac.setData(new Double(radius));
343
                ai.setCode(50);
344
                ai.setData(new Double(initAngle));
345
                ae.setCode(51);
346
                ae.setData(new Double(endAngle));
347
                DxfGroupVector av = new DxfGroupVector();
348
                av.add(arcLayer);
349
                av.add(ax);
350
                av.add(ay);
351
                av.add(ac);
352
                av.add(ai);
353
                av.add(ae);
354
                entityMaker.createArc(av);
355

    
356
        }
357

    
358
        private void createCircle2D(int handle, int k2, FCircle2D geom)
359
                        throws Exception {
360
                DxfGroupVector polv = new DxfGroupVector();
361
                DxfGroup polylineLayer = new DxfGroup(8, "default");
362
                polv.add(polylineLayer);
363
                DxfGroup handleGroup = new DxfGroup();
364
                handleGroup.setCode(5);
365
                handleGroup.setData(new Integer(handle + k).toString());
366
                polv.add(handleGroup);
367
                DxfGroup circleFlag = new DxfGroup();
368
                circleFlag.setCode(100);
369
                polv.add(circleFlag);
370

    
371
                DxfGroup xvertex = new DxfGroup();
372
                xvertex.setCode(10);
373
                xvertex.setData(new Double(geom.getCenter().getX()));
374
                DxfGroup yvertex = new DxfGroup();
375
                yvertex.setCode(20);
376
                yvertex.setData(new Double(geom.getCenter().getY()));
377
                DxfGroup zvertex = new DxfGroup();
378
                zvertex.setCode(30);
379
                zvertex.setData(new Double(0)); // TODO: COORDENADA Z. REVISAR ESTO PARA
380
                                                                                // ENTIDADES 3D
381

    
382
                DxfGroup radius = new DxfGroup();
383
                radius.setCode(40);
384
                radius.setData(new Double(geom.getRadio()));
385

    
386
                polv.add(xvertex);
387
                polv.add(yvertex);
388
                polv.add(zvertex);
389
                polv.add(radius);
390

    
391
                entityMaker.createCircle(polv);
392

    
393
        }
394

    
395
        private void createEllipse2D(FEllipse2D fElip) throws Exception {
396
                Point2D center = new Point2D.Double((fElip.getInit().getX() + fElip
397
                                .getEnd().getX()) / 2, (fElip.getInit().getY() + fElip.getEnd()
398
                                .getY()) / 2);
399
                double mAxisL = fElip.getDist() * 2;
400
                // System.out.println("mAxisL = " + mAxisL);
401
                /*
402
                 * System.out.println("mAxisL/(center.distance(fElip.getEnd()))*2 = " +
403
                 * mAxisL/(center.distance(fElip.getEnd()))*2); minToMaj.setData(new
404
                 * Double(mAxisL/()*2));
405
                 */
406
                double maAxisL = fElip.getInit().distance(fElip.getEnd());
407

    
408
                Point2D endPointOfMajorAxis = fElip.getEnd();
409
                double azimut = Math.atan2(endPointOfMajorAxis.getX() - center.getX(),
410
                                endPointOfMajorAxis.getY() - center.getY());
411
                double azimut2 = azimut + Math.PI / 2.0;
412
                if (azimut2 >= Math.PI * 2)
413
                        azimut2 = azimut2 - Math.PI * 2;
414
                Point2D endPointOfMinorAxis = new Point2D.Double(center.getX()
415
                                + (fElip.getDist() * Math.sin(azimut2)), center.getY()
416
                                + (fElip.getDist() * Math.cos(azimut2)));
417

    
418
                if (mAxisL >= maAxisL) {
419
                        // El menor debe ser menor que el mayor. Los cambiamos.
420
                        double aux = mAxisL;
421
                        mAxisL = maAxisL;
422
                        maAxisL = aux;
423
                        // Tambi?n cambiamos los puntos finales de los ejes.
424
                        Point2D pAux = endPointOfMinorAxis;
425
                        endPointOfMinorAxis = endPointOfMajorAxis;
426
                        endPointOfMajorAxis = pAux;
427
                }
428
                double mToMAR = mAxisL / maAxisL;
429
                // System.out.println("mToMar = " + mToMAR);
430
                DxfGroup arcLayer = new DxfGroup(8, "default");
431
                DxfGroup x = new DxfGroup();
432
                DxfGroup y = new DxfGroup();
433
                DxfGroup xc = new DxfGroup();
434
                DxfGroup yc = new DxfGroup();
435
                DxfGroup minToMaj = new DxfGroup();
436
                // DxfGroup start = new DxfGroup();
437
                // DxfGroup end = new DxfGroup();
438
                x.setCode(10);
439
                x.setData(new Double(center.getX()));
440
                y.setCode(20);
441
                y.setData(new Double(center.getY()));
442
                xc.setCode(11);
443
                xc.setData(new Double(endPointOfMajorAxis.getX() - center.getX()));
444
                yc.setCode(21);
445
                yc.setData(new Double(endPointOfMajorAxis.getY() - center.getY()));
446
                minToMaj.setCode(40);
447
                minToMaj.setData(new Double(mToMAR));
448
                DxfGroupVector av = new DxfGroupVector();
449
                av.add(arcLayer);
450
                av.add(x);
451
                av.add(y);
452
                av.add(xc);
453
                av.add(yc);
454
                av.add(minToMaj);
455
                entityMaker.createEllipse(av);
456
        }
457

    
458
        public void postProcess() throws EditionException {
459
                // Escribimos realmente lo que hemos montado en memoria.
460
                dxfFile = new DxfFile(null, file.getAbsolutePath(), entityMaker);
461
                dxfFile.setCadFlag(true);
462
                if (dxf3DFile)
463
                        dxfFile.setDxf3DFlag(true);
464
                try {
465
                        dxfFile.save(file.getAbsolutePath());
466
                } catch (IOException e) {
467
                        e.printStackTrace();
468
                        throw new EditionException(e);
469
                }
470

    
471
        }
472

    
473
        public String getName() {
474
                return "DXF Writer";
475
        }
476

    
477
        /**
478
         * @return Returns the fieldMapping.
479
         */
480
        public DxfFieldsMapping getFieldMapping() {
481
                return fieldMapping;
482
        }
483

    
484
        /**
485
         * Use this method BEFORE preProcess.
486
         * 
487
         * @param fieldMapping
488
         *            The fieldMapping to set.
489
         */
490
        public void setFieldMapping(DxfFieldsMapping fieldMapping) {
491
                this.fieldMapping = fieldMapping;
492
        }
493

    
494
        public void write(IGeometry[] geometries, File file) throws Exception {
495
                int handle = 40; // Revisar porqu? es 40.
496
                int k = 0;
497
                boolean dxf3DFile = false;
498
                for (int i = 0; i < geometries.length; i++) {
499
                        IGeometry geom = geometries[i];
500
                        if (geom.getGeometryType() == FShape.POINT) {
501
                                createPoint2D(handle, k, geom);
502
                                k++;
503
                        } else if (geom.getGeometryType() == (FShape.POINT | FShape.Z)) {
504
                                dxf3DFile = true;
505
                                createPoint3D(handle, k, geom);
506
                                k++;
507
                        } else if (geom.getGeometryType() == FShape.LINE) {
508
                                createLwPolyline2D(handle, k, geom, false);
509
                                k++;
510
                        } else if (geom.getGeometryType() == (FShape.LINE | FShape.Z)) {
511
                                dxf3DFile = true;
512
                                k = createPolyline3D(handle, k, geom);
513
                        } else if (geom.getGeometryType() == FShape.POLYGON) {
514
                                // createPolygon2D(handle, k, geom);
515
                                createLwPolyline2D(handle, k, geom, true);
516
                                k++;
517
                        } else if (geom.getGeometryType() == (FShape.POLYGON | FShape.Z)) {
518
                                dxf3DFile = true;
519
                                k = createPolygon3D(handle, k, geom);
520
                        } else {
521
                                System.out.println("IGeometry not supported yet");
522
                                k++;
523
                        }
524
                }
525
                dxfFile = new DxfFile(null, file.getAbsolutePath(), entityMaker);
526
                dxfFile.setCadFlag(true);
527
                if (dxf3DFile)
528
                        dxfFile.setDxf3DFlag(true);
529
                dxfFile.save(file.getAbsolutePath());
530
        }
531

    
532
        /**
533
         * @param handle
534
         * @param k
535
         * @param geom
536
         * @return
537
         * @throws Exception
538
         */
539
        private int createPolygon3D(int handle, int k, IGeometry geom)
540
                        throws Exception {
541
                DxfGroupVector polv = new DxfGroupVector();
542
                DxfGroup polylineLayer = new DxfGroup(8, "default");
543
                polv.add(polylineLayer);
544
                DxfGroup handleGroup = new DxfGroup();
545
                handleGroup.setCode(5);
546
                handleGroup.setData(new Integer(handle + k).toString());
547
                polv.add(handleGroup);
548
                Vector vpoints = new Vector();
549
                PathIterator theIterator = geom.getPathIterator(null); // polyLine.getPathIterator(null,
550
                                                                                                                                // flatness);
551
                double[] theData = new double[6];
552
                double[] velev = ((FGeometry) geom).getZs();
553
                while (!theIterator.isDone()) {
554
                        int theType = theIterator.currentSegment(theData);
555
                        switch (theType) {
556
                        case PathIterator.SEG_MOVETO:
557
                                vpoints.add(new FPoint2D(theData[0], theData[1]));
558
                                break;
559
                        case PathIterator.SEG_LINETO:
560
                                vpoints.add(new FPoint2D(theData[0], theData[1]));
561
                                break;
562
                        }
563
                        theIterator.next();
564
                }
565
                if (constantElevation(velev)) {
566
                        DxfGroup polylineFlag = new DxfGroup();
567
                        polylineFlag.setCode(70);
568
                        polylineFlag.setData(new Integer(1));
569
                        polv.add(polylineFlag);
570
                        DxfGroup elevation = new DxfGroup();
571
                        elevation.setCode(38);
572
                        elevation.setData(new Double(velev[0]));
573
                        polv.add(elevation);
574
                        for (int j = 0; j < vpoints.size(); j++) {
575
                                DxfGroup xvertex = new DxfGroup();
576
                                xvertex.setCode(10);
577
                                xvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getX()));
578
                                DxfGroup yvertex = new DxfGroup();
579
                                yvertex.setCode(20);
580
                                yvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getY()));
581
                                polv.add(xvertex);
582
                                polv.add(yvertex);
583
                        }
584
                        entityMaker.createLwPolyline(polv);
585
                        k++;
586
                } else {
587
                        DxfGroup polylineFlag = new DxfGroup();
588
                        polylineFlag.setCode(70);
589
                        polylineFlag.setData(new Integer(9));
590
                        polv.add(polylineFlag);
591
                        DxfGroup xgroup = new DxfGroup();
592
                        xgroup.setCode(10);
593
                        xgroup.setData(new Double(0.0));
594
                        polv.add(xgroup);
595
                        DxfGroup ygroup = new DxfGroup();
596
                        ygroup.setCode(20);
597
                        ygroup.setData(new Double(0.0));
598
                        polv.add(ygroup);
599
                        DxfGroup elevation = new DxfGroup();
600
                        elevation.setCode(30);
601
                        elevation.setData(new Double(0.0));
602
                        polv.add(elevation);
603
                        DxfGroup subclassMarker = new DxfGroup(100, "AcDb3dPolyline");
604
                        polv.add(subclassMarker);
605
                        entityMaker.createPolyline(polv);
606
                        k++;
607
                        for (int j = 0; j < vpoints.size(); j++) {
608
                                DxfGroupVector verv = new DxfGroupVector();
609
                                DxfGroup entityType = new DxfGroup(0, "VERTEX");
610
                                verv.add(entityType);
611
                                DxfGroup generalSubclassMarker = new DxfGroup(100, "AcDbEntity");
612
                                verv.add(generalSubclassMarker);
613
                                DxfGroup layerName = new DxfGroup(8, "default");
614
                                verv.add(layerName);
615
                                DxfGroup vertexSubclassMarker = new DxfGroup(100, "AcDbVertex");
616
                                verv.add(vertexSubclassMarker);
617
                                // DxfGroup vertex3DSubclassMarker = new DxfGroup(100,
618
                                // "AcDb3dPolylineVertex");
619
                                // verv.add(vertex3DSubclassMarker);
620
                                DxfGroup xvertex = new DxfGroup();
621
                                xvertex.setCode(10);
622
                                xvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getX()));
623
                                DxfGroup yvertex = new DxfGroup();
624
                                yvertex.setCode(20);
625
                                yvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getY()));
626
                                DxfGroup zvertex = new DxfGroup();
627
                                zvertex.setCode(30);
628
                                zvertex.setData(new Double(velev[j]));
629
                                verv.add(xvertex);
630
                                verv.add(yvertex);
631
                                verv.add(zvertex);
632
                                entityMaker.addVertex(verv);
633
                                k++;
634
                        }
635
                        DxfGroupVector seqv = new DxfGroupVector();
636
                        DxfGroup entityType = new DxfGroup(0, "SEQEND");
637
                        seqv.add(entityType);
638
                        // DxfGroup handle = new DxfGroup();
639
                        // elevation.setCode(5);
640
                        // elevation.setData(new Integer(getHandle()));
641
                        // seqv.add(handle);
642
                        DxfGroup generalSubclassMarker = new DxfGroup(100, "AcDbEntity");
643
                        seqv.add(generalSubclassMarker);
644
                        DxfGroup layerName = new DxfGroup(8, "default");
645
                        seqv.add(layerName);
646
                        DxfGroup handleSeqGroup = new DxfGroup();
647
                        handleSeqGroup.setCode(5);
648
                        handleSeqGroup.setData(new Integer(handle + k).toString());
649
                        seqv.add(handleSeqGroup);
650
                        entityMaker.endSeq();
651
                        k++;
652
                }
653
                return k;
654
        }
655

    
656
        /**
657
         * @deprecated
658
         * @param handle
659
         * @param k
660
         * @param geom
661
         * @throws Exception
662
         */
663
        private void createPolygon2D(int handle, int k, IGeometry geom)
664
                        throws Exception {
665
                DxfGroupVector polv = new DxfGroupVector();
666
                DxfGroup polylineLayer = new DxfGroup(8, "default");
667
                polv.add(polylineLayer);
668
                DxfGroup handleGroup = new DxfGroup();
669
                handleGroup.setCode(5);
670
                handleGroup.setData(new Integer(handle + k).toString());
671
                polv.add(handleGroup);
672
                DxfGroup polylineFlag = new DxfGroup();
673
                polylineFlag.setCode(70);
674
                polylineFlag.setData(new Integer(1));
675
                polv.add(polylineFlag);
676

    
677
                Vector vpoints = new Vector();
678
                PathIterator theIterator = geom.getPathIterator(null); // polyLine.getPathIterator(null,
679
                                                                                                                                // flatness);
680
                double[] theData = new double[6];
681
                while (!theIterator.isDone()) {
682
                        int theType = theIterator.currentSegment(theData);
683
                        switch (theType) {
684
                        case PathIterator.SEG_MOVETO:
685
                                vpoints.add(new FPoint2D(theData[0], theData[1]));
686
                                break;
687
                        case PathIterator.SEG_LINETO:
688
                                vpoints.add(new FPoint2D(theData[0], theData[1]));
689
                                break;
690
                        }
691
                        theIterator.next();
692
                }
693
                for (int j = 0; j < vpoints.size(); j++) {
694
                        DxfGroup xvertex = new DxfGroup();
695
                        xvertex.setCode(10);
696
                        xvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getX()));
697
                        DxfGroup yvertex = new DxfGroup();
698
                        yvertex.setCode(20);
699
                        yvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getY()));
700
                        polv.add(xvertex);
701
                        polv.add(yvertex);
702
                }
703
                entityMaker.createLwPolyline(polv);
704
        }
705

    
706
        /**
707
         * @param handle
708
         * @param k
709
         * @param geom
710
         * @return
711
         * @throws Exception
712
         */
713
        private int createPolyline3D(int handle, int k, IGeometry geom)
714
                        throws Exception {
715
                DxfGroupVector polv = new DxfGroupVector();
716
                DxfGroup polylineLayer = new DxfGroup(8, "default");
717
                polv.add(polylineLayer);
718
                DxfGroup handleGroup = new DxfGroup();
719
                handleGroup.setCode(5);
720
                handleGroup.setData(new Integer(handle + k).toString());
721
                polv.add(handleGroup);
722
                Vector vpoints = new Vector();
723
                PathIterator theIterator = geom.getPathIterator(null); // polyLine.getPathIterator(null,
724
                                                                                                                                // flatness);
725
                double[] theData = new double[6];
726
                double[] velev = ((FGeometry) geom).getZs();
727
                while (!theIterator.isDone()) {
728
                        int theType = theIterator.currentSegment(theData);
729
                        switch (theType) {
730
                        case PathIterator.SEG_MOVETO:
731
                                vpoints.add(new FPoint2D(theData[0], theData[1]));
732
                                break;
733
                        case PathIterator.SEG_LINETO:
734
                                vpoints.add(new FPoint2D(theData[0], theData[1]));
735
                                break;
736
                        }
737
                        theIterator.next();
738
                }
739
                if (constantElevation(velev)) {
740
                        DxfGroup polylineFlag = new DxfGroup();
741
                        polylineFlag.setCode(70);
742
                        polylineFlag.setData(new Integer(0));
743
                        polv.add(polylineFlag);
744
                        DxfGroup elevation = new DxfGroup();
745
                        elevation.setCode(38);
746
                        elevation.setData(new Double(velev[0]));
747
                        polv.add(elevation);
748
                        for (int j = 0; j < vpoints.size(); j++) {
749
                                DxfGroup xvertex = new DxfGroup();
750
                                xvertex.setCode(10);
751
                                xvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getX()));
752
                                DxfGroup yvertex = new DxfGroup();
753
                                yvertex.setCode(20);
754
                                yvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getY()));
755
                                polv.add(xvertex);
756
                                polv.add(yvertex);
757
                        }
758
                        entityMaker.createLwPolyline(polv);
759
                        k++;
760
                } else {
761
                        DxfGroup polylineFlag = new DxfGroup();
762
                        polylineFlag.setCode(70);
763
                        polylineFlag.setData(new Integer(8));
764
                        polv.add(polylineFlag);
765
                        DxfGroup xgroup = new DxfGroup();
766
                        xgroup.setCode(10);
767
                        xgroup.setData(new Double(0.0));
768
                        polv.add(xgroup);
769
                        DxfGroup ygroup = new DxfGroup();
770
                        ygroup.setCode(20);
771
                        ygroup.setData(new Double(0.0));
772
                        polv.add(ygroup);
773
                        DxfGroup elevation = new DxfGroup();
774
                        elevation.setCode(30);
775
                        elevation.setData(new Double(0.0));
776
                        polv.add(elevation);
777
                        DxfGroup subclassMarker = new DxfGroup(100, "AcDb3dPolyline");
778
                        polv.add(subclassMarker);
779
                        entityMaker.createPolyline(polv);
780
                        k++;
781
                        for (int j = 0; j < vpoints.size(); j++) {
782
                                DxfGroupVector verv = new DxfGroupVector();
783
                                DxfGroup entityType = new DxfGroup(0, "VERTEX");
784
                                verv.add(entityType);
785
                                DxfGroup generalSubclassMarker = new DxfGroup(100, "AcDbEntity");
786
                                verv.add(generalSubclassMarker);
787
                                DxfGroup layerName = new DxfGroup(8, "default");
788
                                verv.add(layerName);
789
                                DxfGroup vertexSubclassMarker = new DxfGroup(100, "AcDbVertex");
790
                                verv.add(vertexSubclassMarker);
791
                                // DxfGroup vertex3DSubclassMarker = new DxfGroup(100,
792
                                // "AcDb3dPolylineVertex");
793
                                // verv.add(vertex3DSubclassMarker);
794
                                DxfGroup xvertex = new DxfGroup();
795
                                xvertex.setCode(10);
796
                                xvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getX()));
797
                                DxfGroup yvertex = new DxfGroup();
798
                                yvertex.setCode(20);
799
                                yvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getY()));
800
                                DxfGroup zvertex = new DxfGroup();
801
                                zvertex.setCode(30);
802
                                zvertex.setData(new Double(velev[j]));
803
                                verv.add(xvertex);
804
                                verv.add(yvertex);
805
                                verv.add(zvertex);
806
                                entityMaker.addVertex(verv);
807
                                k++;
808
                        }
809
                        DxfGroupVector seqv = new DxfGroupVector();
810
                        DxfGroup entityType = new DxfGroup(0, "SEQEND");
811
                        seqv.add(entityType);
812
                        // DxfGroup handle = new DxfGroup();
813
                        // elevation.setCode(5);
814
                        // elevation.setData(new Integer(getHandle()));
815
                        // seqv.add(handle);
816
                        DxfGroup generalSubclassMarker = new DxfGroup(100, "AcDbEntity");
817
                        seqv.add(generalSubclassMarker);
818
                        DxfGroup layerName = new DxfGroup(8, "default");
819
                        seqv.add(layerName);
820
                        DxfGroup handleSeqGroup = new DxfGroup();
821
                        handleSeqGroup.setCode(5);
822
                        handleSeqGroup.setData(new Integer(handle + k).toString());
823
                        seqv.add(handleSeqGroup);
824
                        entityMaker.endSeq();
825
                        k++;
826
                }
827
                return k;
828
        }
829

    
830
        /**
831
         * @param handle
832
         * @param k
833
         * @param geom
834
         * @throws Exception
835
         */
836
        private void createLwPolyline2D(int handle, int k, IGeometry geom, boolean isPolygon)
837
                        throws Exception {
838
                DxfGroupVector polv = null;
839
                DxfGroup polylineLayer = new DxfGroup(8, "default");
840
                
841
                DxfGroup handleGroup = new DxfGroup();
842
                handleGroup.setCode(5);
843
                handleGroup.setData(new Integer(handle + k).toString());
844
                
845
                Vector vpoints = new Vector();
846
                
847
                DxfGroup polylineFlag = new DxfGroup();
848
                polylineFlag.setCode(70);
849
                if (isPolygon)
850
                        polylineFlag.setData(new Integer(1)); // cerrada
851
                else
852
                        polylineFlag.setData(new Integer(0)); // abierta
853
                
854
                PathIterator theIterator = geom.getPathIterator(null); // polyLine.getPathIterator(null,
855
                                                                                                                                // flatness);
856
                
857
                double[] theData = new double[6];
858
                while (!theIterator.isDone()) {
859
                        int theType = theIterator.currentSegment(theData);
860
                        switch (theType) {
861
                        case PathIterator.SEG_MOVETO:
862
                                if (polv != null)
863
                                {
864
                                        for (int j = 0; j < vpoints.size(); j++) {
865
                                                DxfGroup xvertex = new DxfGroup();
866
                                                xvertex.setCode(10);
867
                                                xvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getX()));
868
                                                DxfGroup yvertex = new DxfGroup();
869
                                                yvertex.setCode(20);
870
                                                yvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getY()));
871
                                                polv.add(xvertex);
872
                                                polv.add(yvertex);
873
                                        }                                        
874
                                        entityMaker.createLwPolyline(polv);                                                
875
                                }
876
                                polv = new DxfGroupVector();
877
                                polv.add(polylineLayer);
878
                                polv.add(handleGroup);
879
                                polv.add(polylineFlag);
880
                                vpoints.clear();
881
                                vpoints.add(new FPoint2D(theData[0], theData[1]));
882
                                break;
883
                        case PathIterator.SEG_LINETO:
884
                                vpoints.add(new FPoint2D(theData[0], theData[1]));
885
                                break;
886
                        case PathIterator.SEG_QUADTO:
887
                                break;
888
                        case PathIterator.SEG_CUBICTO:                                
889
                                break;
890
                        case PathIterator.SEG_CLOSE:
891
                                polylineFlag.setData(new Integer(1)); // cerrada
892
                                break;
893
                                
894
                        }
895
                        theIterator.next();
896
                }
897
                
898
                for (int j = 0; j < vpoints.size(); j++) {
899
                        DxfGroup xvertex = new DxfGroup();
900
                        xvertex.setCode(10);
901
                        xvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getX()));
902
                        DxfGroup yvertex = new DxfGroup();
903
                        yvertex.setCode(20);
904
                        yvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getY()));
905
                        polv.add(xvertex);
906
                        polv.add(yvertex);
907
                }
908
                entityMaker.createLwPolyline(polv);
909
        }
910

    
911
        /**
912
         * @param handle
913
         * @param k
914
         * @param geom
915
         * @throws Exception
916
         */
917
        private void createPoint3D(int handle, int k, IGeometry geom)
918
                        throws Exception {
919
                FPoint3D point = new FPoint3D(0, 0, 0);
920
                double[] pointCoords = new double[6];
921
                PathIterator pointIt = geom.getPathIterator(null);
922
                while (!pointIt.isDone()) {
923
                        pointIt.currentSegment(pointCoords);
924
                        point = new FPoint3D(pointCoords[0], pointCoords[1], pointCoords[2]);
925
                        pointIt.next();
926
                }
927
                Point3D pto = new Point3D(point.getX(), point.getY(), point.getZs()[0]);
928
                DxfGroup pointLayer = new DxfGroup(8, "default");
929
                DxfGroup handleGroup = new DxfGroup();
930
                handleGroup.setCode(5);
931
                handleGroup.setData(new Integer(handle + k).toString());
932
                DxfGroup px = new DxfGroup();
933
                DxfGroup py = new DxfGroup();
934
                DxfGroup pz = new DxfGroup();
935
                px.setCode(10);
936
                px.setData(new Double(pto.getX()));
937
                py.setCode(20);
938
                py.setData(new Double(pto.getY()));
939
                pz.setCode(30);
940
                pz.setData(new Double(pto.getZ()));
941
                DxfGroupVector pv = new DxfGroupVector();
942
                pv.add(pointLayer);
943
                pv.add(handleGroup);
944
                pv.add(px);
945
                pv.add(py);
946
                pv.add(pz);
947
                entityMaker.createPoint(pv);
948
        }
949

    
950
        /**
951
         * @param handle
952
         * @param k
953
         * @param geom
954
         * @throws Exception
955
         */
956
        private void createPoint2D(int handle, int k, IGeometry geom)
957
                        throws Exception {
958
                FPoint2D point = new FPoint2D(0, 0);
959
                double[] pointCoords = new double[6];
960
                PathIterator pointIt = geom.getPathIterator(null);
961
                while (!pointIt.isDone()) {
962
                        pointIt.currentSegment(pointCoords);
963
                        point = new FPoint2D(pointCoords[0], pointCoords[1]);
964
                        pointIt.next();
965
                }
966
                Point2D pto = new Point2D.Double(point.getX(), point.getY());
967
                DxfGroup pointLayer = new DxfGroup(8, "default");
968
                DxfGroup handleGroup = new DxfGroup();
969
                handleGroup.setCode(5);
970
                handleGroup.setData(new Integer(handle + k).toString());
971
                DxfGroup px = new DxfGroup();
972
                DxfGroup py = new DxfGroup();
973
                DxfGroup pz = new DxfGroup();
974
                px.setCode(10);
975
                px.setData(new Double(pto.getX()));
976
                py.setCode(20);
977
                py.setData(new Double(pto.getY()));
978
                pz.setCode(30);
979
                // POINT del DXF tiene cota. Le asigno cero arbitrariamente.
980
                pz.setData(new Double(0.0));
981
                DxfGroupVector pv = new DxfGroupVector();
982
                pv.add(pointLayer);
983
                pv.add(handleGroup);
984
                pv.add(px);
985
                pv.add(py);
986
                pv.add(pz);
987
                entityMaker.createPoint(pv);
988
        }
989

    
990
        private boolean constantElevation(double[] velev) {
991
                boolean constant = true;
992
                for (int i = 0; i < velev.length; i++) {
993
                        for (int j = 0; j < velev.length; j++) {
994
                                if (j > i) {
995
                                        if (velev[i] != velev[j]) {
996
                                                constant = false;
997
                                                break;
998
                                        }
999
                                }
1000
                        }
1001
                        break;
1002
                }
1003
                return constant;
1004
        }
1005

    
1006
        /**
1007
         * @return Returns the proj.
1008
         */
1009
        public IProjection getProjection() {
1010
                return proj;
1011
        }
1012

    
1013
        /**
1014
         * Util solo para el entity maker. Yo creo que esto no es necesario pero por
1015
         * ahora lo necesito para que funcione. TODO: Hablar con Luis para que lo
1016
         * aclare.
1017
         * 
1018
         * @param proj
1019
         *            The proj to set.
1020
         */
1021
        public void setProjection(IProjection proj) {
1022
                this.proj = proj;
1023
        }
1024

    
1025
}