Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_916 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / PolylineCADTool.java @ 12327

History | View | Annotate | Download (19.5 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.core.v02.FConverter;
58
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
59
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
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

    
65

    
66
/**
67
 * DOCUMENT ME!
68
 *
69
 * @author Vicente Caballero Navarro
70
 */
71
public class PolylineCADTool extends DefaultCADTool {
72
        private static Logger logger = Logger.getLogger(PolylineCADTool.class.getName());
73
    private PolylineCADToolContext _fsm;
74
    private Point2D firstPoint;
75
    private Point2D antPoint;
76
    private Point2D antantPoint;
77
    private Point2D antCenter;
78
    private Point2D antInter;
79
    private ArrayList list = new ArrayList();
80
        private boolean close=false;
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
    public IGeometry getGeometry() {
97
                IGeometry[] geoms = (IGeometry[]) list.toArray(new IGeometry[0]);
98
                FGeometryCollection fgc = new FGeometryCollection(geoms);
99
                // No queremos guardar FGeometryCollections:
100
                GeneralPathX gp = new GeneralPathX();
101
                gp.append(fgc.getPathIterator(null, FConverter.FLATNESS), true);
102
                IGeometry newGeom = ShapeFactory.createPolyline2D(gp);
103
                return newGeom;
104
        }
105
    public void endGeometry() {
106
             try {
107
                         if (getVLE().getVEA().getShapeType()==FShape.POLYGON && !close){
108
                                 closeGeometry();
109
                         }
110
                 } catch (DriverIOException e) {
111
                         e.printStackTrace();
112
                 }
113
            IGeometry[] geoms = (IGeometry[]) list.toArray(new IGeometry[0]);
114
        FGeometryCollection fgc = new FGeometryCollection(geoms);
115

    
116

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

    
154
        list.add(ShapeFactory.createPolyline2D(elShape));
155

    
156

    
157
        // list.add(ShapeFactory.createPolyline2D(elShape));
158

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

    
167
    /* (non-Javadoc)
168
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
169
     */
170
    public void transition(double d) {
171
        _fsm.addValue(d);
172
    }
173

    
174
    /* (non-Javadoc)
175
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
176
     */
177
    public void transition(String s) throws CommandException {
178
        if (!super.changeCommand(s)){
179
            _fsm.addOption(s);
180
        }
181
    }
182

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

    
195
//        if (status.equals("Polyline.FirstPoint")) {
196
//            antPoint = new Point2D.Double(x, y);
197
//
198
//            if (firstPoint == null) {
199
//                firstPoint = (Point2D) antPoint.clone();
200
//            }
201
//        } else
202
        if (status.equals("Polyline.NextPointOrArcOrClose") || status.equals("Polyline.FirstPoint")) {
203

    
204
           if (firstPoint == null) {
205
               firstPoint = new Point2D.Double(x, y);
206
           }
207
                Point2D point = new Point2D.Double(x, y);
208

    
209
            if (antPoint != null) {
210
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
211
                        2);
212
                elShape.moveTo(antPoint.getX(), antPoint.getY());
213
                elShape.lineTo(point.getX(), point.getY());
214
                list.add(ShapeFactory.createPolyline2D(elShape));
215

    
216
            }
217
            if (antPoint==null)
218
                    antPoint = (Point2D)firstPoint.clone();
219

    
220
            if (antPoint != null) {
221
                antantPoint = antPoint;
222
            }
223

    
224
            antPoint = point;
225
        } else if (status.equals("Polyline.NextPointOrLineOrClose")) {
226
            Point2D point = new Point2D.Double(x, y);
227
            Point2D lastp = antPoint; //(Point2D)points.get(i-1);
228

    
229
            if (antantPoint == null) {
230
                antantPoint = new Point2D.Double(lastp.getX() +
231
                        (point.getX() / 2), lastp.getY() + (point.getY() / 2));
232
            }
233

    
234
            if (((point.getY() == lastp.getY()) &&
235
                    (point.getX() < lastp.getX())) ||
236
                    ((point.getX() == lastp.getX()) &&
237
                    (point.getY() < lastp.getY()))) {
238
            } else {
239
                if (point.getX() == lastp.getX()) {
240
                    point = new Point2D.Double(point.getX() + 0.00000001,
241
                            point.getY());
242
                } else if (point.getY() == lastp.getY()) {
243
                    point = new Point2D.Double(point.getX(),
244
                            point.getY() + 0.00000001);
245
                }
246

    
247
                if (point.getX() == antantPoint.getX()) {
248
                    point = new Point2D.Double(point.getX() + 0.00000001,
249
                            point.getY());
250
                } else if (point.getY() == antantPoint.getY()) {
251
                    point = new Point2D.Double(point.getX(),
252
                            point.getY() + 0.00000001);
253
                }
254

    
255
                if (!(list.size() > 0) ||
256
                        (((IGeometry) list.get(list.size() - 1)).getGeometryType() == FShape.LINE)) {
257
                    Point2D[] ps1 = UtilFunctions.getPerpendicular(antantPoint,
258
                            lastp, lastp);
259
                    Point2D mediop = new Point2D.Double((point.getX() +
260
                            lastp.getX()) / 2, (point.getY() + lastp.getY()) / 2);
261
                    Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp,
262
                            point, mediop);
263
                    Point2D interp = UtilFunctions.getIntersection(ps1[0],
264
                            ps1[1], ps2[0], ps2[1]);
265
                    antInter = interp;
266

    
267
                    double radio = interp.distance(lastp);
268

    
269
                    if (UtilFunctions.isLowAngle(antantPoint, lastp, interp,
270
                                point)) {
271
                        radio = -radio;
272
                    }
273

    
274
                    Point2D centerp = UtilFunctions.getPoint(interp, mediop,
275
                            radio);
276
                    antCenter = centerp;
277

    
278
                    IGeometry ig = ShapeFactory.createArc(lastp, centerp, point);
279

    
280
                    if (ig != null) {
281
                        list.add(ig);
282
                    }
283
                } else {
284
                    Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp,
285
                            antInter, lastp);
286
                    double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]);
287
                    double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]);
