Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / PolygonCADTool.java @ 3997

History | View | Annotate | Download (9.81 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

    
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
            _fsm = new PolygonCADToolContext(this);
82
    }
83

    
84
    /* (non-Javadoc)
85
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
86
     */
87
    public void transition(double x, double y) {
88
        _fsm.addPoint(x, y);
89
    }
90

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

    
98
    /* (non-Javadoc)
99
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
100
     */
101
    public void transition(String s) {
102
        _fsm.addOption(s);
103
    }
104

    
105
    /**
106
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
107
     * editableFeatureSource que ya estar? creado.
108
     *
109
     * @param sel Bitset con las geometr?as que est?n seleccionadas.
110
     * @param x par?metro x del punto que se pase en esta transici?n.
111
     * @param y par?metro y del punto que se pase en esta transici?n.
112
     */
113
    public void addPoint(double x, double y) {
114
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
115
        String status = actualState.getName();
116

    
117
        if (status.equals("Polygon.NumberOrCenterPoint")) {
118
            center = new Point2D.Double(x, y);
119
        } else if (status.equals("Polygon.CenterPoint")) {
120
            center = new Point2D.Double(x, y);
121
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
122
                status.equals("Polygon.RadiusOrPoint")) {
123
            Point2D point = new Point2D.Double(x, y);
124
            //Pol?gono a partir de la circunferencia.
125
            if (isI) {
126
                addGeometry(getIPolygon(point, point.distance(center)));
127
            } else {
128
                addGeometry(getCPolygon(point, point.distance(center)));
129
            }
130
        }
131
    }
132

    
133
    /**
134
     * M?todo para dibujar la lo necesario para el estado en el que nos
135
     * encontremos.
136
     *
137
     * @param g Graphics sobre el que dibujar.
138
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
139
     * @param x par?metro x del punto que se pase para dibujar.
140
     * @param y par?metro x del punto que se pase para dibujar.
141
     */
142
    public void drawOperation(Graphics g, double x,
143
        double y) {
144
        PolygonCADToolState actualState = _fsm.getState();
145
        String status = actualState.getName();
146

    
147
        if (status.equals("Polygon.CenterPoint") ||
148
                status.equals("Polygon.OptionOrRadiusOrPoint") ||
149
                status.equals("Polygon.RadiusOrPoint")) {
150
            Point2D point = new Point2D.Double(x, y);
151
            drawLine((Graphics2D) g, center, point);
152

    
153
            if (isI) {
154
                getIPolygon(point, point.distance(center)).draw((Graphics2D) g,
155
                    getCadToolAdapter().getMapControl().getViewPort(),
156
                    CADTool.drawingSymbol);
157
            } else {
158
                getCPolygon(point, point.distance(center)).draw((Graphics2D) g,
159
                    getCadToolAdapter().getMapControl().getViewPort(),
160
                    CADTool.drawingSymbol);
161
            }
162

    
163
            ShapeFactory.createCircle(center, point.distance(center)).draw((Graphics2D) g,
164
                getCadToolAdapter().getMapControl().getViewPort(),
165
                CADTool.modifySymbol);
166
        }
167
    }
168

    
169
    /**
170
     * Add a diferent option.
171
     *
172
     * @param sel DOCUMENT ME!
173
     * @param s Diferent option.
174
     */
175
    public void addOption(String s) {
176
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
177
        String status = actualState.getName();
178

    
179
        if (status.equals("Polygon.OptionOrRadiusOrPoint")) {
180
            if (s.equals("C") || s.equals("c") || s.equals("Circunscrito")) {
181
                isI = false;
182
            } else if (s.equals("I") || s.equals("i") || s.equals("Inscrito")) {
183
                isI = true;
184
            }
185
        }
186
    }
187

    
188
    /* (non-Javadoc)
189
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
190
     */
191
    public void addValue(double d) {
192
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
193
        String status = actualState.getName();
194

    
195
        if (status.equals("Polygon.NumberOrCenterPoint")) {
196
            numLines = (int) d;
197
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
198
                status.equals("Polygon.RadiusOrPoint")) {
199
            double radio = d;
200

    
201
            //Pol?gono a partir de radio.
202
            Point2D point = UtilFunctions.getPoint(center,
203
                    new Point2D.Double(center.getX(), center.getY() + 10), radio);
204

    
205
            if (isI) {
206
                addGeometry(getIPolygon(point, radio));
207
            } else {
208
                addGeometry(getCPolygon(point, radio));
209
            }
210
        }
211
    }
212

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

    
227
        double initangle = UtilFunctions.getAngle(center, point);
228
        Point2D antPoint = p1;
229
        Point2D antInter = null;
230
        double an = (Math.PI * 2) / numLines;
231

    
232
        for (int i = 1; i < (numLines + 2); i++) {
233
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
234
                    radio);
235
            Point2D[] ps1 = UtilFunctions.getPerpendicular(antPoint, center,
236
                    antPoint);
237
            Point2D[] ps2 = UtilFunctions.getPerpendicular(p2, center, p2);
238
            Point2D inter = UtilFunctions.getIntersection(ps1[0], ps1[1],
239
                    ps2[0], ps2[1]);
240

    
241
            if (antInter != null) {
242
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
243
                        2);
244
                elShape.moveTo(antInter.getX(), antInter.getY());
245
                elShape.lineTo(inter.getX(), inter.getY());
246

    
247
                geoms[i - 2] = (ShapeFactory.createPolyline2D(elShape));
248
            }
249

    
250
            antInter = inter;
251
            antPoint = p2;
252
        }
253

    
254
        return new FGeometryCollection(geoms);
255
    }
256

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

    
273
        for (int i = 1; i < (numLines + 1); i++) {
274
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
275
                    radio);
276
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
277
                    2);
278
            elShape.moveTo(antPoint.getX(), antPoint.getY());
279
            elShape.lineTo(p2.getX(), p2.getY());
280

    
281
            geoms[i - 1] = (ShapeFactory.createPolyline2D(elShape));
282
            antPoint = p2;
283
        }
284

    
285
        return new FGeometryCollection(geoms);
286
    }
287

    
288
}