Statistics
| Revision:

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

History | View | Annotate | Download (14.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.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.data.DataException;
51
import org.gvsig.fmap.data.ReadException;
52
import org.gvsig.fmap.data.feature.Feature;
53
import org.gvsig.fmap.data.feature.FeatureCollection;
54
import org.gvsig.fmap.data.feature.FeatureStore;
55
import org.gvsig.fmap.geom.Geometry;
56
import org.gvsig.fmap.geom.GeometryFactory;
57
import org.gvsig.fmap.geom.GeometryManager;
58
import org.gvsig.fmap.geom.aggregate.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
    private ExploitCADToolContext _fsm;
80
    private GeometryFactory geomFactory=GeometryManager.getInstance().getGeometryFactory();
81

    
82
    /**
83
     * Crea un nuevo ExploitCADTool.
84
     */
85
    public ExploitCADTool() {
86
    }
87

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

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

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

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

    
119
    /**
120
     * DOCUMENT ME!
121
     */
122
    public void selection() {
123
            FeatureCollection selection=null;
124
            try {
125
                    selection = (FeatureCollection)getVLE().getFeatureStore().getSelection();
126
            } catch (ReadException e) {
127
                    // TODO Auto-generated catch block
128
                    e.printStackTrace();
129
            }
130
        if (selection.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
                "_exploit");
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
    }
146

    
147
    /**
148
     * M?todo para dibujar la lo necesario para el estado en el que nos
149
     * encontremos.
150
     *
151
     * @param g Graphics sobre el que dibujar.
152
     * @param x par?metro x del punto que se pase para dibujar.
153
     * @param y par?metro x del punto que se pase para dibujar.
154
     */
155
    public void drawOperation(Graphics g, double x, double y) {
156
    }
157
    public void exploit() {
158
            VectorialLayerEdited vle = getVLE();
159

    
160

    
161
//                VectorialEditableAdapter vea = vle.getVEA();
162
                try {
163
                        FeatureStore featureStore=vle.getFeatureStore();
164
                    Iterator iterator=featureStore.getSelection().iterator();
165
//                        ArrayList selectedRow = getSelectedRows();
166
                        ArrayList selectedRowAux = new ArrayList();
167
                        featureStore.getCommandsRecord().startComplex();
168
                        while (iterator.hasNext()) {
169
                                Feature feature = (Feature) iterator.next();
170

    
171
//                        }
172
//                        for (int i = 0; i < selectedRow.size(); i++) {
173
//                                DefaultRowEdited dre = (DefaultRowEdited) selectedRow.get(i);
174
//                                DefaultFeature df = (DefaultFeature) dre.getLinkedRow()
175
//                                                .cloneRow();
176
                                Geometry geom=((Geometry)feature.getDefaultGeometry()).cloneGeometry();
177
                                featureStore.delete(feature);
178
//                                vea.removeRow(dre.getIndex(), getName(), EditionEvent.GRAPHIC);
179
                                if (geom instanceof GeometryCollection) {
180
                                        exploitGeometryCollection(feature,selectedRowAux);
181
                                }else {
182
                                        if (geom.getType()==Geometry.TYPES.CURVE) {
183
                                                exploitLine(feature,selectedRowAux);
184
                                        }else if (geom.getType()==Geometry.TYPES.SURFACE) {
185
                                                exploitPolygon(feature,selectedRowAux);
186
                                        }
187
                                }
188

    
189
                        }
190
                        featureStore.getCommandsRecord().endComplex(getName());
191
//                        vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
192
                        //clearSelection();
193
                        //selectedRow.addAll(selectedRowAux);
194
                } catch (ReadException e) {
195
                        NotificationManager.addError(e.getMessage(),e);
196
                } catch (DataException e) {
197
                        NotificationManager.addError(e.getMessage(),e);
198
                }
199
        }
200
    private void exploitGeometryCollection(Feature feature,ArrayList selectedRowAux) {
201
//            DefaultFeature df = (DefaultFeature) dre.getLinkedRow()
202
//                        .cloneRow();
203
            Geometry geom=((Geometry)feature.getDefaultGeometry()).cloneGeometry();
204
                BaseMultiPrimitive gc=(BaseMultiPrimitive)geom;
205
                int numGeoms=gc.getPrimitivesNumber();
206
                for (int i=0;i<numGeoms;i++) {
207
                        Geometry primitiveGeom=(Geometry)gc.getPrimitiveAt(i);
208
//                        DefaultFeature dfAux=(DefaultFeature)df.cloneRow();
209
                        Object[] attributes=new Object[feature.size()];
210
                        for (int j = 0; j < feature.size(); j++) {
211
                                attributes[j]=feature.get(j);
212
                        }
213
                        addGeometry(primitiveGeom, attributes);
214
                        selectedRowAux.add(feature);
215
                }
216
                refresh();
217
        }
218
    private void exploitLine(Feature feature,ArrayList selectedRowAux){
219
            GeneralPathX newGp1 = new GeneralPathX();
220
//            DefaultFeature df = (DefaultFeature) dre.getLinkedRow()
221
//                        .cloneRow();
222
//            Point2D firstPoint=null;
223
//            Point2D lastPoint=null;
224
            Object[] attributes=new Object[feature.size()];
225
                for (int j = 0; j < feature.size(); j++) {
226
                        attributes[j]=feature.get(j);
227
                }
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 = geomFactory
249
                                                .createPolyline2D(newGp1);
250
//                                DefaultFeature dfLine = (DefaultFeature) df.cloneRow();
251
//                                dfLine.setGeometry(geomLine);
252

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

    
327
                        theIterator.next();
328
                } // end while loop
329
                refresh();
330
    }
