Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / PolylineCADTool.java @ 5878

History | View | Annotate | Download (20.7 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.gui.cad.tools;
42

    
43
import java.awt.Graphics;
44
import java.awt.Graphics2D;
45
import java.awt.event.InputEvent;
46
import java.awt.geom.Point2D;
47
import java.util.ArrayList;
48

    
49
import org.apache.log4j.Logger;
50
import org.cresques.cts.GeoCalc;
51

    
52
import com.iver.andami.PluginServices;
53
import com.iver.cit.gvsig.fmap.core.FArc2D;
54
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
55
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
56
import com.iver.cit.gvsig.fmap.core.FShape;
57
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
58
import com.iver.cit.gvsig.fmap.core.Handler;
59
import com.iver.cit.gvsig.fmap.core.IGeometry;
60
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
61
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
62
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
63
import com.iver.cit.gvsig.gui.Table;
64
import com.iver.cit.gvsig.gui.cad.CADTool;
65
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
66
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.PolylineCADToolContext;
68
import com.iver.cit.gvsig.gui.cad.tools.smc.PolylineCADToolContext.PolylineCADToolState;
69
import com.vividsolutions.jts.geom.Geometry;
70

    
71

    
72
/**
73
 * DOCUMENT ME!
74
 *
75
 * @author Vicente Caballero Navarro
76
 */
77
public class PolylineCADTool extends DefaultCADTool {
78
        private static Logger logger = Logger.getLogger(PolylineCADTool.class.getName());
79
    private PolylineCADToolContext _fsm;
80
    private Point2D firstPoint;
81
    private Point2D antPoint;
82
    private Point2D antantPoint;
83
    private Point2D antCenter;
84
    private Point2D antInter;
85
    private ArrayList list = new ArrayList();
86
        private boolean isClosed=false;
87

    
88
    /**
89
     * Crea un nuevo PolylineCADTool.
90
     */
91
    public PolylineCADTool() {
92

    
93
    }
94

    
95
    /**
96
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
97
     * carga previa a la utilizaci?n de la herramienta.
98
     */
99
    public void init() {
100
        _fsm = new PolylineCADToolContext(this);
101
    }
102

    
103
    public void endGeometry() {
104
        IGeometry[] geoms = (IGeometry[]) list.toArray(new IGeometry[0]);
105
        FGeometryCollection fgc = new FGeometryCollection(geoms);
106
        if (isClosed) {
107
                GeneralPathX gpx=new GeneralPathX();
108
            for (int i=0;i<geoms.length;i++) {
109
                     gpx.append(geoms[i].getPathIterator(null),true);
110
            }
111
            if (gpx.isCCW()) {
112
                    ArrayList geomsAux=new ArrayList();
113
                    for (int i=0;i<geoms.length;i++) {
114
                            if (geoms[i].getInternalShape() instanceof FPolyline2D) {
115
                                    Handler[] handlers=((FPolyline2D)geoms[i]).getSelectHandlers();
116
                                    GeneralPathX gpxAux=new GeneralPathX();
117
                                    gpxAux.moveTo(handlers[1].getPoint().getX(),handlers[1].getPoint().getY());
118
                                    gpxAux.lineTo(handlers[0].getPoint().getX(),handlers[0].getPoint().getY());
119
                                    geomsAux.add(ShapeFactory.createPolyline2D(gpxAux));
120
                            }else if (geoms[i].getInternalShape() instanceof FArc2D) {
121
                                    Handler[] handlers=((FArc2D)geoms[i]).getSelectHandlers();
122
                                    geomsAux.add(ShapeFactory.createArc(handlers[2].getPoint(),handlers[1].getPoint(),handlers[0].getPoint()));
123
                            }
124
                    }
125
                    fgc = new FGeometryCollection((IGeometry[]) geomsAux.toArray(new IGeometry[0]));
126
            }
127
            fgc.setGeometryType(FShape.POLYGON);
128
        }else {
129
                 fgc.setGeometryType(FShape.LINE);
130
        }
131

    
132

    
133
//
134
//        GeneralPathX gpx=new GeneralPathX();
135
//        for (int i=0;i<geoms.length;i++) {
136
//                 gpx.append(geoms[0].getPathIterator(null),true);
137
//        }
138
//        /* try {
139
//                        if (getVLE().getVEA().getShapeType()==FShape.POLYGON){
140
//                                GeneralPathX gpx=new GeneralPathX();
141
//                                gpx.moveTo(antPoint.getX(),antPoint.getY());
142
//                                gpx.lineTo(firstPoint.getX(),firstPoint.getY());
143
//                                IGeometry line=ShapeFactory.createPolyline2D(gpx);
144
//                                fgc.addGeometry(line);
145
//                        }
146
//                } catch (DriverIOException e) {
147
//                        e.printStackTrace();
148
//                } */
149
//
150
//        ///IGeometry newGeom = ShapeFactory.createPolyline2D(gpx);
151
//        if (gpx.isClosed())
152
//        {
153
//                gpx.ensureOrientation(false); // Poligono exterior.
154
//                // Voy a testear que se puede convertir a JTS:
155
//                IGeometry gAux = ShapeFactory.createPolygon2D(gpx);
156
//                Geometry jtsG = gAux.toJTSGeometry();
157
//                logger.debug("A punto de escribir " + jtsG.toText());
158
//                try {
159
//                        int shapeType = getVLE().getVEA().getShapeType();
160
//                                if ((shapeType ==FShape.POLYGON) || (shapeType == FShape.MULTI)) {
161
////                                        GeneralPathX gpx=new GeneralPathX();
162
////                                        gpx.moveTo(antPoint.getX(),antPoint.getY());
163
////                                        gpx.lineTo(firstPoint.getX(),firstPoint.getY());
164
////                                        IGeometry line=ShapeFactory.createPolyline2D(gpx);
165
////                                        fgc.addGeometry(line);
166
//                                        newGeom = gAux;
167
//                                }
168
//                        } catch (DriverIOException e) {
169
//                                e.printStackTrace();
170
//                                return;
171
//                        }
172
//
173
//
174
//
175
//
176
//
177
//        }
178

    
179
        addGeometry(fgc);
180
        _fsm = new PolylineCADToolContext(this);
181
        list.clear();
182
        antantPoint=antCenter=antInter=antPoint=firstPoint=null;
183
    }
184
    public void closeGeometry(){
185
        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
186
                2);
187
        elShape.moveTo(antPoint.getX(), antPoint.getY());
188
        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
189

    
190
        list.add(ShapeFactory.createPolyline2D(elShape));
191
        isClosed=true;
192

    
193
        // list.add(ShapeFactory.createPolyline2D(elShape));
194
    }
195
    /* (non-Javadoc)
196
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
197
     */
198
    public void transition(double x, double y, InputEvent event) {
199
        _fsm.addPoint(x, y, event);
200
    }
201

    
202
    /* (non-Javadoc)
203
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
204
     */
205
    public void transition(double d) {
206
        _fsm.addValue(d);
207
    }
208

    
209
    /* (non-Javadoc)
210
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
211
     */
212
    public void transition(String s) throws CommandException {
213
        if (!super.changeCommand(s)){
214
            _fsm.addOption(s);
215
        }
216
    }
217

    
218
    /**
219
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
220
     * editableFeatureSource que ya estar? creado.
221
     *
222
     * @param sel Bitset con las geometr?as que est?n seleccionadas.
223
     * @param x par?metro x del punto que se pase en esta transici?n.
224
     * @param y par?metro y del punto que se pase en esta transici?n.
225
     */
226
    public void addPoint(double x, double y,InputEvent event) {
227
        PolylineCADToolState actualState = (PolylineCADToolState) _fsm.getPreviousState();
228
        String status = actualState.getName();
229

    
230
        if (status.equals("Polyline.FirstPoint")) {
231
            antPoint = new Point2D.Double(x, y);
232

    
233
            if (firstPoint == null) {
234
                firstPoint = (Point2D) antPoint.clone();
235
            }
236
        } else if (status.equals("Polyline.NextPointOrArcOrClose")) {
237
            Point2D point = new Point2D.Double(x, y);
238

    
239
            if (antPoint != null) {
240
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
241
                        2);
242
                elShape.moveTo(antPoint.getX(), antPoint.getY());
243
                elShape.lineTo(point.getX(), point.getY());
244
                list.add(ShapeFactory.createPolyline2D(elShape));
245

    
246
            }
247

    
248
            if (antPoint != null) {
249
                antantPoint = antPoint;
250
            }
251

    
252
            antPoint = point;
253
        } else if (status.equals("Polyline.NextPointOrLineOrClose")) {
254
            Point2D point = new Point2D.Double(x, y);
255
            Point2D lastp = antPoint; //(Point2D)points.get(i-1);
256

    
257
            if (antantPoint == null) {
258
                antantPoint = new Point2D.Double(lastp.getX() +
259
                        (point.getX() / 2), lastp.getY() + (point.getY() / 2));
260
            }
261

    
262
            if (((point.getY() == lastp.getY()) &&
263
                    (point.getX() < lastp.getX())) ||
264
                    ((point.getX() == lastp.getX()) &&
265
                    (point.getY() < lastp.getY()))) {
266
            } else {
267
                if (point.getX() == lastp.getX()) {
268
                    point = new Point2D.Double(point.getX() + 0.00000001,
269
                            point.getY());
270
                } else if (point.getY() == lastp.getY()) {
271
                    point = new Point2D.Double(point.getX(),
272
                            point.getY() + 0.00000001);
273
                }
274

    
275
                if (point.getX() == antantPoint.getX()) {
276
                    point = new Point2D.Double(point.getX() + 0.00000001,
277
                            point.getY());
278
                } else if (point.getY() == antantPoint.getY()) {
279
                    point = new Point2D.Double(point.getX(),
280
                            point.getY() + 0.00000001);
281
                }
282

    
283
                if (!(list.size() > 0) ||
284
                        (((IGeometry) list.get(list.size() - 1)).getGeometryType() == FShape.LINE)) {
285
                    Point2D[] ps1 = UtilFunctions.getPerpendicular(antantPoint,
286
                            lastp, lastp);
287
                    Point2D mediop = new Point2D.Double((point.getX() +
288
                            lastp.getX()) / 2, (point.getY() + lastp.getY()) / 2);
289
                    Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp,
290
                            point, mediop);
291
                    Point2D interp = UtilFunctions.getIntersection(ps1[0],
292
                            ps1[1], ps2[0], ps2[1]);
293
                    antInter = interp;
294

    
295
                    double radio = interp.distance(lastp);
296

    
297
                    if (UtilFunctions.isLowAngle(antantPoint, lastp, interp,
298
                                point)) {
299
                        radio = -radio;
300
                    }
301

    
302
                    Point2D centerp = UtilFunctions.getPoint(interp, mediop,
303
                            radio);
304
                    antCenter = centerp;
305

    
306
                    IGeometry ig = ShapeFactory.createArc(lastp, centerp, point);
307

    
308
                    if (ig != null) {
309
                        list.add(ig);
310
                    }
311
                } else {
312
                    Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp,
313
                            antInter, lastp);
314
                    double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]);
