Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / PolygonCADTool.java @ 29973

History | View | Annotate | Download (16.2 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 org.gvsig.editing.gui.cad.tools;
42

    
43
import java.awt.Graphics;
44
import java.awt.Graphics2D;
45
import java.awt.Image;
46
import java.awt.event.InputEvent;
47
import java.awt.geom.Point2D;
48

    
49
import org.gvsig.andami.PluginServices;
50
import org.gvsig.editing.gui.cad.DefaultCADTool;
51
import org.gvsig.editing.gui.cad.exception.CommandException;
52
import org.gvsig.editing.gui.cad.exception.ValueException;
53
import org.gvsig.editing.gui.cad.tools.smc.PolygonCADToolContext;
54
import org.gvsig.editing.gui.cad.tools.smc.PolygonCADToolContext.PolygonCADToolState;
55
import org.gvsig.editing.layers.VectorialLayerEdited;
56
import org.gvsig.fmap.geom.Geometry;
57
import org.gvsig.fmap.geom.operation.Draw;
58
import org.gvsig.fmap.geom.operation.DrawOperationContext;
59
import org.gvsig.fmap.geom.operation.GeometryOperationException;
60
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
61
import org.gvsig.fmap.geom.primitive.GeneralPathX;
62
import org.gvsig.fmap.geom.util.UtilFunctions;
63

    
64

    
65

    
66
/**
67
 * DOCUMENT ME!
68
 *
69
 * @author Vicente Caballero Navarro
70
 */
71
public class PolygonCADTool extends DefaultCADTool {
72
    protected PolygonCADToolContext _fsm;
73
    protected Point2D center;
74
    protected int numLines = 5;
75
    protected boolean isI = true;
76

    
77
    /**
78
     * Crea un nuevo PolygonCADTool.
79
     */
80
    public PolygonCADTool() {
81

    
82
    }
83

    
84
    /**
85
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
86
     * carga previa a la utilizaci?n de la herramienta.
87
     */
88
    public void init() {
89
            _fsm = new PolygonCADToolContext(this);
90
    }
91

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

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

    
106
    /* (non-Javadoc)
107
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
108
     */
109
    public void transition(String s) throws CommandException {
110
            if (!super.changeCommand(s)){
111
                    _fsm.addOption(s);
112
            }
113
    }
114

    
115
    /**
116
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
117
     * editableFeatureSource que ya estar? creado.
118
     *
119
     * @param sel Bitset con las geometr?as que est?n seleccionadas.
120
     * @param x par?metro x del punto que se pase en esta transici?n.
121
     * @param y par?metro y del punto que se pase en esta transici?n.
122
     */
123
    public void addPoint(double x, double y,InputEvent event) {
124
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
125
        String status = actualState.getName();
126

    
127
        if (status.equals("Polygon.NumberOrCenterPoint")) {
128
            center = new Point2D.Double(x, y);
129
        } else if (status.equals("Polygon.CenterPoint")) {
130
            center = new Point2D.Double(x, y);
131
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
132
                status.equals("Polygon.RadiusOrPoint")) {
133
            Point2D point = new Point2D.Double(x, y);
134
            //Pol?gono a partir de la circunferencia.
135
            if (isI) {
136
                insertAndSelectGeometry(getIPolygon(point, point.distance(center)));
137
            } else {
138
                insertAndSelectGeometry(getCPolygon(point, point.distance(center)));
139
            }
140
        }
141
    }
142

    
143
    /**
144
     * M?todo para dibujar la lo necesario para el estado en el que nos
145
     * encontremos.
146
     *
147
     * @param g Graphics sobre el que dibujar.
148
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
149
     * @param x par?metro x del punto que se pase para dibujar.
150
     * @param y par?metro x del punto que se pase para dibujar.
151
     */
152
    public void drawOperation(Graphics g, double x,
153
        double y) {
154
        PolygonCADToolState actualState = _fsm.getState();
155
        String status = actualState.getName();
156

    
157
        if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
158
                status.equals("Polygon.RadiusOrPoint")) {
159
            Point2D point = new Point2D.Double(x, y);
160
            drawLine((Graphics2D) g, center, point,DefaultCADTool.geometrySelectSymbol);
161
            DrawOperationContext doc=new DrawOperationContext();
162
                    doc.setGraphics((Graphics2D)g);
163
                    doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort());
164
                    doc.setSymbol(DefaultCADTool.geometrySelectSymbol);
165
            if (isI) {
166
                    try {
167
                            getIPolygon(point, point.distance(center)).invokeOperation(Draw.CODE,doc);
168
                        } catch (GeometryOperationNotSupportedException e) {
169
                                e.printStackTrace();
170
                        } catch (GeometryOperationException e) {
171
                                e.printStackTrace();
172
                        }
173
//                getIPolygon(point, point.distance(center)).draw((Graphics2D) g,
174
//                    getCadToolAdapter().getMapControl().getViewPort(),
175
//                    DefaultCADTool.geometrySelectSymbol);
176
            } else {
177
                    try {
178
                            getCPolygon(point, point.distance(center)).invokeOperation(Draw.CODE,doc);
179
                        } catch (GeometryOperationNotSupportedException e) {
180
                                e.printStackTrace();
181
                        } catch (GeometryOperationException e) {
182
                                e.printStackTrace();
183
                        }
184
//                    getCPolygon(point, point.distance(center)).draw((Graphics2D) g,
185
//                    getCadToolAdapter().getMapControl().getViewPort(),
186
//                    DefaultCADTool.geometrySelectSymbol);
187
            }
188
            try {
189
                    doc.setSymbol(DefaultCADTool.axisReferencesSymbol);
190
                    createCircle(center, point.distance(center)).invokeOperation(Draw.CODE,doc);
191
                    } catch (GeometryOperationNotSupportedException e) {
192
                            e.printStackTrace();
193
                    } catch (GeometryOperationException e) {
194
                            e.printStackTrace();
195
                    }
196
//            geomFactory.createCircle(center, point.distance(center)).draw((Graphics2D) g,
197
//                getCadToolAdapter().getMapControl().getViewPort(),
198
//                DefaultCADTool.axisReferencesSymbol);
199
        }else{
200
                VectorialLayerEdited vle=getVLE();
201
                if (!vle.getLayer().isVisible())
202
                        return;
203
                try{
204
                        Image imgSel = vle.getSelectionImage();
205
                        if (imgSel!=null)
206
                                g.drawImage(imgSel, 0, 0, null);
207
                        Image imgHand = vle.getHandlersImage();
208
                        if (imgHand!=null)
209
                                g.drawImage(imgHand, 0, 0, null);
210
                }catch (Exception e) {
211
                }
212
        }
213
    }
