Revision 5386

View differences:

trunk/extensions/extCAD/src/com/iver/cit/gvsig/gui/cad/tools/BreakCADTool.java
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.PathIterator;
47
import java.awt.geom.Point2D;
48
import java.awt.geom.Rectangle2D;
49
import java.awt.image.BufferedImage;
50
import java.io.IOException;
51
import java.util.ArrayList;
52

  
53
import com.iver.andami.PluginServices;
54
import com.iver.cit.gvsig.CADExtension;
55
import com.iver.cit.gvsig.fmap.ViewPort;
56
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
57
import com.iver.cit.gvsig.fmap.core.FShape;
58
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
59
import com.iver.cit.gvsig.fmap.core.IGeometry;
60
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
61
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.CADTool;
67
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
68
import com.iver.cit.gvsig.gui.cad.exception.CommadException;
69
import com.iver.cit.gvsig.gui.cad.tools.smc.BreakCADToolContext;
70
import com.iver.cit.gvsig.gui.cad.tools.smc.BreakCADToolContext.BreakCADToolState;
71
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
72

  
73

  
74
/**
75
 * DOCUMENT ME!
76
 *
77
 * @author Vicente Caballero Navarro
78
 */
79
public class BreakCADTool extends DefaultCADTool {
80
	private BreakCADToolContext _fsm;
81
	private Point2D firstPoint;
82
	private Point2D secondPoint;
83
	private DefaultRowEdited rowEdited;
84
	/**
85
     * Crea un nuevo PolylineCADTool.
86
     */
87
    public BreakCADTool() {
88
    }
89

  
90
    /**
91
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
92
     * carga previa a la utilizaci?n de la herramienta.
93
     */
94
    public void init() {
95
        _fsm = new BreakCADToolContext(this);
96
        firstPoint=null;
97
        secondPoint=null;
98
    }
99

  
100
    /* (non-Javadoc)
101
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
102
     */
103
    public void transition(double x, double y, InputEvent event) {
104
        _fsm.addPoint(x, y, event);
105
    }
106

  
107
    /* (non-Javadoc)
108
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
109
     */
110
    public void transition(double d) {
111
        //_fsm.addValue(sel,d);
112
    }
113

  
114
    /* (non-Javadoc)
115
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
116
     */
117
    public void transition(String s) throws CommadException {
118
    	if (!super.changeCommand(s)){
119
    		_fsm.addOption(s);
120
    	}
121
    }
122

  
123
    /**
124
     * DOCUMENT ME!
125
     */
126
    public void selection() {
127
    	ArrayList selectedRow=getSelectedRows();
128
        if (selectedRow.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
129
            CADExtension.setCADTool("_selection",false);
130
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
131
                "_break");
132
        }
133
    }
134

  
135
    /**
136
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
137
     * editableFeatureSource que ya estar? creado.
138
     *
139
     * @param x par?metro x del punto que se pase en esta transici?n.
140
     * @param y par?metro y del punto que se pase en esta transici?n.
141
     */
142
    public void addPoint(double x, double y,InputEvent event) {
143
    	 BreakCADToolState actualState = (BreakCADToolState) _fsm.getPreviousState();
144
         String status = actualState.getName();
145

  
146
         if (status.equals("Break.FirstPoint")) {
147
        	// if (rowEdited!=null && intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new Point2D.Double(x,y)))
148
        		 firstPoint=new Point2D.Double(x,y);
149

  
150
         } else if (status.equals("Break.SecondPoint")) {
151
          // if (rowEdited !=null && intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new Point2D.Double(x,y))){
152
        	   secondPoint=new Point2D.Double(x,y);
153
        	   try {
154
				breakGeometry(rowEdited);
155
			} catch (IOException e) {
156
				e.printStackTrace();
157
			} catch (DriverIOException e) {
158
				e.printStackTrace();
159
			}
160
           }
161
         //}
162
    }
