Statistics
| Revision:

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

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 3832 caballero
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
56 5735 caballero
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
57
import com.iver.cit.gvsig.gui.cad.exception.ValueException;
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 5735 caballero
    public void transition(String s) throws CommandException {
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 11427 caballero
                    DefaultCADTool.drawingSymbol);
162 3782 caballero
            } else {
163
                getCPolygon(point, point.distance(center)).draw((Graphics2D) g,
164
                    getCadToolAdapter().getMapControl().getViewPort(),
165 11427 caballero
                    DefaultCADTool.drawingSymbol);
166 3782 caballero
            }
167
168
            ShapeFactory.createCircle(center, point.distance(center)).draw((Graphics2D) g,
169
                getCadToolAdapter().getMapControl().getViewPort(),
170 11427 caballero
                DefaultCADTool.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 7554 caballero
            if (s.equals("C") || s.equals("c") || s.equals(PluginServices.getText(this,"circumscribed"))) {
186 3782 caballero
                isI = false;
187 7554 caballero
            } else if (s.equals("I") || s.equals("i") || s.equals(PluginServices.getText(this,"into_circle"))) {
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 5735 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 5880 fjp
        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 5735 caballero
     * @throws ValueException
275 4522 caballero
     */
276 5735 caballero
    private IGeometry getIPolygon(Point2D point, double radio){
277
            Point2D p1 = UtilFunctions.getPoint(center, point, radio);
278 4522 caballero
        double initangle = UtilFunctions.getAngle(center, point);
279
        Point2D antPoint = p1;
280 4549 caballero
        //Point2D firstPoint= null;
281 4522 caballero
        double an = (Math.PI * 2) / numLines;
282
        GeneralPathX elShape = new GeneralPathX();
283
        boolean firstTime=true;
284 5735 caballero
285 4799 fjp
        for (int i = numLines-1; i >= 0; i--) {
286 4522 caballero
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
287
                    radio);
288
289
            if (firstTime){
290
                     elShape.moveTo(antPoint.getX(), antPoint.getY());
291 4549 caballero
                     //firstPoint=new Point2D.Double(antPoint.getX(), antPoint.getY());
292 4522 caballero
                     firstTime=false;
293
            }
294
295
            elShape.lineTo(p2.getX(), p2.getY());
296
297
            antPoint = p2;
298
        }
299 4549 caballero
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
300
        elShape.closePath();
301 5880 fjp
        return ShapeFactory.createGeometry(new FPolyline2D(elShape));
302 4522 caballero
    }
303
    /**
304
     * Devuelve la geometr?a con el poligono regular circunscrito a la
305
     * circunferencia formada por el punto central y el radio que se froma
306
     * entre este y el puntero del rat?n..
307
     *
308
     * @param point Puntero del rat?n.
309
     * @param radio Radio
310
     *
311
     * @return GeometryCollection con las geometr?as del pol?gono.
312
     */
313
   /* private IGeometry getCPolygonOld(Point2D point, double radio) {
314 3782 caballero
        IGeometry[] geoms = new IGeometry[numLines];
315
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
316

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

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

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

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

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

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

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

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

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