svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / gui / cad / tools / PolygonCADTool.java @ 40557
History | View | Annotate | Download (12.1 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.editing.gui.cad.tools; |
25 |
|
26 |
import java.awt.event.InputEvent; |
27 |
import java.awt.geom.Point2D; |
28 |
|
29 |
import javax.swing.JOptionPane; |
30 |
|
31 |
import org.gvsig.andami.PluginServices; |
32 |
import org.gvsig.app.ApplicationLocator; |
33 |
import org.gvsig.editing.gui.cad.exception.CommandException; |
34 |
import org.gvsig.editing.gui.cad.exception.ValueException; |
35 |
import org.gvsig.editing.gui.cad.tools.smc.PolygonCADToolContext; |
36 |
import org.gvsig.editing.gui.cad.tools.smc.PolygonCADToolContext.PolygonCADToolState; |
37 |
import org.gvsig.fmap.geom.Geometry; |
38 |
import org.gvsig.fmap.geom.GeometryLocator; |
39 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
40 |
import org.gvsig.fmap.geom.primitive.OrientablePrimitive; |
41 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
42 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
43 |
import org.gvsig.i18n.Messages; |
44 |
import org.gvsig.tools.locator.LocatorException; |
45 |
|
46 |
/**
|
47 |
* DOCUMENT ME!
|
48 |
*
|
49 |
* @author Vicente Caballero Navarro
|
50 |
*/
|
51 |
public class PolygonCADTool extends AbstractCurveSurfaceCADTool { |
52 |
|
53 |
protected PolygonCADToolContext _fsm;
|
54 |
protected Point2D center; |
55 |
protected int numLines = 5; |
56 |
protected boolean isI = true; |
57 |
|
58 |
/**
|
59 |
* M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
60 |
* carga previa a la utilizaci?n de la herramienta.
|
61 |
*/
|
62 |
public void init() { |
63 |
_fsm = new PolygonCADToolContext(this); |
64 |
} |
65 |
|
66 |
public void transition(double x, double y, InputEvent event) { |
67 |
_fsm.addPoint(x, y, event); |
68 |
} |
69 |
|
70 |
public void transition(double d) { |
71 |
_fsm.addValue(d); |
72 |
} |
73 |
|
74 |
public void transition(String s) throws CommandException { |
75 |
if (!super.changeCommand(s)) { |
76 |
_fsm.addOption(s); |
77 |
} |
78 |
} |
79 |
|
80 |
/**
|
81 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
82 |
* editableFeatureSource que ya estar? creado.
|
83 |
*
|
84 |
* @param sel
|
85 |
* Bitset con las geometr?as que est?n seleccionadas.
|
86 |
* @param x
|
87 |
* par?metro x del punto que se pase en esta transici?n.
|
88 |
* @param y
|
89 |
* par?metro y del punto que se pase en esta transici?n.
|
90 |
*/
|
91 |
public void addPoint(double x, double y, InputEvent event) { |
92 |
PolygonCADToolState actualState = |
93 |
(PolygonCADToolState) _fsm.getPreviousState(); |
94 |
String status = actualState.getName();
|
95 |
|
96 |
if (status.equals("Polygon.NumberOrCenterPoint")) { |
97 |
center = new Point2D.Double(x, y); |
98 |
} else
|
99 |
if (status.equals("Polygon.CenterPoint")) { |
100 |
center = new Point2D.Double(x, y); |
101 |
} else
|
102 |
if (status.equals("Polygon.OptionOrRadiusOrPoint") |
103 |
|| status.equals("Polygon.RadiusOrPoint")) {
|
104 |
Point2D point = new Point2D.Double(x, y); |
105 |
// Pol?gono a partir de la circunferencia.
|
106 |
if (isI) {
|
107 |
insertAndSelectGeometry(getIPolygon(point, |
108 |
point.distance(center))); |
109 |
} else {
|
110 |
insertAndSelectGeometry(getCPolygon(point, |
111 |
point.distance(center))); |
112 |
} |
113 |
} |
114 |
} |
115 |
|
116 |
/**
|
117 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
118 |
* encontremos.
|
119 |
*
|
120 |
* @param g
|
121 |
* Graphics sobre el que dibujar.
|
122 |
* @param selectedGeometries
|
123 |
* BitSet con las geometr?as seleccionadas.
|
124 |
* @param x
|
125 |
* par?metro x del punto que se pase para dibujar.
|
126 |
* @param y
|
127 |
* par?metro x del punto que se pase para dibujar.
|
128 |
*/
|
129 |
public void drawOperation(MapControlDrawer renderer, double x, double y) { |
130 |
PolygonCADToolState actualState = _fsm.getState(); |
131 |
String status = actualState.getName();
|
132 |
|
133 |
if (status.equals("Polygon.OptionOrRadiusOrPoint") |
134 |
|| status.equals("Polygon.RadiusOrPoint")) {
|
135 |
Point2D point = new Point2D.Double(x, y); |
136 |
renderer.drawLine(center, point, |
137 |
mapControlManager.getGeometrySelectionSymbol()); |
138 |
|
139 |
Geometry help_geom = null;
|
140 |
Geometry help_geom_perim = null;
|
141 |
|
142 |
/*
|
143 |
* Get helping polygon
|
144 |
*/
|
145 |
if (isI) {
|
146 |
help_geom = getIPolygon(point, point.distance(center)); |
147 |
} else {
|
148 |
help_geom = getCPolygon(point, point.distance(center)); |
149 |
} |
150 |
|
151 |
/*
|
152 |
* Get and draw perimeter of polygon
|
153 |
*/
|
154 |
try {
|
155 |
help_geom_perim = GeometryLocator. |
156 |
getGeometryManager().createCurve( |
157 |
help_geom.getGeneralPath(), |
158 |
Geometry.SUBTYPES.GEOM2D); |
159 |
renderer.draw(help_geom_perim, |
160 |
mapControlManager.getGeometrySelectionSymbol()); |
161 |
} catch (Exception e) { |
162 |
ApplicationLocator.getManager().message( |
163 |
Messages.getText("_Unable_to_draw_help_geometry"),
|
164 |
JOptionPane.ERROR_MESSAGE);
|
165 |
} |
166 |
|
167 |
|
168 |
/*
|
169 |
* Create circle
|
170 |
*/
|
171 |
help_geom = createCircle(center, point.distance(center)); |
172 |
|
173 |
/*
|
174 |
* Get and draw perimeter of circle
|
175 |
*/
|
176 |
try {
|
177 |
help_geom_perim = GeometryLocator. |
178 |
getGeometryManager().createCurve( |
179 |
help_geom.getGeneralPath(), |
180 |
Geometry.SUBTYPES.GEOM2D); |
181 |
renderer.draw(help_geom_perim, |
182 |
mapControlManager.getAxisReferenceSymbol()); |
183 |
} catch (Exception e) { |
184 |
ApplicationLocator.getManager().message( |
185 |
Messages.getText("_Unable_to_draw_help_geometry"),
|
186 |
JOptionPane.ERROR_MESSAGE);
|
187 |
} |
188 |
|
189 |
} |
190 |
} |
191 |
|
192 |
/**
|
193 |
* Add a diferent option.
|
194 |
*
|
195 |
* @param sel
|
196 |
* DOCUMENT ME!
|
197 |
* @param s
|
198 |
* Diferent option.
|
199 |
*/
|
200 |
public void addOption(String s) { |
201 |
PolygonCADToolState actualState = |
202 |
(PolygonCADToolState) _fsm.getPreviousState(); |
203 |
String status = actualState.getName();
|
204 |
|
205 |
if (status.equals("Polygon.OptionOrRadiusOrPoint")) { |
206 |
if (s.equalsIgnoreCase(PluginServices.getText(this, |
207 |
"PolygonCADTool.circumscribed"))
|
208 |
|| s.equals(PluginServices.getText(this, "circumscribed"))) { |
209 |
isI = false;
|
210 |
} else
|
211 |
if (s.equalsIgnoreCase(PluginServices.getText(this, |
212 |
"PolygonCADTool.into_circle"))
|
213 |
|| s.equals(PluginServices.getText(this, "into_circle"))) { |
214 |
isI = true;
|
215 |
} |
216 |
} |
217 |
} |
218 |
|
219 |
public void addValue(double d) { |
220 |
PolygonCADToolState actualState = |
221 |
(PolygonCADToolState) _fsm.getPreviousState(); |
222 |
String status = actualState.getName();
|
223 |
|
224 |
if (status.equals("Polygon.NumberOrCenterPoint")) { |
225 |
numLines = (int) d;
|
226 |
} else
|
227 |
if (status.equals("Polygon.OptionOrRadiusOrPoint") |
228 |
|| status.equals("Polygon.RadiusOrPoint")) {
|
229 |
double radio = d;
|
230 |
|
231 |
// Pol?gono a partir de radio.
|
232 |
Point2D point =
|
233 |
UtilFunctions.getPoint(center, |
234 |
new Point2D.Double(center.getX(), center.getY() + 10), |
235 |
radio); |
236 |
|
237 |
if (isI) {
|
238 |
insertAndSelectGeometry(getIPolygon(point, radio)); |
239 |
} else {
|
240 |
insertAndSelectGeometry(getCPolygon(point, radio)); |
241 |
} |
242 |
} |
243 |
} |
244 |
|
245 |
/**
|
246 |
* Devuelve la geometr?a con el poligono regular circunscrito a la
|
247 |
* circunferencia formada por el punto central y el radio que se froma
|
248 |
* entre este y el puntero del rat?n..
|
249 |
*
|
250 |
* @param point
|
251 |
* Puntero del rat?n.
|
252 |
* @param radio
|
253 |
* Radio
|
254 |
*
|
255 |
* @return GeometryCollection con las geometr?as del pol?gono.
|
256 |
*/
|
257 |
private Geometry getCPolygon(Point2D point, double radio) { |
258 |
Point2D p1 = UtilFunctions.getPoint(center, point, radio);
|
259 |
|
260 |
double initangle = UtilFunctions.getAngle(center, point);
|
261 |
Point2D antPoint = p1;
|
262 |
Point2D antInter = null; |
263 |
double an = (Math.PI * 2) / numLines; |
264 |
boolean firstTime = true; |
265 |
|
266 |
OrientablePrimitive orientablePrimitive = createOrientablePrimitive(); |
267 |
|
268 |
for (int i = numLines - 1; i >= 0; i--) { |
269 |
Point2D p2 =
|
270 |
UtilFunctions.getPoint(center, (an * i) + initangle, radio); |
271 |
Point2D[] ps1 = |
272 |
UtilFunctions.getPerpendicular(antPoint, center, antPoint); |
273 |
Point2D[] ps2 = UtilFunctions.getPerpendicular(p2, center, p2); |
274 |
Point2D inter =
|
275 |
UtilFunctions.getIntersection(ps1[0], ps1[1], ps2[0], ps2[1]); |
276 |
|
277 |
if (antInter != null) { |
278 |
|
279 |
if (firstTime) {
|
280 |
orientablePrimitive.addMoveToVertex(createPoint( |
281 |
antInter.getX(), antInter.getY())); |
282 |
firstTime = false;
|
283 |
} |
284 |
orientablePrimitive.addVertex(createPoint(inter.getX(), |
285 |
inter.getY())); |
286 |
} |
287 |
|
288 |
antInter = inter; |
289 |
antPoint = p2; |
290 |
} |
291 |
orientablePrimitive.closePrimitive(); |
292 |
|
293 |
return orientablePrimitive;
|
294 |
} |
295 |
|
296 |
/**
|
297 |
* Devuelve la geometr?a con el poligono regular inscrito en la
|
298 |
* circunferencia.
|
299 |
*
|
300 |
* @param point
|
301 |
* Puntero del rat?n.
|
302 |
* @param radio
|
303 |
* Radio
|
304 |
*
|
305 |
* @return GeometryCollection con las geometr?as del pol?gono.
|
306 |
* @throws ValueException
|
307 |
*/
|
308 |
private Geometry getIPolygon(Point2D point, double radio) { |
309 |
Point2D p1 = UtilFunctions.getPoint(center, point, radio);
|
310 |
double initangle = UtilFunctions.getAngle(center, point);
|
311 |
Point2D antPoint = p1;
|
312 |
double an = (Math.PI * 2) / numLines; |
313 |
boolean firstTime = true; |
314 |
|
315 |
OrientablePrimitive orientablePrimitive = createOrientablePrimitive(); |
316 |
|
317 |
for (int i = numLines - 1; i > 0; i--) { |
318 |
Point2D p2 =
|
319 |
UtilFunctions.getPoint(center, (an * i) + initangle, radio); |
320 |
|
321 |
if (firstTime) {
|
322 |
orientablePrimitive.addMoveToVertex(createPoint( |
323 |
antPoint.getX(), antPoint.getY())); |
324 |
firstTime = false;
|
325 |
} |
326 |
|
327 |
orientablePrimitive.addVertex(createPoint(p2.getX(), p2.getY())); |
328 |
|
329 |
antPoint = p2; |
330 |
} |
331 |
orientablePrimitive.closePrimitive(); |
332 |
|
333 |
return orientablePrimitive;
|
334 |
} |
335 |
|
336 |
public String getName() { |
337 |
return PluginServices.getText(this, "polygon_"); |
338 |
} |
339 |
|
340 |
/**
|
341 |
* Devuelve la cadena que corresponde al estado en el que nos encontramos.
|
342 |
*
|
343 |
* @return Cadena para mostrar por consola.
|
344 |
*/
|
345 |
public String getQuestion() { |
346 |
PolygonCADToolState actualState = _fsm.getState(); |
347 |
String status = actualState.getName();
|
348 |
|
349 |
if (status.equals("Polygon.NumberOrCenterPoint")) { |
350 |
return super.getQuestion() + "<" + numLines + ">"; |
351 |
} |
352 |
return super.getQuestion(); |
353 |
|
354 |
} |
355 |
|
356 |
public String toString() { |
357 |
return "_polygon"; |
358 |
} |
359 |
|
360 |
} |