Statistics
| Revision:

root / tags / v1_1_Build_1002 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / BreakCADTool.java @ 12070

History | View | Annotate | Download (31.2 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.awt.image.BufferedImage;
50
import java.io.IOException;
51
import java.util.ArrayList;
52

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

    
75

    
76
/**
77
 * DOCUMENT ME!
78
 *
79
 * @author Vicente Caballero Navarro
80
 */
81
public class BreakCADTool extends DefaultCADTool {
82
    private BreakCADToolContext _fsm;
83
    private Point2D firstPoint;
84
    private Point2D secondPoint;
85
    private DefaultRowEdited rowEdited;
86
    /**
87
     * Crea un nuevo PolylineCADTool.
88
     */
89
    public BreakCADTool() {
90
    }
91

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

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

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

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

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

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

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

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

183
            Point2D previous=null;
184

185
            while (!theIterator.isDone()) {
186
                theType = theIterator.currentSegment(theData);
187
                switch (theType) {
188

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

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

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

241
                        break;
242

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

264
                        break;
265

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

274
                theIterator.next();
275
            } //end while loop
276

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

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

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

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

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

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

341
                    vea.endComplexRow();
342
                    return;
343
                }
344

345
            }
346
        }
347
    }
348
*/
349

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

    
362
        Point2D previous=null;
363

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

    
368
                    case PathIterator.SEG_MOVETO:
369
                        numParts++;
370

    
371
                        previous=new Point2D.Double(theData[0], theData[1]);
372

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

    
379
                    case PathIterator.SEG_LINETO:
380

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

    
389
                                if (intersects(geom,p1)){
390
                                    isFirstPart=false;
391
                                    newGp1.lineTo(p1.getX(),p1.getY());
392
                                }
393
                                if (intersects(geom,p2)) {
394
                                    intersectsP2=true;
395
                                    newGp2.moveTo(p2.getX(),p2.getY());
396

    
397
                                }
398
                            }
399
                         previous=new Point2D.Double(theData[0], theData[1]);
400
                        if (isFirstPart)
401
                            newGp1.lineTo(theData[0], theData[1]);
402
                        else if (intersectsP2){
403
                            newGp2.lineTo(theData[0], theData[1]);
404
                        }
405

    
406
                        break;
407

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

    
428
                        break;
429

    
430
                    case PathIterator.SEG_CUBICTO:
431
                         if (previous!=null){
432
                                GeneralPathX gpx=new GeneralPathX();
433
                                gpx.moveTo(previous.getX(),previous.getY());
434
                                gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
435
                                IGeometry geom=ShapeFactory.createPolyline2D(gpx);
436
                                Point2D p1=pointsOrdered[0];
437
                                Point2D p2=pointsOrdered[1];
438
                                if (intersects(geom,p1)){
439
                                    isFirstPart=false;
440
                                    newGp1.lineTo(p1.getX(),p1.getY());
441
                                    newGp2.moveTo(p2.getX(),p2.getY());
442
                                }
443
                            }
444
                         previous=new Point2D.Double(theData[0], theData[1]);
445
                        if (isFirstPart)
446
                            newGp1.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
447
                        else
448
                            newGp2.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
449

    
450
                        break;
451

    
452
                    case PathIterator.SEG_CLOSE:
453
                        //if (isFirstPart)
454
                        //        newGp1.closePath();
455
                        //else
456
                        //        newGp2.closePath();
457
                        break;
458
                } //end switch
459

    
460
                theIterator.next();
461
            } //end while loop
462
            GeneralPathX gpx=new GeneralPathX();
463
            gpx.append(geomAux.getInternalShape(),true);
464
            if (gpx.isClosed()) {
465
                newGp2.append(newGp1.getPathIterator(null,FConverter.FLATNESS),true);
466
                IGeometry geom1=ShapeFactory.createPolyline2D(newGp2);
467
                VectorialLayerEdited vle = getVLE();
468
                VectorialEditableAdapter vea = vle.getVEA();
469
                int num;
470
                                try {
471
                                        num = vea.getNewIndex();
472
                                } catch (DriverException e) {
473
                                        throw new DriverIOException(e);
474
                                }
475
                DefaultFeature df1 = new DefaultFeature(geom1, dre.getAttributes(),String.valueOf(num));
476

    
477
                vea.startComplexRow();
478
                vea.removeRow(dre.getIndex(),getName(),EditionEvent.GRAPHIC);
479
                int index1=vea.addRow(df1,PluginServices.getText(this,"parte1"),EditionEvent.GRAPHIC);
480
                vea.endComplexRow(getName());
481
                ViewPort vp=CADExtension.getEditionManager().getMapControl().getViewPort();
482
                BufferedImage selectionImage = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(), BufferedImage.TYPE_INT_ARGB);