315
                    double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]);
316
                    double angle = UtilFunctions.getAngle(antCenter, lastp);
317
                    Point2D ini1 = null;
318
                    Point2D ini2 = null;
319

    
320
                    if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions.absoluteAngleDistance(
321
                                angle, a2)) {
322
                        ini1 = ps1[0];
323
                        ini2 = ps1[1];
324
                    } else {
325
                        ini1 = ps1[1];
326
                        ini2 = ps1[0];
327
                    }
328

    
329
                    Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
330
                    Point2D correct = new Point2D.Double(lastp.getX() +
331
                            unit.getX(), lastp.getY() + unit.getY());
332
                    Point2D[] ps = UtilFunctions.getPerpendicular(lastp,
333
                            correct, lastp);
334
                    Point2D mediop = new Point2D.Double((point.getX() +
335
                            lastp.getX()) / 2, (point.getY() + lastp.getY()) / 2);
336
                    Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp,
337
                            point, mediop);
338
                    Point2D interp = UtilFunctions.getIntersection(ps[0],
339
                            ps[1], ps2[0], ps2[1]);
340
                    antInter = interp;
341

    
342
                    double radio = interp.distance(lastp);
343

    
344
                    if (UtilFunctions.isLowAngle(correct, lastp, interp, point)) {
345
                        radio = -radio;
346
                    }
