Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / PolygonCADTool.java @ 23535

History | View | Annotate | Download (15.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 21668 vcaballero
import org.gvsig.fmap.geom.Geometry;
49
import org.gvsig.fmap.geom.GeometryFactory;
50
import org.gvsig.fmap.geom.GeometryManager;
51 22025 jmvivo
import org.gvsig.fmap.geom.operation.Draw;
52
import org.gvsig.fmap.geom.operation.DrawOperationContext;
53 21668 vcaballero
import org.gvsig.fmap.geom.operation.GeometryOperationException;
54
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
55
import org.gvsig.fmap.geom.primitive.Curve2D;
56
import org.gvsig.fmap.geom.primitive.GeneralPathX;
57
import org.gvsig.fmap.geom.primitive.Surface2D;
58
import org.gvsig.fmap.geom.util.UtilFunctions;
59
60 4584 caballero
import com.iver.andami.PluginServices;
61 3832 caballero
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
62 5735 caballero
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
63
import com.iver.cit.gvsig.gui.cad.exception.ValueException;
64 3782 caballero
import com.iver.cit.gvsig.gui.cad.tools.smc.PolygonCADToolContext;
65
import com.iver.cit.gvsig.gui.cad.tools.smc.PolygonCADToolContext.PolygonCADToolState;
66
67
68
/**
69
 * DOCUMENT ME!
70
 *
71
 * @author Vicente Caballero Navarro
72
 */
73
public class PolygonCADTool extends DefaultCADTool {
74
    private PolygonCADToolContext _fsm;
75
    private Point2D center;
76
    private int numLines = 5;
77
    private boolean isI = true;
78 21668 vcaballero
    private GeometryFactory geomFactory=GeometryManager.getInstance().getGeometryFactory();
79 3782 caballero
80
    /**
81
     * Crea un nuevo PolygonCADTool.
82
     */
83
    public PolygonCADTool() {
84 3883 caballero
85 3782 caballero
    }
86
87
    /**
88
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
89
     * carga previa a la utilizaci?n de la herramienta.
90
     */
91
    public void init() {
92 3883 caballero
            _fsm = new PolygonCADToolContext(this);
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, double)
97
     */
98 4313 fjp
    public void transition(double x, double y, InputEvent event) {
99 4324 caballero
        _fsm.addPoint(x, y, event);
100 3782 caballero
    }
101
102
    /* (non-Javadoc)
103
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
104
     */
105 3832 caballero
    public void transition(double d) {
106 3883 caballero
        _fsm.addValue(d);
107 3782 caballero
    }
108
109
    /* (non-Javadoc)
110
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
111
     */
112 5735 caballero
    public void transition(String s) throws CommandException {
113 4892 caballero
            if (!super.changeCommand(s)){
114
                    _fsm.addOption(s);
115
            }
116 3782 caballero
    }
117
118
    /**
119
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
120
     * editableFeatureSource que ya estar? creado.
121
     *
122
     * @param sel Bitset con las geometr?as que est?n seleccionadas.
123
     * @param x par?metro x del punto que se pase en esta transici?n.
124
     * @param y par?metro y del punto que se pase en esta transici?n.
125
     */
126 4365 caballero
    public void addPoint(double x, double y,InputEvent event) {
127 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
128
        String status = actualState.getName();
129
130 3978 caballero
        if (status.equals("Polygon.NumberOrCenterPoint")) {
131 3782 caballero
            center = new Point2D.Double(x, y);
132 3978 caballero
        } else if (status.equals("Polygon.CenterPoint")) {
133 3782 caballero
            center = new Point2D.Double(x, y);
134 3978 caballero
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
135
                status.equals("Polygon.RadiusOrPoint")) {
136 3782 caballero
            Point2D point = new Point2D.Double(x, y);
137
            //Pol?gono a partir de la circunferencia.
138
            if (isI) {
139
                addGeometry(getIPolygon(point, point.distance(center)));
140
            } else {
141
                addGeometry(getCPolygon(point, point.distance(center)));
142
            }
143
        }
144
    }
145
146
    /**
147
     * M?todo para dibujar la lo necesario para el estado en el que nos
148
     * encontremos.
149
     *
150
     * @param g Graphics sobre el que dibujar.
151
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
152
     * @param x par?metro x del punto que se pase para dibujar.
153
     * @param y par?metro x del punto que se pase para dibujar.
154
     */
155 3832 caballero
    public void drawOperation(Graphics g, double x,
156 3782 caballero
        double y) {
157
        PolygonCADToolState actualState = _fsm.getState();
158
        String status = actualState.getName();
159
160 5170 caballero
        if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
161 3978 caballero
                status.equals("Polygon.RadiusOrPoint")) {
162 3782 caballero
            Point2D point = new Point2D.Double(x, y);
163 11437 caballero
            drawLine((Graphics2D) g, center, point,DefaultCADTool.geometrySelectSymbol);
164 21668 vcaballero
            DrawOperationContext doc=new DrawOperationContext();
165
                    doc.setGraphics((Graphics2D)g);
166
                    doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort());
167
                    doc.setSymbol(DefaultCADTool.geometrySelectSymbol);
168 3782 caballero
            if (isI) {
169 21668 vcaballero
                    try {
170
                            getIPolygon(point, point.distance(center)).invokeOperation(Draw.CODE,doc);
171
                        } catch (GeometryOperationNotSupportedException e) {
172
                                e.printStackTrace();
173
                        } catch (GeometryOperationException e) {
174
                                e.printStackTrace();
175
                        }
176
//                getIPolygon(point, point.distance(center)).draw((Graphics2D) g,
177
//                    getCadToolAdapter().getMapControl().getViewPort(),
178
//                    DefaultCADTool.geometrySelectSymbol);
179 3782 caballero
            } else {
180 21668 vcaballero
                    try {
181
                            getCPolygon(point, point.distance(center)).invokeOperation(Draw.CODE,doc);
182
                        } catch (GeometryOperationNotSupportedException e) {
183
                                e.printStackTrace();
184
                        } catch (GeometryOperationException e) {
185
                                e.printStackTrace();
186
                        }
187
//                    getCPolygon(point, point.distance(center)).draw((Graphics2D) g,
188
//                    getCadToolAdapter().getMapControl().getViewPort(),
189
//                    DefaultCADTool.geometrySelectSymbol);
190 3782 caballero
            }
191 21668 vcaballero
            try {
192 23535 vcaballero
                    doc.setSymbol(DefaultCADTool.axisReferencesSymbol);
193 21668 vcaballero
                    geomFactory.createCircle(center, point.distance(center)).invokeOperation(Draw.CODE,doc);
194
                    } catch (GeometryOperationNotSupportedException e) {
195
                            e.printStackTrace();
196
                    } catch (GeometryOperationException e) {
197
                            e.printStackTrace();
198
                    }
199
//            geomFactory.createCircle(center, point.distance(center)).draw((Graphics2D) g,
200
//                getCadToolAdapter().getMapControl().getViewPort(),
201
//                DefaultCADTool.axisReferencesSymbol);
202 3782 caballero
        }
203
    }
