Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / PolygonCADTool.java @ 4892

History | View | Annotate | Download (13.1 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

    
48
import com.iver.andami.PluginServices;
49
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
50
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
51
import com.iver.cit.gvsig.fmap.core.IGeometry;
52
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
53
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
54
import com.iver.cit.gvsig.gui.cad.CADTool;
55
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
56
import com.iver.cit.gvsig.gui.cad.tools.smc.PolygonCADToolContext;
57
import com.iver.cit.gvsig.gui.cad.tools.smc.PolygonCADToolContext.PolygonCADToolState;
58

    
59

    
60
/**
61
 * DOCUMENT ME!
62
 *
63
 * @author Vicente Caballero Navarro
64
 */
65
public class PolygonCADTool extends DefaultCADTool {
66
    private PolygonCADToolContext _fsm;
67
    private Point2D center;
68
    private int numLines = 5;
69
    private boolean isI = true;
70

    
71
    /**
72
     * Crea un nuevo PolygonCADTool.
73
     */
74
    public PolygonCADTool() {
75

    
76
    }
77

    
78
    /**
79
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
80
     * carga previa a la utilizaci?n de la herramienta.
81
     */
82
    public void init() {
83
            _fsm = new PolygonCADToolContext(this);
84
    }
85

    
86
    /* (non-Javadoc)
87
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
88
     */
89
    public void transition(double x, double y, InputEvent event) {
90
        _fsm.addPoint(x, y, event);
91
    }
92

    
93
    /* (non-Javadoc)
94
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
95
     */
96
    public void transition(double d) {
97
        _fsm.addValue(d);
98
    }
99

    
100
    /* (non-Javadoc)
101
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
102
     */
103
    public void transition(String s) {
104
            if (!super.changeCommand(s)){
105
                    _fsm.addOption(s);
106
            }
107
    }
108

    
109
    /**
110
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
111
     * editableFeatureSource que ya estar? creado.
112
     *
113
     * @param sel Bitset con las geometr?as que est?n seleccionadas.
114
     * @param x par?metro x del punto que se pase en esta transici?n.
115
     * @param y par?metro y del punto que se pase en esta transici?n.
116
     */
117
    public void addPoint(double x, double y,InputEvent event) {
118
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
119
        String status = actualState.getName();
120

    
121
        if (status.equals("Polygon.NumberOrCenterPoint")) {
122
            center = new Point2D.Double(x, y);
123
        } else if (status.equals("Polygon.CenterPoint")) {
124
            center = new Point2D.Double(x, y);
125
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
126
                status.equals("Polygon.RadiusOrPoint")) {
127
            Point2D point = new Point2D.Double(x, y);
128
            //Pol?gono a partir de la circunferencia.
129
            if (isI) {
130
                addGeometry(getIPolygon(point, point.distance(center)));
131
            } else {
132
                addGeometry(getCPolygon(point, point.distance(center)));
133
            }
134
        }
135
    }
136

    
137
    /**
138
     * M?todo para dibujar la lo necesario para el estado en el que nos
139
     * encontremos.
140
     *
141
     * @param g Graphics sobre el que dibujar.
142
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
143
     * @param x par?metro x del punto que se pase para dibujar.
144
     * @param y par?metro x del punto que se pase para dibujar.
145
     */
146
    public void drawOperation(Graphics g, double x,
147
        double y) {
148
        PolygonCADToolState actualState = _fsm.getState();
149
        String status = actualState.getName();
150

    
151
        if (status.equals("Polygon.CenterPoint") ||
152
                status.equals("Polygon.OptionOrRadiusOrPoint") ||
153
                status.equals("Polygon.RadiusOrPoint")) {
154
            Point2D point = new Point2D.Double(x, y);
155
            drawLine((Graphics2D) g, center, point);
156

    
157
            if (isI) {
158
                getIPolygon(point, point.distance(center)).draw((Graphics2D) g,
159
                    getCadToolAdapter().getMapControl().getViewPort(),
160
                    CADTool.drawingSymbol);
161
            } else {
162
                getCPolygon(point, point.distance(center)).draw((Graphics2D) g,
163
                    getCadToolAdapter().getMapControl().getViewPort(),
164
                    CADTool.drawingSymbol);
165
            }
166

    
167
            ShapeFactory.createCircle(center, point.distance(center)).draw((Graphics2D) g,
168
                getCadToolAdapter().getMapControl().getViewPort(),
169
                CADTool.modifySymbol);
170
        }
171
    }
172

    
173
    /**
174
     * Add a diferent option.
175
     *
176
     * @param sel DOCUMENT ME!
177
     * @param s Diferent option.
178
     */
179
    public void addOption(String s) {
180
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
181
        String status = actualState.getName();
182

    
183
        if (status.equals("Polygon.OptionOrRadiusOrPoint")) {
184
            if (s.equals("C") || s.equals("c") || s.equals("Circunscrito")) {
185
                isI = false;
186
            } else if (s.equals("I") || s.equals("i") || s.equals("Inscrito")) {
187
                isI = true;
188
            }
189
        }
190
    }
191

    
192
    /* (non-Javadoc)
193
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
194
     */
195
    public void addValue(double d) {
196
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
197
        String status = actualState.getName();
198

    
199
        if (status.equals("Polygon.NumberOrCenterPoint")) {
200
            numLines = (int) d;
201
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
202
                status.equals("Polygon.RadiusOrPoint")) {
203
            double radio = d;
204

    
205
            //Pol?gono a partir de radio.
206
            Point2D point = UtilFunctions.getPoint(center,
207
                    new Point2D.Double(center.getX(), center.getY() + 10), radio);
208

    
209
            if (isI) {
210
                addGeometry(getIPolygon(point, radio));
211
            } else {
212
                addGeometry(getCPolygon(point, radio));
213
            }
214
        }
215
    }
216

    
217
    /**
218
     * Devuelve la geometr?a con el poligono regular circunscrito a la
219
     * circunferencia formada por el punto central y el radio que se froma
220
     * entre este y el puntero del rat?n..
221
     *
222
     * @param point Puntero del rat?n.
223
     * @param radio Radio
224
     *
225
     * @return GeometryCollection con las geometr?as del pol?gono.
226
     */
227
    private IGeometry getCPolygon(Point2D point, double radio) {
228
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
229

    
230
        double initangle = UtilFunctions.getAngle(center, point);
231
        Point2D antPoint = p1;
232
        Point2D antInter = null;
233
        //Point2D firstPoint= null;
234
        double an = (Math.PI * 2) / numLines;
235
        GeneralPathX elShape = new GeneralPathX();
236
        boolean firstTime=true;
237
        for (int i = numLines; i >=1 ; i++) {
238
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
239
                    radio);
240
            Point2D[] ps1 = UtilFunctions.getPerpendicular(antPoint, center,
241
                    antPoint);
242
            Point2D[] ps2 = UtilFunctions.getPerpendicular(p2, center, p2);
243
            Point2D inter = UtilFunctions.getIntersection(ps1[0], ps1[1],
244
                    ps2[0], ps2[1]);
245

    
246
            if (antInter != null) {
247

    
248
                if (firstTime){
249
                        elShape.moveTo(antInter.getX(), antInter.getY());
250
                        //firstPoint=new Point2D.Double(antInter.getX(), antInter.getY());
251
                        firstTime=false;
252
                }
253
                elShape.lineTo(inter.getX(), inter.getY());
254

    
255
            }
256

    
257
            antInter = inter;
258
            antPoint = p2;
259
        }
260
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
261
        elShape.closePath();
262
        return ShapeFactory.createGeometry(new FPolyline2D(elShape));
263
    }
264

    
265
    /**
266
     * Devuelve la geometr?a con el poligono regular inscrito en la
267
     * circunferencia.
268
     *
269
     * @param point Puntero del rat?n.
270
     * @param radio Radio
271
     *
272
     * @return GeometryCollection con las geometr?as del pol?gono.
273
     */
274
    private IGeometry getIPolygon(Point2D point, double radio) {
275
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
276
        double initangle = UtilFunctions.getAngle(center, point);
277
        Point2D antPoint = p1;
278
        //Point2D firstPoint= null;
279
        double an = (Math.PI * 2) / numLines;
280
        GeneralPathX elShape = new GeneralPathX();
281
        boolean firstTime=true;
282
        for (int i = numLines-1; i >= 0; i--) {
283
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
284
                    radio);
285

    
286
            if (firstTime){
287
                     elShape.moveTo(antPoint.getX(), antPoint.getY());
288
                     //firstPoint=new Point2D.Double(antPoint.getX(), antPoint.getY());
289
                     firstTime=false;
290
            }
291

    
292
            elShape.lineTo(p2.getX(), p2.getY());
293

    
294
            antPoint = p2;
295
        }
296
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
297
        elShape.closePath();
298
        return ShapeFactory.createGeometry(new FPolyline2D(elShape));
299
    }