347

    
348
                    Point2D centerp = UtilFunctions.getPoint(interp, mediop,
349
                            radio);
350
                    antCenter = centerp;
351
                    list.add(ShapeFactory.createArc(lastp, centerp, point));
352
                }
353

    
354
                antantPoint = antPoint;
355
                antPoint = point;
356
            }
357
        }
358
    }
359

    
360
    /**
361
     * M?todo para dibujar la lo necesario para el estado en el que nos
362
     * encontremos.
363
     *
364
     * @param g Graphics sobre el que dibujar.
365
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
366
     * @param x par?metro x del punto que se pase para dibujar.
367
     * @param y par?metro x del punto que se pase para dibujar.
368
     */
369
    public void drawOperation(Graphics g, double x,
370
        double y) {
371
        PolylineCADToolState actualState = ((PolylineCADToolContext)_fsm).getState();
372
        String status = actualState.getName();
373

    
374
        if (status.equals("Polyline.NextPointOrArcOrClose")) {
375
            for (int i = 0; i < list.size(); i++) {
376
                ((IGeometry) list.get(i)).cloneGeometry().draw((Graphics2D) g,
377
                    getCadToolAdapter().getMapControl().getViewPort(),
378
                    CADTool.drawingSymbol);
379
            }
380

    
381
            drawLine((Graphics2D) g, antPoint, new Point2D.Double(x, y));
382
        } else if ((status.equals("Polyline.NextPointOrLineOrClose"))) {
383
            for (int i = 0; i < list.size(); i++) {
384
                ((IGeometry) list.get(i)).cloneGeometry().draw((Graphics2D) g,
385
                    getCadToolAdapter().getMapControl().getViewPort(),
386
                    CADTool.drawingSymbol);
387
            }
388

    
389
            Point2D point = new Point2D.Double(x, y);
390
            Point2D lastp = antPoint;
391

    
392
            if (!(list.size() > 0) ||
393
                    (((IGeometry) list.get(list.size() - 1)).getGeometryType() == FShape.LINE)) {
394
                if (antantPoint == null) {
395
                    drawArc(point, lastp,
396
                        new Point2D.Double(lastp.getX() + (point.getX() / 2),
397
                            lastp.getY() + (point.getY() / 2)), g);
398
                } else {
399
                    drawArc(point, lastp, antantPoint, g);
400
                }
401
            } else {
402
                if (antInter != null) {
403
                    Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp,
404
                            antInter, lastp);
405
                    double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]);
406
                    double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]);