204
205
    /**
206
     * Add a diferent option.
207
     *
208
     * @param sel DOCUMENT ME!
209
     * @param s Diferent option.
210
     */
211 3832 caballero
    public void addOption(String s) {
212 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
213
        String status = actualState.getName();
214
215 3978 caballero
        if (status.equals("Polygon.OptionOrRadiusOrPoint")) {
216 9121 caballero
            if (s.equalsIgnoreCase(PluginServices.getText(this,"PolygonCADTool.circumscribed")) || s.equals(PluginServices.getText(this,"circumscribed"))) {
217 3782 caballero
                isI = false;
218 9121 caballero
            } else if (s.equalsIgnoreCase(PluginServices.getText(this,"PolygonCADTool.into_circle")) || s.equals(PluginServices.getText(this,"into_circle"))) {
219 3782 caballero
                isI = true;
220
            }
221
        }
222
    }
223
224
    /* (non-Javadoc)
225
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
226
     */
227 3832 caballero
    public void addValue(double d) {
228 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
229
        String status = actualState.getName();
230
231 3978 caballero
        if (status.equals("Polygon.NumberOrCenterPoint")) {
232 5735 caballero
                numLines = (int) d;
233 3978 caballero
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
234
                status.equals("Polygon.RadiusOrPoint")) {
235 3782 caballero
            double radio = d;
236
237
            //Pol?gono a partir de radio.
238
            Point2D point = UtilFunctions.getPoint(center,
239
                    new Point2D.Double(center.getX(), center.getY() + 10), radio);
240
241
            if (isI) {
242
                addGeometry(getIPolygon(point, radio));
243
            } else {
244
                addGeometry(getCPolygon(point, radio));
245
            }
246
        }
247
    }
