Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / PolylineCADTool.java @ 6043

History | View | Annotate | Download (19.3 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

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

    
66

    
67
/**
68
 * DOCUMENT ME!
69
 *
70
 * @author Vicente Caballero Navarro
71
 */
72
public class PolylineCADTool extends DefaultCADTool {
73
        private static Logger logger = Logger.getLogger(PolylineCADTool.class.getName());
74
    private PolylineCADToolContext _fsm;
75
    private Point2D firstPoint;
76
    private Point2D antPoint;
77
    private Point2D antantPoint;
78
    private Point2D antCenter;
79
    private Point2D antInter;
80
    private ArrayList list = new ArrayList();
81

    
82
    /**
83
     * Crea un nuevo PolylineCADTool.
84
     */
85
    public PolylineCADTool() {
86

    
87
    }
88

    
89
    /**
90
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
91
     * carga previa a la utilizaci?n de la herramienta.
92
     */
93
    public void init() {
94
        _fsm = new PolylineCADToolContext(this);
95
    }
96

    
97
    public void endGeometry() {
98
        IGeometry[] geoms = (IGeometry[]) list.toArray(new IGeometry[0]);
99
        FGeometryCollection fgc = new FGeometryCollection(geoms);
100
        /* try {
101
                        if (getVLE().getVEA().getShapeType()==FShape.POLYGON){
102
                                GeneralPathX gpx=new GeneralPathX();
103
                                gpx.moveTo(antPoint.getX(),antPoint.getY());
104
                                gpx.lineTo(firstPoint.getX(),firstPoint.getY());
105
                                IGeometry line=ShapeFactory.createPolyline2D(gpx);
106
                                fgc.addGeometry(line);
107
                        }
108
                } catch (DriverIOException e) {
109
                        e.printStackTrace();
110
                } */
111

    
112
        // No queremos guardar FGeometryCollections:
113
        GeneralPathX gp = new GeneralPathX();
114
        gp.append(fgc.getPathIterator(null), true);
115
        IGeometry newGeom = ShapeFactory.createPolyline2D(gp);
116
//        if (gp.isClosed())
117
//        {
118
//                gp.ensureOrientation(false); // Poligono exterior.
119
//                // Voy a testear que se puede convertir a JTS:
120
//                IGeometry gAux = ShapeFactory.createPolygon2D(gp);
121
//                Geometry jtsG = gAux.toJTSGeometry();
122
//                logger.debug("A punto de escribir " + jtsG.toText());
123
//                try {
124
//                        int shapeType = getVLE().getVEA().getShapeType();
125
//                                if ((shapeType ==FShape.POLYGON) || (shapeType == FShape.MULTI)) {
126
//                                        newGeom = gAux;
127
//                                }
128
//                        } catch (DriverIOException e) {
129
//                                // TODO Auto-generated catch block
130
//                                e.printStackTrace();
131
//                                return;
132
//                        }
133
//
134
//        }
135
//
136
        addGeometry(newGeom);
137
        _fsm = new PolylineCADToolContext(this);
138
        list.clear();
139
        antantPoint=antCenter=antInter=antPoint=firstPoint=null;
140
    }
141
    public void closeGeometry(){
142
        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
143
                2);
144
        elShape.moveTo(antPoint.getX(), antPoint.getY());
145
        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
146

    
147
        list.add(ShapeFactory.createPolyline2D(elShape));
148

    
149

    
150
        // list.add(ShapeFactory.createPolyline2D(elShape));
151

    
152
    }
153
    /* (non-Javadoc)
154
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
155
     */
156
    public void transition(double x, double y, InputEvent event) {
157
        _fsm.addPoint(x, y, event);
158
    }
159

    
160
    /* (non-Javadoc)
161
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
162
     */
163
    public void transition(double d) {
164
        _fsm.addValue(d);
165
    }
166

    
167
    /* (non-Javadoc)
168
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
169
     */
170
    public void transition(String s) throws CommandException {
171
        if (!super.changeCommand(s)){
172
            _fsm.addOption(s);
173
        }
174
    }
175

    
176
    /**
177
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
178
     * editableFeatureSource que ya estar? creado.
179
     *
180
     * @param sel Bitset con las geometr?as que est?n seleccionadas.
181
     * @param x par?metro x del punto que se pase en esta transici?n.
182
     * @param y par?metro y del punto que se pase en esta transici?n.
183
     */
184
    public void addPoint(double x, double y,InputEvent event) {
185
        PolylineCADToolState actualState = (PolylineCADToolState) _fsm.getPreviousState();
186
        String status = actualState.getName();
187

    
188
//        if (status.equals("Polyline.FirstPoint")) {
189
//            antPoint = new Point2D.Double(x, y);
190
//
191
//            if (firstPoint == null) {
192
//                firstPoint = (Point2D) antPoint.clone();
193
//            }
194
//        } else
195
        if (status.equals("Polyline.NextPointOrArcOrClose") || status.equals("Polyline.FirstPoint")) {
196
           if (antPoint==null)
197
                   antPoint = new Point2D.Double(x, y);
198
           if (firstPoint == null) {
199
               firstPoint = (Point2D) antPoint.clone();
200
           }
201
                Point2D point = new Point2D.Double(x, y);
202

    
203
            if (antPoint != null) {
204
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
205
                        2);
206
                elShape.moveTo(antPoint.getX(), antPoint.getY());
207
                elShape.lineTo(point.getX(), point.getY());
208
                list.add(ShapeFactory.createPolyline2D(elShape));
209

    
210
            }
211

    
212
            if (antPoint != null) {
213
                antantPoint = antPoint;
214
            }
215

    
216
            antPoint = point;
217
        } else if (status.equals("Polyline.NextPointOrLineOrClose")) {
218
            Point2D point = new Point2D.Double(x, y);
219
            Point2D lastp = antPoint; //(Point2D)points.get(i-1);
220

    
221
            if (antantPoint == null) {
222
                antantPoint = new Point2D.Double(lastp.getX() +
223
                        (point.getX() / 2), lastp.getY() + (point.getY() / 2));
224
            }
225

    
226
            if (((point.getY() == lastp.getY()) &&
227
                    (point.getX() < lastp.getX())) ||
228
                    ((point.getX() == lastp.getX()) &&
229
                    (point.getY() < lastp.getY()))) {
230
            } else {
231
                if (point.getX() == lastp.getX()) {
232
                    point = new Point2D.Double(point.getX() + 0.00000001,
233
                            point.getY());
234
                } else if (point.getY() == lastp.getY()) {
235
                    point = new Point2D.Double(point.getX(),
236
                            point.getY() + 0.00000001);
237
                }
238

    
239
                if (point.getX() == antantPoint.getX()) {
240
                    point = new Point2D.Double(point.getX() + 0.00000001,
241
                            point.getY());
242
                } else if (point.getY() == antantPoint.getY()) {
243
                    point = new Point2D.Double(point.getX(),
244
                            point.getY() + 0.00000001);
245
                }
246

    
247
                if (!(list.size() > 0) ||
248
                        (((IGeometry) list.get(list.size() - 1)).getGeometryType() == FShape.LINE)) {
249
                    Point2D[] ps1 = UtilFunctions.getPerpendicular(antantPoint,
250
                            lastp, lastp);
251
                    Point2D mediop = new Point2D.Double((point.getX() +
252
                            lastp.getX()) / 2, (point.getY() + lastp.getY()) / 2);
253
                    Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp,
254
                            point, mediop);
255
                    Point2D interp = UtilFunctions.getIntersection(ps1[0],
256
                            ps1[1], ps2[0], ps2[1]);
257
                    antInter = interp;
258

    
259
                    double radio = interp.distance(lastp);
260

    
261
                    if (UtilFunctions.isLowAngle(antantPoint, lastp, interp,
262
                                point)) {
263
                        radio = -radio;
264
                    }
265

    
266
                    Point2D centerp = UtilFunctions.getPoint(interp, mediop,
267
                            radio);
268
                    antCenter = centerp;
269

    
270
                    IGeometry ig = ShapeFactory.createArc(lastp, centerp, point);
271

    
272
                    if (ig != null) {
273
                        list.add(ig);
274
                    }
275
                } else {
276
                    Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp,
277
                            antInter, lastp);
278
                    double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]);
