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