Statistics
| Revision:

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

History | View | Annotate | Download (19.5 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22
package org.gvsig.editing.gui.cad.tools;
23

    
24
import java.awt.event.InputEvent;
25
import java.awt.event.MouseEvent;
26
import java.awt.geom.Point2D;
27
import java.util.ArrayList;
28

    
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.andami.messages.NotificationManager;
31
import org.gvsig.editing.gui.cad.exception.CommandException;
32
import org.gvsig.editing.gui.cad.tools.smc.PolylineCADToolContext;
33
import org.gvsig.editing.gui.cad.tools.smc.PolylineCADToolContext.PolylineCADToolState;
34
import org.gvsig.fmap.dal.exception.ReadException;
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
37
import org.gvsig.fmap.geom.handler.Handler;
38
import org.gvsig.fmap.geom.primitive.Curve;
39
import org.gvsig.fmap.geom.primitive.GeneralPathX;
40
import org.gvsig.fmap.geom.util.UtilFunctions;
41
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
42
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
43

    
44
/**
45
 * DOCUMENT ME!
46
 * 
47
 * @author Vicente Caballero Navarro
48
 */
49
public class PolylineCADTool extends AbstractCurveSurfaceCADTool {
50

    
51
    protected PolylineCADToolContext _fsm;
52
    protected Point2D firstPoint;
53
    protected Point2D antPoint;
54
    protected Point2D antantPoint;
55
    protected Point2D antCenter;
56
    protected Point2D antInter;
57
    protected ArrayList list = new ArrayList();
58
    protected boolean close = false;
59
    protected GeneralPathX gpx = null;
60

    
61
    /**
62
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
63
     * carga previa a la utilizaci?n de la herramienta.
64
     */
65
    public void init() {
66
        _fsm = new PolylineCADToolContext(this);
67
    }
68

    
69
    public Geometry getGeometry() {
70
        Geometry[] geoms = (Geometry[]) list.toArray(new Geometry[0]);
71
        MultiPrimitive fgc = createMultiPrimitive(geoms);
72
        // No queremos guardar FGeometryCollections:
73
        GeneralPathX gp = new GeneralPathX();
74
        gp.append(fgc.getPathIterator(null, geomManager.getFlatness()), true);
75
        Geometry newGeom = createCurve(gp);
76
        return newGeom;
77
    }
78

    
79
    public int getLinesCount() {
80
        return list.size();
81
    }
82

    
83
    public boolean isPolygonLayer() {
84
        try {
85
            return (((FLyrVect) getVLE().getLayer()).getShapeType() == Geometry.TYPES.SURFACE);
86
        } catch (ReadException e) {
87
            return false;
88
        }
89
    }
90

    
91
    public void endGeometry() {
92
        if (gpx == null) {
93
            gpx = new GeneralPathX();
94
            Geometry[] geoms = (Geometry[]) list.toArray(new Geometry[0]);
95
            MultiPrimitive fgc = createMultiPrimitive(geoms);
96
            // No queremos guardar FGeometryCollections:
97
            gpx.append(fgc.getPathIterator(null, geomManager.getFlatness()),
98
                true);
99
        }
100
        try {
101
            if (((FLyrVect) getVLE().getLayer()).getShapeType() == Geometry.TYPES.SURFACE
102
                && !close) {
103
                closeGeometry();
104
            }
105
        } catch (ReadException e) {
106
            NotificationManager.addError(e.getMessage(), e);
107
        }
108

    
109
        Geometry newGeom = null;
110
        int type = getCadToolAdapter().getActiveLayerType();
111
        if ((type == Geometry.TYPES.SURFACE)
112
            || (type == Geometry.TYPES.MULTISURFACE)) {
113
            newGeom = createSurface(gpx);
114
        } else {
115
            newGeom = createCurve(gpx);
116
        }
117
        insertAndSelectGeometry(newGeom);
118
        _fsm = new PolylineCADToolContext(this);
119
        list.clear();
120
        clearTemporalCache();
121
        close = false;
122
        gpx = null;
123
        antantPoint = antCenter = antInter = antPoint = firstPoint = null;
124
    }
125

    
126
    public void closeGeometry() {
127
        if (gpx == null) {
128
            gpx = new GeneralPathX();
129
            Geometry[] geoms = (Geometry[]) list.toArray(new Geometry[0]);
130
            MultiPrimitive fgc = createMultiPrimitive(geoms);
131
            // No queremos guardar FGeometryCollections:
132
            gpx.append(fgc.getPathIterator(null, geomManager.getFlatness()),
133
                true);
134
        }
135
        close = true;
136
        gpx.closePath();
137
    }
138

    
139
    public void transition(double x, double y, InputEvent event) {
140
        _fsm.addPoint(x, y, event);
141
    }
142

    
143
    public void transition(double d) {
144
        _fsm.addValue(d);
145
    }
146

    
147
    public void transition(String s) throws CommandException {
148
        if (!super.changeCommand(s)) {
149
            _fsm.addOption(s);
150
        }
151
    }
152

    
153
    /**
154
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
155
     * editableFeatureSource que ya estar? creado.
156
     * 
157
     * @param sel
158
     *            Bitset con las geometr?as que est?n seleccionadas.
159
     * @param x
160
     *            par?metro x del punto que se pase en esta transici?n.
161
     * @param y
162
     *            par?metro y del punto que se pase en esta transici?n.
163
     */
164
    public void addPoint(double x, double y, InputEvent event) {
165
        PolylineCADToolState actualState =
166
            (PolylineCADToolState) _fsm.getPreviousState();
167
        String status = actualState.getName();
168
        if (status.equals("Polyline.NextPointOrArcOrClose")
169
            || status.equals("Polyline.FirstPoint")) {
170

    
171
            if (firstPoint == null) {
172
                firstPoint = new Point2D.Double(x, y);
173
            }
174
            Point2D point = new Point2D.Double(x, y);
175

    
176
            if (antPoint != null
177
                && (antPoint.getX() != point.getX() || antPoint.getY() != point
178
                    .getY())) {
179
                GeneralPathX elShape =
180
                    new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
181
                elShape.moveTo(antPoint.getX(), antPoint.getY());
182
                elShape.lineTo(point.getX(), point.getY());
183
                Geometry geom = createCurve(elShape);
184
                list.add(geom);
185
                addTemporalCache(geom);
186
            }
187
            if (antPoint == null)
188
                antPoint = (Point2D) firstPoint.clone();
189

    
190
            if (antPoint != null) {
191
                antantPoint = antPoint;
192
            }
193

    
194
            antPoint = point;
195
        } else
196
            if (status.equals("Polyline.NextPointOrLineOrClose")) {
197
                Point2D point = new Point2D.Double(x, y);
198
                Point2D lastp = antPoint; // (Point2D)points.get(i-1);
199

    
200
                if (antantPoint == null) {
201
                    antantPoint =
202
                        new Point2D.Double(lastp.getX() + (point.getX() / 2),
203
                            lastp.getY() + (point.getY() / 2));
204
                }
205

    
206
                if (((point.getY() == lastp.getY()) && (point.getX() < lastp
207
                    .getX()))
208
                    || ((point.getX() == lastp.getX()) && (point.getY() < lastp
209
                        .getY()))) {
210
                } else {
211
                    if (point.getX() == lastp.getX()) {
212
                        point =
213
                            new Point2D.Double(point.getX() + 0.00000001,
214
                                point.getY());
215
                    } else
216
                        if (point.getY() == lastp.getY()) {
217
                            point =
218
                                new Point2D.Double(point.getX(),
219
                                    point.getY() + 0.00000001);
220
                        }
221

    
222
                    if (point.getX() == antantPoint.getX()) {
223
                        point =
224
                            new Point2D.Double(point.getX() + 0.00000001,
225
                                point.getY());
226
                    } else
227
                        if (point.getY() == antantPoint.getY()) {
228
                            point =
229
                                new Point2D.Double(point.getX(),
230
                                    point.getY() + 0.00000001);
231
                        }
232

    
233
                    if (!(list.size() > 0)
234
                        || (((Geometry) list.get(list.size() - 1)).getType() == Geometry.TYPES.CURVE)) {
235
                        Point2D[] ps1 =
236
                            UtilFunctions.getPerpendicular(antantPoint, lastp,
237
                                lastp);
238
                        Point2D mediop =
239
                            new Point2D.Double(
240
                                (point.getX() + lastp.getX()) / 2,
241
                                (point.getY() + lastp.getY()) / 2);
242
                        Point2D[] ps2 =
243
                            UtilFunctions
244
                                .getPerpendicular(lastp, point, mediop);
245
                        Point2D interp =
246
                            UtilFunctions.getIntersection(ps1[0], ps1[1],
247
                                ps2[0], ps2[1]);
248
                        antInter = interp;
249

    
250
                        double radio = interp.distance(lastp);
251

    
252
                        if (UtilFunctions.isLowAngle(antantPoint, lastp,
253
                            interp, point)) {
254
                            radio = -radio;
255
                        }
256

    
257
                        Point2D centerp =
258
                            UtilFunctions.getPoint(interp, mediop, radio);
259
                        antCenter = centerp;
260

    
261
                        Geometry ig = createArc(lastp, centerp, point);
262

    
263
                        if (ig != null) {
264
                            list.add(ig);
265
                            addTemporalCache(ig);
266
                        }
267
                    } else {
268
                        Point2D[] ps1 =
269
                            UtilFunctions.getPerpendicular(lastp, antInter,
270
                                lastp);
271
                        double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]);
272
                        double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]);
273
                        double angle = UtilFunctions.getAngle(antCenter, lastp);
274
                        Point2D ini1 = null;
275
                        Point2D ini2 = null;
276

    
277
                        if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions
278
                            .absoluteAngleDistance(angle, a2)) {
279
                            ini1 = ps1[0];
280
                            ini2 = ps1[1];
281
                        } else {
282
                            ini1 = ps1[1];
283
                            ini2 = ps1[0];
284
                        }
285

    
286
                        Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
287
                        Point2D correct =
288
                            new Point2D.Double(lastp.getX() + unit.getX(),
289
                                lastp.getY() + unit.getY());
290
                        Point2D[] ps =
291
                            UtilFunctions.getPerpendicular(lastp, correct,
292
                                lastp);
293
                        Point2D mediop =
294
                            new Point2D.Double(
295
                                (point.getX() + lastp.getX()) / 2,
296
                                (point.getY() + lastp.getY()) / 2);
297
                        Point2D[] ps2 =
298
                            UtilFunctions
299
                                .getPerpendicular(lastp, point, mediop);
300
                        Point2D interp =
301
                            UtilFunctions.getIntersection(ps[0], ps[1], ps2[0],
302
                                ps2[1]);
303
                        antInter = interp;
304

    
305
                        double radio = interp.distance(lastp);
306

    
307
                        if (UtilFunctions.isLowAngle(correct, lastp, interp,
308
                            point)) {
309
                            radio = -radio;
310
                        }
311

    
312
                        Point2D centerp =
313
                            UtilFunctions.getPoint(interp, mediop, radio);
314
                        antCenter = centerp;
315
                        Geometry geom = createArc(lastp, centerp, point);
316
                        list.add(geom);
317
                        addTemporalCache(geom);
318
                    }
