svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / PolylineCADTool.java @ 5395
History | View | Annotate | Download (18.2 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 |
import java.util.ArrayList; |
48 |
|
49 |
import com.iver.andami.PluginServices; |
50 |
import com.iver.cit.gvsig.fmap.DriverException; |
51 |
import com.iver.cit.gvsig.fmap.core.FGeometryCollection; |
52 |
import com.iver.cit.gvsig.fmap.core.FShape; |
53 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
54 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
55 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
56 |
import com.iver.cit.gvsig.fmap.edition.UtilFunctions; |
57 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
58 |
import com.iver.cit.gvsig.gui.cad.CADTool; |
59 |
import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
60 |
import com.iver.cit.gvsig.gui.cad.exception.CommadException; |
61 |
import com.iver.cit.gvsig.gui.cad.tools.smc.PolylineCADToolContext; |
62 |
import com.iver.cit.gvsig.gui.cad.tools.smc.PolylineCADToolContext.PolylineCADToolState; |
63 |
|
64 |
|
65 |
/**
|
66 |
* DOCUMENT ME!
|
67 |
*
|
68 |
* @author Vicente Caballero Navarro
|
69 |
*/
|
70 |
public class PolylineCADTool extends DefaultCADTool { |
71 |
private PolylineCADToolContext _fsm;
|
72 |
private Point2D firstPoint; |
73 |
private Point2D antPoint; |
74 |
private Point2D antantPoint; |
75 |
private Point2D antCenter; |
76 |
private Point2D antInter; |
77 |
private ArrayList list = new ArrayList(); |
78 |
|
79 |
/**
|
80 |
* Crea un nuevo PolylineCADTool.
|
81 |
*/
|
82 |
public PolylineCADTool() {
|
83 |
|
84 |
} |
85 |
|
86 |
/**
|
87 |
* M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
88 |
* carga previa a la utilizaci?n de la herramienta.
|
89 |
*/
|
90 |
public void init() { |
91 |
_fsm = new PolylineCADToolContext(this); |
92 |
} |
93 |
|
94 |
public void endGeometry() { |
95 |
IGeometry[] geoms = (IGeometry[]) list.toArray(new IGeometry[0]); |
96 |
FGeometryCollection fgc = new FGeometryCollection(geoms);
|
97 |
try {
|
98 |
if (((FLyrVect)getVLE().getLayer()).getShapeType()==FShape.POLYGON){
|
99 |
GeneralPathX gpx=new GeneralPathX();
|
100 |
gpx.moveTo(antPoint.getX(),antPoint.getY()); |
101 |
gpx.lineTo(firstPoint.getX(),firstPoint.getY()); |
102 |
IGeometry line=ShapeFactory.createPolyline2D(gpx); |
103 |
fgc.addGeometry(line); |
104 |
} |
105 |
} catch (DriverException e) {
|
106 |
e.printStackTrace(); |
107 |
} |
108 |
|
109 |
// No queremos guardar FGeometryCollections:
|
110 |
GeneralPathX gp = new GeneralPathX();
|
111 |
gp.append(fgc.getPathIterator(null), true); |
112 |
IGeometry newGeom = ShapeFactory.createPolyline2D(gp); |
113 |
|
114 |
addGeometry(newGeom); |
115 |
_fsm = new PolylineCADToolContext(this); |
116 |
list.clear(); |
117 |
antantPoint=antCenter=antInter=antPoint=firstPoint=null;
|
118 |
} |
119 |
public void closeGeometry(){ |
120 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
|
121 |
2);
|
122 |
elShape.moveTo(antPoint.getX(), antPoint.getY()); |
123 |
elShape.lineTo(firstPoint.getX(), firstPoint.getY()); |
124 |
|
125 |
list.add(ShapeFactory.createPolyline2D(elShape)); |
126 |
// list.add(ShapeFactory.createPolyline2D(elShape));
|
127 |
|
128 |
} |
129 |
/* (non-Javadoc)
|
130 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
|
131 |
*/
|
132 |
public void transition(double x, double y, InputEvent event) { |
133 |
_fsm.addPoint(x, y, event); |
134 |
} |
135 |
|
136 |
/* (non-Javadoc)
|
137 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
|
138 |
*/
|
139 |
public void transition(double d) { |
140 |
//_fsm.addValue(sel,d);
|
141 |
} |
142 |
|
143 |
/* (non-Javadoc)
|
144 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
|
145 |
*/
|
146 |
public void transition(String s) throws CommadException { |
147 |
if (!super.changeCommand(s)){ |
148 |
_fsm.addOption(s); |
149 |
} |
150 |
} |
151 |
|
152 |
/**
|
153 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
154 |
* editableFeatureSource que ya estar? creado.
|
155 |
*
|
156 |
* @param sel Bitset con las geometr?as que est?n seleccionadas.
|
157 |
* @param x par?metro x del punto que se pase en esta transici?n.
|
158 |
* @param y par?metro y del punto que se pase en esta transici?n.
|
159 |
*/
|
160 |
public void addPoint(double x, double y,InputEvent event) { |
161 |
PolylineCADToolState actualState = (PolylineCADToolState) _fsm.getPreviousState(); |
162 |
String status = actualState.getName();
|
163 |
|
164 |
if (status.equals("Polyline.FirstPoint")) { |
165 |
antPoint = new Point2D.Double(x, y); |
166 |
|
167 |
if (firstPoint == null) { |
168 |
firstPoint = (Point2D) antPoint.clone();
|
169 |
} |
170 |
} else if (status.equals("Polyline.NextPointOrArcOrClose")) { |
171 |
Point2D point = new Point2D.Double(x, y); |
172 |
|
173 |
if (antPoint != null) { |
174 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
|
175 |
2);
|
176 |
elShape.moveTo(antPoint.getX(), antPoint.getY()); |
177 |
elShape.lineTo(point.getX(), point.getY()); |
178 |
list.add(ShapeFactory.createPolyline2D(elShape)); |
179 |
|
180 |
} |
181 |
|
182 |
if (antPoint != null) { |
183 |
antantPoint = antPoint; |
184 |
} |
185 |
|
186 |
antPoint = point; |
187 |
} else if (status.equals("Polyline.NextPointOrLineOrClose")) { |
188 |
Point2D point = new Point2D.Double(x, y); |
189 |
Point2D lastp = antPoint; //(Point2D)points.get(i-1); |
190 |
|
191 |
if (antantPoint == null) { |
192 |
antantPoint = new Point2D.Double(lastp.getX() + |
193 |
(point.getX() / 2), lastp.getY() + (point.getY() / 2)); |
194 |
} |
195 |
|
196 |
if (((point.getY() == lastp.getY()) &&
|
197 |
(point.getX() < lastp.getX())) || |
198 |
((point.getX() == lastp.getX()) && |
199 |
(point.getY() < lastp.getY()))) { |
200 |
} else {
|
201 |
if (point.getX() == lastp.getX()) {
|
202 |
point = new Point2D.Double(point.getX() + 0.00000001, |
203 |
point.getY()); |
204 |
} else if (point.getY() == lastp.getY()) { |
205 |
point = new Point2D.Double(point.getX(), |
206 |
point.getY() + 0.00000001);
|
207 |
} |
208 |
|
209 |
if (point.getX() == antantPoint.getX()) {
|
210 |
point = new Point2D.Double(point.getX() + 0.00000001, |
211 |
point.getY()); |
212 |
} else if (point.getY() == antantPoint.getY()) { |
213 |
point = new Point2D.Double(point.getX(), |
214 |
point.getY() + 0.00000001);
|
215 |
} |
216 |
|
217 |
if (!(list.size() > 0) || |
218 |
(((IGeometry) list.get(list.size() - 1)).getGeometryType() == FShape.LINE)) {
|
219 |
Point2D[] ps1 = UtilFunctions.getPerpendicular(antantPoint, |
220 |
lastp, lastp); |
221 |
Point2D mediop = new Point2D.Double((point.getX() + |
222 |
lastp.getX()) / 2, (point.getY() + lastp.getY()) / 2); |
223 |
Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp, |
224 |
point, mediop); |
225 |
Point2D interp = UtilFunctions.getIntersection(ps1[0], |
226 |
ps1[1], ps2[0], ps2[1]); |
227 |
antInter = interp; |
228 |
|
229 |
double radio = interp.distance(lastp);
|
230 |
|
231 |
if (UtilFunctions.isLowAngle(antantPoint, lastp, interp,
|
232 |
point)) { |
233 |
radio = -radio; |
234 |
} |
235 |
|
236 |
Point2D centerp = UtilFunctions.getPoint(interp, mediop,
|
237 |
radio); |
238 |
antCenter = centerp; |
239 |
|
240 |
IGeometry ig = ShapeFactory.createArc(lastp, centerp, point); |
241 |
|
242 |
if (ig != null) { |
243 |
list.add(ig); |
244 |
} |
245 |
} else {
|
246 |
Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp, |
247 |
antInter, lastp); |
248 |
double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]); |
249 |
double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]); |
250 |
double angle = UtilFunctions.getAngle(antCenter, lastp);
|
251 |
Point2D ini1 = null; |
252 |
Point2D ini2 = null; |
253 |
|
254 |
if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions.absoluteAngleDistance(
|
255 |
angle, a2)) { |
256 |
ini1 = ps1[0];
|
257 |
ini2 = ps1[1];
|
258 |
} else {
|
259 |
ini1 = ps1[1];
|
260 |
ini2 = ps1[0];
|
261 |
} |
262 |
|
263 |
Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
|
264 |
Point2D correct = new Point2D.Double(lastp.getX() + |
265 |
unit.getX(), lastp.getY() + unit.getY()); |
266 |
Point2D[] ps = UtilFunctions.getPerpendicular(lastp, |
267 |
correct, lastp); |
268 |
Point2D mediop = new Point2D.Double((point.getX() + |
269 |
lastp.getX()) / 2, (point.getY() + lastp.getY()) / 2); |
270 |
Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp, |
271 |
point, mediop); |
272 |
Point2D interp = UtilFunctions.getIntersection(ps[0], |
273 |
ps[1], ps2[0], ps2[1]); |
274 |
antInter = interp; |
275 |
|
276 |
double radio = interp.distance(lastp);
|
277 |
|
278 |
if (UtilFunctions.isLowAngle(correct, lastp, interp, point)) {
|
279 |
radio = -radio; |
280 |
} |
281 |
|
282 |
Point2D centerp = UtilFunctions.getPoint(interp, mediop,
|
283 |
radio); |
284 |
antCenter = centerp; |
285 |
list.add(ShapeFactory.createArc(lastp, centerp, point)); |
286 |
} |
287 |
|
288 |
antantPoint = antPoint; |
289 |
antPoint = point; |
290 |
} |
291 |
} |
292 |
} |
293 |
|
294 |
/**
|
295 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
296 |
* encontremos.
|
297 |
*
|
298 |
* @param g Graphics sobre el que dibujar.
|
299 |
* @param selectedGeometries BitSet con las geometr?as seleccionadas.
|
300 |
* @param x par?metro x del punto que se pase para dibujar.
|
301 |
* @param y par?metro x del punto que se pase para dibujar.
|
302 |
*/
|
303 |
public void drawOperation(Graphics g, double x, |
304 |
double y) {
|
305 |
PolylineCADToolState actualState = ((PolylineCADToolContext)_fsm).getState(); |
306 |
String status = actualState.getName();
|
307 |
|
308 |
if (status.equals("Polyline.NextPointOrArcOrClose")) { |
309 |
for (int i = 0; i < list.size(); i++) { |
310 |
((IGeometry) list.get(i)).cloneGeometry().draw((Graphics2D) g,
|
311 |
getCadToolAdapter().getMapControl().getViewPort(), |
312 |
CADTool.drawingSymbol); |
313 |
} |
314 |
|
315 |
drawLine((Graphics2D) g, antPoint, new Point2D.Double(x, y)); |
316 |
} else if ((status.equals("Polyline.NextPointOrLineOrClose"))) { |
317 |
for (int i = 0; i < list.size(); i++) { |
318 |
((IGeometry) list.get(i)).cloneGeometry().draw((Graphics2D) g,
|
319 |
getCadToolAdapter().getMapControl().getViewPort(), |
320 |
CADTool.drawingSymbol); |
321 |
} |
322 |
|
323 |
Point2D point = new Point2D.Double(x, y); |
324 |
Point2D lastp = antPoint;
|
325 |
|
326 |
if (!(list.size() > 0) || |
327 |
(((IGeometry) list.get(list.size() - 1)).getGeometryType() == FShape.LINE)) {
|
328 |
if (antantPoint == null) { |
329 |
drawArc(point, lastp, |
330 |
new Point2D.Double(lastp.getX() + (point.getX() / 2), |
331 |
lastp.getY() + (point.getY() / 2)), g);
|
332 |
} else {
|
333 |
drawArc(point, lastp, antantPoint, g); |
334 |
} |
335 |
} else {
|
336 |
if (antInter != null) { |
337 |
Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp, |
338 |
antInter, lastp); |
339 |
double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]); |
340 |
double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]); |
341 |
double angle = UtilFunctions.getAngle(antCenter, lastp);
|
342 |
Point2D ini1 = null; |
343 |
Point2D ini2 = null; |
344 |
|
345 |
if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions.absoluteAngleDistance(
|
346 |
angle, a2)) { |
347 |
ini1 = ps1[0];
|
348 |
ini2 = ps1[1];
|
349 |
} else {
|
350 |
ini1 = ps1[1];
|
351 |
ini2 = ps1[0];
|
352 |
} |
353 |
|
354 |
Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
|
355 |
Point2D correct = new Point2D.Double(lastp.getX() + |
356 |
unit.getX(), lastp.getY() + unit.getY()); |
357 |
drawArc(point, lastp, correct, g); |
358 |
} |
359 |
} |
360 |
} |
361 |
} |
362 |
|
363 |
/**
|
364 |
* Dibuja el arco sobre el graphics.
|
365 |
*
|
366 |
* @param point Puntero del rat?n.
|
367 |
* @param lastp ?ltimo punto de la polilinea.
|
368 |
* @param antp Punto antepenultimo.
|
369 |
* @param g Graphics sobre el que se dibuja.
|
370 |
*/
|
371 |
private void drawArc(Point2D point, Point2D lastp, Point2D antp, Graphics g) { |
372 |
if (((point.getY() == lastp.getY()) && (point.getX() < lastp.getX())) ||
|
373 |
((point.getX() == lastp.getX()) && |
374 |
(point.getY() < lastp.getY()))) { |
375 |
} else {
|
376 |
if (point.getX() == lastp.getX()) {
|
377 |
point = new Point2D.Double(point.getX() + 0.00000001, |
378 |
point.getY()); |
379 |
} else if (point.getY() == lastp.getY()) { |
380 |
point = new Point2D.Double(point.getX(), |
381 |
point.getY() + 0.00000001);
|
382 |
} |
383 |
|
384 |
if (point.getX() == antp.getX()) {
|
385 |
point = new Point2D.Double(point.getX() + 0.00000001, |
386 |
point.getY()); |
387 |
} else if (point.getY() == antp.getY()) { |
388 |
point = new Point2D.Double(point.getX(), |
389 |
point.getY() + 0.00000001);
|
390 |
} |
391 |
|
392 |
Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp, antp, lastp); |
393 |
Point2D mediop = new Point2D.Double((point.getX() + lastp.getX()) / 2, |
394 |
(point.getY() + lastp.getY()) / 2);
|
395 |
Point2D[] ps2 = UtilFunctions.getPerpendicular(lastp, point, mediop); |
396 |
Point2D interp = UtilFunctions.getIntersection(ps1[0], ps1[1], |
397 |
ps2[0], ps2[1]); |
398 |
|
399 |
double radio = interp.distance(lastp);
|
400 |
|
401 |
if (UtilFunctions.isLowAngle(antp, lastp, interp, point)) {
|
402 |
radio = -radio; |
403 |
} |
404 |
|
405 |
Point2D centerp = UtilFunctions.getPoint(interp, mediop, radio);
|
406 |
|
407 |
drawLine((Graphics2D) g, lastp, point);
|
408 |
|
409 |
IGeometry ig = ShapeFactory.createArc(lastp, centerp, point); |
410 |
|
411 |
if (ig != null) { |
412 |
ig.draw((Graphics2D) g,
|
413 |
getCadToolAdapter().getMapControl().getViewPort(), |
414 |
CADTool.modifySymbol); |
415 |
} |
416 |
} |
417 |
} |
418 |
|
419 |
/**
|
420 |
* Add a diferent option.
|
421 |
*
|
422 |
* @param sel DOCUMENT ME!
|
423 |
* @param s Diferent option.
|
424 |
*/
|
425 |
public void addOption(String s) { |
426 |
/* PolylineCADToolState actualState = (PolylineCADToolState) _fsm.getPreviousState();
|
427 |
String status = actualState.getName();
|
428 |
|
429 |
if (status.equals("Polyline.NextPointOrArcOrClose")) {
|
430 |
if (s.equals("A") || s.equals("a") || s.equals(PluginServices.getText(this,"inter_arc"))) {
|
431 |
//Arco
|
432 |
} else if (s.equals("C") || s.equals("c")) {
|
433 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
|
434 |
elShape.moveTo(antPoint.getX(), antPoint.getY());
|
435 |
elShape.lineTo(firstPoint.getX(), firstPoint.getY());
|
436 |
list.add(ShapeFactory.createPolyline2D(elShape));
|
437 |
//closeGeometry();
|
438 |
}
|
439 |
} else if (status.equals("Polyline.NextPointOrLineOrClose")) {
|
440 |
if (s.equals("N") || s.equals("n") || s.equals(PluginServices.getText(this,"inter_line"))) {
|
441 |
//L?nea
|
442 |
} else if (s.equals("C") || s.equals("c")) {
|
443 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
|
444 |
elShape.moveTo(antPoint.getX(), antPoint.getY());
|
445 |
elShape.lineTo(firstPoint.getX(), firstPoint.getY());
|
446 |
list.add(ShapeFactory.createPolyline2D(elShape));
|
447 |
//closeGeometry();
|
448 |
}
|
449 |
}
|
450 |
*/
|
451 |
} |
452 |
|
453 |
/* (non-Javadoc)
|
454 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
455 |
*/
|
456 |
public void addValue(double d) { |
457 |
} |
458 |
|
459 |
public void cancel(){ |
460 |
//endGeometry();
|
461 |
list.clear(); |
462 |
antantPoint=antCenter=antInter=antPoint=firstPoint=null;
|
463 |
} |
464 |
|
465 |
public void end() { |
466 |
/* CADExtension.setCADTool("polyline");
|
467 |
PluginServices.getMainFrame().setSelectedTool("POLYLINE"); */
|
468 |
} |
469 |
|
470 |
public String getName() { |
471 |
return PluginServices.getText(this,"polyline_"); |
472 |
} |
473 |
|
474 |
public String toString() { |
475 |
return "_polyline"; |
476 |
} |
477 |
public boolean isApplicable(int shapeType) { |
478 |
switch (shapeType) {
|
479 |
case FShape.POINT:
|
480 |
return false; |
481 |
} |
482 |
return true; |
483 |
} |
484 |
} |