248
249
    /**
250
     * Devuelve la geometr?a con el poligono regular circunscrito a la
251
     * circunferencia formada por el punto central y el radio que se froma
252
     * entre este y el puntero del rat?n..
253
     *
254
     * @param point Puntero del rat?n.
255
     * @param radio Radio
256
     *
257
     * @return GeometryCollection con las geometr?as del pol?gono.
258
     */
259 21668 vcaballero
    private Geometry getCPolygon(Point2D point, double radio) {
260 4522 caballero
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
261
262
        double initangle = UtilFunctions.getAngle(center, point);
263
        Point2D antPoint = p1;
264
        Point2D antInter = null;
265 4549 caballero
        //Point2D firstPoint= null;
266 4522 caballero
        double an = (Math.PI * 2) / numLines;
267
        GeneralPathX elShape = new GeneralPathX();
268
        boolean firstTime=true;
269 10427 caballero
        for (int i = numLines-1; i >=1 ; i--) {
270 4522 caballero
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
271
                    radio);
272
            Point2D[] ps1 = UtilFunctions.getPerpendicular(antPoint, center,
273
                    antPoint);
274
            Point2D[] ps2 = UtilFunctions.getPerpendicular(p2, center, p2);
275
            Point2D inter = UtilFunctions.getIntersection(ps1[0], ps1[1],
276
                    ps2[0], ps2[1]);
277
278
            if (antInter != null) {
279
280
                if (firstTime){
281
                        elShape.moveTo(antInter.getX(), antInter.getY());
282 4549 caballero
                        //firstPoint=new Point2D.Double(antInter.getX(), antInter.getY());
283 4522 caballero
                        firstTime=false;
284
                }
285
                elShape.lineTo(inter.getX(), inter.getY());
286
287
            }
288
289
            antInter = inter;
290
            antPoint = p2;
291
        }
292 4549 caballero
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
293
        elShape.closePath();
294 12297 caballero
        int type=getCadToolAdapter().getActiveLayerType();
295 21668 vcaballero
        Geometry shape=null;
296
        if (type==Geometry.TYPES.SURFACE){
297
                shape=new Surface2D(elShape);
298 12297 caballero
        }else{
299 21668 vcaballero
                shape=new Curve2D(elShape);
300 12297 caballero
        }
301 21668 vcaballero
        return shape;
302 4522 caballero
    }
303
304
    /**
305
     * Devuelve la geometr?a con el poligono regular inscrito en la
306
     * circunferencia.
307
     *
308
     * @param point Puntero del rat?n.
309
     * @param radio Radio
310
     *
311
     * @return GeometryCollection con las geometr?as del pol?gono.
312 5735 caballero
     * @throws ValueException
313 4522 caballero
     */
314 21668 vcaballero
    private Geometry getIPolygon(Point2D point, double radio){
315 5735 caballero
            Point2D p1 = UtilFunctions.getPoint(center, point, radio);
316 4522 caballero
        double initangle = UtilFunctions.getAngle(center, point);
317
        Point2D antPoint = p1;
318 4549 caballero
        //Point2D firstPoint= null;
319 4522 caballero
        double an = (Math.PI * 2) / numLines;
320
        GeneralPathX elShape = new GeneralPathX();
321
        boolean firstTime=true;
322 5735 caballero
323 13770 caballero
        for (int i = numLines-1; i > 0; i--) {
324 4522 caballero
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
325
                    radio);
326
327
            if (firstTime){
328
                     elShape.moveTo(antPoint.getX(), antPoint.getY());
329 4549 caballero
                     //firstPoint=new Point2D.Double(antPoint.getX(), antPoint.getY());
330 4522 caballero
                     firstTime=false;
331
            }
332
333
            elShape.lineTo(p2.getX(), p2.getY());
334
335
            antPoint = p2;
336
        }
337 4549 caballero
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
338
        elShape.closePath();
339 12297 caballero
        int type=getCadToolAdapter().getActiveLayerType();
340 21668 vcaballero
        Geometry shape=null;