319

    
320
                    antantPoint = antPoint;
321
                    antPoint = point;
322
                }
323
            }
324
    }
325

    
326
    /**
327
     * M?todo para dibujar la lo necesario para el estado en el que nos
328
     * encontremos.
329
     * 
330
     * @param g
331
     *            Graphics sobre el que dibujar.
332
     * @param selectedGeometries
333
     *            BitSet con las geometr?as seleccionadas.
334
     * @param x
335
     *            par?metro x del punto que se pase para dibujar.
336
     * @param y
337
     *            par?metro x del punto que se pase para dibujar.
338
     */
339
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
340
        PolylineCADToolState actualState = _fsm.getState();
341
        String status = actualState.getName();
342

    
343
        if (status.equals("Polyline.NextPointOrArcOrClose")
344
            || status.equals("Polyline.FirstPoint")) {
345
            for (int i = 0; i < list.size(); i++) {
346
                renderer.draw((Geometry) list.get(i),
347
                    mapControlManager.getGeometrySelectionSymbol());
348
            }
349
            if (antPoint != null)
350
                renderer.drawLine(antPoint, new Point2D.Double(x, y),
351
                    mapControlManager.getGeometrySelectionSymbol());
352

    
353
        } else
354
            if ((status.equals("Polyline.NextPointOrLineOrClose"))) {
355
                for (int i = 0; i < list.size(); i++) {
356
                    renderer.draw((Geometry) list.get(i),
357
                        mapControlManager.getGeometrySelectionSymbol());
358
                }
359

    
360
                Point2D point = new Point2D.Double(x, y);
361
                Point2D lastp = antPoint;
362

    
363
                if (!(list.size() > 0)
364
                    || (((Geometry) list.get(list.size() - 1)).getType() == Geometry.TYPES.CURVE)) {
365
                    if (antantPoint == null) {
366
                        drawArc(point, lastp, new Point2D.Double(lastp.getX()
367
                            + (point.getX() / 2), lastp.getY()
368
                            + (point.getY() / 2)), renderer);
369
                    } else {
370
                        drawArc(point, lastp, antantPoint, renderer);
371
                    }
372
                } else {
373
                    if (antInter != null) {
374
                        Point2D[] ps1 =
375
                            UtilFunctions.getPerpendicular(lastp, antInter,
376
                                lastp);
377
                        double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]);
378
                        double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]);