300
    /**
301
     * Devuelve la geometr?a con el poligono regular circunscrito a la
302
     * circunferencia formada por el punto central y el radio que se froma
303
     * entre este y el puntero del rat?n..
304
     *
305
     * @param point Puntero del rat?n.
306
     * @param radio Radio
307
     *
308
     * @return GeometryCollection con las geometr?as del pol?gono.
309
     */
310
   /* private IGeometry getCPolygonOld(Point2D point, double radio) {
311
        IGeometry[] geoms = new IGeometry[numLines];
312
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
313

314
        double initangle = UtilFunctions.getAngle(center, point);
315
        Point2D antPoint = p1;
316
        Point2D antInter = null;
317
        double an = (Math.PI * 2) / numLines;
318

319
        for (int i = 1; i < (numLines + 2); i++) {
320
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
321
                    radio);
322
            Point2D[] ps1 = UtilFunctions.getPerpendicular(antPoint, center,
323
                    antPoint);
324
            Point2D[] ps2 = UtilFunctions.getPerpendicular(p2, center, p2);
325
            Point2D inter = UtilFunctions.getIntersection(ps1[0], ps1[1],
326
                    ps2[0], ps2[1]);
327

328
            if (antInter != null) {
329
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
330
                        2);
331
                elShape.moveTo(antInter.getX(), antInter.getY());
332
                elShape.lineTo(inter.getX(), inter.getY());
333

334
                geoms[i - 2] = (ShapeFactory.createPolyline2D(elShape));
335
            }
336

337
            antInter = inter;
338
            antPoint = p2;
339
        }
340

341
        return new FGeometryCollection(geoms);
342
    }
343
*/
344
    /**
345
     * Devuelve la geometr?a con el poligono regular inscrito en la
346
     * circunferencia.
347
     *
348
     * @param point Puntero del rat?n.
349
     * @param radio Radio
350
     *
351
     * @return GeometryCollection con las geometr?as del pol?gono.
352
     */
353
  /*  private IGeometry getIPolygonOld(Point2D point, double radio) {
354
        IGeometry[] geoms = new IGeometry[numLines];
355
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
356
        double initangle = UtilFunctions.getAngle(center, point);
357
        Point2D antPoint = p1;
358
        double an = (Math.PI * 2) / numLines;
359

360
        for (int i = 1; i < (numLines + 1); i++) {
361
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
362
                    radio);
363
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
364
                    2);
365
            elShape.moveTo(antPoint.getX(), antPoint.getY());
366
            elShape.lineTo(p2.getX(), p2.getY());
367

368
            geoms[i - 1] = (ShapeFactory.createPolyline2D(elShape));
369
            antPoint = p2;
370
        }
371

372
        return new FGeometryCollection(geoms);
373
    }
374
*/
375
        public String getName() {
376
                return PluginServices.getText(this,"polygon_");
377
        }
378

    
379
        public String toString() {
380
                return "_polygon";
381
        }
382

    
383
}