288
                    double angle = UtilFunctions.getAngle(antCenter, lastp);
289
                    Point2D ini1 = null;
290
                    Point2D ini2 = null;
291

    
292
                    if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions.absoluteAngleDistance(
293
                                angle, a2)) {
294
                        ini1 = ps1[0];
295
                        ini2 = ps1[1];
296
                    } else {
297
                        ini1 = ps1[1];
298
                        ini2 = ps1[0];
299
                    }
300

    
301
                    Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
302
                    Point2D correct = new Point2D.Double(lastp.getX() +
303
                            unit.getX(), lastp.getY() + unit.getY());
304
                    Point2D[] ps = UtilFunctions.getPerpendicular(lastp,
305
                            correct, lastp);
306
                    Point2D mediop = new Point2D.Double((point.getX() +
307
                            lastp.getX()) / 2, (point.getY() + lastp.getY()) / 2);
308
                    Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp,
309
                            point, mediop);
310
                    Point2D interp = UtilFunctions.getIntersection(ps[0],
311
                            ps[1], ps2[0], ps2[1]);
312
                    antInter = interp;
313

    
314
                    double radio = interp.distance(lastp);
315

    
316
                    if (UtilFunctions.isLowAngle(correct, lastp, interp, point)) {
317
                        radio = -radio;
318
                    }
319

    
320
                    Point2D centerp = UtilFunctions.getPoint(interp, mediop,
321
                            radio);
322
                    antCenter = centerp;
323
                    list.add(ShapeFactory.createArc(lastp, centerp, point));
324
                }
325

    
326
                antantPoint = antPoint;
327
                antPoint = point;
328
            }
329
        }
330
    }
331

    
332
    /**
333
     * M?todo para dibujar la lo necesario para el estado en el que nos
334
     * encontremos.
335
     *
336
     * @param g Graphics sobre el que dibujar.
337
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
338
     * @param x par?metro x del punto que se pase para dibujar.
339
     * @param y par?metro x del punto que se pase para dibujar.
340
     */
