Statistics
| Revision:

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

History | View | Annotate | Download (19.8 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 = null;
121
        int type=getCadToolAdapter().getActiveLayerType();
122
        if (type==FShape.POLYGON){
123
                newGeom = ShapeFactory.createPolygon2D(gp);
124
        }else{
125
                newGeom = ShapeFactory.createPolyline2D(gp);
126
        }
127

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

    
161
        list.add(ShapeFactory.createPolyline2D(elShape));
162

    
163

    
164
        // list.add(ShapeFactory.createPolyline2D(elShape));
165

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

    
174
    /* (non-Javadoc)
175
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
176
     */
177
    public void transition(double d) {
178
        _fsm.addValue(d);
179
    }
180

    
181
    /* (non-Javadoc)
182
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
183
     */
184
    public void transition(String s) throws CommandException {
185
        if (!super.changeCommand(s)){
186
            _fsm.addOption(s);
187
        }
188
    }
189

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

    
202
//        if (status.equals("Polyline.FirstPoint")) {
203
//            antPoint = new Point2D.Double(x, y);
204
//
205
//            if (firstPoint == null) {
206
//                firstPoint = (Point2D) antPoint.clone();
207
//            }
208
//        } else
209
        if (status.equals("Polyline.NextPointOrArcOrClose") || status.equals("Polyline.FirstPoint")) {
210

    
211
           if (firstPoint == null) {
212
               firstPoint = new Point2D.Double(x, y);
213
           }
214
                Point2D point = new Point2D.Double(x, y);
215

    
216
            if (antPoint != null) {
217
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
218
                        2);
219
                elShape.moveTo(antPoint.getX(), antPoint.getY());
220
                elShape.lineTo(point.getX(), point.getY());
221
                list.add(ShapeFactory.createPolyline2D(elShape));
222

    
223
            }
224
            if (antPoint==null)
225
                    antPoint = (Point2D)firstPoint.clone();
226

    
227
            if (antPoint != null) {
228
                antantPoint = antPoint;
229
            }
230

    
231
            antPoint = point;
232
        } else if (status.equals("Polyline.NextPointOrLineOrClose")) {
233
            Point2D point = new Point2D.Double(x, y);
234
            Point2D lastp = antPoint; //(Point2D)points.get(i-1);
235

    
236
            if (antantPoint == null) {
237
                antantPoint = new Point2D.Double(lastp.getX() +
238
                        (point.getX() / 2), lastp.getY() + (point.getY() / 2));
239
            }
240

    
241
            if (((point.getY() == lastp.getY()) &&
242
                    (point.getX() < lastp.getX())) ||
243
                    ((point.getX() == lastp.getX()) &&
244
                    (point.getY() < lastp.getY()))) {
245
            } else {
246
                if (point.getX() == lastp.getX()) {
247
                    point = new Point2D.Double(point.getX() + 0.00000001,
248
                            point.getY());
249
                } else if (point.getY() == lastp.getY()) {
250
                    point = new Point2D.Double(point.getX(),
251
                            point.getY() + 0.00000001);
252
                }
253

    
254
                if (point.getX() == antantPoint.getX()) {
255
                    point = new Point2D.Double(point.getX() + 0.00000001,
256
                            point.getY());
257
                } else if (point.getY() == antantPoint.getY()) {
258
                    point = new Point2D.Double(point.getX(),
259
                            point.getY() + 0.00000001);
260
                }
261

    
262
                if (!(list.size() > 0) ||
263
                        (((IGeometry) list.get(list.size() - 1)).getGeometryType() == FShape.LINE)) {
264
                    Point2D[] ps1 = UtilFunctions.getPerpendicular(antantPoint,
265
                            lastp, lastp);
266
                    Point2D mediop = new Point2D.Double((point.getX() +
267
                            lastp.getX()) / 2, (point.getY() + lastp.getY()) / 2);
268
                    Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp,
269
                            point, mediop);
270
                    Point2D interp = UtilFunctions.getIntersection(ps1[0],
271
                            ps1[1], ps2[0], ps2[1]);
272
                    antInter = interp;
273

    
274
                    double radio = interp.distance(lastp);
275

    
276
                    if (UtilFunctions.isLowAngle(antantPoint, lastp, interp,
277
                                point)) {
278
                        radio = -radio;
279
                    }
280

    
281
                    Point2D centerp = UtilFunctions.getPoint(interp, mediop,
282
                            radio);
283
                    antCenter = centerp;
284

    
285
                    IGeometry ig = ShapeFactory.createArc(lastp, centerp, point);
286

    
287
                    if (ig != null) {
288
                        list.add(ig);
289
                    }
290
                } else {
291
                    Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp,
292
                            antInter, lastp);
293
                    double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]);
294
                    double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]);
295
                    double angle = UtilFunctions.getAngle(antCenter, lastp);
296
                    Point2D ini1 = null;
297
                    Point2D ini2 = null;
298

    
299
                    if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions.absoluteAngleDistance(
300
                                angle, a2)) {
301
                        ini1 = ps1[0];
302
                        ini2 = ps1[1];
303
                    } else {
304
                        ini1 = ps1[1];
305
                        ini2 = ps1[0];
306
                    }
307

    
308
                    Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
309
                    Point2D correct = new Point2D.Double(lastp.getX() +
310
                            unit.getX(), lastp.getY() + unit.getY());
