Statistics
| Revision:

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

History | View | Annotate | Download (14 KB)

1
/* 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
import java.awt.Graphics;
44
import java.awt.Graphics2D;
45
import java.awt.event.InputEvent;
46
import java.awt.geom.Point2D;
47

    
48
import com.iver.andami.PluginServices;
49
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
50
import com.iver.cit.gvsig.fmap.core.FShape;
51
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.DefaultCADTool;
56
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
57
import com.iver.cit.gvsig.gui.cad.exception.ValueException;
58
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

    
78
    }
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
            _fsm = new PolygonCADToolContext(this);
86
    }
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
    public void transition(double x, double y, InputEvent event) {
92
        _fsm.addPoint(x, y, event);
93
    }
94

    
95
    /* (non-Javadoc)
96
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
97
     */
98
    public void transition(double d) {
99
        _fsm.addValue(d);
100
    }
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
    public void transition(String s) throws CommandException {
106
            if (!super.changeCommand(s)){
107
                    _fsm.addOption(s);
108
            }
109
    }
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
    public void addPoint(double x, double y,InputEvent event) {
120
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
121
        String status = actualState.getName();
122

    
123
        if (status.equals("Polygon.NumberOrCenterPoint")) {
124
            center = new Point2D.Double(x, y);
125
        } else if (status.equals("Polygon.CenterPoint")) {
126
            center = new Point2D.Double(x, y);
127
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
128
                status.equals("Polygon.RadiusOrPoint")) {
129
            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
    public void drawOperation(Graphics g, double x,
149
        double y) {
150
        PolygonCADToolState actualState = _fsm.getState();
151
        String status = actualState.getName();
152

    
153
        if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
154
                status.equals("Polygon.RadiusOrPoint")) {
155
            Point2D point = new Point2D.Double(x, y);
156
            drawLine((Graphics2D) g, center, point,DefaultCADTool.geometrySelectSymbol);
157

    
158
            if (isI) {
159
                getIPolygon(point, point.distance(center)).draw((Graphics2D) g,
160
                    getCadToolAdapter().getMapControl().getViewPort(),
161
                    DefaultCADTool.geometrySelectSymbol);
162
            } else {
163
                getCPolygon(point, point.distance(center)).draw((Graphics2D) g,
164
                    getCadToolAdapter().getMapControl().getViewPort(),
165
                    DefaultCADTool.geometrySelectSymbol);
166
            }
167

    
168
            ShapeFactory.createCircle(center, point.distance(center)).draw((Graphics2D) g,
169
                getCadToolAdapter().getMapControl().getViewPort(),
170
                DefaultCADTool.axisReferencesSymbol);
171
        }
172
    }
173

    
174
    /**
175
     * Add a diferent option.
176
     *
177
     * @param sel DOCUMENT ME!
178
     * @param s Diferent option.
179
     */
180
    public void addOption(String s) {
181
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
182
        String status = actualState.getName();
183

    
184
        if (status.equals("Polygon.OptionOrRadiusOrPoint")) {
185
            if (s.equals("C") || s.equals("c") || s.equals(PluginServices.getText(this,"circumscribed"))) {
186
                isI = false;
187
            } else if (s.equals("I") || s.equals("i") || s.equals(PluginServices.getText(this,"into_circle"))) {
188
                isI = true;
189
            }
190
        }
191
    }
192

    
193
    /* (non-Javadoc)
194
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
195
     */
196
    public void addValue(double d) {
197
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
198
        String status = actualState.getName();
199

    
200
        if (status.equals("Polygon.NumberOrCenterPoint")) {
201
                numLines = (int) d;
202
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
203
                status.equals("Polygon.RadiusOrPoint")) {
204
            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
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
230

    
231
        double initangle = UtilFunctions.getAngle(center, point);
232
        Point2D antPoint = p1;
233
        Point2D antInter = null;
234
        //Point2D firstPoint= null;
235
        double an = (Math.PI * 2) / numLines;
236
        GeneralPathX elShape = new GeneralPathX();
237
        boolean firstTime=true;
238
        for (int i = numLines-1; i >=0 ; i--) {
239
            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
                        //firstPoint=new Point2D.Double(antInter.getX(), antInter.getY());
252
                        firstTime=false;
253
                }
254
                elShape.lineTo(inter.getX(), inter.getY());
255

    
256
            }
257

    
258
            antInter = inter;
259
            antPoint = p2;
260
        }
261
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
262
        elShape.closePath();
263
        return ShapeFactory.createGeometry(new FPolyline2D(elShape));
264
    }
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
     * @throws ValueException
275
     */
276
    private IGeometry getIPolygon(Point2D point, double radio){
277
            Point2D p1 = UtilFunctions.getPoint(center, point, radio);
278
        double initangle = UtilFunctions.getAngle(center, point);
279
        Point2D antPoint = p1;
280
        //Point2D firstPoint= null;
281
        double an = (Math.PI * 2) / numLines;
282
        GeneralPathX elShape = new GeneralPathX();
283
        boolean firstTime=true;
284

    
285
        for (int i = numLines-1; i >= 0; i--) {
286
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
287
                    radio);
288

    
289
            if (firstTime){
290
                     elShape.moveTo(antPoint.getX(), antPoint.getY());
291
                     //firstPoint=new Point2D.Double(antPoint.getX(), antPoint.getY());
292
                     firstTime=false;
293
            }
294

    
295
            elShape.lineTo(p2.getX(), p2.getY());
296

    
297
            antPoint = p2;
298
        }
299
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
300
        elShape.closePath();
301
        return ShapeFactory.createGeometry(new FPolyline2D(elShape));
302
    }
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
        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
*/
347
    /**
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
  /*  private IGeometry getIPolygonOld(Point2D point, double radio) {
357
        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
*/
378
        public String getName() {
379
                return PluginServices.getText(this,"polygon_");
380
        }
381

    
382
        /**
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
        public String toString() {
399
                return "_polygon";
400
        }
401
        public boolean isApplicable(int shapeType) {
402
                switch (shapeType) {
403
                case FShape.POINT:
404
                case FShape.MULTIPOINT:
405
                        return false;
406
                }
407
                return true;
408
        }
409
}