Statistics
| Revision:

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

History | View | Annotate | Download (32.1 KB)

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

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

    
9
import org.cresques.cts.IProjection;
10
import org.cresques.geo.Point3D;
11
import org.cresques.io.DxfFile;
12
import org.cresques.io.DxfGroup;
13
import org.cresques.io.DxfGroupVector;
14
import org.cresques.px.dxf.DxfEntityMaker;
15

    
16
import com.iver.cit.gvsig.fmap.DriverException;
17
import com.iver.cit.gvsig.fmap.core.FArc2D;
18
import com.iver.cit.gvsig.fmap.core.FCircle2D;
19
import com.iver.cit.gvsig.fmap.core.FEllipse2D;
20
import com.iver.cit.gvsig.fmap.core.FGeometry;
21
import com.iver.cit.gvsig.fmap.core.FPoint2D;
22
import com.iver.cit.gvsig.fmap.core.FPoint3D;
23
import com.iver.cit.gvsig.fmap.core.FShape;
24
import com.iver.cit.gvsig.fmap.core.IFeature;
25
import com.iver.cit.gvsig.fmap.core.IGeometry;
26
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
27
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
28
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
29
import com.iver.cit.gvsig.fmap.edition.EditionException;
30
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
31
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
32
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
33
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter;
34
import com.iver.cit.gvsig.fmap.layers.FLayer;
35
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
36
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
37

    
38
public class DxfWriter extends AbstractWriter implements ISpatialWriter {
39

    
40
        private DxfFieldsMapping fieldMapping = null;
41

    
42
        private File file;
43

    
44
        private FieldDescription[] fieldsDesc = null;
45

    
46
        private DxfFile.EntityFactory entityMaker;
47

    
48
        private IProjection proj = null;
49

    
50
        private DxfFile dxfFile;
51

    
52
        int handle = 40; // Revisar porqu? es 40.
53

    
54
        int k = 0;
55

    
56
        boolean dxf3DFile = false;
57

    
58
        public void setFile(File f) {
59
                file = f;
60
        }
61

    
62
        public boolean canWriteAttribute(int sqlType) {
63
                return false;
64
        }
65

    
66
        public boolean canAlterTable() {
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(ITableDefinition lyrDef) throws EditionException {
98
                super.initialize(lyrDef);
99
                fieldsDesc = lyrDef.getFieldsDesc();
100
        }
101

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

    
115
                entityMaker = new DxfEntityMaker(proj);
116

    
117
        }
118

    
119
public void process(IRowEdited row) throws EditionException {
120
                if (row.getStatus() == IRowEdited.STATUS_DELETED) return;
121

    
122
                try
123
                {
124
                        IFeature feat = (IFeature) row.getLinkedRow();
125
                    IGeometry geom = feat.getGeometry();
126
                    // TODO: Tratamiento de los campos
127
                    // y modificar los createXXX para que acepten como par?metro
128
                    // los datos de LAYER, COLOR, ELEVATION, THICKNESS, TEXT
129
                    // HEIGHTTEXT, ROTATIONTEXT y el resto que puedan hacer
130
                    // falta.
131

    
132

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

    
297

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

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

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

    
361
        }
362

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

    
376
                DxfGroup xvertex = new DxfGroup();
377
                xvertex.setCode(10);
378
                xvertex.setData(new Double(geom.getCenter().getX()));
379
                DxfGroup yvertex = new DxfGroup();
380
                yvertex.setCode(20);
381
                yvertex.setData(new Double(geom.getCenter().getY()));
382
                DxfGroup zvertex = new DxfGroup();
383
                zvertex.setCode(30);
384
                zvertex.setData(new Double(0)); // TODO: COORDENADA Z. REVISAR ESTO PARA
385
                                                                                // ENTIDADES 3D
386

    
387
                DxfGroup radius = new DxfGroup();
388
                radius.setCode(40);
389
                radius.setData(new Double(geom.getRadio()));
390

    
391
                polv.add(xvertex);
392
                polv.add(yvertex);
393
                polv.add(zvertex);
394
                polv.add(radius);
395

    
396
                entityMaker.createCircle(polv);
397

    
398
        }
399

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

    
413
                Point2D endPointOfMajorAxis = fElip.getEnd();
414
                double azimut = Math.atan2(endPointOfMajorAxis.getX() - center.getX(),
415
                                endPointOfMajorAxis.getY() - center.getY());
416
                double azimut2 = azimut + Math.PI / 2.0;
417
                if (azimut2 >= Math.PI * 2)
418
                        azimut2 = azimut2 - Math.PI * 2;
419
                Point2D endPointOfMinorAxis = new Point2D.Double(center.getX()
420
                                + (fElip.getDist() * Math.sin(azimut2)), center.getY()
421
                                + (fElip.getDist() * Math.cos(azimut2)));
422

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

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

    
476
        }
