Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / com / iver / cit / gvsig / gui / cad / tools / ExploitCADTool.java @ 27026

History | View | Annotate | Download (15.1 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.event.InputEvent;
45
import java.awt.geom.PathIterator;
46
import java.awt.geom.Point2D;
47
import java.util.ArrayList;
48
import java.util.Iterator;
49

    
50
import org.gvsig.fmap.dal.exception.DataException;
51
import org.gvsig.fmap.dal.exception.ReadException;
52
import org.gvsig.fmap.dal.feature.EditableFeature;
53
import org.gvsig.fmap.dal.feature.Feature;
54
import org.gvsig.fmap.dal.feature.FeatureSelection;
55
import org.gvsig.fmap.dal.feature.FeatureSet;
56
import org.gvsig.fmap.dal.feature.FeatureStore;
57
import org.gvsig.fmap.geom.Geometry;
58
import org.gvsig.fmap.geom.aggregate.impl.BaseMultiPrimitive;
59
import org.gvsig.fmap.geom.primitive.GeneralPathX;
60
import org.gvsig.fmap.geom.util.Converter;
61

    
62
import com.iver.andami.PluginServices;
63
import com.iver.andami.messages.NotificationManager;
64
import com.iver.cit.gvsig.CADExtension;
65
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
66
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.ExploitCADToolContext;
68
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
69
import com.vividsolutions.jts.geom.GeometryCollection;
70

    
71

    
72
/**
73
 * Elimina la geometr?a compuesta y a?ade todas la geometr?as simples que la
74
 * componen, implementado para polil?neas y pol?gonos.
75
 *
76
 * @author Vicente Caballero Navarro
77
 */
78
public class ExploitCADTool extends DefaultCADTool {
79
    protected ExploitCADToolContext _fsm;
80
    
81
    /**
82
     * Crea un nuevo ExploitCADTool.
83
     */
84
    public ExploitCADTool() {
85
    }
86

    
87
    /**
88
     * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
89
     * carga previa a la utilizaci?n de la herramienta.
90
     */
91
    public void init() {
92
        _fsm = new ExploitCADToolContext(this);
93
    }
94

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

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

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

    
118
    /**
119
     * DOCUMENT ME!
120
     */
121
    public void selection() {
122
            FeatureSet selection=null;
123
            try {
124
                    selection = (FeatureSet)getVLE().getFeatureStore().getSelection();
125

    
126
                    if (selection.getSize() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
127
                            CADExtension.setCADTool("_selection",false);
128
                            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
129
                            "_exploit");
130
                    }
131
            } catch (ReadException e) {
132
                    // TODO Auto-generated catch block
133
                    e.printStackTrace();
134
            } catch (DataException e) {
135
                        // TODO Auto-generated catch block
136
                        e.printStackTrace();
137
                }
138
    }
139

    
140
    /**
141
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
142
     * editableFeatureSource que ya estar? creado.
143
     *
144
     * @param x par?metro x del punto que se pase en esta transici?n.
145
     * @param y par?metro y del punto que se pase en esta transici?n.
146
     */
147
    public void addPoint(double x, double y,InputEvent event) {
148
    }
149

    
150
    /**
151
     * M?todo para dibujar la lo necesario para el estado en el que nos
152
     * encontremos.
153
     *
154
     * @param g Graphics sobre el que dibujar.
155
     * @param x par?metro x del punto que se pase para dibujar.
156
     * @param y par?metro x del punto que se pase para dibujar.
157
     */
158
    public void drawOperation(Graphics g, double x, double y) {
159
    }
160
    public void exploit() {
161
            VectorialLayerEdited vle = getVLE();
162
                try {
163
                        FeatureStore featureStore=vle.getFeatureStore();
164
                    Iterator iterator=((FeatureSelection)featureStore.getSelection()).iterator();
165
                        ArrayList selectedRowAux = new ArrayList();
166
                        featureStore.beginEditingGroup(getName());
167
                        while (iterator.hasNext()) {
168
                                Feature feature = (Feature) iterator.next();
169

    
170
                                Geometry geom=((Geometry)feature.getDefaultGeometry()).cloneGeometry();
171
                                featureStore.delete(feature);
172
                                if (geom instanceof GeometryCollection) {
173
                                        exploitGeometryCollection(feature,selectedRowAux);
174
                                }else {
175
                                        if (geom.getType()==Geometry.TYPES.CURVE) {
176
                                                exploitLine(feature,selectedRowAux);
177
                                        }else if (geom.getType()==Geometry.TYPES.SURFACE) {
178
                                                exploitPolygon(feature,selectedRowAux);
179
                                        }
180
                                }
181

    
182
                        }
183
                        featureStore.endEditingGroup();
184
                } catch (ReadException e) {
185
                        NotificationManager.addError(e.getMessage(),e);
186
                } catch (DataException e) {
187
                        NotificationManager.addError(e.getMessage(),e);
188
                }
189
        }
190
    private void exploitGeometryCollection(Feature feature,ArrayList selectedRowAux) {
191
            Geometry geom=((Geometry)feature.getDefaultGeometry()).cloneGeometry();
192
            BaseMultiPrimitive gc=(BaseMultiPrimitive)geom;
193
            int numGeoms=gc.getPrimitivesNumber();
194
            FeatureStore featureStore;
195
            try {
196
                    featureStore = getVLE().getFeatureStore();
197
                    for (int i=0;i<numGeoms;i++) {
198
                            Geometry primitiveGeom=(Geometry)gc.getPrimitiveAt(i);
199
                            EditableFeature eFeature=featureStore.createNewFeature(feature.getType(), feature);
200
                            eFeature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(),primitiveGeom);
201
                            selectedRowAux.add(feature);
202
                    }
203
            } catch (ReadException e) {
204
                    // TODO Auto-generated catch block
205
                    e.printStackTrace();
206
            } catch (DataException e) {
207
                        // TODO Auto-generated catch block
208
                        e.printStackTrace();
209
                }
210
            refresh();
211
        }