214

    
215
    /**
216
     * Add a diferent option.
217
     *
218
     * @param sel DOCUMENT ME!
219
     * @param s Diferent option.
220
     */
221
    public void addOption(String s) {
222
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
223
        String status = actualState.getName();
224

    
225
        if (status.equals("Polygon.OptionOrRadiusOrPoint")) {
226
            if (s.equalsIgnoreCase(PluginServices.getText(this,"PolygonCADTool.circumscribed")) || s.equals(PluginServices.getText(this,"circumscribed"))) {
227
                isI = false;
228
            } else if (s.equalsIgnoreCase(PluginServices.getText(this,"PolygonCADTool.into_circle")) || s.equals(PluginServices.getText(this,"into_circle"))) {
229
                isI = true;
230
            }
231
        }
232
    }
233

    
234
    /* (non-Javadoc)
235
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
236
     */
237
    public void addValue(double d) {
238
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
239
        String status = actualState.getName();
240

    
241
        if (status.equals("Polygon.NumberOrCenterPoint")) {
242
                numLines = (int) d;
243
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
244
                status.equals("Polygon.RadiusOrPoint")) {
245
            double radio = d;
246

    
247
            //Pol?gono a partir de radio.
248
            Point2D point = UtilFunctions.getPoint(center,
249
                    new Point2D.Double(center.getX(), center.getY() + 10), radio);
250

    
251
            if (isI) {
252
                insertAndSelectGeometry(getIPolygon(point, radio));
253
            } else {
254
                insertAndSelectGeometry(getCPolygon(point, radio));
255
            }
256
        }
257
    }
