Statistics
| Revision:

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

History | View | Annotate | Download (13.8 KB)

1 3782 caballero
/* 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 3832 caballero
import java.awt.Graphics;
44
import java.awt.Graphics2D;
45 4313 fjp
import java.awt.event.InputEvent;
46 3832 caballero
import java.awt.geom.Point2D;
47
48 4584 caballero
import com.iver.andami.PluginServices;
49 4549 caballero
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
50 5170 caballero
import com.iver.cit.gvsig.fmap.core.FShape;
51 3782 caballero
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 3832 caballero
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
57 5170 caballero
import com.iver.cit.gvsig.gui.cad.exception.CommadException;
58 3782 caballero
import com.iver.cit.gvsig.gui.cad.tools.smc.PolygonCADToolContext;
59
import com.iver.cit.gvsig.gui.cad.tools.smc.PolygonCADToolContext.PolygonCADToolState;
60
61
62
/**
63
 * DOCUMENT ME!
64
 *
65
 * @author Vicente Caballero Navarro
66
 */
67
public class PolygonCADTool extends DefaultCADTool {
68
    private PolygonCADToolContext _fsm;
69
    private Point2D center;
70
    private int numLines = 5;
71
    private boolean isI = true;
72
73
    /**
74
     * Crea un nuevo PolygonCADTool.
75
     */
76
    public PolygonCADTool() {
77 3883 caballero
78 3782 caballero
    }
79
80
    /**
81
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
82
     * carga previa a la utilizaci?n de la herramienta.
83
     */
84
    public void init() {
85 3883 caballero
            _fsm = new PolygonCADToolContext(this);
86 3782 caballero
    }
87
88
    /* (non-Javadoc)
89
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
90
     */
91 4313 fjp
    public void transition(double x, double y, InputEvent event) {
92 4324 caballero
        _fsm.addPoint(x, y, event);
93 3782 caballero
    }
94
95
    /* (non-Javadoc)
96
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
97
     */
98 3832 caballero
    public void transition(double d) {
99 3883 caballero
        _fsm.addValue(d);
100 3782 caballero
    }
101
102
    /* (non-Javadoc)
103
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
104
     */
105 5170 caballero
    public void transition(String s) throws CommadException {
106 4892 caballero
            if (!super.changeCommand(s)){
107
                    _fsm.addOption(s);
108
            }
109 3782 caballero
    }
110
111
    /**
112
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
113
     * editableFeatureSource que ya estar? creado.
114
     *
115
     * @param sel Bitset con las geometr?as que est?n seleccionadas.
116
     * @param x par?metro x del punto que se pase en esta transici?n.
117
     * @param y par?metro y del punto que se pase en esta transici?n.
118
     */
119 4365 caballero
    public void addPoint(double x, double y,InputEvent event) {
120 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
121
        String status = actualState.getName();
122
123 3978 caballero
        if (status.equals("Polygon.NumberOrCenterPoint")) {
124 3782 caballero
            center = new Point2D.Double(x, y);
125 3978 caballero
        } else if (status.equals("Polygon.CenterPoint")) {
126 3782 caballero
            center = new Point2D.Double(x, y);
127 3978 caballero
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
128
                status.equals("Polygon.RadiusOrPoint")) {
129 3782 caballero
            Point2D point = new Point2D.Double(x, y);
130
            //Pol?gono a partir de la circunferencia.
131
            if (isI) {
132
                addGeometry(getIPolygon(point, point.distance(center)));
133
            } else {
134
                addGeometry(getCPolygon(point, point.distance(center)));
135
            }
136
        }
137
    }
138
139
    /**
140
     * M?todo para dibujar la lo necesario para el estado en el que nos
141
     * encontremos.
142
     *
143
     * @param g Graphics sobre el que dibujar.
144
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
145
     * @param x par?metro x del punto que se pase para dibujar.
146
     * @param y par?metro x del punto que se pase para dibujar.
147
     */
148 3832 caballero
    public void drawOperation(Graphics g, double x,
149 3782 caballero
        double y) {
150
        PolygonCADToolState actualState = _fsm.getState();
151
        String status = actualState.getName();
152
153 5170 caballero
        if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
154 3978 caballero
                status.equals("Polygon.RadiusOrPoint")) {
155 3782 caballero
            Point2D point = new Point2D.Double(x, y);
156
            drawLine((Graphics2D) g, center, point);
157
158
            if (isI) {
159
                getIPolygon(point, point.distance(center)).draw((Graphics2D) g,
160
                    getCadToolAdapter().getMapControl().getViewPort(),
161 3997 fjp
                    CADTool.drawingSymbol);
162 3782 caballero
            } else {
163
                getCPolygon(point, point.distance(center)).draw((Graphics2D) g,
164
                    getCadToolAdapter().getMapControl().getViewPort(),
165 3997 fjp
                    CADTool.drawingSymbol);
166 3782 caballero
            }
167
168
            ShapeFactory.createCircle(center, point.distance(center)).draw((Graphics2D) g,
169
                getCadToolAdapter().getMapControl().getViewPort(),
170 3997 fjp
                CADTool.modifySymbol);
171 3782 caballero
        }
172
    }
