Statistics
| Revision:

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

History | View | Annotate | Download (18.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 com.hardcode.gdbms.driver.exceptions.ReadDriverException;
50
import com.iver.andami.PluginServices;
51
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
52
import com.iver.cit.gvsig.fmap.core.FShape;
53
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
54
import com.iver.cit.gvsig.fmap.core.IGeometry;
55
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
56
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
57
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
58
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
59
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
60
import com.iver.cit.gvsig.gui.cad.tools.smc.PolylineCADToolContext;
61
import com.iver.cit.gvsig.gui.cad.tools.smc.PolylineCADToolContext.PolylineCADToolState;
62

    
63

    
64
/**
65
 * DOCUMENT ME!
66
 *
67
 * @author Vicente Caballero Navarro
68
 */
69
public class PolylineCADTool extends DefaultCADTool {
70
        private PolylineCADToolContext _fsm;
71
    private Point2D firstPoint;
72
    private Point2D antPoint;
73
    private Point2D antantPoint;
74
    private Point2D antCenter;
75
    private Point2D antInter;
76
    private ArrayList list = new ArrayList();
77
        private boolean close=false;
78
        private GeneralPathX gpx = null;
79

    
80
    /**
81
     * Crea un nuevo PolylineCADTool.
82
     */
83
    public PolylineCADTool() {
84

    
85
    }
86

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

    
120
        IGeometry newGeom = ShapeFactory.createPolyline2D(gpx);
121
        addGeometry(newGeom);
122
        _fsm = new PolylineCADToolContext(this);
123
        list.clear();
124
        close=false;
125
        gpx=null;
126
        antantPoint=antCenter=antInter=antPoint=firstPoint=null;
127
    }
128
    public void closeGeometry(){
129
            if (gpx==null) {
130
                    gpx=new GeneralPathX();
131
                    IGeometry[] geoms = (IGeometry[]) list.toArray(new IGeometry[0]);
132
                    FGeometryCollection fgc = new FGeometryCollection(geoms);
133
                    // No queremos guardar FGeometryCollections:
134
                    gpx.append(fgc.getPathIterator(null,FConverter.FLATNESS), true);
135
            }
136
            close=true;
137
            gpx.closePath();
138
    }
139
    /* (non-Javadoc)
140
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
141
     */
142
    public void transition(double x, double y, InputEvent event) {
143
        _fsm.addPoint(x, y, event);
144
    }
145

    
146
    /* (non-Javadoc)
147
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
148
     */
149
    public void transition(double d) {
150
        _fsm.addValue(d);
151
    }
152

    
153
    /* (non-Javadoc)
154
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
155
     */
156
    public void transition(String s) throws CommandException {
157
        if (!super.changeCommand(s)){
158
            _fsm.addOption(s);
159
        }
160
    }
161

    
162
    /**
163
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
164
     * editableFeatureSource que ya estar? creado.
165
     *
166
     * @param sel Bitset con las geometr?as que est?n seleccionadas.
167
     * @param x par?metro x del punto que se pase en esta transici?n.
168
     * @param y par?metro y del punto que se pase en esta transici?n.
169
     */
170
    public void addPoint(double x, double y,InputEvent event) {
171
        PolylineCADToolState actualState = (PolylineCADToolState) _fsm.getPreviousState();
172
        String status = actualState.getName();
173
        if (status.equals("Polyline.NextPointOrArcOrClose") || status.equals("Polyline.FirstPoint")) {
174

    
175
           if (firstPoint == null) {
176
               firstPoint = new Point2D.Double(x, y);
177
           }
178
                Point2D point = new Point2D.Double(x, y);
179

    
180
            if (antPoint != null) {
181
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
182
                        2);
183
                elShape.moveTo(antPoint.getX(), antPoint.getY());
184
                elShape.lineTo(point.getX(), point.getY());
185
                list.add(ShapeFactory.createPolyline2D(elShape));
186

    
187
            }
188
            if (antPoint==null)
189
                    antPoint = (Point2D)firstPoint.clone();
190

    
191
            if (antPoint != null) {
192
                antantPoint = antPoint;
193
            }
194

    
195
            antPoint = point;
196
        } else if (status.equals("Polyline.NextPointOrLineOrClose")) {
197
            Point2D point = new Point2D.Double(x, y);
198
            Point2D lastp = antPoint; //(Point2D)points.get(i-1);
199

    
200
            if (antantPoint == null) {
201
                antantPoint = new Point2D.Double(lastp.getX() +
202
                        (point.getX() / 2), lastp.getY() + (point.getY() / 2));
203
            }
204

    
205
            if (((point.getY() == lastp.getY()) &&
206
                    (point.getX() < lastp.getX())) ||
207
                    ((point.getX() == lastp.getX()) &&
208
                    (point.getY() < lastp.getY()))) {
209
            } else {
210
                if (point.getX() == lastp.getX()) {
211
                    point = new Point2D.Double(point.getX() + 0.00000001,
212
                            point.getY());
213
                } else if (point.getY() == lastp.getY()) {
214
                    point = new Point2D.Double(point.getX(),
215
                            point.getY() + 0.00000001);
216
                }
217

    
218
                if (point.getX() == antantPoint.getX()) {
219
                    point = new Point2D.Double(point.getX() + 0.00000001,
220
                            point.getY());
221
                } else if (point.getY() == antantPoint.getY()) {
222
                    point = new Point2D.Double(point.getX(),
223
                            point.getY() + 0.00000001);
224
                }
225

    
226
                if (!(list.size() > 0) ||
227
                        (((IGeometry) list.get(list.size() - 1)).getGeometryType() == FShape.LINE)) {
228
                    Point2D[] ps1 = UtilFunctions.getPerpendicular(antantPoint,
229
                            lastp, lastp);
230
                    Point2D mediop = new Point2D.Double((point.getX() +
231
                            lastp.getX()) / 2, (point.getY() + lastp.getY()) / 2);
232
                    Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp,
233
                            point, mediop);
234
                    Point2D interp = UtilFunctions.getIntersection(ps1[0],
235
                            ps1[1], ps2[0], ps2[1]);
236
                    antInter = interp;
237

    
238
                    double radio = interp.distance(lastp);
239

    
240
                    if (UtilFunctions.isLowAngle(antantPoint, lastp, interp,
241
                                point)) {
242
                        radio = -radio;
243
                    }
244

    
245
                    Point2D centerp = UtilFunctions.getPoint(interp, mediop,
246
                            radio);
247
                    antCenter = centerp;
248

    
249
                    IGeometry ig = ShapeFactory.createArc(lastp, centerp, point);
250

    
251
                    if (ig != null) {
252
                        list.add(ig);
253
                    }
254
                } else {
255
                    Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp,
256
                            antInter, lastp);
257
                    double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]);
