Statistics
| Revision:

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

History | View | Annotate | Download (19.7 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
                if (geom != null) {
185
                    list.add(geom);
186
                    addTemporalCache(geom);
187
                }
188
            }
189
            if (antPoint == null)
190
                antPoint = (Point2D) firstPoint.clone();
191

    
192
            if (antPoint != null) {
193
                antantPoint = antPoint;
194
            }
195

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

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

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

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

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

    
252
                        double radio = interp.distance(lastp);
253

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

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

    
263
                        Geometry ig = createArc(lastp, centerp, point);
264

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

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

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

    
307
                        double radio = interp.distance(lastp);
308

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

    
314
                        Point2D centerp =
315
                            UtilFunctions.getPoint(interp, mediop, radio);
316
                        antCenter = centerp;
317
                        Geometry geom = createArc(lastp, centerp, point);
318
                        
319
                        if (geom != null) {
320
                            list.add(geom);
321
                            addTemporalCache(geom);
322
                        }
323
                    }
324

    
325
                    antantPoint = antPoint;
326
                    antPoint = point;
327
                }
328
            }
329
    }
330

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

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

    
358
        } else
359
            if ((status.equals("Polyline.NextPointOrLineOrClose"))) {
360
                for (int i = 0; i < list.size(); i++) {
361
                    renderer.draw((Geometry) list.get(i),
362
                        mapControlManager.getGeometrySelectionSymbol());
363
                }
364

    
365
                Point2D point = new Point2D.Double(x, y);
366
                Point2D lastp = antPoint;
367

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

    
388
                        if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions
389
                            .absoluteAngleDistance(angle, a2)) {
390
                            ini1 = ps1[0];
391
                            ini2 = ps1[1];
392
                        } else {
393
                            ini1 = ps1[1];
394
                            ini2 = ps1[0];
395
                        }
396

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

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

    
451
            if (point.getX() == antp.getX()) {
452
                point =
453
                    new Point2D.Double(point.getX() + 0.00000001, point.getY());
454
            } else
455
                if (point.getY() == antp.getY()) {
456
                    point =
457
                        new Point2D.Double(point.getX(),
458
                            point.getY() + 0.00000001);
459
                }
460

    
461
            Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp, antp, lastp);
462
            Point2D mediop =
463
                new Point2D.Double((point.getX() + lastp.getX()) / 2,
464
                    (point.getY() + lastp.getY()) / 2);
465
            Point2D[] ps2 =
466
                UtilFunctions.getPerpendicular(lastp, point, mediop);
467
            Point2D interp =
468
                UtilFunctions.getIntersection(ps1[0], ps1[1], ps2[0], ps2[1]);
469

    
470
            double radio = interp.distance(lastp);
471

    
472
            if (UtilFunctions.isLowAngle(antp, lastp, interp, point)) {
473
                radio = -radio;
474
            }
475

    
476
            Point2D centerp = UtilFunctions.getPoint(interp, mediop, radio);
477
            renderer.drawLine(lastp, point,
478
                mapControlManager.getGeometrySelectionSymbol());
479

    
480
            Geometry ig = createArc(lastp, centerp, point);
481

    
482
            if (ig != null) {
483
                renderer.draw(ig,
484
                    mapControlManager.getGeometrySelectionSymbol());
485
            }
486
        }
487
    }
488

    
489
    /**
490
     * Add a diferent option.
491
     * 
492
     * @param sel
493
     *            DOCUMENT ME!
494
     * @param s
495
     *            Diferent option.
496
     */
497
    public void addOption(String s) {
498
        // Nothing to do
499
    }
500

    
501
    public void addValue(double d) {
502
        // Nothing to do
503
    }
504

    
505
    public void cancel() {
506
        list.clear();
507
        clearTemporalCache();
508
        antantPoint = antCenter = antInter = antPoint = firstPoint = null;
509
    }
510

    
511
    public void end() {
512
        // Nothing to do
513
    }
514

    
515
    public String getName() {
516
        return PluginServices.getText(this, "polyline_");
517
    }
518

    
519
    public String toString() {
520
        return "_polyline";
521
    }
522

    
523
    public void endTransition(double x, double y, MouseEvent event) {
524
        _fsm.endPoint(x, y, event);
525
    }
526

    
527
}