331
    private void exploitPolygon(Feature feature,ArrayList selectedRowAux) {
332
            GeneralPathX newGp1 = null;
333
            Object[] attributes=new Object[feature.size()];
334
                for (int j = 0; j < feature.size(); j++) {
335
                        attributes[j]=feature.get(j);
336
                }
337
//            DefaultFeature df = (DefaultFeature) dre.getLinkedRow()
338
//                        .cloneRow();
339
            PathIterator theIterator = ((Geometry)feature.getDefaultGeometry()).getPathIterator(null,Converter.FLATNESS);
340
                double[] theData = new double[6];
341
                int theType;
342
                int numParts = 0;
343
                Point2D initialPoint=null;
344
                while (!theIterator.isDone()) {
345
                        theType = theIterator.currentSegment(theData);
346
                        switch (theType) {
347

    
348
                        case PathIterator.SEG_MOVETO:
349
                                numParts++;
350
                                if (newGp1!=null) {
351
                                        Geometry geomLine = geomFactory
352
                                                .createPolygon2D(newGp1);
353
//                                        DefaultFeature dfLine = (DefaultFeature) df.cloneRow();
354
//                                        dfLine.setGeometry(geomLine);
355
                                        Feature newFeature = addGeometry(geomLine, attributes);
356
                                        selectedRowAux.add(newFeature);
357
                                }
358
                                newGp1 = new GeneralPathX();
359
                                initialPoint=new Point2D.Double(theData[0], theData[1]);
360
                                newGp1.moveTo(theData[0], theData[1]);
361
                                break;
362

    
363
                        case PathIterator.SEG_LINETO:
364
                                newGp1.lineTo(theData[0], theData[1]);
365
                                //newGp1 = new GeneralPathX();
366
                                //newGp1.moveTo(theData[0], theData[1]);
367
                                break;
368

    
369
                        case PathIterator.SEG_QUADTO:
370
                                newGp1.quadTo(theData[0], theData[1], theData[2],
371
                                                theData[3]);
372
                                //newGp1 = new GeneralPathX();
373
                                //newGp1.moveTo(theData[0], theData[1]);
374
                                break;
375
                        case PathIterator.SEG_CUBICTO:
376
                                newGp1.curveTo(theData[0], theData[1], theData[2],
377
                                                theData[3], theData[4], theData[5]);
378
                                //newGp1 = new GeneralPathX();
379
                                //newGp1.moveTo(theData[0], theData[1]);
380
                                break;
381
                        case PathIterator.SEG_CLOSE:
382
                                if (initialPoint!=null){
383
                                        newGp1.lineTo(initialPoint.getX(), initialPoint.getY());
384
                                }
385
//                                System.out.println("aqu?");
386
                                // if (isFirstPart)
387
                                // newGp1.closePath();
388
                                // else
389
                                // newGp2.closePath();
390
                                break;
391
                        } // end switch
392

    
393
                        theIterator.next();
394
                } // end while loop
395
                if (newGp1!=null) {
396
                        Geometry geomLine = geomFactory
397
                                .createPolygon2D(newGp1);
398
//                        DefaultFeature dfLine = (DefaultFeature) df.cloneRow();
399
//                        dfLine.setGeometry(geomLine);
400
                        Feature newFeature = addGeometry(geomLine, attributes);
401
                        selectedRowAux.add(newFeature);
402
                }
403
                refresh();
404
    }
405
    /**
406
         * Add a diferent option.
407
         *
408
         * @param s
409
         *            Diferent option.
410
         */
411
    public void addOption(String s) {
412
    }
413

    
414
    /*
415
         * (non-Javadoc)
416
         *
417
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
418
         */
419
    public void addValue(double d) {
420

    
421
    }
422

    
423
        public String getName() {
424
                return PluginServices.getText(this,"exploit_");
425
        }
426

    
427
        public String toString() {
428
                return "_exploit";
429
        }
430
        public boolean isApplicable(int shapeType) {
431
                switch (shapeType) {
432
                case Geometry.TYPES.POINT:
433
                case Geometry.TYPES.CIRCLE:
434
                case Geometry.TYPES.ARC:
435
                case Geometry.TYPES.ELLIPSE:
436
                case Geometry.TYPES.SURFACE:
437
                case Geometry.TYPES.MULTIPOINT:
438
                        return false;
439
                }
440
                return true;
441
        }
442
}