279
                    double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]);
280
                    double angle = UtilFunctions.getAngle(antCenter, lastp);
281
                    Point2D ini1 = null;
282
                    Point2D ini2 = null;
283

    
284
                    if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions.absoluteAngleDistance(
285
                                angle, a2)) {
286
                        ini1 = ps1[0];
287
                        ini2 = ps1[1];
288
                    } else {
289
                        ini1 = ps1[1];
290
                        ini2 = ps1[0];
291
                    }
292

    
293
                    Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
294
                    Point2D correct = new Point2D.Double(lastp.getX() +
295
                            unit.getX(), lastp.getY() + unit.getY());
296
                    Point2D[] ps = UtilFunctions.getPerpendicular(lastp,
297
                            correct, lastp);
298
                    Point2D mediop = new Point2D.Double((point.getX() +
299
                            lastp.getX()) / 2, (point.getY() + lastp.getY()) / 2);
300
                    Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp,
301
                            point, mediop);
302
                    Point2D interp = UtilFunctions.getIntersection(ps[0],
303
                            ps[1], ps2[0], ps2[1]);
304
                    antInter = interp;
305

    
306
                    double radio = interp.distance(lastp);
307

    
308
                    if (UtilFunctions.isLowAngle(correct, lastp, interp, point)) {
309
                        radio = -radio;
310
                    }