341
    public void drawOperation(Graphics g, double x,
342
        double y) {
343
        PolylineCADToolState actualState = ((PolylineCADToolContext)_fsm).getState();
344
        String status = actualState.getName();
345

    
346
        if (status.equals("Polyline.NextPointOrArcOrClose") || status.equals("Polyline.FirstPoint")) {
347
            for (int i = 0; i < list.size(); i++) {
348
                ((IGeometry) list.get(i)).cloneGeometry().draw((Graphics2D) g,
349
                    getCadToolAdapter().getMapControl().getViewPort(),
350
                    DefaultCADTool.drawingSymbol);
351
            }
352
            if (antPoint!=null)
353
                    drawLine((Graphics2D) g, antPoint, new Point2D.Double(x, y));
354

    
355
        } else if ((status.equals("Polyline.NextPointOrLineOrClose"))) {
356
            for (int i = 0; i < list.size(); i++) {
357
                ((IGeometry) list.get(i)).cloneGeometry().draw((Graphics2D) g,
358
                    getCadToolAdapter().getMapControl().getViewPort(),
359
                    DefaultCADTool.drawingSymbol);
360
            }
361

    
362
            Point2D point = new Point2D.Double(x, y);
363
            Point2D lastp = antPoint;
364

    
365
            if (!(list.size() > 0) ||
366
                    (((IGeometry) list.get(list.size() - 1)).getGeometryType() == FShape.LINE)) {
367
                if (antantPoint == null) {
368
                    drawArc(point, lastp,
369
                        new Point2D.Double(lastp.getX() + (point.getX() / 2),
370
                            lastp.getY() + (point.getY() / 2)), g);
371
                } else {
372
                    drawArc(point, lastp, antantPoint, g);
373
                }
374
            } else {
375
                if (antInter != null) {
376
                    Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp,
377
                            antInter, lastp);
378
                    double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]);
379
                    double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]);
380
                    double angle = UtilFunctions.getAngle(antCenter, lastp);
381
                    Point2D ini1 = null;
382
                    Point2D ini2 = null;
383

    
384
                    if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions.absoluteAngleDistance(
385
                                angle, a2)) {
386
                        ini1 = ps1[0];
387
                        ini2 = ps1[1];
388
                    } else {
389
                        ini1 = ps1[1];
390
                        ini2 = ps1[0];
391
                    }
392

    
393
                    Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
394
                    Point2D correct = new Point2D.Double(lastp.getX() +
395
                            unit.getX(), lastp.getY() + unit.getY());
396
                    drawArc(point, lastp, correct, g);
397
                }
398
            }
399
        }
400
    }
401

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

    
423
            if (point.getX() == antp.getX()) {
424
                point = new Point2D.Double(point.getX() + 0.00000001,
425
                        point.getY());
426
            } else if (point.getY() == antp.getY()) {
427
                point = new Point2D.Double(point.getX(),
428
                        point.getY() + 0.00000001);
429
            }
430

    
431
            Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp, antp, lastp);
432
            Point2D mediop = new Point2D.Double((point.getX() + lastp.getX()) / 2,
433
                    (point.getY() + lastp.getY()) / 2);
434
            Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp, point, mediop);
435
            Point2D interp = UtilFunctions.getIntersection(ps1[0], ps1[1],
436
                    ps2[0], ps2[1]);
437

    
438
            double radio = interp.distance(lastp);
439

    
440
            if (UtilFunctions.isLowAngle(antp, lastp, interp, point)) {
441
                radio = -radio;
442
            }
443

    
444
            Point2D centerp = UtilFunctions.getPoint(interp, mediop, radio);
445

    
446
            drawLine((Graphics2D) g, lastp, point);
447

    
448
            IGeometry ig = ShapeFactory.createArc(lastp, centerp, point);
449

    
450
            if (ig != null) {
451
                ig.draw((Graphics2D) g,
452
                    getCadToolAdapter().getMapControl().getViewPort(),
453
                    DefaultCADTool.modifySymbol);
454
            }
455
        }
456
    }
457

    
458
    /**
459
     * Add a diferent option.
460
     *
461
     * @param sel DOCUMENT ME!
462
     * @param s Diferent option.
463
     */
464
    public void addOption(String s) {
465
            /*       PolylineCADToolState actualState = (PolylineCADToolState) _fsm.getPreviousState();
466
        String status = actualState.getName();
467

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

    
492
    /* (non-Javadoc)
493
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
494
     */
495
    public void addValue(double d) {
496
    }
497

    
498
    public void cancel(){
499
        //endGeometry();
500
        list.clear();
501
        antantPoint=antCenter=antInter=antPoint=firstPoint=null;
502
    }
503

    
504
    public void end() {
505
        /* CADExtension.setCADTool("polyline");
506
        PluginServices.getMainFrame().setSelectedTool("POLYLINE"); */
507
    }
508

    
509
    public String getName() {
510
        return PluginServices.getText(this,"polyline_");
511
    }
512

    
513
    public String toString() {
514
        return "_polyline";
515
    }
516
    public boolean isApplicable(int shapeType) {
517
        switch (shapeType) {
518
        case FShape.POINT:
519
        case FShape.MULTIPOINT:
520
            return false;
521
        }
522
        return true;
523
    }
524
}