212
    private void exploitLine(Feature feature,ArrayList selectedRowAux){
213
            GeneralPathX newGp1 = new GeneralPathX();
214
//            DefaultFeature df = (DefaultFeature) dre.getLinkedRow()
215
//                        .cloneRow();
216
//            Point2D firstPoint=null;
217
//            Point2D lastPoint=null;
218
//            Object[] attributes=new Object[feature.size()];
219
//                for (int j = 0; j < feature.size(); j++) {
220
//                        attributes[j]=feature.get(j);
221
//                }
222
                FeatureStore featureStore;
223
//            try {
224
            try {
225
                        featureStore = getVLE().getFeatureStore();
226

    
227
//                EditableFeature eFeature=featureStore.createNewFeature(feature.getType(), feature);
228
            PathIterator theIterator = ((Geometry)feature.getDefaultGeometry()).getPathIterator(null,Converter.FLATNESS);
229
                double[] theData = new double[6];
230
                int theType;
231
                int numParts = 0;
232
                Point2D initialPoint=null;
233
                while (!theIterator.isDone()) {
234
                        theType = theIterator.currentSegment(theData);
235
                        switch (theType) {
236

    
237
                        case PathIterator.SEG_MOVETO:
238
                                numParts++;
239
                                newGp1 = new GeneralPathX();
240
//                                firstPoint=new Point2D.Double(theData[0], theData[1]);
241
                                initialPoint=new Point2D.Double(theData[0], theData[1]);
242
                                newGp1.moveTo(theData[0], theData[1]);
243
//                                lastPoint=new Point2D.Double(theData[0], theData[1]);
244
                                break;
245

    
246
                        case PathIterator.SEG_LINETO:
247
                                newGp1.lineTo(theData[0], theData[1]);
248
                                Geometry geomLine = createCurve(newGp1);
249
//                                DefaultFeature dfLine = (DefaultFeature) df.cloneRow();
250
//                                dfLine.setGeometry(geomLine);
251
//                                EditableFeature eFeature=featureStore.createNewFeature(feature.getType(), feature);
252
//                                eFeature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(),geomLine);
253
                                Feature f=insertGeometry(geomLine, feature);
254
//                                featureStore.insert(eFeature);
255
                                selectedRowAux.add(f);
256
                                newGp1 = new GeneralPathX();
257
                                newGp1.moveTo(theData[0], theData[1]);
258
//                                lastPoint=new Point2D.Double(theData[0], theData[1]);
259
                                break;
260
                        case PathIterator.SEG_CLOSE:
261
                                if (initialPoint!=null){
262
                                        newGp1.lineTo(initialPoint.getX(), initialPoint.getY());
263
                                        geomLine = createCurve(newGp1);
264
//                                        dfLine = (DefaultFeature) df.cloneRow();
265
//                                        dfLine.setGeometry(geomLine);
266
                                        EditableFeature eFeatureClose=featureStore.createNewFeature(feature.getType(), feature);
267
                                        eFeatureClose.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(),geomLine);
268
                                        selectedRowAux.add(eFeatureClose);
269
                                        newGp1 = new GeneralPathX();
270
                                        newGp1.moveTo(initialPoint.getX(), initialPoint.getY());
271
                                }
272
                                break;
273
                /*        case PathIterator.SEG_QUADTO:
274
                                Point2D current=newGp1.getCurrentPoint();
275
                                newGp1.quadTo(theData[0], theData[1], theData[2],
276
                                                theData[3]);
277
                                IGeometry geomQuad = ShapeFactory
278
                                                .createArc(current,new Point2D.Double(theData[0], theData[1]),new Point2D.Double(theData[2], theData[3]));
279
                                DefaultFeature dfQuad = (DefaultFeature) df.cloneRow();
280
                                dfQuad.setGeometry(geomQuad);
281
                                int indexQuad = addGeometry(geomQuad, dfQuad
282
                                                .getAttributes());
283
                                selectedRowAux.add(new DefaultRowEdited(dfQuad,
284
                                                IRowEdited.STATUS_ADDED, indexQuad));
285
                                newGp1 = new GeneralPathX();
286
                                newGp1.moveTo(theData[0], theData[1]);
287
//                                lastPoint=new Point2D.Double(theData[0], theData[1]);
288
                                break;
289
                        case PathIterator.SEG_CUBICTO:
290
                                newGp1.curveTo(theData[0], theData[1], theData[2],
291
                                                theData[3], theData[4], theData[5]);
292
                                IGeometry geomCurve = ShapeFactory
293
                                                .createArc(new Point2D.Double(theData[0], theData[1]),new Point2D.Double(theData[2], theData[3]),new Point2D.Double(theData[4], theData[5]));
294
                                DefaultFeature dfCurve = (DefaultFeature) df.cloneRow();
295
                                dfCurve.setGeometry(geomCurve);
296
                                int indexCurve = addGeometry(geomCurve, dfCurve
297
                                                .getAttributes());
298
                                selectedRowAux.add(new DefaultRowEdited(dfCurve,
299
                                                IRowEdited.STATUS_ADDED, indexCurve));
300
                                newGp1 = new GeneralPathX();
301
                                newGp1.moveTo(theData[0], theData[1]);
302
//                                lastPoint=new Point2D.Double(theData[0], theData[1]);
303
                                break;
304
                        case PathIterator.SEG_CLOSE:
305
//                                if (firstPoint!=null) {
306
//                                        newGp1=new GeneralPathX();
307
//                                        newGp1.moveTo(firstPoint.getX(),firstPoint.getY());
308
//                                        newGp1.lineTo(lastPoint.getX(),lastPoint.getY());
309
//                                        IGeometry geomClose = ShapeFactory
310
//                                                        .createPolyline2D(newGp1);
311
//                                        DefaultFeature dfClose = (DefaultFeature) df.cloneRow();
312
//                                        dfClose.setGeometry(geomClose);
313
//                                        int indexClose = addGeometry(geomClose, dfClose
314
//                                                        .getAttributes());
315
//                                        selectedRowAux.add(new DefaultRowEdited(dfClose,
316
//                                                        IRowEdited.STATUS_ADDED, indexClose));
317
//                                        newGp1 = new GeneralPathX();
318
//                                        newGp1.moveTo(theData[0], theData[1]);
319
//                                }
320
                                // if (isFirstPart)
321
                                // newGp1.closePath();
322
                                // else
323
                                // newGp2.closePath();
324
                                break;
325
                                */
326
                        } // end switch
327

    
328
                        theIterator.next();
329
                } // end while loop
330
            } catch (ReadException e) {
331
                        // TODO Auto-generated catch block
332
                        e.printStackTrace();
333
                } catch (DataException e) {
334
                        // TODO Auto-generated catch block
335
                        e.printStackTrace();
336
                }