311

    
312
                    Point2D centerp = UtilFunctions.getPoint(interp, mediop,
313
                            radio);
314
                    antCenter = centerp;
315
                    list.add(ShapeFactory.createArc(lastp, centerp, point));
316
                }
317

    
318
                antantPoint = antPoint;
319
                antPoint = point;
320
            }
321
        }
322
    }
323

    
324
    /**
325
     * M?todo para dibujar la lo necesario para el estado en el que nos
326
     * encontremos.
327
     *
328
     * @param g Graphics sobre el que dibujar.
329
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
330
     * @param x par?metro x del punto que se pase para dibujar.
331
     * @param y par?metro x del punto que se pase para dibujar.
332
     */
333
    public void drawOperation(Graphics g, double x,
334
        double y) {
335
        PolylineCADToolState actualState = ((PolylineCADToolContext)_fsm).getState();
336
        String status = actualState.getName();
337

    
338
        if (status.equals("Polyline.NextPointOrArcOrClose") || status.equals("Polyline.FirstPoint")) {
339
            for (int i = 0; i < list.size(); i++) {
340
                ((IGeometry) list.get(i)).cloneGeometry().draw((Graphics2D) g,
341
                    getCadToolAdapter().getMapControl().getViewPort(),
342
                    CADTool.drawingSymbol);
343
            }
344
            if (antPoint!=null)
345
                    drawLine((Graphics2D) g, antPoint, new Point2D.Double(x, y));
346

    
347
        } else if ((status.equals("Polyline.NextPointOrLineOrClose"))) {
348
            for (int i = 0; i < list.size(); i++) {
349
                ((IGeometry) list.get(i)).cloneGeometry().draw((Graphics2D) g,
350
                    getCadToolAdapter().getMapControl().getViewPort(),
351
                    CADTool.drawingSymbol);
352
            }
353

    
354
            Point2D point = new Point2D.Double(x, y);
355
            Point2D lastp = antPoint;
356

    
357
            if (!(list.size() > 0) ||
358
                    (((IGeometry) list.get(list.size() - 1)).getGeometryType() == FShape.LINE)) {
359
                if (antantPoint == null) {
360
                    drawArc(point, lastp,
361
                        new Point2D.Double(lastp.getX() + (point.getX() / 2),
362
                            lastp.getY() + (point.getY() / 2)), g);
363
                } else {
364
                    drawArc(point, lastp, antantPoint, g);
365
                }
366
            } else {
367
                if (antInter != null) {
368
                    Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp,
369
                            antInter, lastp);
370
                    double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]);
371
                    double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]);
372
                    double angle = UtilFunctions.getAngle(antCenter, lastp);
373
                    Point2D ini1 = null;
374
                    Point2D ini2 = null;
375

    
376
                    if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions.absoluteAngleDistance(
377
                                angle, a2)) {
378
                        ini1 = ps1[0];
379
                        ini2 = ps1[1];
380
                    } else {
381
                        ini1 = ps1[1];
382
                        ini2 = ps1[0];
383
                    }
384

    
385
                    Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
386
                    Point2D correct = new Point2D.Double(lastp.getX() +
387
                            unit.getX(), lastp.getY() + unit.getY());
388
                    drawArc(point, lastp, correct, g);
389
                }
390
            }
391
        }
392
    }
393

    
394
    /**
395
     * Dibuja el arco sobre el graphics.
396
     *
397
     * @param point Puntero del rat?n.
398
     * @param lastp ?ltimo punto de la polilinea.
399
     * @param antp Punto antepenultimo.
400
     * @param g Graphics sobre el que se dibuja.
401
     */