311
                    Point2D[] ps = UtilFunctions.getPerpendicular(lastp,
312
                            correct, lastp);
313
                    Point2D mediop = new Point2D.Double((point.getX() +
314
                            lastp.getX()) / 2, (point.getY() + lastp.getY()) / 2);
315
                    Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp,
316
                            point, mediop);
317
                    Point2D interp = UtilFunctions.getIntersection(ps[0],
318
                            ps[1], ps2[0], ps2[1]);
319
                    antInter = interp;
320

    
321
                    double radio = interp.distance(lastp);
322

    
323
                    if (UtilFunctions.isLowAngle(correct, lastp, interp, point)) {
324
                        radio = -radio;
325
                    }
326

    
327
                    Point2D centerp = UtilFunctions.getPoint(interp, mediop,
328
                            radio);
329
                    antCenter = centerp;
330
                    list.add(ShapeFactory.createArc(lastp, centerp, point));
331
                }
332

    
333
                antantPoint = antPoint;
334
                antPoint = point;
335
            }
336
        }
337
    }
338

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

    
353
        if (status.equals("Polyline.NextPointOrArcOrClose") || status.equals("Polyline.FirstPoint")) {
354
            for (int i = 0; i < list.size(); i++) {
355
                ((IGeometry) list.get(i)).cloneGeometry().draw((Graphics2D) g,
356
                    getCadToolAdapter().getMapControl().getViewPort(),
357
                    DefaultCADTool.geometrySelectSymbol);
358
            }
359
            if (antPoint!=null)
360
                    drawLine((Graphics2D) g, antPoint, new Point2D.Double(x, y),DefaultCADTool.geometrySelectSymbol);
361

    
362
        } else if ((status.equals("Polyline.NextPointOrLineOrClose"))) {
363
            for (int i = 0; i < list.size(); i++) {
364
                ((IGeometry) list.get(i)).cloneGeometry().draw((Graphics2D) g,
365
                    getCadToolAdapter().getMapControl().getViewPort(),
366
                    DefaultCADTool.geometrySelectSymbol);
367
            }
368

    
369
            Point2D point = new Point2D.Double(x, y);
370
            Point2D lastp = antPoint;
371

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

    
391
                    if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions.absoluteAngleDistance(
392
                                angle, a2)) {
393
                        ini1 = ps1[0];
394
                        ini2 = ps1[1];
395
                    } else {
396
                        ini1 = ps1[1];
397
                        ini2 = ps1[0];
398
                    }
399

    
400
                    Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
401
                    Point2D correct = new Point2D.Double(lastp.getX() +
402
                            unit.getX(), lastp.getY() + unit.getY());
403
                    drawArc(point, lastp, correct, g);
404
                }
405
            }
406
        }
407
    }
408

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

    
430
            if (point.getX() == antp.getX()) {
431
                point = new Point2D.Double(point.getX() + 0.00000001,
432
                        point.getY());
433
            } else if (point.getY() == antp.getY()) {
434
                point = new Point2D.Double(point.getX(),
435
                        point.getY() + 0.00000001);
436
            }
437

    
438
            Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp, antp, lastp);
439
            Point2D mediop = new Point2D.Double((point.getX() + lastp.getX()) / 2,
440
                    (point.getY() + lastp.getY()) / 2);
441
            Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp, point, mediop);
442
            Point2D interp = UtilFunctions.getIntersection(ps1[0], ps1[1],
443
                    ps2[0], ps2[1]);
444

    
445
            double radio = interp.distance(lastp);
446

    
447
            if (UtilFunctions.isLowAngle(antp, lastp, interp, point)) {
448
                radio = -radio;
449
            }
450

    
451
            Point2D centerp = UtilFunctions.getPoint(interp, mediop, radio);
452

    
453
            drawLine((Graphics2D) g, lastp, point,DefaultCADTool.geometrySelectSymbol);
454

    
455
            IGeometry ig = ShapeFactory.createArc(lastp, centerp, point);
456

    
457
            if (ig != null) {
458
                ig.draw((Graphics2D) g,
459
                    getCadToolAdapter().getMapControl().getViewPort(),
460
                    DefaultCADTool.axisReferencesSymbol);
461
            }
462
        }
463
    }
464

    
465
    /**
466
     * Add a diferent option.
467
     *
468
     * @param sel DOCUMENT ME!
469
     * @param s Diferent option.
470
     */
471
    public void addOption(String s) {
472
            /*       PolylineCADToolState actualState = (PolylineCADToolState) _fsm.getPreviousState();
473
        String status = actualState.getName();
474

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

    
499
    /* (non-Javadoc)
500
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
501
     */
502
    public void addValue(double d) {
503
    }
504

    
505
    public void cancel(){
506
        //endGeometry();
507
        list.clear();
508
        antantPoint=antCenter=antInter=antPoint=firstPoint=null;
509
    }
510

    
511
    public void end() {
512
        /* CADExtension.setCADTool("polyline");
513
        PluginServices.getMainFrame().setSelectedTool("POLYLINE"); */
514
    }
515

    
516
    public String getName() {
517
        return PluginServices.getText(this,"polyline_");
518
    }
519

    
520
    public String toString() {
521
        return "_polyline";
522
    }
523
    public boolean isApplicable(int shapeType) {
524
        switch (shapeType) {
525
        case FShape.POINT:
526
        case FShape.MULTIPOINT:
527
            return false;
528
        }
529
        return true;
530
    }
531
}