173
174
    /**
175
     * Add a diferent option.
176
     *
177
     * @param sel DOCUMENT ME!
178
     * @param s Diferent option.
179
     */
180 3832 caballero
    public void addOption(String s) {
181 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
182
        String status = actualState.getName();
183
184 3978 caballero
        if (status.equals("Polygon.OptionOrRadiusOrPoint")) {
185 3883 caballero
            if (s.equals("C") || s.equals("c") || s.equals("Circunscrito")) {
186 3782 caballero
                isI = false;
187 3883 caballero
            } else if (s.equals("I") || s.equals("i") || s.equals("Inscrito")) {
188 3782 caballero
                isI = true;
189
            }
190
        }
191
    }
192
193
    /* (non-Javadoc)
194
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
195
     */
196 3832 caballero
    public void addValue(double d) {
197 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
198
        String status = actualState.getName();
199
200 3978 caballero
        if (status.equals("Polygon.NumberOrCenterPoint")) {
201 3782 caballero
            numLines = (int) d;
202 3978 caballero
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
203
                status.equals("Polygon.RadiusOrPoint")) {
204 3782 caballero
            double radio = d;
205
206
            //Pol?gono a partir de radio.
207
            Point2D point = UtilFunctions.getPoint(center,
208
                    new Point2D.Double(center.getX(), center.getY() + 10), radio);
209
210
            if (isI) {
211
                addGeometry(getIPolygon(point, radio));
212
            } else {
213
                addGeometry(getCPolygon(point, radio));
214
            }
215
        }
216
    }
217
218
    /**
219
     * Devuelve la geometr?a con el poligono regular circunscrito a la
220
     * circunferencia formada por el punto central y el radio que se froma
221
     * entre este y el puntero del rat?n..
222
     *
223
     * @param point Puntero del rat?n.
224
     * @param radio Radio
225
     *
226
     * @return GeometryCollection con las geometr?as del pol?gono.
227
     */
228
    private IGeometry getCPolygon(Point2D point, double radio) {
229 4522 caballero
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
230
231
        double initangle = UtilFunctions.getAngle(center, point);
232
        Point2D antPoint = p1;
233
        Point2D antInter = null;
234 4549 caballero
        //Point2D firstPoint= null;
235 4522 caballero
        double an = (Math.PI * 2) / numLines;
236
        GeneralPathX elShape = new GeneralPathX();
237
        boolean firstTime=true;
238 5170 caballero
        for (int i = numLines-1; i >=0 ; i--) {
239 4522 caballero
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
240
                    radio);
241
            Point2D[] ps1 = UtilFunctions.getPerpendicular(antPoint, center,
242
                    antPoint);
243
            Point2D[] ps2 = UtilFunctions.getPerpendicular(p2, center, p2);
244
            Point2D inter = UtilFunctions.getIntersection(ps1[0], ps1[1],
245
                    ps2[0], ps2[1]);
246
247
            if (antInter != null) {
248
249
                if (firstTime){
250
                        elShape.moveTo(antInter.getX(), antInter.getY());
251 4549 caballero
                        //firstPoint=new Point2D.Double(antInter.getX(), antInter.getY());
252 4522 caballero
                        firstTime=false;
253
                }
254
                elShape.lineTo(inter.getX(), inter.getY());
255
256
            }
257
258
            antInter = inter;
259
            antPoint = p2;
260
        }
