Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / writers / dxf / DxfWriter.java @ 10627

History | View | Annotate | Download (32.2 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.hardcode.gdbms.driver.exceptions.InitializeWriterException;
17
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
18
import com.iver.cit.gvsig.exceptions.visitors.ProcessWriterVisitorException;
19
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
20
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException;
21
import com.iver.cit.gvsig.fmap.core.FArc2D;
22
import com.iver.cit.gvsig.fmap.core.FCircle2D;
23
import com.iver.cit.gvsig.fmap.core.FEllipse2D;
24
import com.iver.cit.gvsig.fmap.core.FGeometry;
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.FShape;
28
import com.iver.cit.gvsig.fmap.core.IFeature;
29
import com.iver.cit.gvsig.fmap.core.IGeometry;
30
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
31
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
32
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
33
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
34
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
35
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
36
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter;
37
import com.iver.cit.gvsig.fmap.layers.FLayer;
38
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
39
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
40

    
41
public class DxfWriter extends AbstractWriter implements ISpatialWriter {
42

    
43
        private DxfFieldsMapping fieldMapping = null;
44

    
45
        private File file;
46

    
47
        private FieldDescription[] fieldsDesc = null;
48

    
49
        private DxfFile.EntityFactory entityMaker;
50

    
51
        private IProjection proj = null;
52

    
53
        private DxfFile dxfFile;
54

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

    
57
        int k = 0;
58

    
59
        boolean dxf3DFile = false;
60

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

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

    
69
        public boolean canAlterTable() {
70
                return false;
71
        }
72

    
73
        public boolean canWriteGeometry(int gvSIGgeometryType) {
74
                return true; // I guess all geometries can be here...
75
        }
76

    
77
        public void initialize(FLayer layer) throws InitializeWriterException {
78
                try {
79
                        SelectableDataSource sds = ((FLyrVect) layer).getRecordset();
80
                        // Aqu? hay que revisar los campos de sds y compararlos con los
81
                        // que podemos escribir. (Layer, Color, etc).
82
                        fieldsDesc = sds.getFieldsDescription();
83
                } catch (ReadDriverException e) {
84
                        throw new InitializeWriterException(getName(),e);
85
                }
86

    
87
        }
88

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

    
101
        public void preProcess() throws StartWriterVisitorException {
102
                if (fieldMapping == null) {
103
                        throw new StartWriterVisitorException (getName(),null);
104
                }
105
                // NOTA: La proyecci?n no se usa absolutamente para nada (al menos
106
                // por ahora). Las entidades se escribir?n con las coordenadas con
107
                // las que se crean.
108
                if (proj == null) {
109
                        throw new StartWriterVisitorException (getName(),null);
110
                }
111

    
112
                entityMaker = new DxfEntityMaker(proj);
113

    
114
        }
115

    
116
public void process(IRowEdited row) throws ProcessWriterVisitorException {
117
                if (row.getStatus() == IRowEdited.STATUS_DELETED) return;
118

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

    
129

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

    
293

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

    
311
                // 050307, jmorell: Resoluci?n de un bug sobre el sentido de
312
                // los arcos.
313

    
314
                 // if (!UtilFunctions.isCCW(pts[0],pts[1],pts[2])){
315
                if (!FConverter.isCCW(pts)){
316
                         double aux=initAngle;
317
                         initAngle=endAngle;
318
                         endAngle=aux;
319
                }
320

    
321

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

    
357
        }
358

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

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

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

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

    
392
                entityMaker.createCircle(polv);
393

    
394
        }
395

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

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

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

    
459
        public void postProcess() throws StopWriterVisitorException{
460
                // Escribimos realmente lo que hemos montado en memoria.
461
                dxfFile = new DxfFile(null, file.getAbsolutePath(), entityMaker);
462
                dxfFile.setCadFlag(true);
463
                if (dxf3DFile)
464
                        dxfFile.setDxf3DFlag(true);
465
                try {
466
                        dxfFile.save(file.getAbsolutePath());
467
                } catch (IOException e) {
468
                        throw new StopWriterVisitorException(getName(),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
        public boolean canSaveEdits() {
1026
                if (file.canWrite()) return true;
1027
                return false;
1028
        }
1029

    
1030
}