258
                    double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]);
259
                    double angle = UtilFunctions.getAngle(antCenter, lastp);
260
                    Point2D ini1 = null;
261
                    Point2D ini2 = null;
262

    
263
                    if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions.absoluteAngleDistance(
264
                                angle, a2)) {
265
                        ini1 = ps1[0];
266
                        ini2 = ps1[1];
267
                    } else {
268
                        ini1 = ps1[1];
269
                        ini2 = ps1[0];
270
                    }
271

    
272
                    Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
273
                    Point2D correct = new Point2D.Double(lastp.getX() +
274
                            unit.getX(), lastp.getY() + unit.getY());
275
                    Point2D[] ps = UtilFunctions.getPerpendicular(lastp,
276
                            correct, lastp);
277
                    Point2D mediop = new Point2D.Double((point.getX() +
278
                            lastp.getX()) / 2, (point.getY() + lastp.getY()) / 2);
279
                    Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp,
280
                            point, mediop);
281
                    Point2D interp = UtilFunctions.getIntersection(ps[0],
282
                            ps[1], ps2[0], ps2[1]);
283
                    antInter = interp;
284

    
285
                    double radio = interp.distance(lastp);
286

    
287
                    if (UtilFunctions.isLowAngle(correct, lastp, interp, point)) {
288
                        radio = -radio;
289
                    }
