图像编辑功能

       

   //操作类型
        string strOperator = "";
        //当前地图视图
        IActiveView m_activeView = null;
        //当前操作图层
        IFeatureLayer m_featureLayer = null;
        //当前操作实体
        IFeature m_feature = null;
        //当前线移动反馈对象
        IMoveLineFeedback m_MoveLineFeedback = new MoveLineFeedbackClass();
        //当前点移动反馈对象
        IMovePointFeedback m_MovePointFeedback = new MovePointFeedbackClass();
        //当前面移动反馈对象
        IMovePolygonFeedback m_MovePolygonFeedback = new MovePolygonFeedbackClass();
        public Form1()
        {
            InitializeComponent();
        }
        //更新图层
        private void button1_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            for (int i = 0; i < axMapControl1.LayerCount;i++ )
            {
                ILayer layer = axMapControl1.get_Layer(i);
                comboBox1.Items.Add(layer.Name);
            }
        }
        //选择操作图层,并让map选中图层的第一个实体
        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text !="")
            {
                for (int i = 0; i < axMapControl1.LayerCount;i++ )
                {
                    ILayer layer = axMapControl1.get_Layer(i);
                        if (layer.Name == comboBox1.Text.ToString())
                        {
                            m_featureLayer=layer as IFeatureLayer;
                            m_feature=m_featureLayer.FeatureClass.GetFeature(1);

                            if (m_feature!=null)
                            {
                                axMapControl1.Map.SelectFeature(m_featureLayer,m_feature);
                            }
                            m_activeView=axMapControl1.ActiveView;
                            return;
                        }
                }
            }
        }
        //初始化反馈对象参数
        private void button2_Click(object sender, EventArgs e)
        {
            strOperator = "move";
            m_MovePointFeedback = new MovePointFeedbackClass();
            m_MoveLineFeedback = new MoveLineFeedbackClass();
            m_MovePolygonFeedback = new MovePolygonFeedbackClass();
        }
        //删除节点
        private void button3_Click(object sender, EventArgs e)
        {
            IPointCollection pointCollection;
            if (m_feature.Shape.GeometryType==esriGeometryType.esriGeometryPolyline)
            {
                pointCollection=new PolylineClass();
                IPolyline polyline=m_feature.Shape as IPolyline;
                pointCollection=polyline as IPointCollection;
                //若点数少于两个就无法成直线了
                if (pointCollection.PointCount>2)
                {
                    //移除制定节点
                    pointCollection.RemovePoints(pointCollection.PointCount-1,1);
                }
            }
            else if(m_feature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
            {
                pointCollection=new PolygonClass();
                IPolygon polygon=m_feature.Shape as IPolygon;
                pointCollection=polygon as IPointCollection;
                //如果点个数少于3个就无法构成面了
                if (pointCollection.PointCount>3)
                {
                    //移除指定节点
                    pointCollection.RemovePoints(pointCollection.PointCount-1,1);
                }
            }
            IWorkspaceEdit workspaceEdit;
            IWorkspace workspace;
            IDataset dataset=m_featureLayer.FeatureClass as IDataset;
            workspace=dataset.Workspace;
            workspaceEdit=workspace as IWorkspaceEdit;
            //开始编辑
            workspaceEdit.StartEditing(true);
            workspaceEdit.StartEditOperation();
            //保存数据
            m_feature.Store();
            //结束编辑
            workspaceEdit.StopEditOperation();
            workspaceEdit.StopEditing(true);
            m_activeView.Refresh();
        }
        //增加节点
        private void button4_Click(object sender, EventArgs e)
        {
            IPointCollection pointCollection;
            IPoint point = new PointClass();
            IPoint fromPoint = new PointClass();
            IPoint toPoint = new PointClass();
            if (m_feature.Shape.GeometryType ==esriGeometryType.esriGeometryPolyline )
            {
                pointCollection = new PolylineClass();
                IPolyline polyline = m_feature.Shape as IPolyline;
                object missing1 = Type.Missing;
                object missing2 = Type.Missing;
                pointCollection = polyline as IPointCollection;
                //获取线对象的最后两个点
                fromPoint = pointCollection.get_Point(pointCollection.PointCount - 2);
                toPoint = pointCollection.get_Point(pointCollection.PointCount - 1);
                //根据线最后两个点创建一个新点
                point.PutCoords((fromPoint.X + toPoint.X) / 2, (fromPoint.Y + toPoint.Y) / 2 + 50);
                //将新点添加到线对象的点集合中
                pointCollection.AddPoint(point, ref missing1, ref missing2);
            }
            else if (m_feature.Shape.GeometryType==esriGeometryType.esriGeometryPolygon)
            {
                pointCollection = new PolygonClass();
                IPolygon polygon = m_feature.Shape as IPolygon;
                object missing1 = Type.Missing;
                object missing2 = Type.Missing;
                pointCollection = polygon as IPointCollection;
                //获取对象最后两个点
                fromPoint = pointCollection.get_Point(pointCollection.PointCount-2);
                toPoint = pointCollection.get_Point(pointCollection.PointCount - 1);
                //根据最后两点创建一个新点
                point.PutCoords((fromPoint.X + toPoint.X) / 2, (fromPoint.Y + toPoint.Y) / 2 + 50);
                //将新点添加进点集
                pointCollection.AddPoint(point, ref missing1, ref missing2);
            }
            IWorkspaceEdit workspaceEdit;
            IWorkspace workspace;
            IDataset dataset = m_featureLayer.FeatureClass as IDataset;
            workspace = dataset.Workspace;
            workspaceEdit = workspace as IWorkspaceEdit;
            //开始编辑
            workspaceEdit.StartEditing(true);
            workspaceEdit.StartEditOperation();
            //保存数据
            m_feature.Store();
            //结束编辑
            workspaceEdit.StopEditOperation();
            workspaceEdit.StopEditing(true);
            m_activeView.Refresh();
        }

        private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            IPoint point = new PointClass();
            //IFeatureClass featureClass = null;
            if (m_feature == null) return;
            switch (strOperator)
            {
                case "move":
                    //将当前鼠标光标位置的点转换为地图上的坐标
                    point = m_activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y);
                    if (m_feature.Shape.GeometryType==esriGeometryType.esriGeometryPoint)
                    {
                        //设置显示对象,并启动移动
                        m_MovePointFeedback.Display = m_activeView.ScreenDisplay;
                        m_MovePointFeedback.Start(m_feature.Shape as IPoint, point);
                    }
                    else if (m_feature.Shape.GeometryType == esriGeometryType.esriGeometryPolyline)
                    {
                        //设置显示对象,并启动移动
                        m_MoveLineFeedback.Display = m_activeView.ScreenDisplay;
                        m_MoveLineFeedback.Start(m_feature.Shape as IPolyline, point);
                    }
                    else if (m_feature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
                    {
                        //设置显示对象,并启动移动
                        m_MovePolygonFeedback.Display = m_activeView.ScreenDisplay;
                        m_MovePolygonFeedback.Start(m_feature.Shape as IPolygon, point);
                    }
                    break;
            }    
        }

        private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
        {
            IPoint point = new PointClass();
            switch (strOperator)
            {
                case "move":
                    if (m_feature.Shape.GeometryType==esriGeometryType.esriGeometryPoint)
                    {
                        if (m_MovePointFeedback!=null)
                        {
                            //将当前鼠标光标位置的点转换为地图上的左边
                            point = m_activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x,e.y);
                            //移动对象到当前鼠标位置
                            m_MovePointFeedback.MoveTo(point);
                        }
                    }
                    else if (m_feature.Shape.GeometryType==esriGeometryType.esriGeometryPolyline)
                    {
                        if (m_MoveLineFeedback!=null)
                        {
                            //将当前鼠标光标位置的点转换为地图上的左边
                            point = m_activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x,e.y);
                            //移动对象到当前鼠标位置
                            m_MoveLineFeedback.MoveTo(point);
                        }
                    }
                    else if (m_feature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
                    {
                        if (m_MovePolygonFeedback != null)
                        {
                            //将当前鼠标光标位置的点转换为地图上的左边
                            point = m_activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y);
                            //移动对象到当前鼠标位置
                            m_MovePolygonFeedback.MoveTo(point);
                        }
                    }
                    break;
            }
        }

        private void axMapControl1_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e)
        {
            if (m_feature == null) return;
            IGeometry resultGeometry = null;
            switch (strOperator)
            {
                case "move":
                    if (m_feature.Shape.GeometryType==esriGeometryType.esriGeometryPoint)
                    {
                        //停止移动
                        resultGeometry = m_MovePointFeedback.Stop() as IGeometry;
                        m_feature.Shape = resultGeometry;
                    }
                    else if (m_feature.Shape.GeometryType == esriGeometryType.esriGeometryPolyline)
                    {
                        //停止移动
                        resultGeometry = m_MoveLineFeedback.Stop() as IGeometry;
                        m_feature.Shape = resultGeometry;
                    }
                    else if (m_feature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
                    {
                        //停止移动
                        resultGeometry = m_MovePolygonFeedback.Stop() as IGeometry;
                        m_feature.Shape = resultGeometry;
                    }
                    IWorkspaceEdit workspaceEdit;
                    IWorkspace workspace;
                    IDataset dataset = m_featureLayer.FeatureClass as IDataset;
                    workspace = dataset.Workspace;
                    workspaceEdit = workspace as IWorkspaceEdit;
                    //开始编辑
                    workspaceEdit.StartEditing(true);
                    workspaceEdit.StartEditOperation();
                    //保存数据
                    m_feature.Store();
                    //结束编辑
                    workspaceEdit.StopEditOperation();
                    workspaceEdit.StopEditing(true);
                    m_MovePointFeedback = null;
                    m_MovePolygonFeedback = null;
                    m_MoveLineFeedback = null;
                    break;                  
            }
            m_activeView.Refresh();
            axMapControl1.Map.ClearSelection();
        }

posted on 2012-03-19 14:41  种瓜得瓜  阅读(556)  评论(0编辑  收藏  举报