379
                        double angle = UtilFunctions.getAngle(antCenter, lastp);
380
                        Point2D ini1 = null;
381
                        Point2D ini2 = null;
382

    
383
                        if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions
384
                            .absoluteAngleDistance(angle, a2)) {
385
                            ini1 = ps1[0];
386
                            ini2 = ps1[1];
387
                        } else {
388
                            ini1 = ps1[1];
389
                            ini2 = ps1[0];
390
                        }
391

    
392
                        Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
393
                        Point2D correct =
394
                            new Point2D.Double(lastp.getX() + unit.getX(),
395
                                lastp.getY() + unit.getY());
396
                        drawArc(point, lastp, correct, renderer);
397
                    }
398
                }
399
            }
400
        try {
401
            if (((FLyrVect) getVLE().getLayer()).getShapeType() == Geometry.TYPES.SURFACE
402
                && !list.isEmpty()) {
403
                Handler handler1 =
404
                    ((Geometry) list.get(0))
405
                        .getHandlers(Geometry.SELECTHANDLER)[0];
406
                GeneralPathX gpx = new GeneralPathX();
407
                gpx.moveTo(x, y);
408
                Point2D p1 = handler1.getPoint();
409
                gpx.lineTo(p1.getX(), p1.getY());
410
                Curve curve = createCurve(gpx);
411
                renderer.draw(curve,
412
                    mapControlManager.getGeometrySelectionSymbol());
413
            }
414
        } catch (ReadException e) {
415
            e.printStackTrace();
416
        }
