svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / ExploitCADTool.java @ 10254
History | View | Annotate | Download (12.8 KB)
1 | 5884 | caballero | /* 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.io.IOException; |
||
47 | import java.util.ArrayList; |
||
48 | |||
49 | import com.iver.andami.PluginServices; |
||
50 | import com.iver.cit.gvsig.CADExtension; |
||
51 | import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
||
52 | import com.iver.cit.gvsig.fmap.core.FGeometryCollection; |
||
53 | import com.iver.cit.gvsig.fmap.core.FShape; |
||
54 | import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
||
55 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
56 | import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
||
57 | 6714 | caballero | import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
58 | 5884 | caballero | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
59 | import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
||
60 | import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
||
61 | import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
||
62 | import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
||
63 | import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
||
64 | import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
||
65 | import com.iver.cit.gvsig.gui.cad.tools.smc.ExploitCADToolContext; |
||
66 | import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
||
67 | |||
68 | |||
69 | /**
|
||
70 | * Elimina la geometr?a compuesta y a?ade todas la geometr?as simples que la
|
||
71 | 8938 | caballero | * componen, implementado para polil?neas y pol?gonos.
|
72 | 5884 | caballero | *
|
73 | * @author Vicente Caballero Navarro
|
||
74 | */
|
||
75 | public class ExploitCADTool extends DefaultCADTool { |
||
76 | private ExploitCADToolContext _fsm;
|
||
77 | |||
78 | /**
|
||
79 | * Crea un nuevo ExploitCADTool.
|
||
80 | */
|
||
81 | public ExploitCADTool() {
|
||
82 | } |
||
83 | |||
84 | /**
|
||
85 | * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
|
||
86 | * carga previa a la utilizaci?n de la herramienta.
|
||
87 | */
|
||
88 | public void init() { |
||
89 | _fsm = new ExploitCADToolContext(this); |
||
90 | } |
||
91 | |||
92 | /* (non-Javadoc)
|
||
93 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
|
||
94 | */
|
||
95 | public void transition(double x, double y, InputEvent event) { |
||
96 | _fsm.addPoint(x, y, event); |
||
97 | } |
||
98 | |||
99 | /* (non-Javadoc)
|
||
100 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
|
||
101 | */
|
||
102 | public void transition(double d) { |
||
103 | _fsm.addValue(d); |
||
104 | } |
||
105 | |||
106 | /* (non-Javadoc)
|
||
107 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
|
||
108 | */
|
||
109 | public void transition(String s) throws CommandException { |
||
110 | if (!super.changeCommand(s)){ |
||
111 | _fsm.addOption(s); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | /**
|
||
116 | * DOCUMENT ME!
|
||
117 | */
|
||
118 | public void selection() { |
||
119 | ArrayList selectedRows=getSelectedRows();
|
||
120 | if (selectedRows.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) { |
||
121 | CADExtension.setCADTool("_selection",false); |
||
122 | ((SelectionCADTool) CADExtension.getCADTool()).setNextTool( |
||
123 | "_exploit");
|
||
124 | } |
||
125 | } |
||
126 | |||
127 | /**
|
||
128 | * Equivale al transition del prototipo pero sin pasarle como par?metro el
|
||
129 | * editableFeatureSource que ya estar? creado.
|
||
130 | *
|
||
131 | * @param x par?metro x del punto que se pase en esta transici?n.
|
||
132 | * @param y par?metro y del punto que se pase en esta transici?n.
|
||
133 | */
|
||
134 | public void addPoint(double x, double y,InputEvent event) { |
||
135 | } |
||
136 | |||
137 | /**
|
||
138 | * M?todo para dibujar la lo necesario para el estado en el que nos
|
||
139 | * encontremos.
|
||
140 | *
|
||
141 | * @param g Graphics sobre el que dibujar.
|
||
142 | * @param x par?metro x del punto que se pase para dibujar.
|
||
143 | * @param y par?metro x del punto que se pase para dibujar.
|
||
144 | */
|
||
145 | public void drawOperation(Graphics g, double x, double y) { |
||
146 | } |
||
147 | public void exploit() { |
||
148 | ArrayList selectedRow = getSelectedRows();
|
||
149 | ArrayList selectedRowAux = new ArrayList(); |
||
150 | VectorialLayerEdited vle = getVLE(); |
||
151 | VectorialEditableAdapter vea = vle.getVEA(); |
||
152 | try {
|
||
153 | vea.startComplexRow(); |
||
154 | for (int i = 0; i < selectedRow.size(); i++) { |
||
155 | DefaultRowEdited dre = (DefaultRowEdited) selectedRow.get(i); |
||
156 | DefaultFeature df = (DefaultFeature) dre.getLinkedRow() |
||
157 | .cloneRow(); |
||
158 | IGeometry geom=df.getGeometry(); |
||
159 | if (geom instanceof FGeometryCollection) { |
||
160 | exploitGeometryCollection(dre,selectedRowAux); |
||
161 | }else {
|
||
162 | if (geom.getGeometryType()==FShape.LINE) {
|
||
163 | exploitLine(dre,selectedRowAux); |
||
164 | }else if (geom.getGeometryType()==FShape.POLYGON) { |
||
165 | exploitPolygon(dre,selectedRowAux); |
||
166 | } |
||
167 | } |
||
168 | vea.removeRow(dre.getIndex(), getName(), EditionEvent.GRAPHIC); |
||
169 | } |
||
170 | 6071 | caballero | vea.endComplexRow(getName()); |
171 | 8938 | caballero | vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux); |
172 | 5985 | caballero | //clearSelection();
|
173 | //selectedRow.addAll(selectedRowAux);
|
||
174 | 5884 | caballero | } catch (IOException e) { |
175 | e.printStackTrace(); |
||
176 | } catch (DriverIOException e) {
|
||
177 | e.printStackTrace(); |
||
178 | } |
||
179 | } |
||
180 | private void exploitGeometryCollection(DefaultRowEdited dre,ArrayList selectedRowAux) { |
||
181 | DefaultFeature df = (DefaultFeature) dre.getLinkedRow() |
||
182 | .cloneRow(); |
||
183 | IGeometry geom=df.getGeometry(); |
||
184 | FGeometryCollection gc=(FGeometryCollection)geom; |
||
185 | IGeometry[] geoms=gc.getGeometries();
|
||
186 | for (int i=0;i<geoms.length;i++) { |
||
187 | DefaultFeature dfAux=(DefaultFeature)df.cloneRow(); |
||
188 | int index = addGeometry(geoms[i], dfAux.getAttributes());
|
||
189 | selectedRowAux.add(new DefaultRowEdited(dfAux,
|
||
190 | IRowEdited.STATUS_ADDED, index)); |
||
191 | } |
||
192 | } |
||
193 | private void exploitLine(DefaultRowEdited dre,ArrayList selectedRowAux) throws IOException, DriverIOException { |
||
194 | GeneralPathX newGp1 = new GeneralPathX();
|
||
195 | DefaultFeature df = (DefaultFeature) dre.getLinkedRow() |
||
196 | .cloneRow(); |
||
197 | 5908 | caballero | // Point2D firstPoint=null;
|
198 | // Point2D lastPoint=null;
|
||
199 | 7072 | caballero | PathIterator theIterator = df.getGeometry().getPathIterator(null,FConverter.FLATNESS); |
200 | 5884 | caballero | double[] theData = new double[6]; |
201 | int theType;
|
||
202 | int numParts = 0; |
||
203 | while (!theIterator.isDone()) {
|
||
204 | theType = theIterator.currentSegment(theData); |
||
205 | switch (theType) {
|
||
206 | |||
207 | case PathIterator.SEG_MOVETO: |
||
208 | numParts++; |
||
209 | newGp1 = new GeneralPathX();
|
||
210 | 5908 | caballero | // firstPoint=new Point2D.Double(theData[0], theData[1]);
|
211 | 5884 | caballero | newGp1.moveTo(theData[0], theData[1]); |
212 | 5908 | caballero | // lastPoint=new Point2D.Double(theData[0], theData[1]);
|
213 | 5884 | caballero | break;
|
214 | |||
215 | case PathIterator.SEG_LINETO: |
||
216 | newGp1.lineTo(theData[0], theData[1]); |
||
217 | IGeometry geomLine = ShapeFactory |
||
218 | .createPolyline2D(newGp1); |
||
219 | DefaultFeature dfLine = (DefaultFeature) df.cloneRow(); |
||
220 | dfLine.setGeometry(geomLine); |
||
221 | int indexLine = addGeometry(geomLine, dfLine
|
||
222 | .getAttributes()); |
||
223 | selectedRowAux.add(new DefaultRowEdited(dfLine,
|
||
224 | IRowEdited.STATUS_ADDED, indexLine)); |
||
225 | newGp1 = new GeneralPathX();
|
||
226 | newGp1.moveTo(theData[0], theData[1]); |
||
227 | 5908 | caballero | // lastPoint=new Point2D.Double(theData[0], theData[1]);
|
228 | 5884 | caballero | break;
|
229 | |||
230 | 6071 | caballero | /* case PathIterator.SEG_QUADTO:
|
231 | 5884 | caballero | Point2D current=newGp1.getCurrentPoint();
|
232 | newGp1.quadTo(theData[0], theData[1], theData[2],
|
||
233 | theData[3]);
|
||
234 | IGeometry geomQuad = ShapeFactory
|
||
235 | .createArc(current,new Point2D.Double(theData[0], theData[1]),new Point2D.Double(theData[2], theData[3]));
|
||
236 | DefaultFeature dfQuad = (DefaultFeature) df.cloneRow();
|
||
237 | dfQuad.setGeometry(geomQuad);
|
||
238 | int indexQuad = addGeometry(geomQuad, dfQuad
|
||
239 | .getAttributes());
|
||
240 | selectedRowAux.add(new DefaultRowEdited(dfQuad,
|
||
241 | IRowEdited.STATUS_ADDED, indexQuad));
|
||
242 | newGp1 = new GeneralPathX();
|
||
243 | newGp1.moveTo(theData[0], theData[1]);
|
||
244 | 5908 | caballero | // lastPoint=new Point2D.Double(theData[0], theData[1]);
|
245 | 5884 | caballero | break;
|
246 | case PathIterator.SEG_CUBICTO:
|
||
247 | newGp1.curveTo(theData[0], theData[1], theData[2],
|
||
248 | theData[3], theData[4], theData[5]);
|
||
249 | IGeometry geomCurve = ShapeFactory
|
||
250 | .createArc(new Point2D.Double(theData[0], theData[1]),new Point2D.Double(theData[2], theData[3]),new Point2D.Double(theData[4], theData[5]));
|
||
251 | DefaultFeature dfCurve = (DefaultFeature) df.cloneRow();
|
||
252 | dfCurve.setGeometry(geomCurve);
|
||
253 | int indexCurve = addGeometry(geomCurve, dfCurve
|
||
254 | .getAttributes());
|
||
255 | selectedRowAux.add(new DefaultRowEdited(dfCurve,
|
||
256 | IRowEdited.STATUS_ADDED, indexCurve));
|
||
257 | newGp1 = new GeneralPathX();
|
||
258 | newGp1.moveTo(theData[0], theData[1]);
|
||
259 | 5908 | caballero | // lastPoint=new Point2D.Double(theData[0], theData[1]);
|
260 | 5884 | caballero | break;
|
261 | case PathIterator.SEG_CLOSE:
|
||
262 | 5908 | caballero | // if (firstPoint!=null) {
|
263 | // newGp1=new GeneralPathX();
|
||
264 | // newGp1.moveTo(firstPoint.getX(),firstPoint.getY());
|
||
265 | // newGp1.lineTo(lastPoint.getX(),lastPoint.getY());
|
||
266 | // IGeometry geomClose = ShapeFactory
|
||
267 | // .createPolyline2D(newGp1);
|
||
268 | // DefaultFeature dfClose = (DefaultFeature) df.cloneRow();
|
||
269 | // dfClose.setGeometry(geomClose);
|
||
270 | // int indexClose = addGeometry(geomClose, dfClose
|
||
271 | // .getAttributes());
|
||
272 | // selectedRowAux.add(new DefaultRowEdited(dfClose,
|
||
273 | // IRowEdited.STATUS_ADDED, indexClose));
|
||
274 | // newGp1 = new GeneralPathX();
|
||
275 | // newGp1.moveTo(theData[0], theData[1]);
|
||
276 | // }
|
||
277 | 5884 | caballero | // if (isFirstPart)
|
278 | // newGp1.closePath();
|
||
279 | // else
|
||
280 | // newGp2.closePath();
|
||
281 | break;
|
||
282 | 6071 | caballero | */
|
283 | 5884 | caballero | } // end switch
|
284 | |||
285 | theIterator.next(); |
||
286 | } // end while loop
|
||
287 | |||
288 | } |
||
289 | private void exploitPolygon(DefaultRowEdited dre,ArrayList selectedRowAux) throws IOException, DriverIOException { |
||
290 | GeneralPathX newGp1 = null;
|
||
291 | DefaultFeature df = (DefaultFeature) dre.getLinkedRow() |
||
292 | .cloneRow(); |
||
293 | 7072 | caballero | PathIterator theIterator = df.getGeometry().getPathIterator(null,FConverter.FLATNESS); |
294 | 5884 | caballero | double[] theData = new double[6]; |
295 | int theType;
|
||
296 | int numParts = 0; |
||
297 | while (!theIterator.isDone()) {
|
||
298 | theType = theIterator.currentSegment(theData); |
||
299 | switch (theType) {
|
||
300 | |||
301 | case PathIterator.SEG_MOVETO: |
||
302 | numParts++; |
||
303 | if (newGp1!=null) { |
||
304 | IGeometry geomLine = ShapeFactory |
||
305 | .createPolygon2D(newGp1); |
||
306 | DefaultFeature dfLine = (DefaultFeature) df.cloneRow(); |
||
307 | dfLine.setGeometry(geomLine); |
||
308 | int indexLine = addGeometry(geomLine, dfLine
|
||
309 | .getAttributes()); |
||
310 | selectedRowAux.add(new DefaultRowEdited(dfLine,
|
||
311 | IRowEdited.STATUS_ADDED, indexLine)); |
||
312 | } |
||
313 | newGp1 = new GeneralPathX();
|
||
314 | newGp1.moveTo(theData[0], theData[1]); |
||
315 | break;
|
||
316 | |||
317 | case PathIterator.SEG_LINETO: |
||
318 | newGp1.lineTo(theData[0], theData[1]); |
||
319 | //newGp1 = new GeneralPathX();
|
||
320 | //newGp1.moveTo(theData[0], theData[1]);
|
||
321 | break;
|
||
322 | |||
323 | case PathIterator.SEG_QUADTO: |
||
324 | newGp1.quadTo(theData[0], theData[1], theData[2], |
||
325 | theData[3]);
|
||
326 | //newGp1 = new GeneralPathX();
|
||
327 | //newGp1.moveTo(theData[0], theData[1]);
|
||
328 | break;
|
||
329 | case PathIterator.SEG_CUBICTO: |
||
330 | newGp1.curveTo(theData[0], theData[1], theData[2], |
||
331 | theData[3], theData[4], theData[5]); |
||
332 | //newGp1 = new GeneralPathX();
|
||
333 | //newGp1.moveTo(theData[0], theData[1]);
|
||
334 | break;
|
||
335 | case PathIterator.SEG_CLOSE: |
||
336 | System.out.println("aqu?"); |
||
337 | // if (isFirstPart)
|
||
338 | // newGp1.closePath();
|
||
339 | // else
|
||
340 | // newGp2.closePath();
|
||
341 | break;
|
||
342 | } // end switch
|
||
343 | |||
344 | theIterator.next(); |
||
345 | } // end while loop
|
||
346 | if (newGp1!=null) { |
||
347 | IGeometry geomLine = ShapeFactory |
||
348 | .createPolygon2D(newGp1); |
||
349 | DefaultFeature dfLine = (DefaultFeature) df.cloneRow(); |
||
350 | dfLine.setGeometry(geomLine); |
||
351 | int indexLine = addGeometry(geomLine, dfLine
|
||
352 | .getAttributes()); |
||
353 | selectedRowAux.add(new DefaultRowEdited(dfLine,
|
||
354 | IRowEdited.STATUS_ADDED, indexLine)); |
||
355 | } |
||
356 | |||
357 | } |
||
358 | /**
|
||
359 | * Add a diferent option.
|
||
360 | *
|
||
361 | * @param s
|
||
362 | * Diferent option.
|
||
363 | */
|
||
364 | public void addOption(String s) { |
||
365 | } |
||
366 | |||
367 | /*
|
||
368 | * (non-Javadoc)
|
||
369 | *
|
||
370 | * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
||
371 | */
|
||
372 | public void addValue(double d) { |
||
373 | |||
374 | } |
||
375 | |||
376 | public String getName() { |
||
377 | return PluginServices.getText(this,"exploit_"); |
||
378 | } |
||
379 | |||
380 | public String toString() { |
||
381 | return "_exploit"; |
||
382 | } |
||
383 | public boolean isApplicable(int shapeType) { |
||
384 | switch (shapeType) {
|
||
385 | case FShape.POINT:
|
||
386 | case FShape.CIRCLE:
|
||
387 | case FShape.ARC:
|
||
388 | case FShape.ELLIPSE:
|
||
389 | 6024 | caballero | case FShape.MULTIPOINT:
|
390 | 5884 | caballero | return false; |
391 | } |
||
392 | return true; |
||
393 | } |
||
394 | } |