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 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 29616 jpiera
package org.gvsig.editing.gui.cad.tools;
42 3782 caballero
43 3832 caballero
import java.awt.Graphics;
44
import java.awt.Graphics2D;
45 26336 vcaballero
import java.awt.Image;
46 4313 fjp
import java.awt.event.InputEvent;
47 3832 caballero
import java.awt.geom.Point2D;
48
49 29616 jpiera
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 21668 vcaballero
import org.gvsig.fmap.geom.Geometry;
57 22025 jmvivo
import org.gvsig.fmap.geom.operation.Draw;
58
import org.gvsig.fmap.geom.operation.DrawOperationContext;
59 21668 vcaballero
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 3782 caballero
65
66
/**
67
 * DOCUMENT ME!
68
 *
69
 * @author Vicente Caballero Navarro
70
 */
71
public class PolygonCADTool extends DefaultCADTool {
72 26921 jpiera
    protected PolygonCADToolContext _fsm;
73
    protected Point2D center;
74
    protected int numLines = 5;
75
    protected boolean isI = true;
76 27221 vcaballero
77 3782 caballero
    /**
78
     * Crea un nuevo PolygonCADTool.
79
     */
80
    public PolygonCADTool() {
81 3883 caballero
82 3782 caballero
    }
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 3883 caballero
            _fsm = new PolygonCADToolContext(this);
90 3782 caballero
    }
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 4313 fjp
    public void transition(double x, double y, InputEvent event) {
96 4324 caballero
        _fsm.addPoint(x, y, event);
97 3782 caballero
    }
98
99
    /* (non-Javadoc)
100
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
101
     */
102 3832 caballero
    public void transition(double d) {
103 3883 caballero
        _fsm.addValue(d);
104 3782 caballero
    }
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 5735 caballero
    public void transition(String s) throws CommandException {
110 4892 caballero
            if (!super.changeCommand(s)){
111
                    _fsm.addOption(s);
112
            }
113 3782 caballero
    }
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 4365 caballero
    public void addPoint(double x, double y,InputEvent event) {
124 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
125
        String status = actualState.getName();
126
127 3978 caballero
        if (status.equals("Polygon.NumberOrCenterPoint")) {
128 3782 caballero
            center = new Point2D.Double(x, y);
129 3978 caballero
        } else if (status.equals("Polygon.CenterPoint")) {
130 3782 caballero
            center = new Point2D.Double(x, y);
131 3978 caballero
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
132
                status.equals("Polygon.RadiusOrPoint")) {
133 3782 caballero
            Point2D point = new Point2D.Double(x, y);
134
            //Pol?gono a partir de la circunferencia.
135
            if (isI) {
136 27270 vcaballero
                insertAndSelectGeometry(getIPolygon(point, point.distance(center)));
137 3782 caballero
            } else {
138 27270 vcaballero
                insertAndSelectGeometry(getCPolygon(point, point.distance(center)));
139 3782 caballero
            }
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 3832 caballero
    public void drawOperation(Graphics g, double x,
153 3782 caballero
        double y) {
154
        PolygonCADToolState actualState = _fsm.getState();
155
        String status = actualState.getName();
156
157 5170 caballero
        if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
158 3978 caballero
                status.equals("Polygon.RadiusOrPoint")) {
159 3782 caballero
            Point2D point = new Point2D.Double(x, y);
160 11437 caballero
            drawLine((Graphics2D) g, center, point,DefaultCADTool.geometrySelectSymbol);
161 21668 vcaballero
            DrawOperationContext doc=new DrawOperationContext();
162
                    doc.setGraphics((Graphics2D)g);
163
                    doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort());
164
                    doc.setSymbol(DefaultCADTool.geometrySelectSymbol);
165 3782 caballero
            if (isI) {
166 21668 vcaballero
                    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 3782 caballero
            } else {
177 21668 vcaballero
                    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 3782 caballero
            }
188 21668 vcaballero
            try {
189 23535 vcaballero
                    doc.setSymbol(DefaultCADTool.axisReferencesSymbol);
190 26921 jpiera
                    createCircle(center, point.distance(center)).invokeOperation(Draw.CODE,doc);
191 21668 vcaballero
                    } 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 26336 vcaballero
        }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 3782 caballero
        }
213
    }
214
215
    /**
216
     * Add a diferent option.
217
     *
218
     * @param sel DOCUMENT ME!
219
     * @param s Diferent option.
220
     */
221 3832 caballero
    public void addOption(String s) {
222 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
223
        String status = actualState.getName();
224
225 3978 caballero
        if (status.equals("Polygon.OptionOrRadiusOrPoint")) {
226 9121 caballero
            if (s.equalsIgnoreCase(PluginServices.getText(this,"PolygonCADTool.circumscribed")) || s.equals(PluginServices.getText(this,"circumscribed"))) {
227 3782 caballero
                isI = false;
228 9121 caballero
            } else if (s.equalsIgnoreCase(PluginServices.getText(this,"PolygonCADTool.into_circle")) || s.equals(PluginServices.getText(this,"into_circle"))) {
229 3782 caballero
                isI = true;
230
            }
231
        }
232
    }
233
234
    /* (non-Javadoc)
235
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
236
     */
