root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / BreakCADTool.java @ 9243
History | View | Annotate | Download (30.5 KB)
1 | 5386 | 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.Graphics2D; |
||
45 | import java.awt.event.InputEvent; |
||
46 | import java.awt.geom.PathIterator; |
||
47 | import java.awt.geom.Point2D; |
||
48 | import java.awt.geom.Rectangle2D; |
||
49 | import java.io.IOException; |
||
50 | import java.util.ArrayList; |
||
51 | |||
52 | import com.iver.andami.PluginServices; |
||
53 | import com.iver.cit.gvsig.CADExtension; |
||
54 | import com.iver.cit.gvsig.fmap.ViewPort; |
||
55 | import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
||
56 | import com.iver.cit.gvsig.fmap.core.FShape; |
||
57 | import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
||
58 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
59 | import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
||
60 | 6008 | caballero | import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
61 | 5386 | caballero | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
62 | import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
||
63 | import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
||
64 | import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
||
65 | import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
||
66 | import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
||
67 | 5735 | caballero | import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
68 | 5386 | caballero | import com.iver.cit.gvsig.gui.cad.tools.smc.BreakCADToolContext; |
69 | import com.iver.cit.gvsig.gui.cad.tools.smc.BreakCADToolContext.BreakCADToolState; |
||
70 | import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
||
71 | |||
72 | |||
73 | /**
|
||
74 | 8936 | caballero | * Recorta una polil?nea en dos partes.
|
75 | 5386 | caballero | *
|
76 | * @author Vicente Caballero Navarro
|
||
77 | */
|
||
78 | public class BreakCADTool extends DefaultCADTool { |
||
79 | 6008 | caballero | private BreakCADToolContext _fsm;
|
80 | private Point2D firstPoint; |
||
81 | private Point2D secondPoint; |
||
82 | private DefaultRowEdited rowEdited;
|
||
83 | /**
|
||
84 | 5386 | caballero | * Crea un nuevo PolylineCADTool.
|
85 | */
|
||
86 | public BreakCADTool() {
|
||
87 | } |
||
88 | |||
89 | /**
|
||
90 | * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
||
91 | * carga previa a la utilizaci?n de la herramienta.
|
||
92 | */
|
||
93 | public void init() { |
||
94 | _fsm = new BreakCADToolContext(this); |
||
95 | firstPoint=null;
|
||
96 | secondPoint=null;
|
||
97 | } |
||
98 | |||
99 | /* (non-Javadoc)
|
||
100 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
|
||
101 | */
|
||
102 | public void transition(double x, double y, InputEvent event) { |
||
103 | _fsm.addPoint(x, y, event); |
||
104 | } |
||
105 | |||
106 | /* (non-Javadoc)
|
||
107 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
|
||
108 | */
|
||
109 | public void transition(double d) { |
||
110 | 5735 | caballero | _fsm.addValue(d); |
111 | 5386 | caballero | } |
112 | |||
113 | /* (non-Javadoc)
|
||
114 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
|
||
115 | */
|
||
116 | 5735 | caballero | public void transition(String s) throws CommandException { |
117 | 6008 | caballero | if (!super.changeCommand(s)){ |
118 | _fsm.addOption(s); |
||
119 | } |
||
120 | 5386 | caballero | } |
121 | |||
122 | /**
|
||
123 | * DOCUMENT ME!
|
||
124 | */
|
||
125 | public void selection() { |
||
126 | 6008 | caballero | ArrayList selectedRow=getSelectedRows();
|
127 | 5386 | caballero | if (selectedRow.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) { |
128 | CADExtension.setCADTool("_selection",false); |
||
129 | ((SelectionCADTool) CADExtension.getCADTool()).setNextTool( |
||
130 | "_break");
|
||
131 | } |
||
132 | } |
||
133 | |||
134 | /**
|
||
135 | * Equivale al transition del prototipo pero sin pasarle como par?metro el
|
||
136 | * editableFeatureSource que ya estar? creado.
|
||
137 | *
|
||
138 | * @param x par?metro x del punto que se pase en esta transici?n.
|
||
139 | * @param y par?metro y del punto que se pase en esta transici?n.
|
||
140 | */
|
||
141 | public void addPoint(double x, double y,InputEvent event) { |
||
142 | 6008 | caballero | BreakCADToolState actualState = (BreakCADToolState) _fsm.getPreviousState(); |
143 | 5386 | caballero | String status = actualState.getName();
|
144 | |||
145 | if (status.equals("Break.FirstPoint")) { |
||
146 | 6008 | caballero | // if (rowEdited!=null && intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new Point2D.Double(x,y)))
|
147 | firstPoint=new Point2D.Double(x,y); |
||
148 | 5386 | caballero | |
149 | } else if (status.equals("Break.SecondPoint")) { |
||
150 | // if (rowEdited !=null && intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new Point2D.Double(x,y))){
|
||
151 | 6008 | caballero | secondPoint=new Point2D.Double(x,y); |
152 | try {
|
||
153 | 5884 | caballero | // IGeometry geom=((DefaultFeature)rowEdited.getLinkedRow()).getGeometry();
|
154 | // if (geom instanceof FGeometryCollection) {
|
||
155 | // breakGeometryGC(rowEdited);
|
||
156 | // }else {
|
||
157 | 6008 | caballero | breakGeometry(rowEdited); |
158 | 5884 | caballero | // }
|
159 | 6008 | caballero | } catch (IOException e) { |
160 | e.printStackTrace(); |
||
161 | } catch (DriverIOException e) {
|
||
162 | e.printStackTrace(); |
||
163 | } |
||
164 | 5386 | caballero | } |
165 | //}
|
||
166 | } |
||
167 | 5884 | caballero | /* private void breakGeometryGC(DefaultRowEdited dre) throws IOException, DriverIOException {
|
168 | 6008 | caballero | GeneralPathX newGp1 = new GeneralPathX();
|
169 | GeneralPathX newGp2 = new GeneralPathX();
|
||
170 | FGeometryCollection gc=(FGeometryCollection)((DefaultFeature)rowEdited.getLinkedRow()).getGeometry();
|
||
171 | IGeometry[] geoms=gc.getGeometries();
|
||
172 | for (int i = 0;i<geoms.length;i++) {
|
||
173 | PathIterator theIterator=geoms[i].getPathIterator(null);
|
||
174 | double[] theData = new double[6];
|
||
175 | boolean isFirstPart=true;
|
||
176 | boolean isCut=false;
|
||
177 | int theType;
|
||
178 | int numParts = 0;
|
||
179 | 5386 | caballero | |
180 | 6008 | caballero | Point2D previous=null;
|
181 | 5884 | caballero | |
182 | 6008 | caballero | while (!theIterator.isDone()) {
|
183 | theType = theIterator.currentSegment(theData);
|
||
184 | switch (theType) {
|
||
185 | 5884 | caballero | |
186 | 6008 | caballero | case PathIterator.SEG_MOVETO:
|
187 | numParts++;
|
||
188 | previous=new Point2D.Double(theData[0], theData[1]);
|
||
189 | if (isFirstPart)
|
||
190 | newGp1.moveTo(theData[0], theData[1]);
|
||
191 | else
|
||
192 | newGp2.moveTo(theData[0], theData[1]);
|
||
193 | break;
|
||
194 | 5884 | caballero | |
195 | 6008 | caballero | case PathIterator.SEG_LINETO:
|
196 | if (previous!=null){
|
||
197 | GeneralPathX gpx=new GeneralPathX();
|
||
198 | gpx.moveTo(previous.getX(),previous.getY());
|
||
199 | gpx.lineTo(theData[0], theData[1]);
|
||
200 | IGeometry geom=ShapeFactory.createPolyline2D(gpx);
|
||
201 | Point2D p1=getNearPoint(previous);
|
||
202 | Point2D p2=getDistantPoint(previous);
|
||
203 | if (intersects(geom,p1)){
|
||
204 | isFirstPart=false;
|
||
205 | newGp1.lineTo(p1.getX(),p1.getY());
|
||
206 | newGp2.moveTo(p2.getX(),p2.getY());
|
||
207 | isCut=true;
|
||
208 | }
|
||
209 | }
|
||
210 | previous=new Point2D.Double(theData[0], theData[1]);
|
||
211 | if (isFirstPart)
|
||
212 | newGp1.lineTo(theData[0], theData[1]);
|
||
213 | else
|
||
214 | newGp2.lineTo(theData[0], theData[1]);
|
||
215 | break;
|
||
216 | 5884 | caballero | |
217 | 6008 | caballero | case PathIterator.SEG_QUADTO:
|
218 | if (previous!=null){
|
||
219 | GeneralPathX gpx=new GeneralPathX();
|
||
220 | gpx.moveTo(previous.getX(),previous.getY());
|
||
221 | gpx.quadTo(theData[0], theData[1],theData[2], theData[3]);
|
||
222 | IGeometry geom=ShapeFactory.createPolyline2D(gpx);
|
||
223 | Point2D p1=getNearPoint(previous);
|
||
224 | Point2D p2=getDistantPoint(previous);
|
||
225 | if (intersects(geom,p1)){
|
||
226 | isFirstPart=false;
|
||
227 | newGp1.lineTo(p1.getX(),p1.getY());
|
||
228 | newGp2.moveTo(p2.getX(),p2.getY());
|
||
229 | isCut=true;
|
||
230 | }
|
||
231 | }
|
||
232 | previous=new Point2D.Double(theData[0], theData[1]);
|
||
233 | if (isFirstPart)
|
||
234 | newGp1.quadTo(theData[0], theData[1],theData[2], theData[3]);
|
||
235 | else
|
||
236 | newGp2.quadTo(theData[0], theData[1],theData[2], theData[3]);
|
||
237 | 5884 | caballero | |
238 | 6008 | caballero | break;
|
239 | 5884 | caballero | |
240 | 6008 | caballero | case PathIterator.SEG_CUBICTO:
|
241 | if (previous!=null){
|
||
242 | GeneralPathX gpx=new GeneralPathX();
|
||
243 | gpx.moveTo(previous.getX(),previous.getY());
|
||
244 | gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
|
||
245 | IGeometry geom=ShapeFactory.createPolyline2D(gpx);
|
||
246 | Point2D p1=getNearPoint(previous);
|
||
247 | Point2D p2=getDistantPoint(previous);
|
||
248 | if (intersects(geom,p1)){
|
||
249 | isFirstPart=false;
|
||
250 | newGp1.lineTo(p1.getX(),p1.getY());
|
||
251 | newGp2.moveTo(p2.getX(),p2.getY());
|
||
252 | isCut=true;
|
||
253 | }
|
||
254 | }
|
||
255 | previous=new Point2D.Double(theData[0], theData[1]);
|
||
256 | if (isFirstPart)
|
||
257 | newGp1.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
|
||
258 | else
|
||
259 | newGp2.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
|
||
260 | 5884 | caballero | |
261 | 6008 | caballero | break;
|
262 | 5884 | caballero | |
263 | 6008 | caballero | case PathIterator.SEG_CLOSE:
|
264 | //if (isFirstPart)
|
||
265 | // newGp1.closePath();
|
||
266 | //else
|
||
267 | // newGp2.closePath();
|
||
268 | break;
|
||
269 | } //end switch
|
||
270 | 5884 | caballero | |
271 | 6008 | caballero | theIterator.next();
|
272 | } //end while loop
|
||
273 | 5884 | caballero | |
274 | 6008 | caballero | if (isCut) {
|
275 | IGeometry geom1 = ShapeFactory.createPolyline2D(newGp1);
|
||
276 | IGeometry geom2 = ShapeFactory.createPolyline2D(newGp2);
|
||
277 | VectorialLayerEdited vle = getVLE();
|
||
278 | VectorialEditableAdapter vea = vle.getVEA();
|
||
279 | ArrayList selectedRow = vle.getSelectedRow();
|
||
280 | vea.startComplexRow();
|
||
281 | vea.removeRow(dre.getIndex(), getName(), EditionEvent.GRAPHIC);
|
||
282 | int num = vea.getRowCount();
|
||
283 | if (gc.isClosed()) {
|
||
284 | ArrayList geomsAux1 = new ArrayList();
|
||
285 | geomsAux1.add(geom2);
|
||
286 | for (int k = i + 1; k < geoms.length; k++) {
|
||
287 | geomsAux1.add(geoms[k]);
|
||
288 | }
|
||
289 | for (int k = 0; k < i; k++) {
|
||
290 | geomsAux1.add(geoms[k]);
|
||
291 | }
|
||
292 | geomsAux1.add(geom1);
|
||
293 | 5884 | caballero | |
294 | 6008 | caballero | DefaultFeature df1 = new DefaultFeature(
|
295 | new FGeometryCollection((IGeometry[]) geomsAux1
|
||
296 | .toArray(new IGeometry[0])), dre
|
||
297 | .getAttributes(), String.valueOf(num));
|
||
298 | int index1 = vea.addRow(df1, PluginServices.getText(this,
|
||
299 | "parte1"), EditionEvent.GRAPHIC);
|
||
300 | 5884 | caballero | |
301 | 6008 | caballero | clearSelection();
|
302 | selectedRow.add(new DefaultRowEdited(df1,
|
||
303 | IRowEdited.STATUS_ADDED, index1));
|
||
304 | vea.endComplexRow();
|
||
305 | return;
|
||
306 | }else {
|
||
307 | 5884 | caballero | |
308 | 6008 | caballero | ArrayList geomsAux1 = new ArrayList();
|
309 | for (int k = 0; k < i; k++) {
|
||
310 | geomsAux1.add(geoms[k]);
|
||
311 | }
|
||
312 | geomsAux1.add(geom1);
|
||
313 | 5884 | caballero | |
314 | 6008 | caballero | ArrayList geomsAux2 = new ArrayList();
|
315 | geomsAux2.add(geom2);
|
||
316 | for (int k = i + 1; k < geoms.length; k++) {
|
||
317 | geomsAux2.add(geoms[k]);
|
||
318 | }
|
||
319 | 5884 | caballero | |
320 | 6008 | caballero | DefaultFeature df1 = new DefaultFeature(
|
321 | new FGeometryCollection((IGeometry[]) geomsAux1
|
||
322 | .toArray(new IGeometry[0])), dre
|
||
323 | .getAttributes(), String.valueOf(num));
|
||
324 | int index1 = vea.addRow(df1, PluginServices.getText(this,
|
||
325 | "parte1"), EditionEvent.GRAPHIC);
|
||
326 | DefaultFeature df2 = new DefaultFeature(
|
||
327 | new FGeometryCollection((IGeometry[]) geomsAux2
|
||
328 | .toArray(new IGeometry[0])), dre
|
||
329 | .getAttributes(), String.valueOf(num + 1));
|
||
330 | int index2 = vea.addRow(df2, PluginServices.getText(this,
|
||
331 | "parte2"), EditionEvent.GRAPHIC);
|
||
332 | clearSelection();
|
||
333 | selectedRow.add(new DefaultRowEdited(df2,
|
||
334 | IRowEdited.STATUS_ADDED, index2));
|
||
335 | selectedRow.add(new DefaultRowEdited(df1,
|
||
336 | IRowEdited.STATUS_ADDED, index1));
|
||
337 | 5884 | caballero | |
338 | 6008 | caballero | vea.endComplexRow();
|
339 | return;
|
||
340 | }
|
||
341 | 5884 | caballero | |
342 | 6008 | caballero | }
|
343 | }
|
||
344 | }
|
||
345 | 5884 | caballero | */
|
346 | |||
347 | 6008 | caballero | private void breakGeometry(DefaultRowEdited dre) throws IOException, DriverIOException { |
348 | 8936 | caballero | breakGeom(dre); |
349 | } |
||
350 | |||
351 | |||
352 | private void breakGeom(DefaultRowEdited dre) throws DriverIOException, IOException { |
||
353 | GeneralPathX newGp1 = new GeneralPathX();
|
||
354 | 6008 | caballero | GeneralPathX newGp2 = new GeneralPathX();
|
355 | IGeometry geomAux=((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(); |
||
356 | 8936 | caballero | PathIterator theIterator=geomAux.getPathIterator(null,FConverter.FLATNESS); |
357 | 6008 | caballero | Point2D[] pointsOrdered=getOrderPoints(geomAux); |
358 | double[] theData = new double[6]; |
||
359 | boolean isFirstPart=true; |
||
360 | boolean intersectsP2=false; |
||
361 | int theType;
|
||
362 | int numParts = 0; |
||
363 | 8936 | caballero | boolean isBreaked=false; |
364 | 6008 | caballero | Point2D previous=null; |
365 | 5884 | caballero | |
366 | 6008 | caballero | while (!theIterator.isDone()) {
|
367 | theType = theIterator.currentSegment(theData); |
||
368 | switch (theType) {
|
||
369 | 5386 | caballero | |
370 | 6008 | caballero | case PathIterator.SEG_MOVETO: |
371 | numParts++; |
||
372 | 5386 | caballero | |
373 | 6008 | caballero | previous=new Point2D.Double(theData[0], theData[1]); |
374 | 5386 | caballero | |
375 | 6008 | caballero | if (isFirstPart)
|
376 | newGp1.moveTo(theData[0], theData[1]); |
||
377 | else
|
||
378 | newGp2.moveTo(theData[0], theData[1]); |
||
379 | break;
|
||
380 | 5386 | caballero | |
381 | 6008 | caballero | case PathIterator.SEG_LINETO: |
382 | 5386 | caballero | |
383 | 6008 | caballero | if (previous!=null){ |
384 | GeneralPathX gpx=new GeneralPathX();
|
||
385 | gpx.moveTo(previous.getX(),previous.getY()); |
||
386 | gpx.lineTo(theData[0], theData[1]); |
||
387 | IGeometry geom=ShapeFactory.createPolyline2D(gpx); |
||
388 | Point2D p1=pointsOrdered[0]; |
||
389 | Point2D p2=pointsOrdered[1]; |
||
390 | 5386 | caballero | |
391 | 8936 | caballero | if (intersects(geom,p1) && !isBreaked){
|
392 | isFirstPart=false;
|
||
393 | 6008 | caballero | newGp1.lineTo(p1.getX(),p1.getY()); |
394 | } |
||
395 | 8936 | caballero | if (intersects(geom,p2) && !isBreaked) {
|
396 | isBreaked=true;
|
||
397 | 6008 | caballero | intersectsP2=true;
|
398 | newGp2.moveTo(p2.getX(),p2.getY()); |
||
399 | } |
||
400 | } |
||
401 | previous=new Point2D.Double(theData[0], theData[1]); |
||
402 | if (isFirstPart)
|
||
403 | newGp1.lineTo(theData[0], theData[1]); |
||
404 | else if (intersectsP2){ |
||
405 | newGp2.lineTo(theData[0], theData[1]); |
||
406 | } |
||
407 | 5386 | caballero | |
408 | 6008 | caballero | break;
|
409 | 5386 | caballero | |
410 | 6008 | caballero | case PathIterator.SEG_QUADTO: |
411 | if (previous!=null){ |
||
412 | GeneralPathX gpx=new GeneralPathX();
|
||
413 | gpx.moveTo(previous.getX(),previous.getY()); |
||
414 | gpx.quadTo(theData[0], theData[1],theData[2], theData[3]); |
||
415 | IGeometry geom=ShapeFactory.createPolyline2D(gpx); |
||
416 | Point2D p1=pointsOrdered[0]; |
||
417 | Point2D p2=pointsOrdered[1]; |
||
418 | 8936 | caballero | if (intersects(geom,p1) && !isBreaked){
|
419 | isFirstPart=false;
|
||
420 | 6008 | caballero | newGp1.lineTo(p1.getX(),p1.getY()); |
421 | 8936 | caballero | } |
422 | if (intersects(geom,p2) && !isBreaked) {
|
||
423 | isBreaked=true;
|
||
424 | intersectsP2=true;
|
||
425 | 6008 | caballero | newGp2.moveTo(p2.getX(),p2.getY()); |
426 | 8936 | caballero | |
427 | 6008 | caballero | } |
428 | } |
||
429 | previous=new Point2D.Double(theData[0], theData[1]); |
||
430 | if (isFirstPart)
|
||
431 | newGp1.quadTo(theData[0], theData[1],theData[2], theData[3]); |
||
432 | else
|
||
433 | newGp2.quadTo(theData[0], theData[1],theData[2], theData[3]); |
||
434 | 5386 | caballero | |
435 | 6008 | caballero | break;
|
436 | 5386 | caballero | |
437 | 6008 | caballero | case PathIterator.SEG_CUBICTO: |
438 | if (previous!=null){ |
||
439 | GeneralPathX gpx=new GeneralPathX();
|
||
440 | gpx.moveTo(previous.getX(),previous.getY()); |
||
441 | gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]); |
||
442 | IGeometry geom=ShapeFactory.createPolyline2D(gpx); |
||
443 | Point2D p1=pointsOrdered[0]; |
||
444 | Point2D p2=pointsOrdered[1]; |
||
445 | 8936 | caballero | if (intersects(geom,p1) && !isBreaked){
|
446 | isFirstPart=false;
|
||
447 | 6008 | caballero | newGp1.lineTo(p1.getX(),p1.getY()); |
448 | 8936 | caballero | } |
449 | if (intersects(geom,p2) && !isBreaked) {
|
||
450 | isBreaked=true;
|
||
451 | intersectsP2=true;
|
||
452 | 6008 | caballero | newGp2.moveTo(p2.getX(),p2.getY()); |
453 | 8936 | caballero | |
454 | 6008 | caballero | } |
455 | 8936 | caballero | } |
456 | 6008 | caballero | previous=new Point2D.Double(theData[0], theData[1]); |
457 | if (isFirstPart)
|
||
458 | newGp1.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]); |
||
459 | else
|
||
460 | newGp2.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]); |
||
461 | 5908 | caballero | |
462 | 6008 | caballero | break;
|
463 | 5884 | caballero | |
464 | 6008 | caballero | case PathIterator.SEG_CLOSE: |
465 | //if (isFirstPart)
|
||
466 | // newGp1.closePath();
|
||
467 | //else
|
||
468 | // newGp2.closePath();
|
||
469 | break;
|
||
470 | } //end switch
|
||
471 | 5960 | caballero | |
472 | 6008 | caballero | theIterator.next(); |
473 | } //end while loop
|
||
474 | GeneralPathX gpx=new GeneralPathX();
|
||
475 | gpx.append(geomAux.getInternalShape(),true);
|
||
476 | 8936 | caballero | VectorialLayerEdited vle = getVLE(); |
477 | VectorialEditableAdapter vea = vle.getVEA(); |
||
478 | ArrayList selectedRowAux=new ArrayList(); |
||
479 | vea.startComplexRow(); |
||
480 | 6008 | caballero | if (gpx.isClosed()) {
|
481 | 8936 | caballero | |
482 | 7072 | caballero | newGp2.append(newGp1.getPathIterator(null,FConverter.FLATNESS),true); |
483 | 6008 | caballero | IGeometry geom1=ShapeFactory.createPolyline2D(newGp2); |
484 | 5386 | caballero | |
485 | 8936 | caballero | DefaultFeature dfLine1 = (DefaultFeature) dre.getLinkedRow().cloneRow(); |
486 | dfLine1.setGeometry(geom1); |
||
487 | int indexLine1 = addGeometry(geom1, dfLine1
|
||
488 | .getAttributes()); |
||
489 | 5386 | caballero | |
490 | 8936 | caballero | selectedRowAux.add(new DefaultRowEdited(dfLine1,
|
491 | IRowEdited.STATUS_ADDED, indexLine1)); |
||
492 | 5386 | caballero | |
493 | 8936 | caballero | }else {
|
494 | IGeometry geom1=ShapeFactory.createPolyline2D(newGp1); |
||
495 | IGeometry geom2=ShapeFactory.createPolyline2D(newGp2); |
||
496 | |||
497 | DefaultFeature dfLine1 = (DefaultFeature) dre.getLinkedRow().cloneRow(); |
||
498 | dfLine1.setGeometry(geom1); |
||
499 | int indexLine1 = addGeometry(geom1, dfLine1
|
||
500 | .getAttributes()); |
||
501 | |||
502 | selectedRowAux.add(new DefaultRowEdited(dfLine1,
|
||
503 | IRowEdited.STATUS_ADDED, indexLine1)); |
||
504 | |||
505 | DefaultFeature dfLine2 = (DefaultFeature) dre.getLinkedRow().cloneRow(); |
||
506 | dfLine2.setGeometry(geom2); |
||
507 | int indexLine2 = addGeometry(geom2, dfLine2
|
||
508 | .getAttributes()); |
||
509 | |||
510 | selectedRowAux.add(new DefaultRowEdited(dfLine2,
|
||
511 | IRowEdited.STATUS_ADDED, indexLine2)); |
||
512 | |||
513 | 6008 | caballero | } |
514 | 8936 | caballero | vea.removeRow(dre.getIndex(), getName(), EditionEvent.GRAPHIC); |
515 | vea.endComplexRow(getName()); |
||
516 | vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux); |
||
517 | |||
518 | 6008 | caballero | } |
519 | |||
520 | 8936 | caballero | |
521 | 6008 | caballero | private Point2D[] getOrderPoints(IGeometry geomAux) { |
522 | 7072 | caballero | PathIterator theIterator=geomAux.getPathIterator(null,FConverter.FLATNESS); |
523 | 6008 | caballero | double[] theData = new double[6]; |
524 | Point2D previous=null; |
||
525 | ArrayList points =new ArrayList(); |
||
526 | 8936 | caballero | boolean isFirstPointBreak=false; |
527 | boolean isSecondPointBreak=false; |
||
528 | 6008 | caballero | while (!theIterator.isDone()) {
|
529 | int theType = theIterator.currentSegment(theData);
|
||
530 | switch (theType) {
|
||
531 | |||
532 | case PathIterator.SEG_MOVETO: |
||
533 | previous=new Point2D.Double(theData[0], theData[1]); |
||
534 | break;
|
||
535 | |||
536 | case PathIterator.SEG_LINETO: |
||
537 | |||
538 | if (previous!=null){ |
||
539 | GeneralPathX gpx=new GeneralPathX();
|
||
540 | gpx.moveTo(previous.getX(),previous.getY()); |
||
541 | gpx.lineTo(theData[0], theData[1]); |
||
542 | IGeometry geom=ShapeFactory.createPolyline2D(gpx); |
||
543 | boolean intersectFirst=intersects(geom,firstPoint);
|
||
544 | boolean intersectSecond=intersects(geom,secondPoint);
|
||
545 | 8936 | caballero | if (intersectFirst && intersectSecond && !isFirstPointBreak){
|
546 | isFirstPointBreak=true;
|
||
547 | isSecondPointBreak=true;
|
||
548 | points.add(getNearPoint(previous)); |
||
549 | 6008 | caballero | points.add(getDistantPoint(previous)); |
550 | return (Point2D[])points.toArray(new Point2D[0]); |
||
551 | 8936 | caballero | }else if (intersectFirst && !isFirstPointBreak) { |
552 | isFirstPointBreak=true;
|
||
553 | 6008 | caballero | points.add(firstPoint); |
554 | 8936 | caballero | }else if (intersectSecond && !isSecondPointBreak) { |
555 | isSecondPointBreak=true;
|
||
556 | points.add(secondPoint); |
||
557 | 6008 | caballero | } |
558 | } |
||
559 | previous=new Point2D.Double(theData[0], theData[1]); |
||
560 | break;
|
||
561 | |||
562 | case PathIterator.SEG_QUADTO: |
||
563 | if (previous!=null){ |
||
564 | GeneralPathX gpx=new GeneralPathX();
|
||
565 | gpx.moveTo(previous.getX(),previous.getY()); |
||
566 | gpx.quadTo(theData[0], theData[1],theData[2], theData[3]); |
||
567 | IGeometry geom=ShapeFactory.createPolyline2D(gpx); |
||
568 | boolean intersectFirst=intersects(geom,firstPoint);
|
||
569 | boolean intersectSecond=intersects(geom,secondPoint);
|
||
570 | 8936 | caballero | if (intersectFirst && intersectSecond && !isFirstPointBreak){
|
571 | isFirstPointBreak=true;
|
||
572 | isSecondPointBreak=true;
|
||
573 | 6008 | caballero | points.add(getNearPoint(previous)); |
574 | points.add(getDistantPoint(previous)); |
||
575 | return (Point2D[])points.toArray(new Point2D[0]); |
||
576 | 8936 | caballero | }else if (intersectFirst && !isFirstPointBreak){ |
577 | isFirstPointBreak=true;
|
||
578 | points.add(firstPoint); |
||
579 | }else if (intersectSecond && !isSecondPointBreak){ |
||
580 | isSecondPointBreak=true;
|
||
581 | points.add(secondPoint); |
||
582 | 6008 | caballero | } |
583 | } |
||
584 | previous=new Point2D.Double(theData[0], theData[1]); |
||
585 | |||
586 | break;
|
||
587 | |||
588 | case PathIterator.SEG_CUBICTO: |
||
589 | if (previous!=null){ |
||
590 | GeneralPathX gpx=new GeneralPathX();
|
||
591 | gpx.moveTo(previous.getX(),previous.getY()); |
||
592 | gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]); |
||
593 | IGeometry geom=ShapeFactory.createPolyline2D(gpx); |
||
594 | boolean intersectFirst=intersects(geom,firstPoint);
|
||
595 | boolean intersectSecond=intersects(geom,secondPoint);
|
||
596 | 8936 | caballero | if (intersectFirst && intersectSecond && !isFirstPointBreak){
|
597 | isFirstPointBreak=true;
|
||
598 | isSecondPointBreak=true;
|
||
599 | points.add(getNearPoint(previous)); |
||
600 | 6008 | caballero | points.add(getDistantPoint(previous)); |
601 | return (Point2D[])points.toArray(new Point2D[0]); |
||
602 | 8936 | caballero | }else if (intersectFirst && !isFirstPointBreak){ |
603 | isFirstPointBreak=true;
|
||
604 | points.add(firstPoint); |
||
605 | }else if (intersectSecond && !isSecondPointBreak){ |
||
606 | isSecondPointBreak=true;
|
||
607 | 6008 | caballero | points.add(secondPoint); |
608 | } |
||
609 | } |
||
610 | previous=new Point2D.Double(theData[0], theData[1]); |
||
611 | |||
612 | break;
|
||
613 | |||
614 | case PathIterator.SEG_CLOSE: |
||
615 | //if (isFirstPart)
|
||
616 | // newGp1.closePath();
|
||
617 | //else
|
||
618 | // newGp2.closePath();
|
||
619 | break;
|
||
620 | } //end switch
|
||
621 | |||
622 | theIterator.next(); |
||
623 | } //end while loop
|
||
624 | |||
625 | return (Point2D[])points.toArray(new Point2D[0]); |
||
626 | } |
||
627 | private Point2D getDistantPoint(Point2D previous){ |
||
628 | if (firstPoint.distance(previous)>secondPoint.distance(previous)){
|
||
629 | return firstPoint;
|
||
630 | } |
||
631 | return secondPoint;
|
||
632 | } |
||
633 | private Point2D getNearPoint(Point2D previous) { |
||
634 | if (firstPoint.distance(previous)<=secondPoint.distance(previous)){
|
||
635 | return firstPoint;
|
||
636 | } |
||
637 | return secondPoint;
|
||
638 | } |
||
639 | /**
|
||
640 | 5386 | caballero | * M?todo para dibujar la lo necesario para el estado en el que nos
|
641 | * encontremos.
|
||
642 | *
|
||
643 | * @param g Graphics sobre el que dibujar.
|
||
644 | * @param x par?metro x del punto que se pase para dibujar.
|
||
645 | * @param y par?metro x del punto que se pase para dibujar.
|
||
646 | */
|
||
647 | public void drawOperation(Graphics g, double x, double y) { |
||
648 | 6008 | caballero | VectorialLayerEdited vle=getVLE(); |
649 | 5386 | caballero | ArrayList selectedRows=vle.getSelectedRow();
|
650 | ViewPort vp=CADExtension.getEditionManager().getMapControl().getViewPort(); |
||
651 | if (selectedRows.size()==1){ |
||
652 | 6008 | caballero | if (firstPoint!=null){ |
653 | IGeometry g1=ShapeFactory.createCircle(firstPoint,vp.toMapDistance(3));
|
||
654 | IGeometry g2=ShapeFactory.createCircle(firstPoint,vp.toMapDistance(5));
|
||
655 | g1.draw((Graphics2D)g,vp,DefaultCADTool.modifySymbol);
|
||
656 | g2.draw((Graphics2D)g,vp,DefaultCADTool.modifySymbol);
|
||
657 | } |
||
658 | 8936 | caballero | rowEdited = (DefaultRowEdited)selectedRows.get(0);
|
659 | 6008 | caballero | //.getLinkedRow()
|
660 | // .cloneRow();
|
||
661 | IGeometry geom=((DefaultFeature)rowEdited.getLinkedRow()).getGeometry().cloneGeometry(); |
||
662 | if (intersects(geom,new Point2D.Double(x,y))){ |
||
663 | geom.draw((Graphics2D)g,vp,DefaultCADTool.drawingSymbol);
|
||
664 | } |
||
665 | 5386 | caballero | } |
666 | } |
||
667 | public boolean intersects(double x,double y){ |
||
668 | 6008 | caballero | Point2D p = new Point2D.Double(x, y); |
669 | VectorialLayerEdited vle = getVLE(); |
||
670 | ArrayList selectedRows = vle.getSelectedRow();
|
||
671 | if (selectedRows.size() == 1) { |
||
672 | 8936 | caballero | rowEdited = (DefaultRowEdited) selectedRows |
673 | .get(0);
|
||
674 | 6008 | caballero | IGeometry g = ((DefaultFeature) rowEdited.getLinkedRow()) |
675 | .getGeometry().cloneGeometry(); |
||
676 | return intersects(g, p);
|
||
677 | } |
||
678 | return false; |
||
679 | 5386 | caballero | } |
680 | private boolean intersects(IGeometry geom, Point2D p){ |
||
681 | 6008 | caballero | double tol = 1; |
682 | tol = CADExtension.getEditionManager().getMapControl().getViewPort() |
||
683 | .toMapDistance((int) tol);
|
||
684 | Rectangle2D r = new Rectangle2D.Double(p.getX() - tol / 2, p.getY() |
||
685 | - tol / 2, tol, tol);
|
||
686 | return (geom.intersects(r) && !geom.contains(r));
|
||
687 | 5386 | caballero | } |
688 | /**
|
||
689 | * Add a diferent option.
|
||
690 | *
|
||
691 | * @param s Diferent option.
|
||
692 | */
|
||
693 | public void addOption(String s) { |
||
694 | 6008 | caballero | if (s.equals(PluginServices.getText(this,"cancel"))|| s.equals("c")|| s.equals("C")){ |
695 | init(); |
||
696 | } |
||
697 | 5386 | caballero | } |
698 | /* (non-Javadoc)
|
||
699 | * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
||
700 | */
|
||
701 | public void addValue(double d) { |
||
702 | } |
||
703 | public String getName() { |
||
704 | 6008 | caballero | return PluginServices.getText(this,"break_"); |
705 | } |
||
706 | 5386 | caballero | |
707 | 6008 | caballero | public String toString() { |
708 | return "_break"; |
||
709 | } |
||
710 | 5386 | caballero | |
711 | 6008 | caballero | public boolean isApplicable(int shapeType) { |
712 | switch (shapeType) {
|
||
713 | case FShape.MULTI:
|
||
714 | case FShape.LINE:
|
||
715 | return true; |
||
716 | } |
||
717 | return false; |
||
718 | } |
||
719 | 5386 | caballero | |
720 | |||
721 | } |