Statistics
| Revision:

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

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.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

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

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

    
149
        list.add(ShapeFactory.createPolyline2D(elShape));
150

    
151

    
152
        // list.add(ShapeFactory.createPolyline2D(elShape));
153

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

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

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

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

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

    
199
           if (firstPoint == null) {
200
               firstPoint = new Point2D.Double(x, y);
201
           }
202
                Point2D point = new Point2D.Double(x, y);
203

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

    
211
            }
212
            if (antPoint==null)
213
                    antPoint = (Point2D)firstPoint.clone();
214

    
215
            if (antPoint != null) {
216
                antantPoint = antPoint;
217
            }
218

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

    
224
            if (antantPoint == null) {
225
                antantPoint = new Point2D.Double(lastp.getX() +
226
                        (point.getX() / 2), lastp.getY() + (point.getY() / 2));
227
            }
228

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

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

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

    
262
                    double radio = interp.distance(lastp);
263

    
264
                    if (UtilFunctions.isLowAngle(antantPoint, lastp, interp,
265
                                point)) {
266
                        radio = -radio;
267
                    }
268

    
269
                    Point2D centerp = UtilFunctions.getPoint(interp, mediop,
270
                            radio);
271
                    antCenter = centerp;
272

    
273
                    IGeometry ig = ShapeFactory.createArc(lastp, centerp, point);
274

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

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

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

    
309
                    double radio = interp.distance(lastp);
310

    
311
                    if (UtilFunctions.isLowAngle(correct, lastp, interp, point)) {
312
                        radio = -radio;
313
                    }
314

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

    
321
                antantPoint = antPoint;
322
                antPoint = point;
323
            }
324
        }
325
    }
326

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

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

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

    
357
            Point2D point = new Point2D.Double(x, y);
358
            Point2D lastp = antPoint;
359

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

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

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

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

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

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

    
433
            double radio = interp.distance(lastp);
434

    
435
            if (UtilFunctions.isLowAngle(antp, lastp, interp, point)) {
436
                radio = -radio;
437
            }
438

    
439
            Point2D centerp = UtilFunctions.getPoint(interp, mediop, radio);
440

    
441
            drawLine((Graphics2D) g, lastp, point);
442

    
443
            IGeometry ig = ShapeFactory.createArc(lastp, centerp, point);
444

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

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

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

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

    
493
    public void cancel(){
494
        //endGeometry();
495
        list.clear();
496
        antantPoint=antCenter=antInter=antPoint=firstPoint=null;
497
    }
498

    
499
    public void end() {
500
        /* CADExtension.setCADTool("polyline");
501
        PluginServices.getMainFrame().setSelectedTool("POLYLINE"); */
502
    }
503

    
504
    public String getName() {
505
        return PluginServices.getText(this,"polyline_");
506
    }
507

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