Statistics
| Revision:

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

History | View | Annotate | Download (19.6 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.CADTool;
61
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
62
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
63
import com.iver.cit.gvsig.gui.cad.tools.smc.PolylineCADToolContext;
64
import com.iver.cit.gvsig.gui.cad.tools.smc.PolylineCADToolContext.PolylineCADToolState;
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
        private boolean close=false;
82

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

    
88
    }
89

    
90
    /**
91
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
92
     * carga previa a la utilizaci?n de la herramienta.
93
     */
94
    public void init() {
95
        _fsm = new PolylineCADToolContext(this);
96
    }
97
    public IGeometry getGeometry() {
98
                IGeometry[] geoms = (IGeometry[]) list.toArray(new IGeometry[0]);
99
                FGeometryCollection fgc = new FGeometryCollection(geoms);
100
                // No queremos guardar FGeometryCollections:
101
                GeneralPathX gp = new GeneralPathX();
102
                gp.append(fgc.getPathIterator(null, FConverter.FLATNESS), true);
103
                IGeometry newGeom = ShapeFactory.createPolyline2D(gp);
104
                return newGeom;
105
        }
106
    public void endGeometry() {
107
             try {
108
                         if (getVLE().getVEA().getShapeType()==FShape.POLYGON && !close){
109
                                 closeGeometry();
110
                         }
111
                 } catch (DriverIOException e) {
112
                         e.printStackTrace();
113
                 }
114
            IGeometry[] geoms = (IGeometry[]) list.toArray(new IGeometry[0]);
115
        FGeometryCollection fgc = new FGeometryCollection(geoms);
116

    
117

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

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

    
157

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
347
        if (status.equals("Polyline.NextPointOrArcOrClose") || status.equals("Polyline.FirstPoint")) {
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
            if (antPoint!=null)
354
                    drawLine((Graphics2D) g, antPoint, new Point2D.Double(x, y));
355

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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