Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / BreakCADTool.java @ 8936

History | View | Annotate | Download (30.5 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.gui.cad.tools;
42

    
43
import java.awt.Graphics;
44
import java.awt.Graphics2D;
45
import java.awt.event.InputEvent;
46
import java.awt.geom.PathIterator;
47
import java.awt.geom.Point2D;
48
import java.awt.geom.Rectangle2D;
49
import java.io.IOException;
50
import java.util.ArrayList;
51

    
52
import com.iver.andami.PluginServices;
53
import com.iver.cit.gvsig.CADExtension;
54
import com.iver.cit.gvsig.fmap.ViewPort;
55
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
56
import com.iver.cit.gvsig.fmap.core.FShape;
57
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
58
import com.iver.cit.gvsig.fmap.core.IGeometry;
59
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
60
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
61
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
62
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
63
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
64
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
65
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
66
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
67
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
68
import com.iver.cit.gvsig.gui.cad.tools.smc.BreakCADToolContext;
69
import com.iver.cit.gvsig.gui.cad.tools.smc.BreakCADToolContext.BreakCADToolState;
70
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
71

    
72

    
73
/**
74
 * Recorta una polil?nea en dos partes.
75
 *
76
 * @author Vicente Caballero Navarro
77
 */
78
public class BreakCADTool extends DefaultCADTool {
79
    private BreakCADToolContext _fsm;
80
    private Point2D firstPoint;
81
    private Point2D secondPoint;
82
    private DefaultRowEdited rowEdited;
83
    /**
84
     * Crea un nuevo PolylineCADTool.
85
     */
86
    public BreakCADTool() {
87
    }
88

    
89
    /**
90
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
91
     * carga previa a la utilizaci?n de la herramienta.
92
     */
93
    public void init() {
94
        _fsm = new BreakCADToolContext(this);
95
        firstPoint=null;
96
        secondPoint=null;
97
    }
98

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

    
106
    /* (non-Javadoc)
107
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
108
     */
109
    public void transition(double d) {
110
        _fsm.addValue(d);
111
    }
112

    
113
    /* (non-Javadoc)
114
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
115
     */
116
    public void transition(String s) throws CommandException {
117
        if (!super.changeCommand(s)){
118
            _fsm.addOption(s);
119
        }
120
    }
121

    
122
    /**
123
     * DOCUMENT ME!
124
     */
125
    public void selection() {
126
        ArrayList selectedRow=getSelectedRows();
127
        if (selectedRow.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
128
            CADExtension.setCADTool("_selection",false);
129
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
130
                "_break");
131
        }
132
    }
133

    
134
    /**
135
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
136
     * editableFeatureSource que ya estar? creado.
137
     *
138
     * @param x par?metro x del punto que se pase en esta transici?n.
139
     * @param y par?metro y del punto que se pase en esta transici?n.
140
     */
141
    public void addPoint(double x, double y,InputEvent event) {
142
         BreakCADToolState actualState = (BreakCADToolState) _fsm.getPreviousState();
143
         String status = actualState.getName();
144

    
145
         if (status.equals("Break.FirstPoint")) {
146
            // if (rowEdited!=null && intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new Point2D.Double(x,y)))
147
                 firstPoint=new Point2D.Double(x,y);
148

    
149
         } else if (status.equals("Break.SecondPoint")) {
150
          // if (rowEdited !=null && intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new Point2D.Double(x,y))){
151
               secondPoint=new Point2D.Double(x,y);
152
               try {
153
//                           IGeometry geom=((DefaultFeature)rowEdited.getLinkedRow()).getGeometry();
154
//                           if (geom instanceof FGeometryCollection) {
155
//                             breakGeometryGC(rowEdited);
156
//                        }else {
157
                    breakGeometry(rowEdited);
158
//                                }
159
            } catch (IOException e) {
160
                e.printStackTrace();
161
            } catch (DriverIOException e) {
162
                e.printStackTrace();
163
            }
164
           }
165
         //}
166
    }
167
  /*  private void breakGeometryGC(DefaultRowEdited dre) throws IOException, DriverIOException {
168
        GeneralPathX newGp1 = new GeneralPathX();
169
        GeneralPathX newGp2 = new GeneralPathX();
170
        FGeometryCollection gc=(FGeometryCollection)((DefaultFeature)rowEdited.getLinkedRow()).getGeometry();
171
        IGeometry[] geoms=gc.getGeometries();
172
        for (int i = 0;i<geoms.length;i++) {
173
            PathIterator theIterator=geoms[i].getPathIterator(null);
174
            double[] theData = new double[6];
175
            boolean isFirstPart=true;
176
            boolean isCut=false;
177
            int theType;
178
            int numParts = 0;
179

180
            Point2D previous=null;
181

182
            while (!theIterator.isDone()) {
183
                theType = theIterator.currentSegment(theData);
184
                switch (theType) {
185

186
                    case PathIterator.SEG_MOVETO:
187
                        numParts++;
188
                        previous=new Point2D.Double(theData[0], theData[1]);
189
                        if (isFirstPart)
190
                            newGp1.moveTo(theData[0], theData[1]);
191
                        else
192
                            newGp2.moveTo(theData[0], theData[1]);
193
                        break;
194

195
                    case PathIterator.SEG_LINETO:
196
                         if (previous!=null){
197
                                GeneralPathX gpx=new GeneralPathX();
198
                                gpx.moveTo(previous.getX(),previous.getY());
199
                                gpx.lineTo(theData[0], theData[1]);
200
                                IGeometry geom=ShapeFactory.createPolyline2D(gpx);
201
                                Point2D p1=getNearPoint(previous);
202
                                Point2D p2=getDistantPoint(previous);
203
                                if (intersects(geom,p1)){
204
                                    isFirstPart=false;
205
                                    newGp1.lineTo(p1.getX(),p1.getY());
206
                                    newGp2.moveTo(p2.getX(),p2.getY());
207
                                    isCut=true;
208
                                }
209
                            }
210
                         previous=new Point2D.Double(theData[0], theData[1]);
211
                        if (isFirstPart)
212
                            newGp1.lineTo(theData[0], theData[1]);
213
                        else
214
                            newGp2.lineTo(theData[0], theData[1]);
215
                        break;
216

217
                    case PathIterator.SEG_QUADTO:
218
                         if (previous!=null){
219
                                GeneralPathX gpx=new GeneralPathX();
220
                                gpx.moveTo(previous.getX(),previous.getY());
221
                                gpx.quadTo(theData[0], theData[1],theData[2], theData[3]);
222
                                IGeometry geom=ShapeFactory.createPolyline2D(gpx);
223
                                Point2D p1=getNearPoint(previous);
224
                                Point2D p2=getDistantPoint(previous);
225
                                if (intersects(geom,p1)){
226
                                    isFirstPart=false;
227
                                    newGp1.lineTo(p1.getX(),p1.getY());
228
                                    newGp2.moveTo(p2.getX(),p2.getY());
229
                                    isCut=true;
230
                                }
231
                            }
232
                         previous=new Point2D.Double(theData[0], theData[1]);
233
                        if (isFirstPart)
234
                            newGp1.quadTo(theData[0], theData[1],theData[2], theData[3]);
235
                        else
236
                            newGp2.quadTo(theData[0], theData[1],theData[2], theData[3]);
237

238
                        break;
239

240
                    case PathIterator.SEG_CUBICTO:
241
                         if (previous!=null){
242
                                GeneralPathX gpx=new GeneralPathX();
243
                                gpx.moveTo(previous.getX(),previous.getY());
244
                                gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
245
                                IGeometry geom=ShapeFactory.createPolyline2D(gpx);
246
                                Point2D p1=getNearPoint(previous);
247
                                Point2D p2=getDistantPoint(previous);
248
                                if (intersects(geom,p1)){
249
                                    isFirstPart=false;
250
                                    newGp1.lineTo(p1.getX(),p1.getY());
251
                                    newGp2.moveTo(p2.getX(),p2.getY());
252
                                    isCut=true;
253
                                }
254
                            }
255
                         previous=new Point2D.Double(theData[0], theData[1]);
256
                        if (isFirstPart)
257
                            newGp1.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
258
                        else
259
                            newGp2.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
260

261
                        break;
262

263
                    case PathIterator.SEG_CLOSE:
264
                        //if (isFirstPart)
265
                        //        newGp1.closePath();
266
                        //else
267
                        //        newGp2.closePath();
268
                        break;
269
                } //end switch
270

271
                theIterator.next();
272
            } //end while loop
273

274
            if (isCut) {
275
                IGeometry geom1 = ShapeFactory.createPolyline2D(newGp1);
276
                IGeometry geom2 = ShapeFactory.createPolyline2D(newGp2);
277
                VectorialLayerEdited vle = getVLE();
278
                VectorialEditableAdapter vea = vle.getVEA();
279
                ArrayList selectedRow = vle.getSelectedRow();
280
                vea.startComplexRow();
281
                vea.removeRow(dre.getIndex(), getName(), EditionEvent.GRAPHIC);
282
                int num = vea.getRowCount();
283
                if (gc.isClosed()) {
284
                    ArrayList geomsAux1 = new ArrayList();
285
                    geomsAux1.add(geom2);
286
                    for (int k = i + 1; k < geoms.length; k++) {
287
                        geomsAux1.add(geoms[k]);
288
                    }
289
                    for (int k = 0; k < i; k++) {
290
                        geomsAux1.add(geoms[k]);
291
                    }
292
                    geomsAux1.add(geom1);
293

294
                    DefaultFeature df1 = new DefaultFeature(
295
                            new FGeometryCollection((IGeometry[]) geomsAux1
296
                                    .toArray(new IGeometry[0])), dre
297
                                    .getAttributes(), String.valueOf(num));
298
                    int index1 = vea.addRow(df1, PluginServices.getText(this,
299
                            "parte1"), EditionEvent.GRAPHIC);
300

301
                    clearSelection();
302
                    selectedRow.add(new DefaultRowEdited(df1,
303
                            IRowEdited.STATUS_ADDED, index1));
304
                    vea.endComplexRow();
305
                    return;
306
                }else {
307

308
                    ArrayList geomsAux1 = new ArrayList();
309
                    for (int k = 0; k < i; k++) {
310
                        geomsAux1.add(geoms[k]);
311
                    }
312
                    geomsAux1.add(geom1);
313

314
                    ArrayList geomsAux2 = new ArrayList();
315
                    geomsAux2.add(geom2);
316
                    for (int k = i + 1; k < geoms.length; k++) {
317
                        geomsAux2.add(geoms[k]);
318
                    }
319

320
                    DefaultFeature df1 = new DefaultFeature(
321
                            new FGeometryCollection((IGeometry[]) geomsAux1
322
                                    .toArray(new IGeometry[0])), dre
323
                                    .getAttributes(), String.valueOf(num));
324
                    int index1 = vea.addRow(df1, PluginServices.getText(this,
325
                            "parte1"), EditionEvent.GRAPHIC);
326
                    DefaultFeature df2 = new DefaultFeature(
327
                            new FGeometryCollection((IGeometry[]) geomsAux2
328
                                .toArray(new IGeometry[0])), dre
329
                                .getAttributes(), String.valueOf(num + 1));
330
                    int index2 = vea.addRow(df2, PluginServices.getText(this,
331
                        "parte2"), EditionEvent.GRAPHIC);
332
                    clearSelection();
333
                    selectedRow.add(new DefaultRowEdited(df2,
334
                            IRowEdited.STATUS_ADDED, index2));
335
                    selectedRow.add(new DefaultRowEdited(df1,
336
                            IRowEdited.STATUS_ADDED, index1));
337

338
                    vea.endComplexRow();
339
                    return;
340
                }
341

342
            }
343
        }
344
    }
345
*/
346

    
347
    private void breakGeometry(DefaultRowEdited dre) throws IOException, DriverIOException {
348
        breakGeom(dre);
349
    }
350

    
351

    
352
        private void breakGeom(DefaultRowEdited dre) throws DriverIOException, IOException {
353
            GeneralPathX newGp1 = new GeneralPathX();
354
        GeneralPathX newGp2 = new GeneralPathX();
355
        IGeometry geomAux=((DefaultFeature)rowEdited.getLinkedRow()).getGeometry();
356
            PathIterator theIterator=geomAux.getPathIterator(null,FConverter.FLATNESS);
357
        Point2D[] pointsOrdered=getOrderPoints(geomAux);
358
        double[] theData = new double[6];
359
        boolean isFirstPart=true;
360
        boolean intersectsP2=false;
361
        int theType;
362
        int numParts = 0;
363
        boolean isBreaked=false;
364
        Point2D previous=null;
365

    
366
            while (!theIterator.isDone()) {
367
                theType = theIterator.currentSegment(theData);
368
                switch (theType) {
369

    
370
                    case PathIterator.SEG_MOVETO:
371
                        numParts++;
372

    
373
                        previous=new Point2D.Double(theData[0], theData[1]);
374

    
375
                        if (isFirstPart)
376
                            newGp1.moveTo(theData[0], theData[1]);
377
                        else
378
                            newGp2.moveTo(theData[0], theData[1]);
379
                        break;
380

    
381
                    case PathIterator.SEG_LINETO:
382

    
383
                        if (previous!=null){
384
                                GeneralPathX gpx=new GeneralPathX();
385
                                gpx.moveTo(previous.getX(),previous.getY());
386
                                gpx.lineTo(theData[0], theData[1]);
387
                                IGeometry geom=ShapeFactory.createPolyline2D(gpx);
388
                                Point2D p1=pointsOrdered[0];
389
                                Point2D p2=pointsOrdered[1];
390

    
391
                                if (intersects(geom,p1) && !isBreaked){
392
                                        isFirstPart=false;
393
                                    newGp1.lineTo(p1.getX(),p1.getY());
394
                                }
395
                                if (intersects(geom,p2) && !isBreaked) {
396
                                        isBreaked=true;
397
                                    intersectsP2=true;
398
                                    newGp2.moveTo(p2.getX(),p2.getY());
399
                                }
400
                            }
401
                         previous=new Point2D.Double(theData[0], theData[1]);
402
                        if (isFirstPart)
403
                            newGp1.lineTo(theData[0], theData[1]);
404
                        else if (intersectsP2){
405
                            newGp2.lineTo(theData[0], theData[1]);
406
                        }
407

    
408
                        break;
409

    
410
                    case PathIterator.SEG_QUADTO:
411
                         if (previous!=null){
412
                                GeneralPathX gpx=new GeneralPathX();
413
                                gpx.moveTo(previous.getX(),previous.getY());
414
                                gpx.quadTo(theData[0], theData[1],theData[2], theData[3]);
415
                                IGeometry geom=ShapeFactory.createPolyline2D(gpx);
416
                                Point2D p1=pointsOrdered[0];
417
                                Point2D p2=pointsOrdered[1];
418
                                if (intersects(geom,p1) && !isBreaked){
419
                                        isFirstPart=false;
420
                                    newGp1.lineTo(p1.getX(),p1.getY());
421
                                }
422
                                if (intersects(geom,p2) && !isBreaked) {
423
                                        isBreaked=true;
424
                                    intersectsP2=true;
425
                                    newGp2.moveTo(p2.getX(),p2.getY());
426

    
427
                                }
428
                            }
429
                         previous=new Point2D.Double(theData[0], theData[1]);
430
                        if (isFirstPart)
431
                            newGp1.quadTo(theData[0], theData[1],theData[2], theData[3]);
432
                        else
433
                            newGp2.quadTo(theData[0], theData[1],theData[2], theData[3]);
434

    
435
                        break;
436

    
437
                    case PathIterator.SEG_CUBICTO:
438
                         if (previous!=null){
439
                                GeneralPathX gpx=new GeneralPathX();
440
                                gpx.moveTo(previous.getX(),previous.getY());
441
                                gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
442
                                IGeometry geom=ShapeFactory.createPolyline2D(gpx);
443
                                Point2D p1=pointsOrdered[0];
444
                                Point2D p2=pointsOrdered[1];
445
                                if (intersects(geom,p1) && !isBreaked){
446
                                        isFirstPart=false;
447
                                    newGp1.lineTo(p1.getX(),p1.getY());
448
                                }
449
                                if (intersects(geom,p2) && !isBreaked) {
450
                                        isBreaked=true;
451
                                    intersectsP2=true;
452
                                    newGp2.moveTo(p2.getX(),p2.getY());
453

    
454
                                }
455
                          }
456
                         previous=new Point2D.Double(theData[0], theData[1]);
457
                        if (isFirstPart)
458
                            newGp1.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
459
                        else
460
                            newGp2.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
461

    
462
                        break;
463

    
464
                    case PathIterator.SEG_CLOSE:
465
                        //if (isFirstPart)
466
                        //        newGp1.closePath();
467
                        //else
468
                        //        newGp2.closePath();
469
                        break;
470
                } //end switch
471

    
472
                theIterator.next();
473
            } //end while loop
474
            GeneralPathX gpx=new GeneralPathX();
475
            gpx.append(geomAux.getInternalShape(),true);
476
            VectorialLayerEdited vle = getVLE();
477
            VectorialEditableAdapter vea = vle.getVEA();
478
            ArrayList selectedRowAux=new ArrayList();
479
            vea.startComplexRow();
480
            if (gpx.isClosed()) {
481

    
482
                newGp2.append(newGp1.getPathIterator(null,FConverter.FLATNESS),true);
483
                IGeometry geom1=ShapeFactory.createPolyline2D(newGp2);
484

    
485
                DefaultFeature dfLine1 = (DefaultFeature) dre.getLinkedRow().cloneRow();
486
                    dfLine1.setGeometry(geom1);
487
                                int indexLine1 = addGeometry(geom1, dfLine1
488
                                                .getAttributes());
489

    
490
                                selectedRowAux.add(new DefaultRowEdited(dfLine1,
491
                                                IRowEdited.STATUS_ADDED, indexLine1));
492

    
493
                         }else {
494
                    IGeometry geom1=ShapeFactory.createPolyline2D(newGp1);
495
                    IGeometry geom2=ShapeFactory.createPolyline2D(newGp2);
496

    
497
                    DefaultFeature dfLine1 = (DefaultFeature) dre.getLinkedRow().cloneRow();
498
                    dfLine1.setGeometry(geom1);
499
                                int indexLine1 = addGeometry(geom1, dfLine1
500
                                                .getAttributes());
501

    
502
                                selectedRowAux.add(new DefaultRowEdited(dfLine1,
503
                                                IRowEdited.STATUS_ADDED, indexLine1));
504

    
505
                                DefaultFeature dfLine2 = (DefaultFeature) dre.getLinkedRow().cloneRow();
506
                        dfLine2.setGeometry(geom2);
507
                                int indexLine2 = addGeometry(geom2, dfLine2
508
                                                        .getAttributes());
509

    
510
                                selectedRowAux.add(new DefaultRowEdited(dfLine2,
511
                                                        IRowEdited.STATUS_ADDED, indexLine2));
512

    
513
            }
514
                vea.removeRow(dre.getIndex(), getName(), EditionEvent.GRAPHIC);
515
                          vea.endComplexRow(getName());
516
                        vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
517

    
518
    }
519

    
520

    
521
    private Point2D[] getOrderPoints(IGeometry geomAux) {
522
            PathIterator theIterator=geomAux.getPathIterator(null,FConverter.FLATNESS);
523
            double[] theData = new double[6];
524
        Point2D previous=null;
525
        ArrayList points =new ArrayList();
526
        boolean isFirstPointBreak=false;
527
        boolean isSecondPointBreak=false;
528
        while (!theIterator.isDone()) {
529
            int theType = theIterator.currentSegment(theData);
530
            switch (theType) {
531

    
532
                case PathIterator.SEG_MOVETO:
533
                    previous=new Point2D.Double(theData[0], theData[1]);
534
                    break;
535

    
536
                case PathIterator.SEG_LINETO:
537

    
538
                    if (previous!=null){
539
                            GeneralPathX gpx=new GeneralPathX();
540
                            gpx.moveTo(previous.getX(),previous.getY());
541
                            gpx.lineTo(theData[0], theData[1]);
542
                            IGeometry geom=ShapeFactory.createPolyline2D(gpx);
543
                            boolean intersectFirst=intersects(geom,firstPoint);
544
                            boolean intersectSecond=intersects(geom,secondPoint);
545
                            if (intersectFirst && intersectSecond && !isFirstPointBreak){
546
                                    isFirstPointBreak=true;
547
                                    isSecondPointBreak=true;
548
                                    points.add(getNearPoint(previous));
549
                                points.add(getDistantPoint(previous));
550
                                return (Point2D[])points.toArray(new Point2D[0]);
551
                            }else if (intersectFirst && !isFirstPointBreak) {
552
                                    isFirstPointBreak=true;
553
                                points.add(firstPoint);
554
                            }else if (intersectSecond && !isSecondPointBreak) {
555
                                    isSecondPointBreak=true;
556
                                    points.add(secondPoint);
557
                            }
558
                        }
559
                     previous=new Point2D.Double(theData[0], theData[1]);
560
                                      break;
561

    
562
                case PathIterator.SEG_QUADTO:
563
                     if (previous!=null){
564
                            GeneralPathX gpx=new GeneralPathX();
565
                            gpx.moveTo(previous.getX(),previous.getY());
566
                            gpx.quadTo(theData[0], theData[1],theData[2], theData[3]);
567
                            IGeometry geom=ShapeFactory.createPolyline2D(gpx);
568
                            boolean intersectFirst=intersects(geom,firstPoint);
569
                            boolean intersectSecond=intersects(geom,secondPoint);
570
                            if (intersectFirst && intersectSecond && !isFirstPointBreak){
571
                                    isFirstPointBreak=true;
572
                                    isSecondPointBreak=true;
573
                                points.add(getNearPoint(previous));
574
                                points.add(getDistantPoint(previous));
575
                                return (Point2D[])points.toArray(new Point2D[0]);
576
                            }else if (intersectFirst && !isFirstPointBreak){
577
                                    isFirstPointBreak=true;
578
                                    points.add(firstPoint);
579
                            }else if (intersectSecond && !isSecondPointBreak){
580
                                    isSecondPointBreak=true;
581
                                    points.add(secondPoint);
582
                            }
583
                     }
584
                     previous=new Point2D.Double(theData[0], theData[1]);
585

    
586
                    break;
587

    
588
                case PathIterator.SEG_CUBICTO:
589
                     if (previous!=null){
590
                            GeneralPathX gpx=new GeneralPathX();
591
                            gpx.moveTo(previous.getX(),previous.getY());
592
                            gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
593
                            IGeometry geom=ShapeFactory.createPolyline2D(gpx);
594
                            boolean intersectFirst=intersects(geom,firstPoint);
595
                            boolean intersectSecond=intersects(geom,secondPoint);
596
                            if (intersectFirst && intersectSecond && !isFirstPointBreak){
597
                                    isFirstPointBreak=true;
598
                                    isSecondPointBreak=true;
599
                                    points.add(getNearPoint(previous));
600
                                points.add(getDistantPoint(previous));
601
                                return (Point2D[])points.toArray(new Point2D[0]);
602
                            }else if (intersectFirst && !isFirstPointBreak){
603
                                    isFirstPointBreak=true;
604
                                    points.add(firstPoint);
605
                            }else if (intersectSecond && !isSecondPointBreak){
606
                                    isSecondPointBreak=true;
607
                                points.add(secondPoint);
608
                            }
609
                     }
610
                     previous=new Point2D.Double(theData[0], theData[1]);
611

    
612
                    break;
613

    
614
                case PathIterator.SEG_CLOSE:
615
                    //if (isFirstPart)
616
                    //        newGp1.closePath();
617
                    //else
618
                    //        newGp2.closePath();
619
                    break;
620
            } //end switch
621

    
622
            theIterator.next();
623
        } //end while loop
624

    
625
        return (Point2D[])points.toArray(new Point2D[0]);
626
    }
627
    private Point2D getDistantPoint(Point2D previous){
628
        if (firstPoint.distance(previous)>secondPoint.distance(previous)){
629
            return firstPoint;
630
        }
631
        return secondPoint;
632
    }
633
private Point2D getNearPoint(Point2D previous) {
634
        if (firstPoint.distance(previous)<=secondPoint.distance(previous)){
635
        return firstPoint;
636
    }
637
    return secondPoint;
638
}
639
    /**
640
     * M?todo para dibujar la lo necesario para el estado en el que nos
641
     * encontremos.
642
     *
643
     * @param g Graphics sobre el que dibujar.
644
     * @param x par?metro x del punto que se pase para dibujar.
645
     * @param y par?metro x del punto que se pase para dibujar.
646
     */
647
    public void drawOperation(Graphics g, double x, double y) {
648
         VectorialLayerEdited vle=getVLE();
649
         ArrayList selectedRows=vle.getSelectedRow();
650
         ViewPort vp=CADExtension.getEditionManager().getMapControl().getViewPort();
651
         if (selectedRows.size()==1){
652
             if (firstPoint!=null){
653
                 IGeometry g1=ShapeFactory.createCircle(firstPoint,vp.toMapDistance(3));
654
                 IGeometry g2=ShapeFactory.createCircle(firstPoint,vp.toMapDistance(5));
655
                 g1.draw((Graphics2D)g,vp,DefaultCADTool.modifySymbol);
656
                 g2.draw((Graphics2D)g,vp,DefaultCADTool.modifySymbol);
657
             }
658
             rowEdited = (DefaultRowEdited)selectedRows.get(0);
659
                 //.getLinkedRow()
660
                 //        .cloneRow();
661
             IGeometry geom=((DefaultFeature)rowEdited.getLinkedRow()).getGeometry().cloneGeometry();
662
             if (intersects(geom,new Point2D.Double(x,y))){
663
                 geom.draw((Graphics2D)g,vp,DefaultCADTool.drawingSymbol);
664
             }
665
         }
666
    }
667
    public boolean intersects(double x,double y){
668
        Point2D p = new Point2D.Double(x, y);
669
        VectorialLayerEdited vle = getVLE();
670
        ArrayList selectedRows = vle.getSelectedRow();
671
        if (selectedRows.size() == 1) {
672
            rowEdited = (DefaultRowEdited) selectedRows
673
                    .get(0);
674
            IGeometry g = ((DefaultFeature) rowEdited.getLinkedRow())
675
                    .getGeometry().cloneGeometry();
676
            return intersects(g, p);
677
        }
678
        return false;
679
    }
680
    private boolean intersects(IGeometry geom, Point2D p){
681
        double tol = 1;
682
        tol = CADExtension.getEditionManager().getMapControl().getViewPort()
683
                .toMapDistance((int) tol);
684
        Rectangle2D r = new Rectangle2D.Double(p.getX() - tol / 2, p.getY()
685
                - tol / 2, tol, tol);
686
        return (geom.intersects(r) && !geom.contains(r));
687
    }
688
    /**
689
     * Add a diferent option.
690
     *
691
     * @param s Diferent option.
692
     */
693
    public void addOption(String s) {
694
        if (s.equals(PluginServices.getText(this,"cancel"))|| s.equals("c")|| s.equals("C")){
695
            init();
696
        }
697
    }
698
    /* (non-Javadoc)
699
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
700
     */
701
    public void addValue(double d) {
702
    }
703
    public String getName() {
704
        return PluginServices.getText(this,"break_");
705
    }
706

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

    
711
    public boolean isApplicable(int shapeType) {
712
        switch (shapeType) {
713
        case FShape.MULTI:
714
        case FShape.LINE:
715
            return true;
716
        }
717
        return false;
718
    }
719

    
720

    
721
}