261 4549 caballero
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
262
        elShape.closePath();
263
        return ShapeFactory.createGeometry(new FPolyline2D(elShape));
264 4522 caballero
    }
265
266
    /**
267
     * Devuelve la geometr?a con el poligono regular inscrito en la
268
     * circunferencia.
269
     *
270
     * @param point Puntero del rat?n.
271
     * @param radio Radio
272
     *
273
     * @return GeometryCollection con las geometr?as del pol?gono.
274
     */
275
    private IGeometry getIPolygon(Point2D point, double radio) {
276
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
277
        double initangle = UtilFunctions.getAngle(center, point);
278
        Point2D antPoint = p1;
279 4549 caballero
        //Point2D firstPoint= null;
280 4522 caballero
        double an = (Math.PI * 2) / numLines;
281
        GeneralPathX elShape = new GeneralPathX();
282
        boolean firstTime=true;
283 4799 fjp
        for (int i = numLines-1; i >= 0; i--) {
284 4522 caballero
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
285
                    radio);
286
287
            if (firstTime){
288
                     elShape.moveTo(antPoint.getX(), antPoint.getY());
289 4549 caballero
                     //firstPoint=new Point2D.Double(antPoint.getX(), antPoint.getY());
290 4522 caballero
                     firstTime=false;
291
            }
292
293
            elShape.lineTo(p2.getX(), p2.getY());
294
295
            antPoint = p2;
296
        }
297 4549 caballero
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
298
        elShape.closePath();
299
        return ShapeFactory.createGeometry(new FPolyline2D(elShape));
300 4522 caballero
    }
301
    /**
302
     * Devuelve la geometr?a con el poligono regular circunscrito a la
303
     * circunferencia formada por el punto central y el radio que se froma
304
     * entre este y el puntero del rat?n..
305
     *
306
     * @param point Puntero del rat?n.
307
     * @param radio Radio
308
     *
309
     * @return GeometryCollection con las geometr?as del pol?gono.
310
     */
311
   /* private IGeometry getCPolygonOld(Point2D point, double radio) {
312 3782 caballero
        IGeometry[] geoms = new IGeometry[numLines];
313
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
314

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

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

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

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

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

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

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

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

373
        return new FGeometryCollection(geoms);
374
    }
375 4522 caballero
*/
376 4118 caballero
        public String getName() {
377 4584 caballero
                return PluginServices.getText(this,"polygon_");
378 4118 caballero
        }
379
380 5170 caballero
        /**
381
         * Devuelve la cadena que corresponde al estado en el que nos encontramos.
382
         *
383
         * @return Cadena para mostrar por consola.
384
         */
385
        public String getQuestion() {
386
                PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getState();
387
        String status = actualState.getName();
388
389
        if (status.equals("Polygon.NumberOrCenterPoint")) {
390
                return super.getQuestion()+"<"+numLines+">";
391
        }
392
        else
393
                return super.getQuestion();
394
395
        }
396 4892 caballero
        public String toString() {
397
                return "_polygon";
398
        }
399 5170 caballero
        public boolean isApplicable(int shapeType) {
400
                switch (shapeType) {
401
                case FShape.POINT:
402
                        return false;
403
                }
404
                return true;
405
        }
406 3782 caballero
}