Statistics
| Revision:

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

History | View | Annotate | Download (14.4 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 12159 caballero
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
50 4549 caballero
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
51 5170 caballero
import com.iver.cit.gvsig.fmap.core.FShape;
52 3782 caballero
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
53
import com.iver.cit.gvsig.fmap.core.IGeometry;
54
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
55
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
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 11436 caballero
            drawLine((Graphics2D) g, center, point,DefaultCADTool.geometrySelectSymbol);
158 3782 caballero
159
            if (isI) {
160
                getIPolygon(point, point.distance(center)).draw((Graphics2D) g,
161
                    getCadToolAdapter().getMapControl().getViewPort(),
162 11436 caballero
                    DefaultCADTool.geometrySelectSymbol);
163 3782 caballero
            } else {
164
                getCPolygon(point, point.distance(center)).draw((Graphics2D) g,
165
                    getCadToolAdapter().getMapControl().getViewPort(),
166 11436 caballero
                    DefaultCADTool.geometrySelectSymbol);
167 3782 caballero
            }
168
169
            ShapeFactory.createCircle(center, point.distance(center)).draw((Graphics2D) g,
170
                getCadToolAdapter().getMapControl().getViewPort(),
171 11436 caballero
                DefaultCADTool.axisReferencesSymbol);
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 12159 caballero
        int type=getCadToolAdapter().getActiveLayerType();
265
        FShape shape=null;
266
        if (type==FShape.POLYGON){
267
                shape=new FPolygon2D(elShape);
268
        }else{
269
                shape=new FPolyline2D(elShape);
270
        }
271
        return ShapeFactory.createGeometry(shape);
272 4522 caballero
    }
273
274
    /**
275
     * Devuelve la geometr?a con el poligono regular inscrito en la
276
     * circunferencia.
277
     *
278
     * @param point Puntero del rat?n.
279
     * @param radio Radio
280
     *
281
     * @return GeometryCollection con las geometr?as del pol?gono.
282 5735 caballero
     * @throws ValueException
283 4522 caballero
     */
284 5735 caballero
    private IGeometry getIPolygon(Point2D point, double radio){
285
            Point2D p1 = UtilFunctions.getPoint(center, point, radio);
286 4522 caballero
        double initangle = UtilFunctions.getAngle(center, point);
287
        Point2D antPoint = p1;
288 4549 caballero
        //Point2D firstPoint= null;
289 4522 caballero
        double an = (Math.PI * 2) / numLines;
290
        GeneralPathX elShape = new GeneralPathX();
291
        boolean firstTime=true;
292 5735 caballero
293 4799 fjp
        for (int i = numLines-1; i >= 0; i--) {
294 4522 caballero
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
295
                    radio);
296
297
            if (firstTime){
298
                     elShape.moveTo(antPoint.getX(), antPoint.getY());
299 4549 caballero
                     //firstPoint=new Point2D.Double(antPoint.getX(), antPoint.getY());
300 4522 caballero
                     firstTime=false;
301
            }
302
303
            elShape.lineTo(p2.getX(), p2.getY());
304
305
            antPoint = p2;
306
        }
307 4549 caballero
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
308
        elShape.closePath();
309 12159 caballero
        int type=getCadToolAdapter().getActiveLayerType();
310
        FShape shape=null;
311
        if (type==FShape.POLYGON){
312
                shape=new FPolygon2D(elShape);
313
        }else{
314
                shape=new FPolyline2D(elShape);
315
        }
316
        return ShapeFactory.createGeometry(shape);
317 4522 caballero
    }
318
    /**
319
     * Devuelve la geometr?a con el poligono regular circunscrito a la
320
     * circunferencia formada por el punto central y el radio que se froma
321
     * entre este y el puntero del rat?n..
322
     *
323
     * @param point Puntero del rat?n.
324
     * @param radio Radio
325
     *
326
     * @return GeometryCollection con las geometr?as del pol?gono.
327
     */
328
   /* private IGeometry getCPolygonOld(Point2D point, double radio) {
329 3782 caballero
        IGeometry[] geoms = new IGeometry[numLines];
330
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
331

332
        double initangle = UtilFunctions.getAngle(center, point);
333
        Point2D antPoint = p1;
334
        Point2D antInter = null;
335
        double an = (Math.PI * 2) / numLines;
336

337
        for (int i = 1; i < (numLines + 2); i++) {
338
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
339
                    radio);
340
            Point2D[] ps1 = UtilFunctions.getPerpendicular(antPoint, center,
341
                    antPoint);
342
            Point2D[] ps2 = UtilFunctions.getPerpendicular(p2, center, p2);
343
            Point2D inter = UtilFunctions.getIntersection(ps1[0], ps1[1],
344
                    ps2[0], ps2[1]);
345

346
            if (antInter != null) {
347
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
348
                        2);
349
                elShape.moveTo(antInter.getX(), antInter.getY());
350
                elShape.lineTo(inter.getX(), inter.getY());
351

352
                geoms[i - 2] = (ShapeFactory.createPolyline2D(elShape));
353
            }
354

355
            antInter = inter;
356
            antPoint = p2;
357
        }
358

359
        return new FGeometryCollection(geoms);
360
    }
361 4522 caballero
*/
362 3782 caballero
    /**
363
     * Devuelve la geometr?a con el poligono regular inscrito en la
364
     * circunferencia.
365
     *
366
     * @param point Puntero del rat?n.
367
     * @param radio Radio
368
     *
369
     * @return GeometryCollection con las geometr?as del pol?gono.
370
     */
371 4522 caballero
  /*  private IGeometry getIPolygonOld(Point2D point, double radio) {
372 3782 caballero
        IGeometry[] geoms = new IGeometry[numLines];
373
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
374
        double initangle = UtilFunctions.getAngle(center, point);
375
        Point2D antPoint = p1;
376
        double an = (Math.PI * 2) / numLines;
377

378
        for (int i = 1; i < (numLines + 1); i++) {
379
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
380
                    radio);
381
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
382
                    2);
383
            elShape.moveTo(antPoint.getX(), antPoint.getY());
384
            elShape.lineTo(p2.getX(), p2.getY());
385

386
            geoms[i - 1] = (ShapeFactory.createPolyline2D(elShape));
387
            antPoint = p2;
388
        }
389

390
        return new FGeometryCollection(geoms);
391
    }
392 4522 caballero
*/
393 4118 caballero
        public String getName() {
394 4584 caballero
                return PluginServices.getText(this,"polygon_");
395 4118 caballero
        }
396
397 5170 caballero
        /**
398
         * Devuelve la cadena que corresponde al estado en el que nos encontramos.
399
         *
400
         * @return Cadena para mostrar por consola.
401
         */
402
        public String getQuestion() {
403
                PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getState();
404
        String status = actualState.getName();
405
406
        if (status.equals("Polygon.NumberOrCenterPoint")) {
407
                return super.getQuestion()+"<"+numLines+">";
408
        }
409
        else
410
                return super.getQuestion();
411
412
        }
413 4892 caballero
        public String toString() {
414
                return "_polygon";
415
        }
416 5170 caballero
        public boolean isApplicable(int shapeType) {
417
                switch (shapeType) {
418
                case FShape.POINT:
419 6024 caballero
                case FShape.MULTIPOINT:
420 5170 caballero
                        return false;
421
                }
422
                return true;
423
        }
424 3782 caballero
}