Statistics
| Revision:

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

History | View | Annotate | Download (9.83 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.geom.Point2D;
46

    
47
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
48
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
49
import com.iver.cit.gvsig.fmap.core.IGeometry;
50
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
51
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
52
import com.iver.cit.gvsig.gui.cad.CADTool;
53
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
54
import com.iver.cit.gvsig.gui.cad.tools.smc.PolygonCADToolContext;
55
import com.iver.cit.gvsig.gui.cad.tools.smc.PolygonCADToolContext.PolygonCADToolState;
56

    
57

    
58
/**
59
 * DOCUMENT ME!
60
 *
61
 * @author Vicente Caballero Navarro
62
 */
63
public class PolygonCADTool extends DefaultCADTool {
64
    private PolygonCADToolContext _fsm;
65
    private Point2D center;
66
    private int numLines = 5;
67
    private boolean isI = true;
68

    
69
    /**
70
     * Crea un nuevo PolygonCADTool.
71
     */
72
    public PolygonCADTool() {
73
        _fsm = new PolygonCADToolContext(this);
74
    }
75

    
76
    /**
77
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
78
     * carga previa a la utilizaci?n de la herramienta.
79
     */
80
    public void init() {
81
    }
82

    
83
    /* (non-Javadoc)
84
     * @see com.iver.cit.gvsig.gui.cad.CADTool#end()
85
     */
86
    public void end() {
87
        _fsm = new PolygonCADToolContext(this);
88
    }
89

    
90
    /* (non-Javadoc)
91
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
92
     */
93
    public void transition(double x, double y) {
94
        _fsm.addPoint(x, y);
95
    }
96

    
97
    /* (non-Javadoc)
98
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
99
     */
100
    public void transition(double d) {
101
        //_fsm.addValue(sel,d);
102
    }
103

    
104
    /* (non-Javadoc)
105
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
106
     */
107
    public void transition(String s) {
108
        _fsm.addOption(s);
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) {
120
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
121
        String status = actualState.getName();
122

    
123
        if (status.equals("ExecuteMap.Initial")) {
124
            center = new Point2D.Double(x, y);
125
        } else if (status.equals("ExecuteMap.First")) {
126
            center = new Point2D.Double(x, y);
127
        } else if (status.equals("ExecuteMap.Second") ||
128
                status.equals("ExecuteMap.Third")) {
129
            Point2D point = new Point2D.Double(x, y);
130

    
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
    public void drawOperation(Graphics g, double x,
150
        double y) {
151
        PolygonCADToolState actualState = _fsm.getState();
152
        String status = actualState.getName();
153

    
154
        if (status.equals("ExecuteMap.First") ||
155
                status.equals("ExecuteMap.Second") ||
156
                status.equals("ExecuteMap.Third")) {
157
            Point2D point = new Point2D.Double(x, y);
158
            drawLine((Graphics2D) g, center, point);
159

    
160
            if (isI) {
161
                getIPolygon(point, point.distance(center)).draw((Graphics2D) g,
162
                    getCadToolAdapter().getMapControl().getViewPort(),
163
                    CADTool.modifySymbol);
164
            } else {
165
                getCPolygon(point, point.distance(center)).draw((Graphics2D) g,
166
                    getCadToolAdapter().getMapControl().getViewPort(),
167
                    CADTool.modifySymbol);
168
            }
169

    
170
            ShapeFactory.createCircle(center, point.distance(center)).draw((Graphics2D) g,
171
                getCadToolAdapter().getMapControl().getViewPort(),
172
                CADTool.drawingSymbol);
173
        }
174
    }
175

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

    
186
        if (status.equals("ExecuteMap.Second")) {
187
            if (s.equals("C") || s.equals("c")) {
188
                isI = false;
189
            } else if (s.equals("I") || s.equals("i")) {
190
                isI = true;
191
            }
192
        }
193
    }
194

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

    
202
        if (status.equals("ExecuteMap.Initial")) {
203
            numLines = (int) d;
204
        } else if (status.equals("ExecuteMap.Second") ||
205
                status.equals("ExecuteMap.Third")) {
206
            double radio = d;
207

    
208
            //Pol?gono a partir de radio.
209
            Point2D point = UtilFunctions.getPoint(center,
210
                    new Point2D.Double(center.getX(), center.getY() + 10), radio);
211

    
212
            if (isI) {
213
                addGeometry(getIPolygon(point, radio));
214
            } else {
215
                addGeometry(getCPolygon(point, radio));
216
            }
217
        }
218
    }
219

    
220
    /**
221
     * Devuelve la geometr?a con el poligono regular circunscrito a la
222
     * circunferencia formada por el punto central y el radio que se froma
223
     * entre este y el puntero del rat?n..
224
     *
225
     * @param point Puntero del rat?n.
226
     * @param radio Radio
227
     *
228
     * @return GeometryCollection con las geometr?as del pol?gono.
229
     */
230
    private IGeometry getCPolygon(Point2D point, double radio) {
231
        IGeometry[] geoms = new IGeometry[numLines];
232
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
233

    
234
        double initangle = UtilFunctions.getAngle(center, point);
235
        Point2D antPoint = p1;
236
        Point2D antInter = null;
237
        double an = (Math.PI * 2) / numLines;
238

    
239
        for (int i = 1; i < (numLines + 2); i++) {
240
            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
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
250
                        2);
251
                elShape.moveTo(antInter.getX(), antInter.getY());
252
                elShape.lineTo(inter.getX(), inter.getY());
253

    
254
                geoms[i - 2] = (ShapeFactory.createPolyline2D(elShape));
255
            }
256

    
257
            antInter = inter;
258
            antPoint = p2;
259
        }
260

    
261
        return new FGeometryCollection(geoms);
262
    }
263

    
264
    /**
265
     * Devuelve la geometr?a con el poligono regular inscrito en la
266
     * circunferencia.
267
     *
268
     * @param point Puntero del rat?n.
269
     * @param radio Radio
270
     *
271
     * @return GeometryCollection con las geometr?as del pol?gono.
272
     */
273
    private IGeometry getIPolygon(Point2D point, double radio) {
274
        IGeometry[] geoms = new IGeometry[numLines];
275
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
276
        double initangle = UtilFunctions.getAngle(center, point);
277
        Point2D antPoint = p1;
278
        double an = (Math.PI * 2) / numLines;
279

    
280
        for (int i = 1; i < (numLines + 1); i++) {
281
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
282
                    radio);
283
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
284
                    2);
285
            elShape.moveTo(antPoint.getX(), antPoint.getY());
286
            elShape.lineTo(p2.getX(), p2.getY());
287

    
288
            geoms[i - 1] = (ShapeFactory.createPolyline2D(elShape));
289
            antPoint = p2;
290
        }
291

    
292
        return new FGeometryCollection(geoms);
293
    }
294
}