337
                refresh();
338
    }
339
    private void exploitPolygon(Feature feature,ArrayList selectedRowAux) {
340
            GeneralPathX newGp1 = null;
341
//            Object[] attributes=new Object[feature.size()];
342
//                for (int j = 0; j < feature.size(); j++) {
343
//                        attributes[j]=feature.get(j);
344
//                }
345

    
346
            FeatureStore featureStore;
347
                    try {
348
                                featureStore = getVLE().getFeatureStore();
349

    
350

    
351
            PathIterator theIterator = ((Geometry)feature.getDefaultGeometry()).getPathIterator(null,Converter.FLATNESS);
352
                double[] theData = new double[6];
353
                int theType;
354
                int numParts = 0;
355
                Point2D initialPoint=null;
356
                while (!theIterator.isDone()) {
357
                        theType = theIterator.currentSegment(theData);
358
                        switch (theType) {
359

    
360
                        case PathIterator.SEG_MOVETO:
361
                                numParts++;
362
                                if (newGp1!=null) {
363
                                        Geometry geomLine = createCurve(newGp1);
364
                                        EditableFeature eFeature=featureStore.createNewFeature(feature.getType(), feature);
365
                                        eFeature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(),geomLine);
366

    
367
                                        selectedRowAux.add(eFeature);
368
                                }
369
                                newGp1 = new GeneralPathX();
370
                                initialPoint=new Point2D.Double(theData[0], theData[1]);
371
                                newGp1.moveTo(theData[0], theData[1]);
372
                                break;
373

    
374
                        case PathIterator.SEG_LINETO:
375
                                newGp1.lineTo(theData[0], theData[1]);
376
                                //newGp1 = new GeneralPathX();
377
                                //newGp1.moveTo(theData[0], theData[1]);
378
                                break;
379

    
380
                        case PathIterator.SEG_QUADTO:
381
                                newGp1.quadTo(theData[0], theData[1], theData[2],
382
                                                theData[3]);
383
                                //newGp1 = new GeneralPathX();
384
                                //newGp1.moveTo(theData[0], theData[1]);
385
                                break;
386
                        case PathIterator.SEG_CUBICTO:
387
                                newGp1.curveTo(theData[0], theData[1], theData[2],
388
                                                theData[3], theData[4], theData[5]);
389
                                //newGp1 = new GeneralPathX();
390
                                //newGp1.moveTo(theData[0], theData[1]);
391
                                break;
392
                        case PathIterator.SEG_CLOSE:
393
                                if (initialPoint!=null){
394
                                        newGp1.lineTo(initialPoint.getX(), initialPoint.getY());
395
                                }
396
//                                System.out.println("aqu?");
397
                                // if (isFirstPart)
398
                                // newGp1.closePath();
399
                                // else
400
                                // newGp2.closePath();
401
                                break;
402
                        } // end switch
403

    
404
                        theIterator.next();
405
                } // end while loop
406
                if (newGp1!=null) {
407
                        Geometry geomLine = createCurve(newGp1);
408
                        EditableFeature eFeature=featureStore.createNewFeature(feature.getType(), feature);
409
                        eFeature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(),geomLine);
410
                        selectedRowAux.add(eFeature);
411
                }
412
                    } catch (ReadException e) {
413
                                // TODO Auto-generated catch block
414
                                e.printStackTrace();
415
                        } catch (DataException e) {
416
                                // TODO Auto-generated catch block
417
                                e.printStackTrace();
418
                        }
419
                refresh();
420
    }
421
    /**
422
         * Add a diferent option.
423
         *
424
         * @param s
425
         *            Diferent option.
426
         */
427
    public void addOption(String s) {
428
    }
429

    
430
    /*
431
         * (non-Javadoc)
432
         *
433
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
434
         */
435
    public void addValue(double d) {
436

    
437
    }
438

    
439
        public String getName() {
440
                return PluginServices.getText(this,"exploit_");
441
        }
442

    
443
        public String toString() {
444
                return "_exploit";
445
        }
446
        public boolean isApplicable(int shapeType) {
447
                switch (shapeType) {
448
                case Geometry.TYPES.POINT:
449
                case Geometry.TYPES.CIRCLE:
450
                case Geometry.TYPES.ARC:
451
                case Geometry.TYPES.ELLIPSE:
452
                case Geometry.TYPES.SURFACE:
453
                case Geometry.TYPES.MULTIPOINT:
454
                        return false;
455
                }
456
                return true;
457
        }
458
}