User Tools

Site Tools


source_code_desktopeditionsample_snappingtovertex_cs_091127.zip

Source Code DesktopEditionSample SnappingToVertex CS 091127.zip

Program.cs

using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Windows.Forms;  
 
 namespace SnappingToVertex  
 {  
     static class Program  
     {  
         /// <summary>  
         /// The main entry point for the application.  
         /// </summary>  
         [STAThread]  
         static void Main()  
         {  
             Application.EnableVisualStyles();  
             Application.SetCompatibleTextRenderingDefault(false);  
             Application.Run(new TestForm());  
         }  
     }  
 }  
 

SnappingToVertexInteractiveOverlay.cs

using System.Collections.ObjectModel;  
 using System.Drawing;  
 using System.Windows.Forms;  
 using ThinkGeo.MapSuite.Core;  
 using ThinkGeo.MapSuite.DesktopEdition;  
 
 namespace SnappingToVertex  
 {  
     class SnappingToVertexInteractiveOverlay : EditInteractiveOverlay  
     {  
         private InteractionArguments arguments;  
         private double tolerance;  
         private DistanceUnit toleranceUnit;  
         bool inControlPointDraggingMode;  
 
         public double Tolerance  
         {  
             get { return tolerance; }  
             set { tolerance = value; }  
         }  
 
         public DistanceUnit ToleranceUnit  
         {  
             get { return toleranceUnit; }  
             set { toleranceUnit = value; }  
         }  
 
         public SnappingToVertexInteractiveOverlay()  
             : base()  
         {  
         }  
 
         protected override InteractiveResult MouseDownCore(InteractionArguments interactionArguments)  
         {  
             arguments = interactionArguments;  
             return base.MouseDownCore(interactionArguments);  
         }  
 
         protected override InteractiveResult MouseUpCore(InteractionArguments interactionArguments)  
         {  
             //Sets inControlPointDraggingMode to false to show the tolerance circle when not dragging vertex.  
             inControlPointDraggingMode = false;  
             return base.MouseUpCore(interactionArguments);  
         }  
 
         protected override Feature SetSelectedControlPointCore(PointShape targetPointShape, double searchingTolerance)  
         {  
             //If the mouse pointer is within the tolerance of a vertex, allows the dragging of the vertex.  
             //If not, the map will pan normally.  
             Collection<Feature> existingControlPoints = ExistingControlPointsLayer.FeatureSource.GetFeaturesNearestTo(targetPointShape, GeographyUnit.Meter, 1, ReturningColumnsType.AllColumns);  
             if (existingControlPoints.Count == 1)  
             {  
                 PointShape existingControlPointShape = (PointShape)existingControlPoints[0].GetShape();  
                 double Distance = existingControlPointShape.GetDistanceTo(targetPointShape, arguments.MapUnit, toleranceUnit);  
 
                 if (Distance <= tolerance)  
                 {  
                     ControlPointType = ControlPointType.Vertex;  
                     if (arguments != null)  
                     {  
                         //Snapps the location of the mouse pointer to the location of the nearest vertex.  
                         ScreenPointF controlPoint = ExtentHelper.ToScreenCoordinate(arguments.CurrentExtent, existingControlPoints[0], arguments.MapWidth, arguments.MapHeight);  
                         ScreenPointF targetPoint = ExtentHelper.ToScreenCoordinate(arguments.CurrentExtent, targetPointShape, arguments.MapWidth, arguments.MapHeight);  
                         Cursor.Position = new Point(Cursor.Position.X + (int)(controlPoint.X - targetPoint.X), Cursor.Position.Y + (int)(controlPoint.Y - targetPoint.Y));  
                     }  
                 }  
 
                 inControlPointDraggingMode = true;  
                 return existingControlPoints[0];  
             }  
 
             return base.SetSelectedControlPointCore(targetPointShape, searchingTolerance);  
         }  
 
         protected override void DrawCore(GeoCanvas canvas)  
         {  
             base.DrawCore(canvas);  
 
             //If not dragging a vertex, show tolerance as circles around the vertices.  
             if (!inControlPointDraggingMode)  
             {  
                 Collection<Feature> controlPoints = ExistingControlPointsLayer.FeatureSource.GetAllFeatures(ReturningColumnsType.AllColumns);  
 
                 foreach (Feature feature in controlPoints)  
                 {  
                     PointShape pointShape = (PointShape)feature.GetShape();  
                     EllipseShape ellipseShape = new EllipseShape(pointShape, tolerance, canvas.MapUnit, toleranceUnit);  
                     canvas.DrawArea(ellipseShape, new GeoPen(GeoColor.StandardColors.Black), DrawingLevel.LevelFour);  
                 }  
             }  
        }  
     }  
 
 }  
 

