Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / BreakCADTool.java @ 39127

History | View | Annotate | Download (28.3 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.geom.PathIterator;
26
import java.awt.geom.Point2D;
27
import java.awt.geom.Rectangle2D;
28
import java.util.ArrayList;
29

    
30
import org.gvsig.andami.PluginServices;
31
import org.gvsig.andami.messages.NotificationManager;
32
import org.gvsig.editing.CADExtension;
33
import org.gvsig.editing.gui.cad.exception.CommandException;
34
import org.gvsig.editing.gui.cad.tools.smc.BreakCADToolContext;
35
import org.gvsig.editing.gui.cad.tools.smc.BreakCADToolContext.BreakCADToolState;
36
import org.gvsig.editing.layers.VectorialLayerEdited;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.exception.ReadException;
39
import org.gvsig.fmap.dal.feature.EditableFeature;
40
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureSet;
42
import org.gvsig.fmap.dal.feature.FeatureStore;
43
import org.gvsig.fmap.geom.Geometry;
44
import org.gvsig.fmap.geom.primitive.GeneralPathX;
45
import org.gvsig.fmap.mapcontext.ViewPort;
46
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
47
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
48
import org.gvsig.tools.dispose.DisposableIterator;
49

    
50
/**
51
 * Recorta una polil?nea en dos partes.
52
 * 
53
 * @author Vicente Caballero Navarro
54
 */
55
public class BreakCADTool extends AbstractCurveCADTool {
56

    
57
    protected BreakCADToolContext _fsm;
58
    protected Point2D firstPoint;
59
    protected Point2D secondPoint;
60
    protected Feature rowEdited;
61

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

    
72
    public void transition(double x, double y, InputEvent event) {
73
        _fsm.addPoint(x, y, event);
74
    }
75

    
76
    public void transition(double d) {
77
        _fsm.addValue(d);
78
    }
79

    
80
    public void transition(String s) throws CommandException {
81
        if (!super.changeCommand(s)) {
82
            _fsm.addOption(s);
83
        }
84
    }
85

    
86
    public void selection() {
87
        FeatureSet selection = null;
88
        try {
89
            selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
90

    
91
            if (selection.getSize() == 0
92
                && !CADExtension
93
                    .getCADTool()
94
                    .getClass()
95
                    .getName()
96
                    .equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
97
                CADExtension.setCADTool("_selection", false);
98
                ((SelectionCADTool) CADExtension.getCADTool())
99
                    .setNextTool("_break");
100
            }
101
        } catch (ReadException e) {
102
            // TODO Auto-generated catch block
103
            e.printStackTrace();
104
        } catch (DataException e) {
105
            // TODO Auto-generated catch block
106
            e.printStackTrace();
107
        }
108
    }
109

    
110
    /**
111
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
112
     * editableFeatureSource que ya estar? creado.
113
     * 
114
     * @param x
115
     *            par?metro x del punto que se pase en esta transici?n.
116
     * @param y
117
     *            par?metro y del punto que se pase en esta transici?n.
118
     */
119
    public void addPoint(double x, double y, InputEvent event) {
120
        BreakCADToolState actualState =
121
            (BreakCADToolState) _fsm.getPreviousState();
122
        String status = actualState.getName();
123

    
124
        if (status.equals("Break.FirstPoint")) {
125
            // if (rowEdited!=null &&
126
            // intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new
127
            // Point2D.Double(x,y)))
128
            firstPoint = new Point2D.Double(x, y);
129

    
130
        } else
131
            if (status.equals("Break.SecondPoint")) {
132
                // if (rowEdited !=null &&
133
                // intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new
134
                // Point2D.Double(x,y))){
135
                secondPoint = new Point2D.Double(x, y);
136
                try {
137
                    // IGeometry
138
                    // geom=((DefaultFeature)rowEdited.getLinkedRow()).getGeometry();
139
                    // if (geom instanceof FGeometryCollection) {
140
                    // breakGeometryGC(rowEdited);
141
                    // }else {
142
                    breakGeometry(rowEdited);
143
                    // }
144
                } catch (ReadException e) {
145
                    NotificationManager.addError(e.getMessage(), e);
146
                }
147
            }
148
        // }
149
    }
150

    
151
    /*
152
     * private void breakGeometryGC(DefaultRowEdited dre) throws IOException,
153
     * DriverIOException { GeneralPathX newGp1 = new GeneralPathX();
154
     * GeneralPathX newGp2 = new GeneralPathX(); FGeometryCollection
155
     * gc=(FGeometryCollection)((DefaultFeature)rowEdited.getLinkedRow()).
156
     * getGeometry();
157
     * IGeometry[] geoms=gc.getGeometries(); for (int i = 0;i<geoms.length;i++)
158
     * {
159
     * PathIterator theIterator=geoms[i].getPathIterator(null); double[] theData
160
     * =
161
     * new double[6]; boolean isFirstPart=true; boolean isCut=false; int
162
     * theType; int numParts = 0;
163
     * 
164
     * Point2D previous=null;
165
     * 
166
     * while (!theIterator.isDone()) { theType =
167
     * theIterator.currentSegment(theData); switch (theType) {
168
     * 
169
     * case PathIterator.SEG_MOVETO: numParts++; previous=new
170
     * Point2D.Double(theData[0], theData[1]); if (isFirstPart)
171
     * newGp1.moveTo(theData[0], theData[1]); else newGp2.moveTo(theData[0],
172
     * theData[1]); break;
173
     * 
174
     * case PathIterator.SEG_LINETO: if (previous!=null){ GeneralPathX gpx=new
175
     * GeneralPathX(); gpx.moveTo(previous.getX(),previous.getY());
176
     * gpx.lineTo(theData[0], theData[1]); IGeometry
177
     * geom=ShapeFactory.createPolyline2D(gpx); Point2D
178
     * p1=getNearPoint(previous); Point2D p2=getDistantPoint(previous); if
179
     * (intersects(geom,p1)){ isFirstPart=false;
180
     * newGp1.lineTo(p1.getX(),p1.getY()); newGp2.moveTo(p2.getX(),p2.getY());
181
     * isCut=true; } } previous=new Point2D.Double(theData[0], theData[1]); if
182
     * (isFirstPart) newGp1.lineTo(theData[0], theData[1]); else
183
     * newGp2.lineTo(theData[0], theData[1]); break;
184
     * 
185
     * case PathIterator.SEG_QUADTO: if (previous!=null){ GeneralPathX gpx=new
186
     * GeneralPathX(); gpx.moveTo(previous.getX(),previous.getY());
187
     * gpx.quadTo(theData[0], theData[1],theData[2], theData[3]); IGeometry
188
     * geom=ShapeFactory.createPolyline2D(gpx); Point2D
189
     * p1=getNearPoint(previous); Point2D p2=getDistantPoint(previous); if
190
     * (intersects(geom,p1)){ isFirstPart=false;
191
     * newGp1.lineTo(p1.getX(),p1.getY()); newGp2.moveTo(p2.getX(),p2.getY());
192
     * isCut=true; } } previous=new Point2D.Double(theData[0], theData[1]); if
193
     * (isFirstPart) newGp1.quadTo(theData[0], theData[1],theData[2],
194
     * theData[3]); else newGp2.quadTo(theData[0], theData[1],theData[2],
195
     * theData[3]);
196
     * 
197
     * break;
198
     * 
199
     * case PathIterator.SEG_CUBICTO: if (previous!=null){ GeneralPathX gpx=new
200
     * GeneralPathX(); gpx.moveTo(previous.getX(),previous.getY());
201
     * gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4],
202
     * theData[5]); IGeometry geom=ShapeFactory.createPolyline2D(gpx); Point2D
203
     * p1=getNearPoint(previous); Point2D p2=getDistantPoint(previous); if
204
     * (intersects(geom,p1)){ isFirstPart=false;
205
     * newGp1.lineTo(p1.getX(),p1.getY()); newGp2.moveTo(p2.getX(),p2.getY());
206
     * isCut=true; } } previous=new Point2D.Double(theData[0], theData[1]); if
207
     * (isFirstPart) newGp1.curveTo(theData[0], theData[1],theData[2],
208
     * theData[3],theData[4], theData[5]); else newGp2.curveTo(theData[0],
209
     * theData[1],theData[2], theData[3],theData[4], theData[5]);
210
     * 
211
     * break;
212
     * 
213
     * case PathIterator.SEG_CLOSE: //if (isFirstPart) // newGp1.closePath();
214
     * //else // newGp2.closePath(); break; } //end switch
215
     * 
216
     * theIterator.next(); } //end while loop
217
     * 
218
     * if (isCut) { IGeometry geom1 = ShapeFactory.createPolyline2D(newGp1);
219
     * IGeometry geom2 = ShapeFactory.createPolyline2D(newGp2);
220
     * VectorialLayerEdited vle = getVLE(); VectorialEditableAdapter vea =
221
     * vle.getVEA(); ArrayList selectedRow = vle.getSelectedRow();
222
     * vea.startComplexRow(); vea.removeRow(dre.getIndex(), getName(),
223
     * EditionEvent.GRAPHIC); int num = vea.getRowCount(); if (gc.isClosed()) {
224
     * ArrayList geomsAux1 = new ArrayList(); geomsAux1.add(geom2); for (int k =
225
     * i + 1; k < geoms.length; k++) { geomsAux1.add(geoms[k]); } for (int k =
226
     * 0; k < i; k++) { geomsAux1.add(geoms[k]); } geomsAux1.add(geom1);
227
     * 
228
     * DefaultFeature df1 = new DefaultFeature( new
229
     * FGeometryCollection((IGeometry[]) geomsAux1 .toArray(new IGeometry[0])),
230
     * dre .getAttributes(), String.valueOf(num)); int index1 = vea.addRow(df1,
231
     * PluginServices.getText(this, "parte1"), EditionEvent.GRAPHIC);
232
     * 
233
     * clearSelection(); selectedRow.add(new DefaultRowEdited(df1,
234
     * IRowEdited.STATUS_ADDED, index1)); vea.endComplexRow(); return; }else {
235
     * 
236
     * ArrayList geomsAux1 = new ArrayList(); for (int k = 0; k < i; k++) {
237
     * geomsAux1.add(geoms[k]); } geomsAux1.add(geom1);
238
     * 
239
     * ArrayList geomsAux2 = new ArrayList(); geomsAux2.add(geom2); for (int k =
240
     * i + 1; k < geoms.length; k++) { geomsAux2.add(geoms[k]); }
241
     * 
242
     * DefaultFeature df1 = new DefaultFeature( new
243
     * FGeometryCollection((IGeometry[]) geomsAux1 .toArray(new IGeometry[0])),
244
     * dre .getAttributes(), String.valueOf(num)); int index1 = vea.addRow(df1,
245
     * PluginServices.getText(this, "parte1"), EditionEvent.GRAPHIC);
246
     * DefaultFeature df2 = new DefaultFeature( new
247
     * FGeometryCollection((IGeometry[]) geomsAux2 .toArray(new IGeometry[0])),
248
     * dre .getAttributes(), String.valueOf(num + 1)); int index2 =
249
     * vea.addRow(df2, PluginServices.getText(this, "parte2"),
250
     * EditionEvent.GRAPHIC); clearSelection(); selectedRow.add(new
251
     * DefaultRowEdited(df2, IRowEdited.STATUS_ADDED, index2));
252
     * selectedRow.add(new DefaultRowEdited(df1, IRowEdited.STATUS_ADDED,
253
     * index1));
254
     * 
255
     * vea.endComplexRow(); return; }
256
     * } } }
257
     */
258

    
259
    private void breakGeometry(Feature dre) throws ReadException {
260
        breakGeom(dre);
261
    }
262

    
263
    private void breakGeom(Feature dre) throws ReadException {
264
        GeneralPathX newGp1 = new GeneralPathX();
265
        GeneralPathX newGp2 = new GeneralPathX();
266
        Geometry geomAux = (Geometry) rowEdited.getDefaultGeometry();
267
        PathIterator theIterator =
268
            geomAux.getPathIterator(null, geomManager.getFlatness());
269
        Point2D[] pointsOrdered = getOrderPoints(geomAux);
270
        double[] theData = new double[6];
271
        boolean isFirstPart = true;
272
        boolean intersectsP2 = false;
273
        int theType;
274
        int numParts = 0;
275
        boolean isBreaked = false;
276
        Point2D previous = null;
277

    
278
        while (!theIterator.isDone()) {
279
            theType = theIterator.currentSegment(theData);
280
            switch (theType) {
281

    
282
            case PathIterator.SEG_MOVETO:
283
                numParts++;
284

    
285
                previous = new Point2D.Double(theData[0], theData[1]);
286

    
287
                if (isFirstPart)
288
                    newGp1.moveTo(theData[0], theData[1]);
289
                else
290
                    newGp2.moveTo(theData[0], theData[1]);
291
                break;
292

    
293
            case PathIterator.SEG_LINETO:
294

    
295
                if (previous != null) {
296
                    GeneralPathX gpx = new GeneralPathX();
297
                    gpx.moveTo(previous.getX(), previous.getY());
298
                    gpx.lineTo(theData[0], theData[1]);
299
                    Geometry geom = createCurve(gpx);
300
                    Point2D p1 = pointsOrdered[0];
301
                    Point2D p2 = pointsOrdered[1];
302

    
303
                    if (intersects(geom, p1) && !isBreaked) {
304
                        isFirstPart = false;
305
                        newGp1.lineTo(p1.getX(), p1.getY());
306
                    }
307
                    if (intersects(geom, p2) && !isBreaked) {
308
                        isBreaked = true;
309
                        intersectsP2 = true;
310
                        newGp2.moveTo(p2.getX(), p2.getY());
311
                    }
312
                }
313
                previous = new Point2D.Double(theData[0], theData[1]);
314
                if (isFirstPart)
315
                    newGp1.lineTo(theData[0], theData[1]);
316
                else
317
                    if (intersectsP2) {
318
                        newGp2.lineTo(theData[0], theData[1]);
319
                    }
320

    
321
                break;
322

    
323
            case PathIterator.SEG_QUADTO:
324
                if (previous != null) {
325
                    GeneralPathX gpx = new GeneralPathX();
326
                    gpx.moveTo(previous.getX(), previous.getY());
327
                    gpx.quadTo(theData[0], theData[1], theData[2], theData[3]);
328
                    Geometry geom = createCurve(gpx);
329
                    Point2D p1 = pointsOrdered[0];
330
                    Point2D p2 = pointsOrdered[1];
331
                    if (intersects(geom, p1) && !isBreaked) {
332
                        isFirstPart = false;
333
                        newGp1.lineTo(p1.getX(), p1.getY());
334
                    }
335
                    if (intersects(geom, p2) && !isBreaked) {
336
                        isBreaked = true;
337
                        intersectsP2 = true;
338
                        newGp2.moveTo(p2.getX(), p2.getY());
339

    
340
                    }
341
                }
342
                previous = new Point2D.Double(theData[0], theData[1]);
343
                if (isFirstPart)
344
                    newGp1.quadTo(theData[0], theData[1], theData[2],
345
                        theData[3]);
346
                else
347
                    newGp2.quadTo(theData[0], theData[1], theData[2],
348
                        theData[3]);
349

    
350
                break;
351

    
352
            case PathIterator.SEG_CUBICTO:
353
                if (previous != null) {
354
                    GeneralPathX gpx = new GeneralPathX();
355
                    gpx.moveTo(previous.getX(), previous.getY());
356
                    gpx.curveTo(theData[0], theData[1], theData[2], theData[3],
357
                        theData[4], theData[5]);
358
                    Geometry geom = createCurve(gpx);
359
                    Point2D p1 = pointsOrdered[0];
360
                    Point2D p2 = pointsOrdered[1];
361
                    if (intersects(geom, p1) && !isBreaked) {
362
                        isFirstPart = false;
363
                        newGp1.lineTo(p1.getX(), p1.getY());
364
                    }
365
                    if (intersects(geom, p2) && !isBreaked) {
366
                        isBreaked = true;
367
                        intersectsP2 = true;
368
                        newGp2.moveTo(p2.getX(), p2.getY());
369

    
370
                    }
371
                }
372
                previous = new Point2D.Double(theData[0], theData[1]);
373
                if (isFirstPart)
374
                    newGp1.curveTo(theData[0], theData[1], theData[2],
375
                        theData[3], theData[4], theData[5]);
376
                else
377
                    newGp2.curveTo(theData[0], theData[1], theData[2],
378
                        theData[3], theData[4], theData[5]);
379

    
380
                break;
381

    
382
            case PathIterator.SEG_CLOSE:
383
                // if (isFirstPart)
384
                // newGp1.closePath();
385
                // else
386
                // newGp2.closePath();
387
                break;
388
            } // end switch
389

    
390
            theIterator.next();
391
        } // end while loop
392
        GeneralPathX gpx = new GeneralPathX();
393
        gpx.append(geomAux.getInternalShape().getPathIterator(null), true);
394
        VectorialLayerEdited vle = getVLE();
395
        FeatureStore featureStore =
396
            ((FLyrVect) vle.getLayer()).getFeatureStore();
397
        // VectorialEditableAdapter vea = vle.getVEA();
398
        ArrayList selectedRowAux = new ArrayList();
399
        try {
400
            featureStore.beginEditingGroup(getName());
401

    
402
            featureStore.delete(dre);
403
            // vea.startComplexRow();
404
            if (gpx.isClosed()) {
405

    
406
                newGp2.append(
407
                    newGp1.getPathIterator(null, geomManager.getFlatness()),
408
                    true);
409
                Geometry geom1 = createCurve(newGp2);
410

    
411
                // Feature dfLine1 = dre.cloneRow();
412
                EditableFeature eFeature =
413
                    featureStore.createNewFeature(dre.getType(), dre);
414
                eFeature.setGeometry(featureStore.getDefaultFeatureType()
415
                    .getDefaultGeometryAttributeName(), geom1);
416
                selectedRowAux.add(eFeature);
417

    
418
            } else {
419
                Geometry geom1 = createCurve(newGp1);
420
                Geometry geom2 = createCurve(newGp2);
421

    
422
                // DefaultFeature dfLine1 = (DefaultFeature)
423
                // dre.getLinkedRow().cloneRow();
424
                EditableFeature eFeature =
425
                    featureStore.createNewFeature(dre.getType(), dre);
426
                eFeature.setGeometry(featureStore.getDefaultFeatureType()
427
                    .getDefaultGeometryAttributeName(), geom2);
428

    
429
                selectedRowAux.add(eFeature);
430

    
431
                // dre.editing();
432
                // dre.setGeometry(geom2);
433
                // DefaultFeature dfLine2 = (DefaultFeature)
434
                // dre.getLinkedRow().cloneRow();
435
                // dfLine2.setGeometry(geom2);
436
                // int indexLine2 = addGeometry(dre);
437

    
438
                // selectedRowAux.add(new DefaultRowEdited(dfLine2,
439
                // IRowEdited.STATUS_ADDED, indexLine2));
440

    
441
            }
442

    
443
            // vea.removeRow(dre.getIndex(), getName(), EditionEvent.GRAPHIC);
444
            // vea.endComplexRow(getName());
445
            featureStore.endEditingGroup();
446
        } catch (DataException e) {
447
            // TODO Auto-generated catch block
448
            e.printStackTrace();
449
        }
450
        // vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
451
        // selectedRowAux);
452

    
453
    }
454

    
455
    private Point2D[] getOrderPoints(Geometry geomAux) {
456
        PathIterator theIterator =
457
            geomAux.getPathIterator(null, geomManager.getFlatness());
458
        double[] theData = new double[6];
459
        Point2D previous = null;
460
        ArrayList points = new ArrayList();
461
        boolean isFirstPointBreak = false;
462
        boolean isSecondPointBreak = false;
463
        while (!theIterator.isDone()) {
464
            int theType = theIterator.currentSegment(theData);
465
            switch (theType) {
466

    
467
            case PathIterator.SEG_MOVETO:
468
                previous = new Point2D.Double(theData[0], theData[1]);
469
                break;
470

    
471
            case PathIterator.SEG_LINETO:
472

    
473
                if (previous != null) {
474
                    GeneralPathX gpx = new GeneralPathX();
475
                    gpx.moveTo(previous.getX(), previous.getY());
476
                    gpx.lineTo(theData[0], theData[1]);
477
                    Geometry geom = createCurve(gpx);
478
                    boolean intersectFirst = intersects(geom, firstPoint);
479
                    boolean intersectSecond = intersects(geom, secondPoint);
480
                    if (intersectFirst && intersectSecond && !isFirstPointBreak) {
481
                        isFirstPointBreak = true;
482
                        isSecondPointBreak = true;
483
                        points.add(getNearPoint(previous));
484
                        points.add(getDistantPoint(previous));
485
                        return (Point2D[]) points.toArray(new Point2D[0]);
486
                    } else
487
                        if (intersectFirst && !isFirstPointBreak) {
488
                            isFirstPointBreak = true;
489
                            points.add(firstPoint);
490
                        } else
491
                            if (intersectSecond && !isSecondPointBreak) {
492
                                isSecondPointBreak = true;
493
                                points.add(secondPoint);
494
                            }
495
                }
496
                previous = new Point2D.Double(theData[0], theData[1]);
497
                break;
498

    
499
            case PathIterator.SEG_QUADTO:
500
                if (previous != null) {
501
                    GeneralPathX gpx = new GeneralPathX();
502
                    gpx.moveTo(previous.getX(), previous.getY());
503
                    gpx.quadTo(theData[0], theData[1], theData[2], theData[3]);
504
                    Geometry geom = createCurve(gpx);
505
                    boolean intersectFirst = intersects(geom, firstPoint);
506
                    boolean intersectSecond = intersects(geom, secondPoint);
507
                    if (intersectFirst && intersectSecond && !isFirstPointBreak) {
508
                        isFirstPointBreak = true;
509
                        isSecondPointBreak = true;
510
                        points.add(getNearPoint(previous));
511
                        points.add(getDistantPoint(previous));
512
                        return (Point2D[]) points.toArray(new Point2D[0]);
513
                    } else
514
                        if (intersectFirst && !isFirstPointBreak) {
515
                            isFirstPointBreak = true;
516
                            points.add(firstPoint);
517
                        } else
518
                            if (intersectSecond && !isSecondPointBreak) {
519
                                isSecondPointBreak = true;
520
                                points.add(secondPoint);
521
                            }
522
                }
523
                previous = new Point2D.Double(theData[0], theData[1]);
524

    
525
                break;
526

    
527
            case PathIterator.SEG_CUBICTO:
528
                if (previous != null) {
529
                    GeneralPathX gpx = new GeneralPathX();
530
                    gpx.moveTo(previous.getX(), previous.getY());
531
                    gpx.curveTo(theData[0], theData[1], theData[2], theData[3],
532
                        theData[4], theData[5]);
533
                    Geometry geom = createCurve(gpx);
534
                    boolean intersectFirst = intersects(geom, firstPoint);
535
                    boolean intersectSecond = intersects(geom, secondPoint);
536
                    if (intersectFirst && intersectSecond && !isFirstPointBreak) {
537
                        isFirstPointBreak = true;
538
                        isSecondPointBreak = true;
539
                        points.add(getNearPoint(previous));
540
                        points.add(getDistantPoint(previous));
541
                        return (Point2D[]) points.toArray(new Point2D[0]);
542
                    } else
543
                        if (intersectFirst && !isFirstPointBreak) {
544
                            isFirstPointBreak = true;
545
                            points.add(firstPoint);
546
                        } else
547
                            if (intersectSecond && !isSecondPointBreak) {
548
                                isSecondPointBreak = true;
549
                                points.add(secondPoint);
550
                            }
551
                }
552
                previous = new Point2D.Double(theData[0], theData[1]);
553

    
554
                break;
555

    
556
            case PathIterator.SEG_CLOSE:
557
                // if (isFirstPart)
558
                // newGp1.closePath();
559
                // else
560
                // newGp2.closePath();
561
                break;
562
            } // end switch
563

    
564
            theIterator.next();
565
        } // end while loop
566

    
567
        return (Point2D[]) points.toArray(new Point2D[0]);
568
    }
569

    
570
    private Point2D getDistantPoint(Point2D previous) {
571
        if (firstPoint.distance(previous) > secondPoint.distance(previous)) {
572
            return firstPoint;
573
        }
574
        return secondPoint;
575
    }
576

    
577
    private Point2D getNearPoint(Point2D previous) {
578
        if (firstPoint.distance(previous) <= secondPoint.distance(previous)) {
579
            return firstPoint;
580
        }
581
        return secondPoint;
582
    }
583

    
584
    /**
585
     * M?todo para dibujar la lo necesario para el estado en el que nos
586
     * encontremos.
587
     * 
588
     * @param g
589
     *            Graphics sobre el que dibujar.
590
     * @param x
591
     *            par?metro x del punto que se pase para dibujar.
592
     * @param y
593
     *            par?metro x del punto que se pase para dibujar.
594
     */
595
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
596
        VectorialLayerEdited vle = getVLE();
597
        FeatureSet selection = null;
598
        DisposableIterator iterator = null;
599
        try {
600
            selection = (FeatureSet) vle.getFeatureStore().getSelection();
601

    
602
            ViewPort vp =
603
                CADExtension.getEditionManager().getMapControl().getViewPort();
604
            if (selection.getSize() == 1) {
605
                if (firstPoint != null) {
606
                    Geometry g1 =
607
                        createCircle(createPoint(firstPoint),
608
                            vp.toMapDistance(3));
609
                    Geometry g2 =
610
                        createCircle(createPoint(firstPoint),
611
                            vp.toMapDistance(5));
612

    
613
                    renderer.draw(g1,
614
                        mapControlManager.getAxisReferenceSymbol());
615
                    renderer.draw(g2,
616
                        mapControlManager.getAxisReferenceSymbol());
617
                }
618
                iterator = selection.iterator();
619
                rowEdited = (Feature) iterator.next();
620
                Geometry geom =
621
                    ((Geometry) rowEdited.getDefaultGeometry()).cloneGeometry();
622
                if (intersects(geom, new Point2D.Double(x, y))) {
623
                    renderer.draw(geom,
624
                        mapControlManager.getGeometrySelectionSymbol());
625
                }
626
            }
627
        } catch (ReadException e1) {
628
            // TODO Auto-generated catch block
629
            e1.printStackTrace();
630
        } catch (DataException e) {
631
            // TODO Auto-generated catch block
632
            e.printStackTrace();
633
        } finally {
634
            if (iterator != null) {
635
                iterator.dispose();
636
            }
637
        }
638

    
639
    }
640

    
641
    public boolean intersects(double x, double y) {
642
        Point2D p = new Point2D.Double(x, y);
643
        VectorialLayerEdited vle = getVLE();
644
        FeatureSet selection = null;
645
        DisposableIterator iterator = null;
646
        try {
647
            selection = (FeatureSet) vle.getFeatureStore().getSelection();
648

    
649
            if (selection.getSize() == 1) {
650
                iterator = selection.iterator();
651
                rowEdited = (Feature) iterator.next();
652
                Geometry g =
653
                    ((Geometry) rowEdited.getDefaultGeometry()).cloneGeometry();
654
                return intersects(g, p);
655
            }
656
        } catch (ReadException e) {
657
            // TODO Auto-generated catch block
658
            e.printStackTrace();
659
        } catch (DataException e) {
660
            // TODO Auto-generated catch block
661
            e.printStackTrace();
662
        } finally {
663
            if (iterator != null) {
664
                iterator.dispose();
665
            }
666
        }
667
        return false;
668
    }
669

    
670
    private boolean intersects(Geometry geom, Point2D p) {
671
        double tol = 1;
672
        tol =
673
            CADExtension.getEditionManager().getMapControl().getViewPort()
674
                .toMapDistance((int) tol);
675
        Rectangle2D r =
676
            new Rectangle2D.Double(p.getX() - tol / 2, p.getY() - tol / 2, tol,
677
                tol);
678
        return (geom.intersects(r) && !geom.getShape().contains(r));
679
    }
680

    
681
    /**
682
     * Add a diferent option.
683
     * 
684
     * @param s
685
     *            Diferent option.
686
     */
687
    public void addOption(String s) {
688
        if (s.equals(PluginServices.getText(this, "cancel")) || s.equals("c")
689
            || s.equals("C")) {
690
            init();
691
        }
692
    }
693

    
694
    /*
695
     * (non-Javadoc)
696
     * 
697
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
698
     */
699
    public void addValue(double d) {
700
    }
701

    
702
    public String getName() {
703
        return PluginServices.getText(this, "break_");
704
    }
705

    
706
    public String toString() {
707
        return "_break";
708
    }
709

    
710
    @Override
711
    protected int getSupportedPrimitiveGeometryType() {
712
        return CURVE;
713
    }
714

    
715
}