341
        if (type==Geometry.TYPES.SURFACE){
342
                shape=new Surface2D(elShape);
343 12297 caballero
        }else{
344 21668 vcaballero
                shape=new Curve2D(elShape);
345 12297 caballero
        }
346 21668 vcaballero
        return shape;
347 4522 caballero
    }
348
    /**
349
     * Devuelve la geometr?a con el poligono regular circunscrito a la
350
     * circunferencia formada por el punto central y el radio que se froma
351
     * entre este y el puntero del rat?n..
352
     *
353
     * @param point Puntero del rat?n.
354
     * @param radio Radio
355
     *
356
     * @return GeometryCollection con las geometr?as del pol?gono.
357
     */
358
   /* private IGeometry getCPolygonOld(Point2D point, double radio) {
359 3782 caballero
        IGeometry[] geoms = new IGeometry[numLines];
360
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
361

362
        double initangle = UtilFunctions.getAngle(center, point);
363
        Point2D antPoint = p1;
364
        Point2D antInter = null;
365
        double an = (Math.PI * 2) / numLines;
366

367
        for (int i = 1; i < (numLines + 2); i++) {
368
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
369
                    radio);
370
            Point2D[] ps1 = UtilFunctions.getPerpendicular(antPoint, center,
371
                    antPoint);
372
            Point2D[] ps2 = UtilFunctions.getPerpendicular(p2, center, p2);
373
            Point2D inter = UtilFunctions.getIntersection(ps1[0], ps1[1],
374
                    ps2[0], ps2[1]);
375

376
            if (antInter != null) {
377
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
378
                        2);
379
                elShape.moveTo(antInter.getX(), antInter.getY());
380
                elShape.lineTo(inter.getX(), inter.getY());
381

382
                geoms[i - 2] = (ShapeFactory.createPolyline2D(elShape));
383
            }
384

385
            antInter = inter;
386
            antPoint = p2;
387
        }
388

389
        return new FGeometryCollection(geoms);
390
    }
391 4522 caballero
*/
392 3782 caballero
    /**
393
     * Devuelve la geometr?a con el poligono regular inscrito en la
394
     * circunferencia.
395
     *
396
     * @param point Puntero del rat?n.
397
     * @param radio Radio
398
     *
399
     * @return GeometryCollection con las geometr?as del pol?gono.
400
     */
401 4522 caballero
  /*  private IGeometry getIPolygonOld(Point2D point, double radio) {
402 3782 caballero
        IGeometry[] geoms = new IGeometry[numLines];
403
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
404
        double initangle = UtilFunctions.getAngle(center, point);
405
        Point2D antPoint = p1;
406
        double an = (Math.PI * 2) / numLines;
407

408
        for (int i = 1; i < (numLines + 1); i++) {
409
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
410
                    radio);
411
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
412
                    2);
413
            elShape.moveTo(antPoint.getX(), antPoint.getY());
414
            elShape.lineTo(p2.getX(), p2.getY());
415

416
            geoms[i - 1] = (ShapeFactory.createPolyline2D(elShape));
417
            antPoint = p2;
418
        }
419

420
        return new FGeometryCollection(geoms);
421
    }
422 4522 caballero
*/
423 4118 caballero
        public String getName() {
424 4584 caballero
                return PluginServices.getText(this,"polygon_");
425 4118 caballero
        }
426
427 5170 caballero
        /**
428
         * Devuelve la cadena que corresponde al estado en el que nos encontramos.
429
         *
430
         * @return Cadena para mostrar por consola.
431
         */
432
        public String getQuestion() {
433 9121 caballero
                PolygonCADToolState actualState = _fsm.getState();
434 5170 caballero
        String status = actualState.getName();
435
436
        if (status.equals("Polygon.NumberOrCenterPoint")) {
437
                return super.getQuestion()+"<"+numLines+">";
438
        }
439 9121 caballero
               return super.getQuestion();
440 5170 caballero
441
        }
442 4892 caballero
        public String toString() {
443
                return "_polygon";
444
        }
445 5170 caballero
        public boolean isApplicable(int shapeType) {
446
                switch (shapeType) {
447 21668 vcaballero
                case Geometry.TYPES.POINT:
448
                case Geometry.TYPES.MULTIPOINT:
449 5170 caballero
                        return false;
450
                }
451
                return true;
452
        }
453 3782 caballero
}