258

    
259
    /**
260
     * Devuelve la geometr?a con el poligono regular circunscrito a la
261
     * circunferencia formada por el punto central y el radio que se froma
262
     * entre este y el puntero del rat?n..
263
     *
264
     * @param point Puntero del rat?n.
265
     * @param radio Radio
266
     *
267
     * @return GeometryCollection con las geometr?as del pol?gono.
268
     */
269
    private Geometry getCPolygon(Point2D point, double radio) {
270
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
271

    
272
        double initangle = UtilFunctions.getAngle(center, point);
273
        Point2D antPoint = p1;
274
        Point2D antInter = null;
275
        //Point2D firstPoint= null;
276
        double an = (Math.PI * 2) / numLines;
277
        GeneralPathX elShape = new GeneralPathX();
278
        boolean firstTime=true;
279
        for (int i = numLines-1; i >= 0 ; i--) {
280
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
281
                    radio);
282
            Point2D[] ps1 = UtilFunctions.getPerpendicular(antPoint, center,
283
                    antPoint);
284
            Point2D[] ps2 = UtilFunctions.getPerpendicular(p2, center, p2);
285
            Point2D inter = UtilFunctions.getIntersection(ps1[0], ps1[1],
286
                    ps2[0], ps2[1]);
287

    
288
            if (antInter != null) {
289

    
290
                if (firstTime){
291
                        elShape.moveTo(antInter.getX(), antInter.getY());
292
                        //firstPoint=new Point2D.Double(antInter.getX(), antInter.getY());
293
                        firstTime=false;
294
                }
295
                elShape.lineTo(inter.getX(), inter.getY());
296

    
297
            }
298

    
299
            antInter = inter;
300
            antPoint = p2;
301
        }
302
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
303
        elShape.closePath();
304
        int type=getCadToolAdapter().getActiveLayerType();
305
        Geometry shape=null;
306
        if (type==Geometry.TYPES.SURFACE){
307
                shape = createSurface(elShape);
308
        }else{
309
                shape = createCurve(elShape);
310
        }
311
        return shape;
312
    }
313

    
314
    /**
315
     * Devuelve la geometr?a con el poligono regular inscrito en la
316
     * circunferencia.
317
     *
318
     * @param point Puntero del rat?n.
319
     * @param radio Radio
320
     *
321
     * @return GeometryCollection con las geometr?as del pol?gono.
322
     * @throws ValueException
323
     */
324
    private Geometry getIPolygon(Point2D point, double radio){
325
            Point2D p1 = UtilFunctions.getPoint(center, point, radio);
326
        double initangle = UtilFunctions.getAngle(center, point);
327
        Point2D antPoint = p1;
328
        //Point2D firstPoint= null;
329
        double an = (Math.PI * 2) / numLines;
330
        GeneralPathX elShape = new GeneralPathX();
331
        boolean firstTime=true;
332

    
333
        for (int i = numLines-1; i > 0; i--) {
334
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
335
                    radio);
336

    
337
            if (firstTime){
338
                     elShape.moveTo(antPoint.getX(), antPoint.getY());
339
                     //firstPoint=new Point2D.Double(antPoint.getX(), antPoint.getY());
340
                     firstTime=false;
341
            }
342

    
343
            elShape.lineTo(p2.getX(), p2.getY());
344

    
345
            antPoint = p2;
346
        }
347
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
348
        elShape.closePath();
349
        int type=getCadToolAdapter().getActiveLayerType();