163

  
164
	private void breakGeometry(DefaultRowEdited dre) throws IOException, DriverIOException {
165
		GeneralPathX newGp1 = new GeneralPathX();
166
		GeneralPathX newGp2 = new GeneralPathX();
167

  
168
		PathIterator theIterator=((DefaultFeature)dre.getLinkedRow()).getGeometry().getPathIterator(null);
169
		double[] theData = new double[6];
170
		boolean isFirstPart=true;
171
		int theType;
172
	    int numParts = 0;
173
	    Point2D previous=null;
174

  
175
	        while (!theIterator.isDone()) {
176
	            theType = theIterator.currentSegment(theData);
177
	            switch (theType) {
178

  
179
	                case PathIterator.SEG_MOVETO:
180
	                    numParts++;
181

  
182
	                    previous=new Point2D.Double(theData[0], theData[1]);
183

  
184
	                    if (isFirstPart)
185
	                		newGp1.moveTo(theData[0], theData[1]);
186
	                	else
187
	                		newGp2.moveTo(theData[0], theData[1]);
188
	                	break;
189

  
190
	                case PathIterator.SEG_LINETO:
191
	                	 if (previous!=null){
192
		                    	GeneralPathX gpx=new GeneralPathX();
193
		                    	gpx.moveTo(previous.getX(),previous.getY());
194
		                    	gpx.lineTo(theData[0], theData[1]);
195
		                    	IGeometry geom=ShapeFactory.createPolyline2D(gpx);
196
		                    	Point2D p1=getNearPoint(previous);
197
		                    	Point2D p2=getDistantPoint(previous);
198
		                    	if (intersects(geom,p1)){
199
		                    		isFirstPart=false;
200
		                    		newGp1.lineTo(p1.getX(),p1.getY());
201
		                    		newGp2.moveTo(p2.getX(),p2.getY());
202
		                    	}
203
		                    }
204
	                	 previous=new Point2D.Double(theData[0], theData[1]);
205
	                	if (isFirstPart)
206
	                		newGp1.lineTo(theData[0], theData[1]);
207
	                	else
208
	                		newGp2.lineTo(theData[0], theData[1]);
209
	                	break;
210

  
211
	                case PathIterator.SEG_QUADTO:
212
	                	 if (previous!=null){
213
		                    	GeneralPathX gpx=new GeneralPathX();
214
		                    	gpx.moveTo(previous.getX(),previous.getY());
215
		                    	gpx.quadTo(theData[0], theData[1],theData[2], theData[3]);
216
		                    	IGeometry geom=ShapeFactory.createPolyline2D(gpx);
217
		                    	Point2D p1=getNearPoint(previous);
218
		                    	Point2D p2=getDistantPoint(previous);
219
		                    	if (intersects(geom,p1)){
220
		                    		isFirstPart=false;
221
		                    		newGp1.lineTo(p1.getX(),p1.getY());
222
		                    		newGp2.moveTo(p2.getX(),p2.getY());
223
		                    	}
224
		                    }
225
	                	 previous=new Point2D.Double(theData[0], theData[1]);
226
	                	if (isFirstPart)
227
	                		newGp1.quadTo(theData[0], theData[1],theData[2], theData[3]);
228
	                	else
229
	                		newGp2.quadTo(theData[0], theData[1],theData[2], theData[3]);
230

  
231
	                	break;
232

  
233
	                case PathIterator.SEG_CUBICTO:
234
	                	 if (previous!=null){
235
		                    	GeneralPathX gpx=new GeneralPathX();
236
		                    	gpx.moveTo(previous.getX(),previous.getY());
237
		                    	gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
238
		                    	IGeometry geom=ShapeFactory.createPolyline2D(gpx);
239
		                    	Point2D p1=getNearPoint(previous);
240
		                    	Point2D p2=getDistantPoint(previous);
241
		                    	if (intersects(geom,p1)){
242
		                    		isFirstPart=false;
243
		                    		newGp1.lineTo(p1.getX(),p1.getY());
244
		                    		newGp2.moveTo(p2.getX(),p2.getY());
245
		                    	}
246
		                    }
247
	                	 previous=new Point2D.Double(theData[0], theData[1]);
248
	                	if (isFirstPart)
249
	                		newGp1.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
250
	                	else
251
	                		newGp2.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
252

  
253
	                    break;
254

  
255
	                case PathIterator.SEG_CLOSE:
256
	                	//if (isFirstPart)
257
	                	//	newGp1.closePath();
258
	                	//else
259
	                	//	newGp2.closePath();
260
	                    break;
261
	            } //end switch
262

  
263
	            theIterator.next();
264
	        } //end while loop
265
	        IGeometry geom1=ShapeFactory.createPolyline2D(newGp1);
266
	        IGeometry geom2=ShapeFactory.createPolyline2D(newGp2);
267
	        DefaultFeature df1 = new DefaultFeature(geom1, dre.getAttributes());
268
	        DefaultFeature df2 = new DefaultFeature(geom2, dre.getAttributes());
269
	        VectorialEditableAdapter vea = getVLE().getVEA();
270
	        vea.startComplexRow();
271
	        vea.removeRow(dre.getIndex(),getName(),EditionEvent.GRAPHIC);
272
	        int index1=vea.addRow(df1,"parte1",EditionEvent.GRAPHIC);
273
	        int index2=vea.addRow(df2,"parte2",EditionEvent.GRAPHIC);
274
	        vea.endComplexRow();
275
	        ViewPort vp=CADExtension.getEditionManager().getMapControl().getViewPort();
276
	        BufferedImage selectionImage = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(), BufferedImage.TYPE_INT_ARGB);
