root / branches / v2_0_0_prep / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / PolygonCADTool.java @ 24429
History | View | Annotate | Download (15.9 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.Point2D; |
47 |
|
48 |
import org.gvsig.fmap.geom.Geometry; |
49 |
import org.gvsig.fmap.geom.GeometryFactory; |
50 |
import org.gvsig.fmap.geom.GeometryManager; |
51 |
import org.gvsig.fmap.geom.operation.Draw; |
52 |
import org.gvsig.fmap.geom.operation.DrawOperationContext; |
53 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
54 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
55 |
import org.gvsig.fmap.geom.primitive.Curve2D; |
56 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
57 |
import org.gvsig.fmap.geom.primitive.Surface2D; |
58 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
59 |
|
60 |
import com.iver.andami.PluginServices; |
61 |
import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
62 |
import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
63 |
import com.iver.cit.gvsig.gui.cad.exception.ValueException; |
64 |
import com.iver.cit.gvsig.gui.cad.tools.smc.PolygonCADToolContext; |
65 |
import com.iver.cit.gvsig.gui.cad.tools.smc.PolygonCADToolContext.PolygonCADToolState; |
66 |
|
67 |
|
68 |
/**
|
69 |
* DOCUMENT ME!
|
70 |
*
|
71 |
* @author Vicente Caballero Navarro
|
72 |
*/
|
73 |
public class PolygonCADTool extends DefaultCADTool { |
74 |
private PolygonCADToolContext _fsm;
|
75 |
private Point2D center; |
76 |
private int numLines = 5; |
77 |
private boolean isI = true; |
78 |
private GeometryFactory geomFactory=GeometryManager.getInstance().getGeometryFactory();
|
79 |
|
80 |
/**
|
81 |
* Crea un nuevo PolygonCADTool.
|
82 |
*/
|
83 |
public PolygonCADTool() {
|
84 |
|
85 |
} |
86 |
|
87 |
/**
|
88 |
* M?todo de incio, 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 PolygonCADToolContext(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 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
120 |
* editableFeatureSource que ya estar? creado.
|
121 |
*
|
122 |
* @param sel Bitset con las geometr?as que est?n seleccionadas.
|
123 |
* @param x par?metro x del punto que se pase en esta transici?n.
|
124 |
* @param y par?metro y del punto que se pase en esta transici?n.
|
125 |
*/
|
126 |
public void addPoint(double x, double y,InputEvent event) { |
127 |
PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState(); |
128 |
String status = actualState.getName();
|
129 |
|
130 |
if (status.equals("Polygon.NumberOrCenterPoint")) { |
131 |
center = new Point2D.Double(x, y); |
132 |
} else if (status.equals("Polygon.CenterPoint")) { |
133 |
center = new Point2D.Double(x, y); |
134 |
} else if (status.equals("Polygon.OptionOrRadiusOrPoint") || |
135 |
status.equals("Polygon.RadiusOrPoint")) {
|
136 |
Point2D point = new Point2D.Double(x, y); |
137 |
//Pol?gono a partir de la circunferencia.
|
138 |
if (isI) {
|
139 |
insertGeometry(getIPolygon(point, point.distance(center))); |
140 |
} else {
|
141 |
insertGeometry(getCPolygon(point, point.distance(center))); |
142 |
} |
143 |
} |
144 |
} |
145 |
|
146 |
/**
|
147 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
148 |
* encontremos.
|
149 |
*
|
150 |
* @param g Graphics sobre el que dibujar.
|
151 |
* @param selectedGeometries BitSet con las geometr?as seleccionadas.
|
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, |
156 |
double y) {
|
157 |
PolygonCADToolState actualState = _fsm.getState(); |
158 |
String status = actualState.getName();
|
159 |
|
160 |
if (status.equals("Polygon.OptionOrRadiusOrPoint") || |
161 |
status.equals("Polygon.RadiusOrPoint")) {
|
162 |
Point2D point = new Point2D.Double(x, y); |
163 |
drawLine((Graphics2D) g, center, point,DefaultCADTool.geometrySelectSymbol);
|
164 |
DrawOperationContext doc=new DrawOperationContext();
|
165 |
doc.setGraphics((Graphics2D)g);
|
166 |
doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort()); |
167 |
doc.setSymbol(DefaultCADTool.geometrySelectSymbol); |
168 |
if (isI) {
|
169 |
try {
|
170 |
getIPolygon(point, point.distance(center)).invokeOperation(Draw.CODE,doc); |
171 |
} catch (GeometryOperationNotSupportedException e) {
|
172 |
e.printStackTrace(); |
173 |
} catch (GeometryOperationException e) {
|
174 |
e.printStackTrace(); |
175 |
} |
176 |
// getIPolygon(point, point.distance(center)).draw((Graphics2D) g,
|
177 |
// getCadToolAdapter().getMapControl().getViewPort(),
|
178 |
// DefaultCADTool.geometrySelectSymbol);
|
179 |
} else {
|
180 |
try {
|
181 |
getCPolygon(point, point.distance(center)).invokeOperation(Draw.CODE,doc); |
182 |
} catch (GeometryOperationNotSupportedException e) {
|
183 |
e.printStackTrace(); |
184 |
} catch (GeometryOperationException e) {
|
185 |
e.printStackTrace(); |
186 |
} |
187 |
// getCPolygon(point, point.distance(center)).draw((Graphics2D) g,
|
188 |
// getCadToolAdapter().getMapControl().getViewPort(),
|
189 |
// DefaultCADTool.geometrySelectSymbol);
|
190 |
} |
191 |
try {
|
192 |
doc.setSymbol(DefaultCADTool.axisReferencesSymbol); |
193 |
geomFactory.createCircle(center, point.distance(center)).invokeOperation(Draw.CODE,doc); |
194 |
} catch (GeometryOperationNotSupportedException e) {
|
195 |
e.printStackTrace(); |
196 |
} catch (GeometryOperationException e) {
|
197 |
e.printStackTrace(); |
198 |
} |
199 |
// geomFactory.createCircle(center, point.distance(center)).draw((Graphics2D) g,
|
200 |
// getCadToolAdapter().getMapControl().getViewPort(),
|
201 |
// DefaultCADTool.axisReferencesSymbol);
|
202 |
} |
203 |
} |
204 |
|
205 |
/**
|
206 |
* Add a diferent option.
|
207 |
*
|
208 |
* @param sel DOCUMENT ME!
|
209 |
* @param s Diferent option.
|
210 |
*/
|
211 |
public void addOption(String s) { |
212 |
PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState(); |
213 |
String status = actualState.getName();
|
214 |
|
215 |
if (status.equals("Polygon.OptionOrRadiusOrPoint")) { |
216 |
if (s.equalsIgnoreCase(PluginServices.getText(this,"PolygonCADTool.circumscribed")) || s.equals(PluginServices.getText(this,"circumscribed"))) { |
217 |
isI = false;
|
218 |
} else if (s.equalsIgnoreCase(PluginServices.getText(this,"PolygonCADTool.into_circle")) || s.equals(PluginServices.getText(this,"into_circle"))) { |
219 |
isI = true;
|
220 |
} |
221 |
} |
222 |
} |
223 |
|
224 |
/* (non-Javadoc)
|
225 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
226 |
*/
|
227 |
public void addValue(double d) { |
228 |
PolygonCADToolState actualState = (PolygonCADToolState) _fsm.getPreviousState(); |
229 |
String status = actualState.getName();
|
230 |
|
231 |
if (status.equals("Polygon.NumberOrCenterPoint")) { |
232 |
numLines = (int) d;
|
233 |
} else if (status.equals("Polygon.OptionOrRadiusOrPoint") || |
234 |
status.equals("Polygon.RadiusOrPoint")) {
|
235 |
double radio = d;
|
236 |
|
237 |
//Pol?gono a partir de radio.
|
238 |
Point2D point = UtilFunctions.getPoint(center,
|
239 |
new Point2D.Double(center.getX(), center.getY() + 10), radio); |
240 |
|
241 |
if (isI) {
|
242 |
insertGeometry(getIPolygon(point, radio)); |
243 |
} else {
|
244 |
insertGeometry(getCPolygon(point, radio)); |
245 |
} |
246 |
} |
247 |
} |
248 |
|
249 |
/**
|
250 |
* Devuelve la geometr?a con el poligono regular circunscrito a la
|
251 |
* circunferencia formada por el punto central y el radio que se froma
|
252 |
* entre este y el puntero del rat?n..
|
253 |
*
|
254 |
* @param point Puntero del rat?n.
|
255 |
* @param radio Radio
|
256 |
*
|
257 |
* @return GeometryCollection con las geometr?as del pol?gono.
|
258 |
*/
|
259 |
private Geometry getCPolygon(Point2D point, double radio) { |
260 |
Point2D p1 = UtilFunctions.getPoint(center, point, radio);
|
261 |
|
262 |
double initangle = UtilFunctions.getAngle(center, point);
|
263 |
Point2D antPoint = p1;
|
264 |
Point2D antInter = null; |
265 |
//Point2D firstPoint= null;
|
266 |
double an = (Math.PI * 2) / numLines; |
267 |
GeneralPathX elShape = new GeneralPathX();
|
268 |
boolean firstTime=true; |
269 |
for (int i = numLines-1; i >=1 ; i--) { |
270 |
Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
|
271 |
radio); |
272 |
Point2D[] ps1 = UtilFunctions.getPerpendicular(antPoint, center, |
273 |
antPoint); |
274 |
Point2D[] ps2 = UtilFunctions.getPerpendicular(p2, center, p2); |
275 |
Point2D inter = UtilFunctions.getIntersection(ps1[0], ps1[1], |
276 |
ps2[0], ps2[1]); |
277 |
|
278 |
if (antInter != null) { |
279 |
|
280 |
if (firstTime){
|
281 |
elShape.moveTo(antInter.getX(), antInter.getY()); |
282 |
//firstPoint=new Point2D.Double(antInter.getX(), antInter.getY());
|
283 |
firstTime=false;
|
284 |
} |
285 |
elShape.lineTo(inter.getX(), inter.getY()); |
286 |
|
287 |
} |
288 |
|
289 |
antInter = inter; |
290 |
antPoint = p2; |
291 |
} |
292 |
//elShape.lineTo(firstPoint.getX(),firstPoint.getY());
|
293 |
elShape.closePath(); |
294 |
int type=getCadToolAdapter().getActiveLayerType();
|
295 |
Geometry shape=null;
|
296 |
if (type==Geometry.TYPES.SURFACE){
|
297 |
shape=new Surface2D(elShape);
|
298 |
}else{
|
299 |
shape=new Curve2D(elShape);
|
300 |
} |
301 |
return shape;
|
302 |
} |
303 |
|
304 |
/**
|
305 |
* Devuelve la geometr?a con el poligono regular inscrito en la
|
306 |
* circunferencia.
|
307 |
*
|
308 |
* @param point Puntero del rat?n.
|
309 |
* @param radio Radio
|
310 |
*
|
311 |
* @return GeometryCollection con las geometr?as del pol?gono.
|
312 |
* @throws ValueException
|
313 |
*/
|
314 |
private Geometry getIPolygon(Point2D point, double radio){ |
315 |
Point2D p1 = UtilFunctions.getPoint(center, point, radio);
|
316 |
double initangle = UtilFunctions.getAngle(center, point);
|
317 |
Point2D antPoint = p1;
|
318 |
//Point2D firstPoint= null;
|
319 |
double an = (Math.PI * 2) / numLines; |
320 |
GeneralPathX elShape = new GeneralPathX();
|
321 |
boolean firstTime=true; |
322 |
|
323 |
for (int i = numLines-1; i > 0; i--) { |
324 |
Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
|
325 |
radio); |
326 |
|
327 |
if (firstTime){
|
328 |
elShape.moveTo(antPoint.getX(), antPoint.getY()); |
329 |
//firstPoint=new Point2D.Double(antPoint.getX(), antPoint.getY());
|
330 |
firstTime=false;
|
331 |
} |
332 |
|
333 |
elShape.lineTo(p2.getX(), p2.getY()); |
334 |
|
335 |
antPoint = p2; |
336 |
} |
337 |
//elShape.lineTo(firstPoint.getX(),firstPoint.getY());
|
338 |
elShape.closePath(); |
339 |
int type=getCadToolAdapter().getActiveLayerType();
|
340 |
Geometry shape=null;
|
341 |
if (type==Geometry.TYPES.SURFACE){
|
342 |
shape=new Surface2D(elShape);
|
343 |
}else{
|
344 |
shape=new Curve2D(elShape);
|
345 |
} |
346 |
return shape;
|
347 |
} |
348 |
/**
|
349 |
* Devuelve la geometr?a con el poligono regular circunscrito a la
|
350 |
* circunferencia formada por el punto central y el radio que se froma
|
351 |
* entre este y el puntero del rat?n..
|
352 |
*
|
353 |
* @param point Puntero del rat?n.
|
354 |
* @param radio Radio
|
355 |
*
|
356 |
* @return GeometryCollection con las geometr?as del pol?gono.
|
357 |
*/
|
358 |
/* private IGeometry getCPolygonOld(Point2D point, double radio) {
|
359 |
IGeometry[] geoms = new IGeometry[numLines];
|
360 |
Point2D p1 = UtilFunctions.getPoint(center, point, radio);
|
361 |
|
362 |
double initangle = UtilFunctions.getAngle(center, point);
|
363 |
Point2D antPoint = p1;
|
364 |
Point2D antInter = null;
|
365 |
double an = (Math.PI * 2) / numLines;
|
366 |
|
367 |
for (int i = 1; i < (numLines + 2); i++) {
|
368 |
Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
|
369 |
radio);
|
370 |
Point2D[] ps1 = UtilFunctions.getPerpendicular(antPoint, center,
|
371 |
antPoint);
|
372 |
Point2D[] ps2 = UtilFunctions.getPerpendicular(p2, center, p2);
|
373 |
Point2D inter = UtilFunctions.getIntersection(ps1[0], ps1[1],
|
374 |
ps2[0], ps2[1]);
|
375 |
|
376 |
if (antInter != null) {
|
377 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
|
378 |
2);
|
379 |
elShape.moveTo(antInter.getX(), antInter.getY());
|
380 |
elShape.lineTo(inter.getX(), inter.getY());
|
381 |
|
382 |
geoms[i - 2] = (ShapeFactory.createPolyline2D(elShape));
|
383 |
}
|
384 |
|
385 |
antInter = inter;
|
386 |
antPoint = p2;
|
387 |
}
|
388 |
|
389 |
return new FGeometryCollection(geoms);
|
390 |
}
|
391 |
*/
|
392 |
/**
|
393 |
* Devuelve la geometr?a con el poligono regular inscrito en la
|
394 |
* circunferencia.
|
395 |
*
|
396 |
* @param point Puntero del rat?n.
|
397 |
* @param radio Radio
|
398 |
*
|
399 |
* @return GeometryCollection con las geometr?as del pol?gono.
|
400 |
*/
|
401 |
/* private IGeometry getIPolygonOld(Point2D point, double radio) {
|
402 |
IGeometry[] geoms = new IGeometry[numLines];
|
403 |
Point2D p1 = UtilFunctions.getPoint(center, point, radio);
|
404 |
double initangle = UtilFunctions.getAngle(center, point);
|
405 |
Point2D antPoint = p1;
|
406 |
double an = (Math.PI * 2) / numLines;
|
407 |
|
408 |
for (int i = 1; i < (numLines + 1); i++) {
|
409 |
Point2D p2 = UtilFunctions.getPoint(center, (an * i) + initangle,
|
410 |
radio);
|
411 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
|
412 |
2);
|
413 |
elShape.moveTo(antPoint.getX(), antPoint.getY());
|
414 |
elShape.lineTo(p2.getX(), p2.getY());
|
415 |
|
416 |
geoms[i - 1] = (ShapeFactory.createPolyline2D(elShape));
|
417 |
antPoint = p2;
|
418 |
}
|
419 |
|
420 |
return new FGeometryCollection(geoms);
|
421 |
}
|
422 |
*/
|
423 |
public String getName() { |
424 |
return PluginServices.getText(this,"polygon_"); |
425 |
} |
426 |
|
427 |
/**
|
428 |
* Devuelve la cadena que corresponde al estado en el que nos encontramos.
|
429 |
*
|
430 |
* @return Cadena para mostrar por consola.
|
431 |
*/
|
432 |
public String getQuestion() { |
433 |
PolygonCADToolState actualState = _fsm.getState(); |
434 |
String status = actualState.getName();
|
435 |
|
436 |
if (status.equals("Polygon.NumberOrCenterPoint")) { |
437 |
return super.getQuestion()+"<"+numLines+">"; |
438 |
} |
439 |
return super.getQuestion(); |
440 |
|
441 |
} |
442 |
public String toString() { |
443 |
return "_polygon"; |
444 |
} |
445 |
public boolean isApplicable(int shapeType) { |
446 |
switch (shapeType) {
|
447 |
case Geometry.TYPES.POINT:
|
448 |
case Geometry.TYPES.MULTIPOINT:
|
449 |
return false; |
450 |
} |
451 |
return true; |
452 |
} |
453 |
} |