Statistics
| Revision:

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

History | View | Annotate | Download (13.1 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 3782 caballero
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 3832 caballero
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
56 3782 caballero
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 3883 caballero
76 3782 caballero
    }
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 3883 caballero
            _fsm = new PolygonCADToolContext(this);
84 3782 caballero
    }
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 4313 fjp
    public void transition(double x, double y, InputEvent event) {
90 4324 caballero
        _fsm.addPoint(x, y, event);
91 3782 caballero
    }
92
93
    /* (non-Javadoc)
94
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
95
     */
96 3832 caballero
    public void transition(double d) {
97 3883 caballero
        _fsm.addValue(d);
98 3782 caballero
    }
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 3832 caballero
    public void transition(String s) {
104 4892 caballero
            if (!super.changeCommand(s)){
105
                    _fsm.addOption(s);
106
            }
107 3782 caballero
    }
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 4365 caballero
    public void addPoint(double x, double y,InputEvent event) {
118 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
119
        String status = actualState.getName();
120
121 3978 caballero
        if (status.equals("Polygon.NumberOrCenterPoint")) {
122 3782 caballero
            center = new Point2D.Double(x, y);
123 3978 caballero
        } else if (status.equals("Polygon.CenterPoint")) {
124 3782 caballero
            center = new Point2D.Double(x, y);
125 3978 caballero
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
126
                status.equals("Polygon.RadiusOrPoint")) {
127 3782 caballero
            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 3832 caballero
    public void drawOperation(Graphics g, double x,
147 3782 caballero
        double y) {
148
        PolygonCADToolState actualState = _fsm.getState();
149
        String status = actualState.getName();
150
151 3978 caballero
        if (status.equals("Polygon.CenterPoint") ||
152
                status.equals("Polygon.OptionOrRadiusOrPoint") ||
153
                status.equals("Polygon.RadiusOrPoint")) {
154 3782 caballero
            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 3997 fjp
                    CADTool.drawingSymbol);
161 3782 caballero
            } else {
162
                getCPolygon(point, point.distance(center)).draw((Graphics2D) g,
163
                    getCadToolAdapter().getMapControl().getViewPort(),
164 3997 fjp
                    CADTool.drawingSymbol);
165 3782 caballero
            }
166
167
            ShapeFactory.createCircle(center, point.distance(center)).draw((Graphics2D) g,
168
                getCadToolAdapter().getMapControl().getViewPort(),
169 3997 fjp
                CADTool.modifySymbol);
170 3782 caballero
        }
171
    }
172
173
    /**
174
     * Add a diferent option.
175
     *
176
     * @param sel DOCUMENT ME!
177
     * @param s Diferent option.
178
     */
179 3832 caballero
    public void addOption(String s) {
180 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
181
        String status = actualState.getName();
182
183 3978 caballero
        if (status.equals("Polygon.OptionOrRadiusOrPoint")) {
184 3883 caballero
            if (s.equals("C") || s.equals("c") || s.equals("Circunscrito")) {
185 3782 caballero
                isI = false;
186 3883 caballero
            } else if (s.equals("I") || s.equals("i") || s.equals("Inscrito")) {
187 3782 caballero
                isI = true;
188
            }
189
        }
190
    }
191
192
    /* (non-Javadoc)
193
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
194
     */
195 3832 caballero
    public void addValue(double d) {
196 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
197
        String status = actualState.getName();
198
199 3978 caballero
        if (status.equals("Polygon.NumberOrCenterPoint")) {
200 3782 caballero
            numLines = (int) d;
201 3978 caballero
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
202
                status.equals("Polygon.RadiusOrPoint")) {
203 3782 caballero
            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 4522 caballero
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
229
230
        double initangle = UtilFunctions.getAngle(center, point);
231
        Point2D antPoint = p1;
232
        Point2D antInter = null;
233 4549 caballero
        //Point2D firstPoint= null;
234 4522 caballero
        double an = (Math.PI * 2) / numLines;
235
        GeneralPathX elShape = new GeneralPathX();
236
        boolean firstTime=true;
237 4799 fjp
        for (int i = numLines; i >=1 ; i++) {
238 4522 caballero
            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 4549 caballero
                        //firstPoint=new Point2D.Double(antInter.getX(), antInter.getY());
251 4522 caballero
                        firstTime=false;
252
                }
253
                elShape.lineTo(inter.getX(), inter.getY());
254
255
            }
256
257
            antInter = inter;
258
            antPoint = p2;
259
        }
260 4549 caballero
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
261
        elShape.closePath();
262
        return ShapeFactory.createGeometry(new FPolyline2D(elShape));
263 4522 caballero
    }
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 4549 caballero
        //Point2D firstPoint= null;
279 4522 caballero
        double an = (Math.PI * 2) / numLines;
280
        GeneralPathX elShape = new GeneralPathX();
281
        boolean firstTime=true;
282 4799 fjp
        for (int i = numLines-1; i >= 0; i--) {
283 4522 caballero
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
284
                    radio);
285
286
            if (firstTime){
287
                     elShape.moveTo(antPoint.getX(), antPoint.getY());
288 4549 caballero
                     //firstPoint=new Point2D.Double(antPoint.getX(), antPoint.getY());
289 4522 caballero
                     firstTime=false;
290
            }
291
292
            elShape.lineTo(p2.getX(), p2.getY());
293
294
            antPoint = p2;
295
        }
296 4549 caballero
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
297
        elShape.closePath();
298
        return ShapeFactory.createGeometry(new FPolyline2D(elShape));
299 4522 caballero
    }
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 3782 caballero
        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 4522 caballero
*/
344 3782 caballero
    /**
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 4522 caballero
  /*  private IGeometry getIPolygonOld(Point2D point, double radio) {
354 3782 caballero
        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 4522 caballero
*/
375 4118 caballero
        public String getName() {
376 4584 caballero
                return PluginServices.getText(this,"polygon_");
377 4118 caballero
        }
378
379 4892 caballero
        public String toString() {
380
                return "_polygon";
381
        }
382
383 3782 caballero
}