477

    
478
        public String getName() {
479
                return "DXF Writer";
480
        }
481

    
482
        /**
483
         * @return Returns the fieldMapping.
484
         */
485
        public DxfFieldsMapping getFieldMapping() {
486
                return fieldMapping;
487
        }
488

    
489
        /**
490
         * Use this method BEFORE preProcess.
491
         *
492
         * @param fieldMapping
493
         *            The fieldMapping to set.
494
         */
495
        public void setFieldMapping(DxfFieldsMapping fieldMapping) {
496
                this.fieldMapping = fieldMapping;
497
        }
498

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

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

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

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

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

    
835
        /**
836
         * @param handle
837
         * @param k
838
         * @param geom
839
         * @throws Exception
840
         */
841
        private void createLwPolyline2D(int handle, int k, IGeometry geom, boolean isPolygon)
842
                        throws Exception {
843
                DxfGroupVector polv = null;
844
                DxfGroup polylineLayer = new DxfGroup(8, "default");
845

    
846
                DxfGroup handleGroup = new DxfGroup();
847
                handleGroup.setCode(5);
848
                handleGroup.setData(new Integer(handle + k).toString());
849

    
850
                Vector vpoints = new Vector();
851

    
852
                DxfGroup polylineFlag = new DxfGroup();
853
                polylineFlag.setCode(70);
854
                if (isPolygon)
855
                        polylineFlag.setData(new Integer(1)); // cerrada
856
                else
857
                        polylineFlag.setData(new Integer(0)); // abierta
858

    
859
                PathIterator theIterator = geom.getPathIterator(null); // polyLine.getPathIterator(null,
860
                                                                                                                                // flatness);
861

    
862
                double[] theData = new double[6];
863
                while (!theIterator.isDone()) {
864
                        int theType = theIterator.currentSegment(theData);
865
                        switch (theType) {
866
                        case PathIterator.SEG_MOVETO:
867
                                if (polv != null)
868
                                {
869
                                        for (int j = 0; j < vpoints.size(); j++) {
870
                                                DxfGroup xvertex = new DxfGroup();
871
                                                xvertex.setCode(10);
872
                                                xvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getX()));
873
                                                DxfGroup yvertex = new DxfGroup();
874
                                                yvertex.setCode(20);
875
                                                yvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getY()));
876
                                                polv.add(xvertex);
877
                                                polv.add(yvertex);
878
                                        }
879
                                        entityMaker.createLwPolyline(polv);
880
                                }
881
                                polv = new DxfGroupVector();
882
                                polv.add(polylineLayer);
883
                                polv.add(handleGroup);
884
                                polv.add(polylineFlag);
885
                                vpoints.clear();
886
                                vpoints.add(new FPoint2D(theData[0], theData[1]));
887
                                break;
888
                        case PathIterator.SEG_LINETO:
889
                                vpoints.add(new FPoint2D(theData[0], theData[1]));
890
                                break;
891
                        case PathIterator.SEG_QUADTO:
892
                                break;
893
                        case PathIterator.SEG_CUBICTO:
894
                                break;
895
                        case PathIterator.SEG_CLOSE:
896
                                polylineFlag.setData(new Integer(1)); // cerrada
897
                                break;
898

    
899
                        }
900
                        theIterator.next();
901
                }
902

    
903
                for (int j = 0; j < vpoints.size(); j++) {
904
                        DxfGroup xvertex = new DxfGroup();
905
                        xvertex.setCode(10);
906
                        xvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getX()));
907
                        DxfGroup yvertex = new DxfGroup();
908
                        yvertex.setCode(20);
909
                        yvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getY()));
910
                        polv.add(xvertex);
911
                        polv.add(yvertex);
912
                }
913
                entityMaker.createLwPolyline(polv);
914
        }
915

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

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

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

    
1011
        /**
1012
         * @return Returns the proj.
1013
         */
1014
        public IProjection getProjection() {
1015
                return proj;
1016
        }
1017

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

    
1030
        public void setFlatness(double flatness) {
1031
                // TODO Auto-generated method stub
1032

    
1033
        }
1034

    
1035
}