Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_1_RELEASE / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / PolygonCADTool.java @ 9531

History | View | Annotate | Download (13.9 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.FShape;
51
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
52
import com.iver.cit.gvsig.fmap.core.IGeometry;
53
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
54
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
55
import com.iver.cit.gvsig.gui.cad.CADTool;
56
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
57
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
58
import com.iver.cit.gvsig.gui.cad.exception.ValueException;
59
import com.iver.cit.gvsig.gui.cad.tools.smc.PolygonCADToolContext;
60
import com.iver.cit.gvsig.gui.cad.tools.smc.PolygonCADToolContext.PolygonCADToolState;
61

    
62

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

    
74
    /**
75
     * Crea un nuevo PolygonCADTool.
76
     */
77
    public PolygonCADTool() {
78

    
79
    }
80

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

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

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

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

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

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

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

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

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

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

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

    
185
        if (status.equals("Polygon.OptionOrRadiusOrPoint")) {
186
            if (s.equals("C") || s.equals("c") || s.equals(PluginServices.getText(this,"circumscribed"))) {
187
                isI = false;
188
            } else if (s.equals("I") || s.equals("i") || s.equals(PluginServices.getText(this,"into_circle"))) {
189
                isI = true;
190
            }
191
        }
192
    }
193

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

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

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

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

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

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

    
248
            if (antInter != null) {
249

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

    
257
            }
258

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

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

    
286
        for (int i = numLines-1; i >= 0; i--) {
287
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
288
                    radio);
289

    
290
            if (firstTime){
291
                     elShape.moveTo(antPoint.getX(), antPoint.getY());
292
                     //firstPoint=new Point2D.Double(antPoint.getX(), antPoint.getY());
293
                     firstTime=false;
294
            }
295

    
296
            elShape.lineTo(p2.getX(), p2.getY());
297

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

318
        double initangle = UtilFunctions.getAngle(center, point);
319
        Point2D antPoint = p1;
320
        Point2D antInter = null;
321
        double an = (Math.PI * 2) / numLines;
322

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

332
            if (antInter != null) {
333
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
334
                        2);
335
                elShape.moveTo(antInter.getX(), antInter.getY());
336
                elShape.lineTo(inter.getX(), inter.getY());
337

338
                geoms[i - 2] = (ShapeFactory.createPolyline2D(elShape));
339
            }
340

341
            antInter = inter;
342
            antPoint = p2;
343
        }
344

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

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

372
            geoms[i - 1] = (ShapeFactory.createPolyline2D(elShape));
373
            antPoint = p2;
374
        }
375

376
        return new FGeometryCollection(geoms);
377
    }
378
*/
379
        public String getName() {
380
                return PluginServices.getText(this,"polygon_");
381
        }
382

    
383
        /**
384
         * Devuelve la cadena que corresponde al estado en el que nos encontramos.
385
         *
386
         * @return Cadena para mostrar por consola.
387
         */
388
        public String getQuestion() {
389
                PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getState();
390
        String status = actualState.getName();
391

    
392
        if (status.equals("Polygon.NumberOrCenterPoint")) {
393
                return super.getQuestion()+"<"+numLines+">";
394
        }
395
        else
396
                return super.getQuestion();
397

    
398
        }
399
        public String toString() {
400
                return "_polygon";
401
        }
402
        public boolean isApplicable(int shapeType) {
403
                switch (shapeType) {
404
                case FShape.POINT:
405
                case FShape.MULTIPOINT:
406
                        return false;
407
                }
408
                return true;
409
        }
410
}