417
    }
418

    
419
    /**
420
     * Dibuja el arco sobre el graphics.
421
     * 
422
     * @param point
423
     *            Puntero del rat?n.
424
     * @param lastp
425
     *            ?ltimo punto de la polilinea.
426
     * @param antp
427
     *            Punto antepenultimo.
428
     * @param g
429
     *            Graphics sobre el que se dibuja.
430
     */
431
    private void drawArc(Point2D point, Point2D lastp, Point2D antp,
432
        MapControlDrawer renderer) {
433
        if (((point.getY() == lastp.getY()) && (point.getX() < lastp.getX()))
434
            || ((point.getX() == lastp.getX()) && (point.getY() < lastp.getY()))) {
435
        } else {
436
            if (point.getX() == lastp.getX()) {
437
                point =
438
                    new Point2D.Double(point.getX() + 0.00000001, point.getY());
439
            } else
440
                if (point.getY() == lastp.getY()) {
441
                    point =
442
                        new Point2D.Double(point.getX(),
443
                            point.getY() + 0.00000001);
444
                }
445

    
446
            if (point.getX() == antp.getX()) {
447
                point =
448
                    new Point2D.Double(point.getX() + 0.00000001, point.getY());
449
            } else
450
                if (point.getY() == antp.getY()) {
451
                    point =
452
                        new Point2D.Double(point.getX(),
453
                            point.getY() + 0.00000001);
454
                }
455

    
456
            Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp, antp, lastp);
457
            Point2D mediop =
458
                new Point2D.Double((point.getX() + lastp.getX()) / 2,
459
                    (point.getY() + lastp.getY()) / 2);
460
            Point2D[] ps2 =
461
                UtilFunctions.getPerpendicular(lastp, point, mediop);
462
            Point2D interp =
463
                UtilFunctions.getIntersection(ps1[0], ps1[1], ps2[0], ps2[1]);
464

    
465
            double radio = interp.distance(lastp);
466

    
467
            if (UtilFunctions.isLowAngle(antp, lastp, interp, point)) {
468
                radio = -radio;
469
            }
470

    
471
            Point2D centerp = UtilFunctions.getPoint(interp, mediop, radio);
472
            renderer.drawLine(lastp, point,
473
                mapControlManager.getGeometrySelectionSymbol());
474

    
475
            Geometry ig = createArc(lastp, centerp, point);
476

    
477
            if (ig != null) {
478
                renderer.draw(ig,
479
                    mapControlManager.getGeometrySelectionSymbol());
480
            }
481
        }
482
    }
483

    
484
    /**
485
     * Add a diferent option.
486
     * 
487
     * @param sel
488
     *            DOCUMENT ME!
489
     * @param s
490
     *            Diferent option.
491
     */
492
    public void addOption(String s) {
493
        // Nothing to do
494
    }
495

    
496
    public void addValue(double d) {
497
        // Nothing to do
498
    }
499

    
500
    public void cancel() {
501
        list.clear();
502
        clearTemporalCache();
503
        antantPoint = antCenter = antInter = antPoint = firstPoint = null;
504
    }
505

    
506
    public void end() {
507
        // Nothing to do
508
    }
509

    
510
    public String getName() {
511
        return PluginServices.getText(this, "polyline_");
512
    }
513

    
514
    public String toString() {
515
        return "_polyline";
516
    }
517

    
518
    public void endTransition(double x, double y, MouseEvent event) {
519
        _fsm.endPoint(x, y, event);
520
    }
521

    
522
}