277
			Graphics2D gs = selectionImage.createGraphics();
278
			VectorialLayerEdited vle = getVLE();
279
			ArrayList selectedHandler = vle.getSelectedHandler();
280
			ArrayList selectedRow = vle.getSelectedRow();
281
			selectedHandler.clear();
282
			selectedRow.clear();
283
			selectedRow.add(new DefaultRowEdited(df1, IRowEdited.STATUS_ADDED, index1));
284
			selectedRow.add(new DefaultRowEdited(df2, IRowEdited.STATUS_ADDED, index2));
285
			geom1.cloneGeometry().draw(gs, vp, CADTool.drawingSymbol);
286
			vle.drawHandlers(geom1.cloneGeometry(),gs,vp);
287
			geom2.cloneGeometry().draw(gs, vp, CADTool.drawingSymbol);
288
			vle.drawHandlers(geom2.cloneGeometry(),gs,vp);
289
			vea.setSelectionImage(selectionImage);
290

  
291

  
292

  
293
	}
294

  
295
	private Point2D getNearPoint(Point2D previous) {
296
		if (firstPoint.distance(previous)<=secondPoint.distance(previous)){
297
			return firstPoint;
298
		}
299
		return secondPoint;
300

  
301
	}
302
	private Point2D getDistantPoint(Point2D previous){
303
		if (firstPoint.distance(previous)>secondPoint.distance(previous)){
304
			return firstPoint;
305
		}
306
		return secondPoint;
307
	}
308

  
309
	/**
310
     * M?todo para dibujar la lo necesario para el estado en el que nos
311
     * encontremos.
312
     *
313
     * @param g Graphics sobre el que dibujar.
314
     * @param x par?metro x del punto que se pase para dibujar.
315
     * @param y par?metro x del punto que se pase para dibujar.
316
     */
317
    public void drawOperation(Graphics g, double x, double y) {
318
    	 VectorialLayerEdited vle=getVLE();
319
         ArrayList selectedRows=vle.getSelectedRow();
320
         ViewPort vp=CADExtension.getEditionManager().getMapControl().getViewPort();
321
         if (selectedRows.size()==1){
322
        	 if (firstPoint!=null){
323
        		 IGeometry g1=ShapeFactory.createCircle(firstPoint,vp.toMapDistance(3));
324
        		 IGeometry g2=ShapeFactory.createCircle(firstPoint,vp.toMapDistance(5));
325
        		 g1.draw((Graphics2D)g,vp,DefaultCADTool.modifySymbol);
326
        		 g2.draw((Graphics2D)g,vp,DefaultCADTool.modifySymbol);
327
        	 }
328
        	 rowEdited = (DefaultRowEdited)((DefaultRowEdited)selectedRows.get(0));
329
             	//.getLinkedRow()
330
             	//	.cloneRow();
331
        	 IGeometry geom=((DefaultFeature)rowEdited.getLinkedRow()).getGeometry().cloneGeometry();
332
        	 if (intersects(geom,new Point2D.Double(x,y))){
333
        		 geom.draw((Graphics2D)g,vp,DefaultCADTool.drawingSymbol);
334
        	 }
335
         }
336
    }
