wpf GIS 在地图上画正方形和圆形

1、效果图

 

2、代码

using System; using System.Collections.Generic; using System.Linq; using System.Text;

using System.Drawing; using System.Windows.Forms; using MeteoGIS.Data; using MeteoGIS.Realspace; using MeteoGIS.UI;

namespace NewTraffic3DSystem

{   

  public class GraphicsRender     {

 

        private bool mouseStatus = false;//鼠标状态,false为松开         private Point2D startPoint;//         //private Point startPoint;//鼠标按下的点         private Point2D endPoint;//         private double minStartX, minStartY, maxEndX, maxEndY;//最大重绘矩形的上下左右的坐标,这样重绘的效率更高。         private GeoStyle3D gs3D = new GeoStyle3D();         private SceneControl m_sceneControl = null;         public ShapeType drawingShapeType;       

  public enum ShapeType         {         

    Rectangle,             Circle,             Polygon       

  }

        public GraphicsRender(SceneControl sctrol)     

    {             m_sceneControl = sctrol;             m_sceneControl.Action = Action3D.Select;             drawingShapeType = ShapeType.Circle;         }         public void m_sceneControl_MouseDown(object sender, MouseEventArgs e)         {             if (e.Button == MouseButtons.Left)             {                 Point2D p2d = ConvertToGlobePoint2D(e.Location);                 mouseStatus = true;                 startPoint.X = p2d.X;                 startPoint.Y = p2d.Y;                 //重新一个矩形,重置最大重绘矩形的上下左右的坐标                 minStartX = p2d.X;                 minStartY = p2d.Y;                 maxEndX = p2d.X;                 maxEndY = p2d.Y;             }             else             {                 //重新一个矩形,重置最大重绘矩形的上下左右的坐标                 minStartX = 0;                 minStartY = 0;                 maxEndX = 0;                 maxEndY = 0;                 int indexNum = m_sceneControl.Scene.TrackingLayer.IndexOf("rect");                 if (indexNum != -1)                     m_sceneControl.Scene.TrackingLayer.Remove(indexNum);                 m_sceneControl.Action = Action3D.Pan;

                m_sceneControl.MouseDown -= new MouseEventHandler(m_sceneControl_MouseDown);

                m_sceneControl.MouseMove -= new MouseEventHandler(m_sceneControl_MouseMove);             }         }

        public void m_sceneControl_MouseMove(object sender, MouseEventArgs e)         {             if (mouseStatus)             {                 Point2D p2d = ConvertToGlobePoint2D(e.Location);                 endPoint.X = p2d.X; endPoint.Y = p2d.Y;                 //这一段是获取要绘制矩形的上下左右的坐标,如果不这样处理的话,只有从左上开始往右下角才能画出矩形。                 //这样处理的话,可以任意方向,当然中途可以更换方向。                 double realStartX = Math.Min(startPoint.X, endPoint.X);                 double realStartY = Math.Min(startPoint.Y, endPoint.Y);                 double realEndX = Math.Max(startPoint.X, endPoint.X);                 double realEndY = Math.Max(startPoint.Y, endPoint.Y);

                minStartX = Math.Min(minStartX, realStartX);                 minStartY = Math.Min(minStartY, realStartY);                 maxEndX = Math.Max(maxEndX, realEndX);                 maxEndY = Math.Max(maxEndY, realEndY);             }             int indexNum = m_sceneControl.Scene.TrackingLayer.IndexOf("rect");             if (indexNum != -1)                 m_sceneControl.Scene.TrackingLayer.Remove(indexNum);             gs3D.FillForeColor = Color.Gray;             gs3D.LineColor = Color.Red;             gs3D.LineWidth = 0.5;             gs3D.FillMode = FillMode3D.Line;             switch (drawingShapeType)             {                 case ShapeType.Rectangle:                     Point3Ds pnsRectangleNodes = new Point3Ds();                     Point3D pnt1 = new Point3D(minStartX, minStartY, 0.0f);                     Point3D pnt2 = new Point3D(minStartX, maxEndY, 0.0f);                     Point3D pnt3 = new Point3D(maxEndX, maxEndY, 0.0f);                     Point3D pnt4 = new Point3D(maxEndX, minStartY, 0.0f);                     pnsRectangleNodes.Add(pnt1);                     pnsRectangleNodes.Add(pnt2);                     pnsRectangleNodes.Add(pnt3);                     pnsRectangleNodes.Add(pnt4);                     GeoRegion3D tempRectangle = new GeoRegion3D(pnsRectangleNodes);

                    tempRectangle.Style3D = gs3D;                     m_sceneControl.Scene.TrackingLayer.Add(tempRectangle, "rect");                     break;                 case ShapeType.Circle:                     double radius = CalculateDistance(startPoint, endPoint) + 0.0001;                     Point3D pntCircleCenter = new Point3D(startPoint.X, startPoint.Y, 0.0f);                     //GeoCircle3D tempCircle3D = new GeoCircle3D(pntCircleCenter, radius);

                    Point3Ds pt = new Point3Ds();                     for (int i = 0; i < 128; i++)                     {                         pt.Add(new Point3D(pntCircleCenter.X + radius * Math.Cos(i * Math.PI * 2 / 128.0),                             pntCircleCenter.Y + radius * Math.Sin(i * Math.PI * 2 / 128.0),                                0.0));                     }                     pt.Add(new Point3D(pntCircleCenter.X + radius, pntCircleCenter.Y, 0.0));

                    GeoRegion3D tempCircle = new GeoRegion3D(pt);                     tempCircle.Style3D = gs3D;                     m_sceneControl.Scene.TrackingLayer.Add(tempCircle, "rect");                     break;             }

        }         public void m_sceneControl_MouseUp(object sender, MouseEventArgs e)         {             mouseStatus = false;         }         Point2D ConvertToGlobePoint2D(Point screenPoint)         {             Point2D globePoint = new Point2D();             Point3D globePoint3D = m_sceneControl.Scene.PixelToGlobe(screenPoint);             globePoint.X = globePoint3D.X;             globePoint.Y = globePoint3D.Y;             return globePoint;         }         double CalculateDistance(Point2D point1, Point2D point2)         {             double distance = 0;             distance = Math.Sqrt((point1.X - point2.X) * (point1.X - point2.X) + (point1.Y - point2.Y) * (point1.Y - point2.Y));             return distance;         }     } }

posted @ 2013-11-26 17:12  露草  阅读(1763)  评论(0编辑  收藏  举报