基于C#的ArcEngine二次开发教程(13):点、线、面要素的绘制

目录

1 利用ArcMap绘制点、线和面的操作

1.1 绘图工具,绘制临时要素

1.2 在矢量图层中直接添加要素的方法

2 以Element的方式加入点要素

2.1 IGraphicsContainer

2.2 IElement

2.3 IGeometry

2.4 点元素添加实现源码

3 以Element形式添加线元素

3.1 IPolyline接口

3.2 IPointCollection接口

3.3 源码实现

4 以Element形式添加线元素

4.1 PolygonClass

4.2 源码实现

5 以矢量要素的形式添加点线面

5.1 点要素的添加

5.2 线要素的添加

5.3 面要素添加


1 利用ArcMap绘制点、线和面的操作

1.1 绘图工具,绘制临时要素

添加Element图形元素,它仅保存在内存中,相当于以Map为画布,进行绘制的;软件关闭之后就会消失

1.2 在矢量图层中直接添加要素的方法

在矢量图层中添加Feature要素,添加到矢量数据,并永久保存到硬盘中

2 以Element的方式加入点要素

2.1 IGraphicsContainer

图形容器接口,用于存放临时的点线面要素数据

接口介绍见缓冲区分析博文

2.2 IElement

以下三个类均对该接口进行了实现

 

2.3 IGeometry

类Point对其进行了实现

同时也实现了IPoint类,满足接口跳转的条件

2.4 点元素添加实现源码

(1)定义全局变量

IPoint pt;
IElement gele;

(2)添加axMapControl1_OnMouseDown事件,用于记录鼠标点下的位置坐标

        private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            pt = new PointClass();
            pt.X = e.mapX;
            pt.Y = e.mapY;
            gele = new MarkerElementClass();
            gele.Geometry = pt as IGeometry;
            //生成线
        }

(3)添加点要素

        private void 添加点元素ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IGraphicsContainer igc = axMapControl1.Map as IGraphicsContainer; 
            igc.AddElement(gele, 0);
            axMapControl1.Refresh();
        }

3 以Element形式添加线元素

3.1 IPolyline接口

3.2 IPointCollection接口

通过接口IPolyline定义的Polyline类对象可以在接口IGeomtry、和IPointCollection之间跳转

3.3 源码实现

(1)定义全局变量

IPointCollection lineCollect;//添加点集合;
IPoint linePts;

(2)Form1_Load创建IPointCollection接口实例对象

        private void Form1_Load(object sender, EventArgs e)
        {       
            lineCollect = new PolylineClass();//添加线元素
        }

(3)添加axMapControl1_OnMouseDown事件,用于记录鼠标点下的位置坐标

 private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            //生成线
            linePts = new PointClass();
            linePts.X = e.mapX;
            linePts.Y = e.mapY;
            //object a = Type.Missing;
            lineCollect.AddPoint(linePts);//添加多个点到点集合中
        }

(4)添加线要素

        private void 添加线元素ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IGraphicsContainer igc = axMapControl1.Map as IGraphicsContainer; 
            IElement lineEle = new LineElementClass();
            IPolyline pline = new PolylineClass();
            pline = lineCollect as IPolyline;

            lineEle.Geometry = pline as IGeometry;
            igc.AddElement(lineEle, 0);
            axMapControl1.Refresh();
        }

4 以Element形式添加线元素

4.1 PolygonClass

继承接口

4.2 源码实现

(1)定义全局变量

        //面
        IPointCollection ploygonCollect;//添加点集合;
        IPoint ploygonPts;

(2)Form1_Load创建IPointCollection接口实例对象

        private void Form1_Load(object sender, EventArgs e)
        {            
            ploygonCollect = new PolygonClass();
        }

(3)添加axMapControl1_OnMouseDown事件,用于记录鼠标点下的位置坐标

        private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {

            //生成面
            ploygonPts = new PointClass();
            ploygonPts.X = e.mapX;
            ploygonPts.Y = e.mapY;
            //object a = Type.Missing;
            ploygonCollect.AddPoint(ploygonPts);

        }

(4)添加面要素

        private void 添加面元素ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IGraphicsContainer igc = axMapControl1.Map as IGraphicsContainer; 
            IElement polygonele = new PolygonElementClass();
            IPolygon polyg = new PolygonClass();
            polyg = ploygonCollect as IPolygon;
            polygonele.Geometry = polyg as IGeometry;
            igc.AddElement(polygonele, 0);
            axMapControl1.Refresh();
        }

5 以矢量要素的形式添加点线面

5.1 点要素的添加

        private void 添加点到矢量图层ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IFeatureLayer felyer = axMapControl1.get_Layer(0) as IFeatureLayer;
            IFeatureClass feclass = felyer.FeatureClass;
            IFeature fea = feclass.CreateFeature();//点要素的创建
            fea.Shape = pt as IGeometry;
            fea.Store();//点要素的存储
            axMapControl1.Refresh();
        }

5.2 线要素的添加

        private void 添加线到矢量图层ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IFeatureLayer felyer = axMapControl1.get_Layer(0) as IFeatureLayer;
            IFeatureClass feclass = felyer.FeatureClass;
            IFeature fea = feclass.CreateFeature();
            IPolyline pline = new PolylineClass();
            pline = lineCollect as IPolyline;
            fea.Shape = pline as IGeometry;
            fea.Store();
            axMapControl1.Refresh();
        }

5.3 面要素添加

        private void 添加面到矢量图层ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IFeatureLayer felyer = axMapControl1.get_Layer(0) as IFeatureLayer;
            IFeatureClass feclass = felyer.FeatureClass;
            IFeature fea = feclass.CreateFeature();
            IPolygon polygon = new PolygonClass();
            polygon = ploygonCollect as IPolygon;
            fea.Shape = polygon as IGeometry;
            fea.Store();
            axMapControl1.Refresh();

        }

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小薛引路

喜欢的读者,可以打赏鼓励一下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值