290

    
291
                    Point2D centerp = UtilFunctions.getPoint(interp, mediop,
292
                            radio);
293
                    antCenter = centerp;
294
                    list.add(ShapeFactory.createArc(lastp, centerp, point));
295
                }
296

    
297
                antantPoint = antPoint;
298
                antPoint = point;
299
            }
300
        }
301
    }
302

    
303
    /**
304
     * M?todo para dibujar la lo necesario para el estado en el que nos
305
     * encontremos.
306
     *
307
     * @param g Graphics sobre el que dibujar.
308
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
309
     * @param x par?metro x del punto que se pase para dibujar.
310
     * @param y par?metro x del punto que se pase para dibujar.
311
     */
312
    public void drawOperation(Graphics g, double x,
313
        double y) {
314
        PolylineCADToolState actualState = _fsm.getState();
315
        String status = actualState.getName();
316

    
317
        if (status.equals("Polyline.NextPointOrArcOrClose") || status.equals("Polyline.FirstPoint")) {
318
            for (int i = 0; i < list.size(); i++) {
319
                ((IGeometry) list.get(i)).cloneGeometry().draw((Graphics2D) g,
320
                    getCadToolAdapter().getMapControl().getViewPort(),
321
                    DefaultCADTool.drawingSymbol);
322
            }
323
            if (antPoint!=null)
324
                    drawLine((Graphics2D) g, antPoint, new Point2D.Double(x, y));
325

    
326
        } else if ((status.equals("Polyline.NextPointOrLineOrClose"))) {
327
            for (int i = 0; i < list.size(); i++) {
328
                ((IGeometry) list.get(i)).cloneGeometry().draw((Graphics2D) g,
329
                    getCadToolAdapter().getMapControl().getViewPort(),
330
                    DefaultCADTool.drawingSymbol);
331
            }
332

    
333
            Point2D point = new Point2D.Double(x, y);
334
            Point2D lastp = antPoint;
335

    
336
            if (!(list.size() > 0) ||
337
                    (((IGeometry) list.get(list.size() - 1)).getGeometryType() == FShape.LINE)) {
338
                if (antantPoint == null) {
339
                    drawArc(point, lastp,
340
                        new Point2D.Double(lastp.getX() + (point.getX() / 2),
341
                            lastp.getY() + (point.getY() / 2)), g);
342
                } else {
343
                    drawArc(point, lastp, antantPoint, g);
344
                }
345
            } else {
346
                if (antInter != null) {
347
                    Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp,
348
                            antInter, lastp);
349
                    double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]);
350
                    double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]);
351
                    double angle = UtilFunctions.getAngle(antCenter, lastp);
352
                    Point2D ini1 = null;
353
                    Point2D ini2 = null;
354

    
355
                    if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions.absoluteAngleDistance(
356
                                angle, a2)) {
357
                        ini1 = ps1[0];
358
                        ini2 = ps1[1];
359
                    } else {
360
                        ini1 = ps1[1];
361
                        ini2 = ps1[0];
362
                    }
363

    
364
                    Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
365
                    Point2D correct = new Point2D.Double(lastp.getX() +
366
                            unit.getX(), lastp.getY() + unit.getY());
367
                    drawArc(point, lastp, correct, g);
368
                }
369
            }
370
        }
371
    }
372

    
373
    /**
374
     * Dibuja el arco sobre el graphics.
375
     *
376
     * @param point Puntero del rat?n.
377
     * @param lastp ?ltimo punto de la polilinea.
378
     * @param antp Punto antepenultimo.
379
     * @param g Graphics sobre el que se dibuja.
380
     */