337
    public boolean intersects(double x,double y){
338
    	Point2D p = new Point2D.Double(x, y);
339
		VectorialLayerEdited vle = getVLE();
340
		ArrayList selectedRows = vle.getSelectedRow();
341
		if (selectedRows.size() == 1) {
342
			rowEdited = (DefaultRowEdited) ((DefaultRowEdited) selectedRows
343
					.get(0));
344
			IGeometry g = ((DefaultFeature) rowEdited.getLinkedRow())
345
					.getGeometry().cloneGeometry();
346
			return intersects(g, p);
347
		}
348
		return false;
349
    }
350
    private boolean intersects(IGeometry geom, Point2D p){
351
    	double tol = 1;
352
		tol = CADExtension.getEditionManager().getMapControl().getViewPort()
353
				.toMapDistance((int) tol);
354
		Rectangle2D r = new Rectangle2D.Double(p.getX() - tol / 2, p.getY()
355
				- tol / 2, tol, tol);
356
		return (geom.intersects(r) && !geom.contains(r));
357
    }
358
    /**
359
     * Add a diferent option.
360
     *
361
     * @param s Diferent option.
362
     */
363
    public void addOption(String s) {
364
    	if (s.equals(PluginServices.getText(this,"cancel"))|| s.equals("c")|| s.equals("C")){
365
    		init();
366
    	}
367
    }
368
    /* (non-Javadoc)
369
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
370
     */
371
    public void addValue(double d) {
372
    }
373
    public String getName() {
374
		return PluginServices.getText(this,"break_");
375
	}
376

  
377
	public String toString() {
378
		return "_break";
379
	}
380

  
381
	public boolean isApplicable(int shapeType) {
382
		switch (shapeType) {
383
		case FShape.MULTI:
384
		case FShape.LINE:
385
			return true;
386
		}
387
		return false;
388
	}
389

  
390

  
391
}
0 392

  
trunk/extensions/extCAD/src/com/iver/cit/gvsig/BreakExtension.java
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;
42

  
43
import com.iver.andami.PluginServices;
44
import com.iver.andami.plugins.Extension;
45
import com.iver.cit.gvsig.fmap.DriverException;
46
import com.iver.cit.gvsig.fmap.MapControl;
47
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
48
import com.iver.cit.gvsig.gui.View;
49
import com.iver.cit.gvsig.gui.cad.tools.BreakCADTool;
50

  
51
/**
52
 * Extensi?n que gestiona la partici?n de geometr?as en edici?n.
53
 *
54
 * @author Vicente Caballero Navarro
55
 */
56
public class BreakExtension extends Extension {
57
	private View view;
58

  
59
	private MapControl mapControl;
60
	private BreakCADTool breakCADTool;
61

  
62
	/**
63
	 * @see com.iver.andami.plugins.IExtension#initialize()
64
	 */
65
	public void initialize() {
66
		breakCADTool=new BreakCADTool();
67
		CADExtension.addCADTool("_break",breakCADTool);
68
	}
69

  
70
	/**
71
	 * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
72
	 */
73
	public void execute(String s) {
74
		CADExtension.initFocus();
75
		if (s.equals("_break")) {
76
        	CADExtension.setCADTool(s,true);
77
        }
78
		CADExtension.getEditionManager().setMapControl(mapControl);
79
		CADExtension.getCADToolAdapter().configureMenu();
80
	}
81

  
82
	/**
83
	 * @see com.iver.andami.plugins.IExtension#isEnabled()
84
	 */
85
	public boolean isEnabled() {
86

  
87
		try {
88
			if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
89
				view = (View) PluginServices.getMDIManager().getActiveView();
90
				mapControl = (MapControl) view.getMapControl();
91
				FLyrVect lv=(FLyrVect)CADExtension.getEditionManager().getActiveLayerEdited().getLayer();
92
				if (breakCADTool.isApplicable(lv.getShapeType())){
93
					return true;
94
				}
95
			}
96
		} catch (DriverException e) {
97
			e.printStackTrace();
98
		}
99
		return false;
100
	}
101

  
102
	/**
103
	 * @see com.iver.andami.plugins.IExtension#isVisible()
104
	 */
105
	public boolean isVisible() {
106
		if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
107
		{
108
			return true;
109
		}
110
		else
111
			return false;
112
	}
113
}
0 114

  

Also available in: Unified diff