402
    private void drawArc(Point2D point, Point2D lastp, Point2D antp, Graphics g) {
403
        if (((point.getY() == lastp.getY()) && (point.getX() < lastp.getX())) ||
404
                ((point.getX() == lastp.getX()) &&
405
                (point.getY() < lastp.getY()))) {
406
        } else {
407
            if (point.getX() == lastp.getX()) {
408
                point = new Point2D.Double(point.getX() + 0.00000001,
409
                        point.getY());
410
            } else if (point.getY() == lastp.getY()) {
411
                point = new Point2D.Double(point.getX(),
412
                        point.getY() + 0.00000001);
413
            }
414

    
415
            if (point.getX() == antp.getX()) {
416
                point = new Point2D.Double(point.getX() + 0.00000001,
417
                        point.getY());
418
            } else if (point.getY() == antp.getY()) {
419
                point = new Point2D.Double(point.getX(),
420
                        point.getY() + 0.00000001);
421
            }
422

    
423
            Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp, antp, lastp);
424
            Point2D mediop = new Point2D.Double((point.getX() + lastp.getX()) / 2,
425
                    (point.getY() + lastp.getY()) / 2);
426
            Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp, point, mediop);
427
            Point2D interp = UtilFunctions.getIntersection(ps1[0], ps1[1],
428
                    ps2[0], ps2[1]);
429

    
430
            double radio = interp.distance(lastp);
431

    
432
            if (UtilFunctions.isLowAngle(antp, lastp, interp, point)) {
433
                radio = -radio;
434
            }
435

    
436
            Point2D centerp = UtilFunctions.getPoint(interp, mediop, radio);
437

    
438
            drawLine((Graphics2D) g, lastp, point);
439

    
440
            IGeometry ig = ShapeFactory.createArc(lastp, centerp, point);
441

    
442
            if (ig != null) {
443
                ig.draw((Graphics2D) g,
444
                    getCadToolAdapter().getMapControl().getViewPort(),
445
                    CADTool.modifySymbol);
446
            }
447
        }
448
    }
449

    
450
    /**
451
     * Add a diferent option.
452
     *
453
     * @param sel DOCUMENT ME!
454
     * @param s Diferent option.
455
     */
456
    public void addOption(String s) {
457
            /*       PolylineCADToolState actualState = (PolylineCADToolState) _fsm.getPreviousState();
458
        String status = actualState.getName();
459

460
        if (status.equals("Polyline.NextPointOrArcOrClose")) {
461
            if (s.equals("A") || s.equals("a") || s.equals(PluginServices.getText(this,"inter_arc"))) {
462
                //Arco
463
            } else if (s.equals("C") || s.equals("c")) {
464
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
465
                elShape.moveTo(antPoint.getX(), antPoint.getY());
466
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
467
                list.add(ShapeFactory.createPolyline2D(elShape));
468
                //closeGeometry();
469
            }
470
        } else if (status.equals("Polyline.NextPointOrLineOrClose")) {
471
            if (s.equals("N") || s.equals("n") || s.equals(PluginServices.getText(this,"inter_line"))) {
472
                //L?nea
473
            } else if (s.equals("C") || s.equals("c")) {
474
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
475
                elShape.moveTo(antPoint.getX(), antPoint.getY());
476
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
477
                list.add(ShapeFactory.createPolyline2D(elShape));
478
                //closeGeometry();
479
            }
480
        }
481
  */
482
    }
483

    
484
    /* (non-Javadoc)
485
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
486
     */
487
    public void addValue(double d) {
488
    }
489

    
490
    public void cancel(){
491
        //endGeometry();
492
        list.clear();
493
        antantPoint=antCenter=antInter=antPoint=firstPoint=null;
494
    }
495

    
496
    public void end() {
497
        /* CADExtension.setCADTool("polyline");
498
        PluginServices.getMainFrame().setSelectedTool("POLYLINE"); */
499
    }
500

    
501
    public String getName() {
502
        return PluginServices.getText(this,"polyline_");
503
    }
504

    
505
    public String toString() {
506
        return "_polyline";
507
    }
508
    public boolean isApplicable(int shapeType) {
509
        switch (shapeType) {
510
        case FShape.POINT:
511
        case FShape.MULTIPOINT:
512
            return false;
513
        }
514
        return true;
515
    }
516
}