Statistics
| Revision:

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

History | View | Annotate | Download (19.2 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
             try {
100
                         if (getVLE().getVEA().getShapeType()==FShape.POLYGON && !close){
101
                                 closeGeometry();
102
                         }
103
                 } catch (DriverIOException e) {
104
                         e.printStackTrace();
105
                 }
106
            IGeometry[] geoms = (IGeometry[]) list.toArray(new IGeometry[0]);
107
        FGeometryCollection fgc = new FGeometryCollection(geoms);
108

    
109

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

    
147
        list.add(ShapeFactory.createPolyline2D(elShape));
148

    
149

    
150
        // list.add(ShapeFactory.createPolyline2D(elShape));
151

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

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

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

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

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

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

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

    
209
            }
210
            if (antPoint==null)
211
                    antPoint = (Point2D)firstPoint.clone();
212

    
213
            if (antPoint != null) {
214
                antantPoint = antPoint;
215
            }
216

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

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

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

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

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

    
260
                    double radio = interp.distance(lastp);
261

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

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

    
271
                    IGeometry ig = ShapeFactory.createArc(lastp, centerp, point);
272

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

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

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

    
307
                    double radio = interp.distance(lastp);
308

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

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

    
319
                antantPoint = antPoint;
320
                antPoint = point;
321
            }
322
        }
323
    }
324

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

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

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

    
355
            Point2D point = new Point2D.Double(x, y);
356
            Point2D lastp = antPoint;
357

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

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

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

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

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

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

    
431
            double radio = interp.distance(lastp);
432

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

    
437
            Point2D centerp = UtilFunctions.getPoint(interp, mediop, radio);
438

    
439
            drawLine((Graphics2D) g, lastp, point);
440

    
441
            IGeometry ig = ShapeFactory.createArc(lastp, centerp, point);
442

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

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

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

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

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

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

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

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