381
    private void drawArc(Point2D point, Point2D lastp, Point2D antp, Graphics g) {
382
        if (((point.getY() == lastp.getY()) && (point.getX() < lastp.getX())) ||
383
                ((point.getX() == lastp.getX()) &&
384
                (point.getY() < lastp.getY()))) {
385
        } else {
386
            if (point.getX() == lastp.getX()) {
387
                point = new Point2D.Double(point.getX() + 0.00000001,
388
                        point.getY());
389
            } else if (point.getY() == lastp.getY()) {
390
                point = new Point2D.Double(point.getX(),
391
                        point.getY() + 0.00000001);
392
            }
393

    
394
            if (point.getX() == antp.getX()) {
395
                point = new Point2D.Double(point.getX() + 0.00000001,
396
                        point.getY());
397
            } else if (point.getY() == antp.getY()) {
398
                point = new Point2D.Double(point.getX(),
399
                        point.getY() + 0.00000001);
400
            }
401

    
402
            Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp, antp, lastp);
403
            Point2D mediop = new Point2D.Double((point.getX() + lastp.getX()) / 2,
404
                    (point.getY() + lastp.getY()) / 2);
405
            Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp, point, mediop);
406
            Point2D interp = UtilFunctions.getIntersection(ps1[0], ps1[1],
407
                    ps2[0], ps2[1]);
408

    
409
            double radio = interp.distance(lastp);
410

    
411
            if (UtilFunctions.isLowAngle(antp, lastp, interp, point)) {
412
                radio = -radio;
413
            }
414

    
415
            Point2D centerp = UtilFunctions.getPoint(interp, mediop, radio);
416

    
417
            drawLine((Graphics2D) g, lastp, point);
418

    
419
            IGeometry ig = ShapeFactory.createArc(lastp, centerp, point);
420

    
421
            if (ig != null) {
422
                ig.draw((Graphics2D) g,
423
                    getCadToolAdapter().getMapControl().getViewPort(),
424
                    DefaultCADTool.modifySymbol);
425
            }
426
        }
427
    }
428

    
429
    /**
430
     * Add a diferent option.
431
     *
432
     * @param sel DOCUMENT ME!
433
     * @param s Diferent option.
434
     */
435
    public void addOption(String s) {
436
            /*       PolylineCADToolState actualState = (PolylineCADToolState) _fsm.getPreviousState();
437
        String status = actualState.getName();
438

439
        if (status.equals("Polyline.NextPointOrArcOrClose")) {
440
            if (s.equals("A") || s.equals("a") || s.equals(PluginServices.getText(this,"inter_arc"))) {
441
                //Arco
442
            } else if (s.equals("C") || s.equals("c")) {
443
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
444
                elShape.moveTo(antPoint.getX(), antPoint.getY());
445
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
446
                list.add(ShapeFactory.createPolyline2D(elShape));
447
                //closeGeometry();
448
            }
449
        } else if (status.equals("Polyline.NextPointOrLineOrClose")) {
450
            if (s.equals("N") || s.equals("n") || s.equals(PluginServices.getText(this,"inter_line"))) {
451
                //L?nea
452
            } else if (s.equals("C") || s.equals("c")) {
453
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
454
                elShape.moveTo(antPoint.getX(), antPoint.getY());
455
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
456
                list.add(ShapeFactory.createPolyline2D(elShape));
457
                //closeGeometry();
458
            }
459
        }
460
  */
461
    }
462

    
463
    /* (non-Javadoc)
464
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
465
     */
466
    public void addValue(double d) {
467
    }
468

    
469
    public void cancel(){
470
        //endGeometry();
471
        list.clear();
472
        antantPoint=antCenter=antInter=antPoint=firstPoint=null;
473
    }
474

    
475
    public void end() {
476
        /* CADExtension.setCADTool("polyline");
477
        PluginServices.getMainFrame().setSelectedTool("POLYLINE"); */
478
    }
479

    
480
    public String getName() {
481
        return PluginServices.getText(this,"polyline_");
482
    }
483

    
484
    public String toString() {
485
        return "_polyline";
486
    }
487
    public boolean isApplicable(int shapeType) {
488
        switch (shapeType) {
489
        case FShape.POINT:
490
        case FShape.MULTIPOINT:
491
            return false;
492
        }
493
        return true;
494
    }
495
}