User Tools

Site Tools


source_code_desktopeditionsample_editingrectangles_cs_100204.zip

Source Code DesktopEditionSample EditingRectangles CS 100204.zip

CustomEditOverlay.cs

using System;  
 using System.Collections.Generic;  
 using System.Collections.ObjectModel;  
 using ThinkGeo.MapSuite.Core;  
 using ThinkGeo.MapSuite.DesktopEdition;  
 
 namespace EditingRectangles  
 {  
     class CustomEditInteractiveOverlay : EditInteractiveOverlay  
     {  
         public CustomEditInteractiveOverlay()  
             : base()  
         {  
         }  
 
         protected override Feature AddVertexCore(Feature targetFeature, PointShape targetPointShape, double searchingTolerance)  
         {  
             // Override the base method and disable the function of AddVertex if the shape is the "custom"  
             if (targetFeature.ColumnValues.ContainsKey("Edit") && targetFeature.ColumnValues["Edit"] == "rectangle")  
             {  
                 return new Feature();  
             }  
 
             return base.AddVertexCore(targetFeature, targetPointShape, searchingTolerance);  
         }  
 
         protected override Feature RemoveVertexCore(Feature editShapeFeature, Vertex selectedVertex, double searchingTolerance)  
         {  
             // Override the base method and disable the function of RemoveVertex if the shape is the "custom"  
             if (editShapeFeature.ColumnValues.ContainsKey("Edit") && editShapeFeature.ColumnValues["Edit"] == "rectangle")  
             {  
                 return new Feature();  
             }  
 
             return base.RemoveVertexCore(editShapeFeature, selectedVertex, searchingTolerance);  
         }  
 
         protected override IEnumerable<Feature> CalculateResizeControlPointsCore(Feature feature)  
         {  
             // Override the base method and modify the control points for resizing if the shape is the "custom"  
             if (feature.ColumnValues.ContainsKey("Edit") && feature.ColumnValues["Edit"] == "rectangle")  
             {  
                 Collection<Feature> resizeControlPoints = new Collection<Feature>();  
 
                 PolygonShape polygonShape = feature.GetShape() as PolygonShape;  
                 if (polygonShape != null)  
                 {  
                     foreach (Vertex vertex in polygonShape.OuterRing.Vertices)  
                     {  
                         resizeControlPoints.Add(new Feature(vertex, feature.Id));  
                     }  
                 }  
                 return resizeControlPoints;  
             }  
 
             return base.CalculateResizeControlPointsCore(feature);  
         }  
 
         protected override Feature ResizeFeatureCore(Feature sourceFeature, PointShape sourceControlPoint, PointShape targetControlPoint)  
         {  
             // Override the base method and modify the logic for resizing if the shape is the "custom"  
             if (sourceFeature.ColumnValues.ContainsKey("Edit") && sourceFeature.ColumnValues["Edit"] == "rectangle")  
             {  
                 PolygonShape polygonShape = sourceFeature.GetShape() as PolygonShape;  
 
                 if (polygonShape != null)  
                 {  
                     // If the rectangle is horizontal or vertical, it will use the custom method.  
                     if (string.Equals(polygonShape.GetBoundingBox().GetWellKnownText(), polygonShape.GetWellKnownText()))  
                     {  
                         int fixedPointIndex = GetFixedPointIndex(polygonShape, sourceControlPoint);  
 
                         PointShape fixedPointShape = new PointShape(polygonShape.OuterRing.Vertices[fixedPointIndex]);  
 
                         RectangleShape newRectangleShape = new LineShape(new Vertex[] { new Vertex(fixedPointShape), new Vertex(targetControlPoint) }).GetBoundingBox();  
 
                         return new Feature(newRectangleShape.GetWellKnownBinary(), sourceFeature.Id, sourceFeature.ColumnValues);  
                     }  
                 }  
             }  
 
             return base.ResizeFeatureCore(sourceFeature, sourceControlPoint, targetControlPoint);  
         }  
 
         private int GetFixedPointIndex(PolygonShape sourcePolygonShape, PointShape sourceControlPointShape)  
         {  
             int index = 0;  
             for (int i = 0; i < sourcePolygonShape.OuterRing.Vertices.Count; i++)  
             {  
                 Vertex vertex = sourcePolygonShape.OuterRing.Vertices[i];  
                 if (Math.Abs(vertex.X - sourceControlPointShape.X) <= 10E-6 && Math.Abs(vertex.Y - sourceControlPointShape.Y) <= 10E-6)  
                 {  
                     index = i;  
                     break;  
                 }  
             }  
             int fixedPointIndex = 0;  
             if (index <= 2)  
             {  
                 fixedPointIndex = index + 2;  
             }  
             else  
             {  
                 fixedPointIndex = index - 2;  
             }  
 
             return fixedPointIndex;  
         }  
     }  
 }  
 

Program.cs

using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Windows.Forms;  
 
 namespace EditingRectangles  
 {  
     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());  
         }  
     }  
 }  
 

TestForm.cs

using System;  
 using System.Windows.Forms;  
 using System.Collections.ObjectModel;  
 using ThinkGeo.MapSuite.Core;  
 using ThinkGeo.MapSuite.DesktopEdition;  
 
 
 namespace  EditingRectangles  
 {  
     public partial class TestForm : Form  
     {  
         public TestForm()  
         {  
             InitializeComponent();  
         }  
 
         private void TestForm_Load(object sender, EventArgs e)  
         {  
             // Set the full extent and the background color  
             winformsMap1.CurrentExtent = new RectangleShape(-155, 70, 78, -65);  
             winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);  
 
             WorldMapKitWmsDesktopOverlay worldMapKitDesktopOverlay = new WorldMapKitWmsDesktopOverlay();  
             winformsMap1.Overlays.Add(worldMapKitDesktopOverlay);  
 
             winformsMap1.EditOverlay = new CustomEditInteractiveOverlay();  
 
             Feature rectangle = new Feature(new RectangleShape(-48, 0, 52, -40));  
             // Set the value of column "Edit" to "rectangle", so this shape will be editing by custom way.  
             rectangle.ColumnValues.Add("Edit", "rectangle");  
             winformsMap1.EditOverlay.EditShapesLayer.InternalFeatures.Add(rectangle);  
 
             Feature polygon = new Feature(new PolygonShape("POLYGON((-120 49,-66 44,-81 26,-97 24,-119 33,-125 40,-120 49))"));  
             // Set the value of column "Edit" to "polygon" not "rectangle" so this shape will be editing by original way.  
             polygon.ColumnValues.Add("Edit", "polygon");  
             winformsMap1.EditOverlay.EditShapesLayer.InternalFeatures.Add(polygon);  
 
             winformsMap1.EditOverlay.EditShapesLayer.Open();  
             winformsMap1.EditOverlay.EditShapesLayer.Columns.Add(new FeatureSourceColumn("Edit"));  
             winformsMap1.EditOverlay.EditShapesLayer.Close();  
             winformsMap1.EditOverlay.EditShapesLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = new TextStyle("Edit", new GeoFont("Arial", 18), new GeoSolidBrush(GeoColor.StandardColors.Black));  
             winformsMap1.EditOverlay.EditShapesLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
             winformsMap1.EditOverlay.CalculateAllControlPoints();  
 
             // Draw the map image on the screen  
             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_editingrectangles_cs_100204.zip.txt · Last modified: 2015/09/08 04:14 by admin