483
                Graphics2D gs = selectionImage.createGraphics();
484
                //clearSelection();
485
                ArrayList selectedRowAux = new ArrayList();//vle.getSelectedRow();
486
                selectedRowAux.add(new DefaultRowEdited(df1, IRowEdited.STATUS_ADDED, vea.getInversedIndex(index1)));
487
                vle.setSelectionCache(selectedRowAux);
488
                geom1.cloneGeometry().draw(gs, vp, DefaultCADTool.selectionSymbol);
489
                vle.drawHandlers(geom1.cloneGeometry(),gs,vp);
490
                vea.setSelectionImage(selectionImage);
491
            }else {
492
            IGeometry geom1=ShapeFactory.createPolyline2D(newGp1);
493
            IGeometry geom2=ShapeFactory.createPolyline2D(newGp2);
494
            VectorialLayerEdited vle = getVLE();
495
            VectorialEditableAdapter vea = vle.getVEA();
496
            int num;
497
                        try {
498
                                num = vea.getNewIndex();
499
                        } catch (DriverException e) {
500
                                throw new DriverIOException(e);
501
                        }
502
            Value[] values=dre.getAttributes();
503
            DefaultFeature df1 = new DefaultFeature(geom1,values,String.valueOf(num));
504
            DefaultFeature df2 = new DefaultFeature(geom2, values,String.valueOf(num+1));
505

    
506
            vea.startComplexRow();
507
            int index1=vea.addRow(df1,PluginServices.getText(this,"parte1"),EditionEvent.GRAPHIC);
508
            int index2=vea.addRow(df2,PluginServices.getText(this,"parte2"),EditionEvent.GRAPHIC);
509
            vea.removeRow(dre.getIndex(),getName(),EditionEvent.GRAPHIC);
510

    
511
            vea.endComplexRow(getName());
512
            ViewPort vp=CADExtension.getEditionManager().getMapControl().getViewPort();
513
            BufferedImage selectionImage = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(), BufferedImage.TYPE_INT_ARGB);
514
            Graphics2D gs = selectionImage.createGraphics();
515
            //clearSelection();
516
            ArrayList selectedRowAux = new ArrayList();//vle.getSelectedRow();
517
            selectedRowAux.add(new DefaultRowEdited(df1, IRowEdited.STATUS_ADDED, vea.getInversedIndex(index1)));
518
            selectedRowAux.add(new DefaultRowEdited(df2, IRowEdited.STATUS_ADDED, vea.getInversedIndex(index2)));
519
            vle.setSelectionCache(selectedRowAux);
520
            geom1.cloneGeometry().draw(gs, vp, DefaultCADTool.selectionSymbol);
521
            vle.drawHandlers(geom1.cloneGeometry(),gs,vp);
522
            geom2.cloneGeometry().draw(gs, vp, DefaultCADTool.selectionSymbol);
523
            vle.drawHandlers(geom2.cloneGeometry(),gs,vp);
524
            vea.setSelectionImage(selectionImage);
525
            }
526
    }
527

    
528
    private Point2D[] getOrderPoints(IGeometry geomAux) {
529
            PathIterator theIterator=geomAux.getPathIterator(null,FConverter.FLATNESS);
530
            double[] theData = new double[6];
531
        Point2D previous=null;
532
        ArrayList points =new ArrayList();
533
        while (!theIterator.isDone()) {
534
            int theType = theIterator.currentSegment(theData);
535
            switch (theType) {
536

    
537
                case PathIterator.SEG_MOVETO:
538
                    previous=new Point2D.Double(theData[0], theData[1]);
539
                    break;
540

    
541
                case PathIterator.SEG_LINETO:
542

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

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

    
587
                    break;
588

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

    
611
                    break;
612

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

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

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

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

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

    
719

    
720
}