237 3832 caballero
    public void addValue(double d) {
238 3782 caballero
        PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState();
239
        String status = actualState.getName();
240
241 3978 caballero
        if (status.equals("Polygon.NumberOrCenterPoint")) {
242 5735 caballero
                numLines = (int) d;
243 3978 caballero
        } else if (status.equals("Polygon.OptionOrRadiusOrPoint") ||
244
                status.equals("Polygon.RadiusOrPoint")) {
245 3782 caballero
            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 27270 vcaballero
                insertAndSelectGeometry(getIPolygon(point, radio));
253 3782 caballero
            } else {
254 27270 vcaballero
                insertAndSelectGeometry(getCPolygon(point, radio));
255 3782 caballero
            }
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 21668 vcaballero
    private Geometry getCPolygon(Point2D point, double radio) {
270 4522 caballero
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
271
272
        double initangle = UtilFunctions.getAngle(center, point);
273
        Point2D antPoint = p1;
274
        Point2D antInter = null;
275 4549 caballero
        //Point2D firstPoint= null;
276 4522 caballero
        double an = (Math.PI * 2) / numLines;
277
        GeneralPathX elShape = new GeneralPathX();
278
        boolean firstTime=true;
279 27221 vcaballero
        for (int i = numLines-1; i >= 0 ; i--) {
280 4522 caballero
            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 4549 caballero
                        //firstPoint=new Point2D.Double(antInter.getX(), antInter.getY());
293 4522 caballero
                        firstTime=false;
294
                }
295
                elShape.lineTo(inter.getX(), inter.getY());
296
297
            }
298
299
            antInter = inter;
300
            antPoint = p2;
301
        }
302 4549 caballero
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
303
        elShape.closePath();
304 12297 caballero
        int type=getCadToolAdapter().getActiveLayerType();
305 21668 vcaballero
        Geometry shape=null;
306
        if (type==Geometry.TYPES.SURFACE){
307 27012 jpiera
                shape = createSurface(elShape);
308 12297 caballero
        }else{
309 27012 jpiera
                shape = createCurve(elShape);
310 12297 caballero
        }
311 21668 vcaballero
        return shape;
312 4522 caballero
    }
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 5735 caballero
     * @throws ValueException
323 4522 caballero
     */
324 21668 vcaballero
    private Geometry getIPolygon(Point2D point, double radio){
325 5735 caballero
            Point2D p1 = UtilFunctions.getPoint(center, point, radio);
326 4522 caballero
        double initangle = UtilFunctions.getAngle(center, point);
327
        Point2D antPoint = p1;
328 4549 caballero
        //Point2D firstPoint= null;
329 4522 caballero
        double an = (Math.PI * 2) / numLines;
330
        GeneralPathX elShape = new GeneralPathX();
331
        boolean firstTime=true;
332 5735 caballero
333 13770 caballero
        for (int i = numLines-1; i > 0; i--) {
334 4522 caballero
            Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
335
                    radio);
336
337
            if (firstTime){
338
                     elShape.moveTo(antPoint.getX(), antPoint.getY());
339 4549 caballero
                     //firstPoint=new Point2D.Double(antPoint.getX(), antPoint.getY());
340 4522 caballero
                     firstTime=false;
341
            }
342
343
            elShape.lineTo(p2.getX(), p2.getY());
344
345
            antPoint = p2;
346
        }
347 4549 caballero
        //elShape.lineTo(firstPoint.getX(),firstPoint.getY());
348
        elShape.closePath();
349 12297 caballero
        int type=getCadToolAdapter().getActiveLayerType();
350 21668 vcaballero
        Geometry shape=null;
351
        if (type==Geometry.TYPES.SURFACE){
352 27012 jpiera
                shape = createSurface(elShape);
353 12297 caballero
        }else{
354 27012 jpiera
                shape = createCurve(elShape);
355 12297 caballero
        }
356 21668 vcaballero
        return shape;
357 4522 caballero
    }
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 3782 caballero
        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 4522 caballero
*/
402 3782 caballero
    /**
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 4522 caballero
  /*  private IGeometry getIPolygonOld(Point2D point, double radio) {
412 3782 caballero
        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 4522 caballero
*/
433 4118 caballero
        public String getName() {
434 4584 caballero
                return PluginServices.getText(this,"polygon_");
435 4118 caballero
        }
436
437 5170 caballero
        /**
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 9121 caballero
                PolygonCADToolState actualState = _fsm.getState();
444 5170 caballero
        String status = actualState.getName();
445
446
        if (status.equals("Polygon.NumberOrCenterPoint")) {
447
                return super.getQuestion()+"<"+numLines+">";
448
        }
449 9121 caballero
               return super.getQuestion();
450 5170 caballero
451
        }
452 4892 caballero
        public String toString() {
453
                return "_polygon";
454
        }
455 5170 caballero
        public boolean isApplicable(int shapeType) {
456
                switch (shapeType) {
457 21668 vcaballero
                case Geometry.TYPES.POINT:
458
                case Geometry.TYPES.MULTIPOINT:
459 5170 caballero
                        return false;
460
                }
461
                return true;
462
        }
463 3782 caballero
}