Statistics
| Revision:

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

History | View | Annotate | Download (13.9 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 5735 caballero
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
58
import com.iver.cit.gvsig.gui.cad.exception.ValueException;
59 3782 caballero
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 3883 caballero
79 3782 caballero
    }
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 3883 caballero
            _fsm = new PolygonCADToolContext(this);
87 3782 caballero
    }
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 4313 fjp
    public void transition(double x, double y, InputEvent event) {
93 4324 caballero
        _fsm.addPoint(x, y, event);
94 3782 caballero
    }
95
96
    /* (non-Javadoc)
97
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
98
     */
99 3832 caballero
    public void transition(double d) {
100 3883 caballero
        _fsm.addValue(d);
101 3782 caballero
    }
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 5735 caballero
    public void transition(String s) throws CommandException {
107 4892 caballero
            if (!super.changeCommand(s)){
108
                    _fsm.addOption(s);
109
            }
110 3782 caballero
    }
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 4365 caballero
    public void addPoint(double x, double y,InputEvent event) {
121 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
122
        String status = actualState.getName();
123
124 3978 caballero
        if (status.equals("Polygon.NumberOrCenterPoint")) {
125 3782 caballero
            center = new Point2D.Double(x, y);
126 3978 caballero
        } else if (status.equals("Polygon.CenterPoint")) {
127 3782 caballero
            center = new Point2D.Double(x, y);
128 3978 caballero
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
129
                status.equals("Polygon.RadiusOrPoint")) {
130 3782 caballero
            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 3832 caballero
    public void drawOperation(Graphics g, double x,
150 3782 caballero
        double y) {
151
        PolygonCADToolState actualState = _fsm.getState();
152
        String status = actualState.getName();
153
154 5170 caballero
        if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
155 3978 caballero
                status.equals("Polygon.RadiusOrPoint")) {
156 3782 caballero
            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 3997 fjp
                    CADTool.drawingSymbol);
163 3782 caballero
            } else {
164
                getCPolygon(point, point.distance(center)).draw((Graphics2D) g,
165
                    getCadToolAdapter().getMapControl().getViewPort(),
166 3997 fjp
                    CADTool.drawingSymbol);
167 3782 caballero
            }
168
169
            ShapeFactory.createCircle(center, point.distance(center)).draw((Graphics2D) g,
170
                getCadToolAdapter().getMapControl().getViewPort(),
171 3997 fjp
                CADTool.modifySymbol);
172 3782 caballero
        }
173
    }
174
175
    /**
176
     * Add a diferent option.
177
     *
178
     * @param sel DOCUMENT ME!
179
     * @param s Diferent option.
180
     */
181 3832 caballero
    public void addOption(String s) {
182 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
183
        String status = actualState.getName();
184
185 3978 caballero
        if (status.equals("Polygon.OptionOrRadiusOrPoint")) {
186 7554 caballero
            if (s.equals("C") || s.equals("c") || s.equals(PluginServices.getText(this,"circumscribed"))) {
187 3782 caballero
                isI = false;
188 7554 caballero
            } else if (s.equals("I") || s.equals("i") || s.equals(PluginServices.getText(this,"into_circle"))) {
189 3782 caballero
                isI = true;
190
            }
191
        }
192
    }
193
194
    /* (non-Javadoc)
195
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
196
     */
197 3832 caballero
    public void addValue(double d) {
198 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
199
        String status = actualState.getName();
200
201 3978 caballero
        if (status.equals("Polygon.NumberOrCenterPoint")) {
202 5735 caballero
                numLines = (int) d;
203 3978 caballero
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
204
                status.equals("Polygon.RadiusOrPoint")) {
205 3782 caballero
            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 4522 caballero
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
231
232
        double initangle = UtilFunctions.getAngle(center, point);
233
        Point2D antPoint = p1;
234
        Point2D antInter = null;
235 4549 caballero
        //Point2D firstPoint= null;
236 4522 caballero
        double an = (Math.PI * 2) / numLines;
237
        GeneralPathX elShape = new GeneralPathX();
238
        boolean firstTime=true;
239 5170 caballero
        for (int i = numLines-1; i >=0 ; i--) {
240 4522 caballero
            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 4549 caballero
                        //firstPoint=new Point2D.Double(antInter.getX(), antInter.getY());
253 4522 caballero
                        firstTime=false;
254
                }
255
                elShape.lineTo(inter.getX(), inter.getY());
256
257
            }
258
259
            antInter = inter;
260
            antPoint = p2;
261
        }
262 4549 caballero
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
263
        elShape.closePath();
264 5880 fjp
        return ShapeFactory.createGeometry(new FPolyline2D(elShape));
265 4522 caballero
    }
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 5735 caballero
     * @throws ValueException
276 4522 caballero
     */
277 5735 caballero
    private IGeometry getIPolygon(Point2D point, double radio){
278
            Point2D p1 = UtilFunctions.getPoint(center, point, radio);
279 4522 caballero
        double initangle = UtilFunctions.getAngle(center, point);
280
        Point2D antPoint = p1;
281 4549 caballero
        //Point2D firstPoint= null;
282 4522 caballero
        double an = (Math.PI * 2) / numLines;
283
        GeneralPathX elShape = new GeneralPathX();
284
        boolean firstTime=true;
285 5735 caballero
286 4799 fjp
        for (int i = numLines-1; i >= 0; i--) {
287 4522 caballero
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
288
                    radio);
289
290
            if (firstTime){
291
                     elShape.moveTo(antPoint.getX(), antPoint.getY());
292 4549 caballero
                     //firstPoint=new Point2D.Double(antPoint.getX(), antPoint.getY());
293 4522 caballero
                     firstTime=false;
294
            }
295
296
            elShape.lineTo(p2.getX(), p2.getY());
297
298
            antPoint = p2;
299
        }
300 4549 caballero
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
301
        elShape.closePath();
302 5880 fjp
        return ShapeFactory.createGeometry(new FPolyline2D(elShape));
303 4522 caballero
    }
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 3782 caballero
        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 4522 caballero
*/
348 3782 caballero
    /**
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 4522 caballero
  /*  private IGeometry getIPolygonOld(Point2D point, double radio) {
358 3782 caballero
        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 4522 caballero
*/
379 4118 caballero
        public String getName() {
380 4584 caballero
                return PluginServices.getText(this,"polygon_");
381 4118 caballero
        }
382
383 5170 caballero
        /**
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 4892 caballero
        public String toString() {
400
                return "_polygon";
401
        }
402 5170 caballero
        public boolean isApplicable(int shapeType) {
403
                switch (shapeType) {
404
                case FShape.POINT:
405 6024 caballero
                case FShape.MULTIPOINT:
406 5170 caballero
                        return false;
407
                }
408
                return true;
409
        }
410 3782 caballero
}