407
                    double angle = UtilFunctions.getAngle(antCenter, lastp);
408
                    Point2D ini1 = null;
409
                    Point2D ini2 = null;
410

    
411
                    if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions.absoluteAngleDistance(
412
                                angle, a2)) {
413
                        ini1 = ps1[0];
414
                        ini2 = ps1[1];
415
                    } else {
416
                        ini1 = ps1[1];
417
                        ini2 = ps1[0];
418
                    }
419

    
420
                    Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
421
                    Point2D correct = new Point2D.Double(lastp.getX() +
422
                            unit.getX(), lastp.getY() + unit.getY());
423
                    drawArc(point, lastp, correct, g);
424
                }
425
            }
426
        }
427
    }
428

    
429
    /**
430
     * Dibuja el arco sobre el graphics.
431
     *
432
     * @param point Puntero del rat?n.
433
     * @param lastp ?ltimo punto de la polilinea.
434
     * @param antp Punto antepenultimo.
435
     * @param g Graphics sobre el que se dibuja.
436
     */
437
    private void drawArc(Point2D point, Point2D lastp, Point2D antp, Graphics g) {
438
        if (((point.getY() == lastp.getY()) && (point.getX() < lastp.getX())) ||
439
                ((point.getX() == lastp.getX()) &&
440
                (point.getY() < lastp.getY()))) {
441
        } else {
442
            if (point.getX() == lastp.getX()) {
443
                point = new Point2D.Double(point.getX() + 0.00000001,
444
                        point.getY());
445
            } else if (point.getY() == lastp.getY()) {
446
                point = new Point2D.Double(point.getX(),
447
                        point.getY() + 0.00000001);
448
            }
449

    
450
            if (point.getX() == antp.getX()) {
451
                point = new Point2D.Double(point.getX() + 0.00000001,
452
                        point.getY());
453
            } else if (point.getY() == antp.getY()) {
454
                point = new Point2D.Double(point.getX(),
455
                        point.getY() + 0.00000001);
456
            }
457

    
458
            Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp, antp, lastp);
459
            Point2D mediop = new Point2D.Double((point.getX() + lastp.getX()) / 2,
460
                    (point.getY() + lastp.getY()) / 2);
461
            Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp, point, mediop);