350
        Geometry shape=null;
351
        if (type==Geometry.TYPES.SURFACE){
352
                shape = createSurface(elShape);
353
        }else{
354
                shape = createCurve(elShape);
355
        }
356
        return shape;
357
    }
358
    /**
359
     * Devuelve la geometr?a con el poligono regular circunscrito a la
360
     * circunferencia formada por el punto central y el radio que se froma
361
     * entre este y el puntero del rat?n..
362
     *
363
     * @param point Puntero del rat?n.
364
     * @param radio Radio
365
     *
366
     * @return GeometryCollection con las geometr?as del pol?gono.
367
     */
368
   /* private IGeometry getCPolygonOld(Point2D point, double radio) {
369
        IGeometry[] geoms = new IGeometry[numLines];
370
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
371

372
        double initangle = UtilFunctions.getAngle(center, point);
373
        Point2D antPoint = p1;
374
        Point2D antInter = null;
375
        double an = (Math.PI * 2) / numLines;
376

377
        for (int i = 1; i < (numLines + 2); i++) {
378
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
379
                    radio);
380
            Point2D[] ps1 = UtilFunctions.getPerpendicular(antPoint, center,
381
                    antPoint);
382
            Point2D[] ps2 = UtilFunctions.getPerpendicular(p2, center, p2);
383
            Point2D inter = UtilFunctions.getIntersection(ps1[0], ps1[1],
384
                    ps2[0], ps2[1]);
385

386
            if (antInter != null) {
387
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
388
                        2);
389
                elShape.moveTo(antInter.getX(), antInter.getY());
390
                elShape.lineTo(inter.getX(), inter.getY());
391

392
                geoms[i - 2] = (ShapeFactory.createPolyline2D(elShape));
393
            }
394

395
            antInter = inter;
396
            antPoint = p2;
397
        }
398

399
        return new FGeometryCollection(geoms);
400
    }
401
*/
402
    /**
403
     * Devuelve la geometr?a con el poligono regular inscrito en la
404
     * circunferencia.
405
     *
406
     * @param point Puntero del rat?n.
407
     * @param radio Radio
408
     *
409
     * @return GeometryCollection con las geometr?as del pol?gono.
410
     */
411
  /*  private IGeometry getIPolygonOld(Point2D point, double radio) {
412
        IGeometry[] geoms = new IGeometry[numLines];
413
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
414
        double initangle = UtilFunctions.getAngle(center, point);
415
        Point2D antPoint = p1;
416
        double an = (Math.PI * 2) / numLines;
417

418
        for (int i = 1; i < (numLines + 1); i++) {
419
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
420
                    radio);
421
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
422
                    2);
423
            elShape.moveTo(antPoint.getX(), antPoint.getY());
424
            elShape.lineTo(p2.getX(), p2.getY());
425

426
            geoms[i - 1] = (ShapeFactory.createPolyline2D(elShape));
427
            antPoint = p2;
428
        }
429

430
        return new FGeometryCollection(geoms);
431
    }
432
*/
433
        public String getName() {
434
                return PluginServices.getText(this,"polygon_");
435
        }
436

    
437
        /**
438
         * Devuelve la cadena que corresponde al estado en el que nos encontramos.
439
         *
440
         * @return Cadena para mostrar por consola.
441
         */
442
        public String getQuestion() {
443
                PolygonCADToolState actualState = _fsm.getState();
444
        String status = actualState.getName();
445

    
446
        if (status.equals("Polygon.NumberOrCenterPoint")) {
447
                return super.getQuestion()+"<"+numLines+">";
448
        }
449
               return super.getQuestion();
450

    
451
        }
452
        public String toString() {
453
                return "_polygon";
454
        }
455
        public boolean isApplicable(int shapeType) {
456
                switch (shapeType) {
457
                case Geometry.TYPES.POINT:
458
                case Geometry.TYPES.MULTIPOINT:
459
                        return false;
460
                }
461
                return true;
462
        }
463
}