TestForm.cs

using System;  
 using System.Windows.Forms;  
 using System.Collections.ObjectModel;  
 using System.IO;  
 using ThinkGeo.MapSuite.Core;  
 using ThinkGeo.MapSuite.DesktopEdition;  
 
 namespace  SnappingToVertex  
 {  
     public partial class TestForm : Form  
     {  
         public TestForm()  
         {  
             InitializeComponent();  
         }  
 
         private void TestForm_Load(object sender, EventArgs e)  
         {  
             winformsMap1.MapUnit = GeographyUnit.DecimalDegree;  
             winformsMap1.CurrentExtent = new RectangleShape(-97.7591, 30.3126, -97.7317, 30.2964);  
             winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.FromArgb(255, 198, 255, 255));  
 
             //Displays the World Map Kit as a background.  
             ThinkGeo.MapSuite.DesktopEdition.WorldMapKitWmsDesktopOverlay worldMapKitDesktopOverlay = new ThinkGeo.MapSuite.DesktopEdition.WorldMapKitWmsDesktopOverlay();  
             winformsMap1.Overlays.Add(worldMapKitDesktopOverlay);  
 
             string fileName1 = @"..\..\data\polygon.txt";  
             StreamReader sr1 = new StreamReader(fileName1);  
 
             string fileName2 = @"..\..\data\line.txt";  
             StreamReader sr2 = new StreamReader(fileName2);  
 
             //InteractiveOverlay for snapping the mouse pointer to the nearest vertex if within tolerance.  
             SnappingToVertexInteractiveOverlay snappingToVertexInteractiveOverlay = new SnappingToVertexInteractiveOverlay();  
             //Sets tolerance to 75 meters.  
             snappingToVertexInteractiveOverlay.Tolerance = 75;  
             snappingToVertexInteractiveOverlay.ToleranceUnit = DistanceUnit.Meter;  
 
             winformsMap1.EditOverlay = snappingToVertexInteractiveOverlay;  
 
             winformsMap1.EditOverlay.EditShapesLayer.InternalFeatures.Add("Polygon", new Feature(BaseShape.CreateShapeFromWellKnownData(sr1.ReadLine())));  
             winformsMap1.EditOverlay.EditShapesLayer.InternalFeatures.Add("MultiLine", new Feature(BaseShape.CreateShapeFromWellKnownData(sr2.ReadLine())));  
 
             winformsMap1.EditOverlay.CanAddVertex = false;  
             winformsMap1.EditOverlay.CanDrag = false;  
             winformsMap1.EditOverlay.CanRemoveVertex = false;  
             winformsMap1.EditOverlay.CanResize = false;  
             winformsMap1.EditOverlay.CanRotate = false;  
             winformsMap1.EditOverlay.CalculateAllControlPoints();  
 
             winformsMap1.Refresh();  
         }  
 
 
         private void winformsMap1_MouseMove(object sender, MouseEventArgs e)  
         {  
             //Displays the X and Y in screen coordinates.  
             statusStrip1.Items["toolStripStatusLabelScreen"].Text = "X:" + e.X + " Y:" + e.Y;  
 
             //Gets the PointShape in world coordinates from screen coordinates.  
             PointShape pointShape = ExtentHelper.ToWorldCoordinate(winformsMap1.CurrentExtent, new ScreenPointF(e.X, e.Y), winformsMap1.Width, winformsMap1.Height);  
 
             //Displays world coordinates.  
             statusStrip1.Items["toolStripStatusLabelWorld"].Text = "(world) X:" + Math.Round(pointShape.X, 4) + " Y:" + Math.Round(pointShape.Y, 4);  
         }  
 
         private void btnClose_Click(object sender, EventArgs e)  
         {  
             this.Close();  
         }  
     }  
 }  
 
source_code_desktopeditionsample_snappingtovertex_cs_091127.zip.txt · Last modified: 2015/09/08 05:08 by admin