462
            Point2D interp = UtilFunctions.getIntersection(ps1[0], ps1[1],
463
                    ps2[0], ps2[1]);
464

    
465
            double radio = interp.distance(lastp);
466

    
467
            if (UtilFunctions.isLowAngle(antp, lastp, interp, point)) {
468
                radio = -radio;
469
            }
470

    
471
            Point2D centerp = UtilFunctions.getPoint(interp, mediop, radio);
472

    
473
            drawLine((Graphics2D) g, lastp, point);
474

    
475
            IGeometry ig = ShapeFactory.createArc(lastp, centerp, point);
476

    
477
            if (ig != null) {
478
                ig.draw((Graphics2D) g,
479
                    getCadToolAdapter().getMapControl().getViewPort(),
480
                    CADTool.modifySymbol);
481
            }
482
        }
483
    }
484

    
485
    /**
486
     * Add a diferent option.
487
     *
488
     * @param sel DOCUMENT ME!
489
     * @param s Diferent option.
490
     */
491
    public void addOption(String s) {
492
            /*       PolylineCADToolState actualState = (PolylineCADToolState) _fsm.getPreviousState();
493
        String status = actualState.getName();
494

495
        if (status.equals("Polyline.NextPointOrArcOrClose")) {
496
            if (s.equals("A") || s.equals("a") || s.equals(PluginServices.getText(this,"inter_arc"))) {
497
                //Arco
498
            } else if (s.equals("C") || s.equals("c")) {
499
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
500
                elShape.moveTo(antPoint.getX(), antPoint.getY());
501
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
502
                list.add(ShapeFactory.createPolyline2D(elShape));
503
                //closeGeometry();
504
            }
505
        } else if (status.equals("Polyline.NextPointOrLineOrClose")) {
506
            if (s.equals("N") || s.equals("n") || s.equals(PluginServices.getText(this,"inter_line"))) {
507
                //L?nea
508
            } else if (s.equals("C") || s.equals("c")) {
509
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
510
                elShape.moveTo(antPoint.getX(), antPoint.getY());
511
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
512
                list.add(ShapeFactory.createPolyline2D(elShape));
513
                //closeGeometry();
514
            }
515
        }
516
  */
517
    }
518

    
519
    /* (non-Javadoc)
520
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
521
     */
522
    public void addValue(double d) {
523
    }
524

    
525
    public void cancel(){
526
        //endGeometry();
527
        list.clear();
528
        antantPoint=antCenter=antInter=antPoint=firstPoint=null;
529
    }
530

    
531
    public void end() {
532
        /* CADExtension.setCADTool("polyline");
533
        PluginServices.getMainFrame().setSelectedTool("POLYLINE"); */
534
    }
535

    
536
    public String getName() {
537
        return PluginServices.getText(this,"polyline_");
538
    }
539

    
540
    public String toString() {
541
        return "_polyline";
542
    }
543
    public boolean isApplicable(int shapeType) {
544
        switch (shapeType) {
545
        case FShape.POINT:
546
            return false;
547
        }
548
        return true;
549
    }
550
}