ArcGIS Engine创建要素

1、获取指定类型的图层

在创建要素之前,要获取相应的图层。

以下函数根据指定要素类型获取图层:

 private IFeatureLayer getLayerByShape(esriGeometryType type)
        {
            int lyerNum = axMapControl1.LayerCount;
            IFeatureLayer resLayer = null;
            for (int i = 0; i < lyerNum; i++)
            {
                ILayer pLayer = axMapControl1.get_Layer(i);
                IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
                if (pFeatureLayer == null)
                {
                    continue;
                }
                else
                {
                    esriGeometryType thistype = pFeatureLayer.FeatureClass.GetFeature(1).Shape.GeometryType;
                    if (thistype == type)
                    {
                        resLayer = pFeatureLayer;
                    }
                }
            }
            return resLayer;
        }

2、创建点要素

添加点的函数

        private void addPoint(IPoint pPoint, IFeatureLayer pFeaLyr)
        {
            IFeatureClass pFeaClass = pFeaLyr.FeatureClass;
            IFeature pFeature = pFeaClass.CreateFeature();
            pFeature.Shape = pPoint;
            pFeature.Store();
        }

添加axMapControl的点击事件

                    IPoint pPoint = new PointClass();
                    pPoint.X = e.mapX;
                    pPoint.Y = e.mapY;
                    IFeatureLayer pFealyr = getLayerByShape(esriGeometryType.esriGeometryPoint);
                    if (pFealyr != null)
                    {
                        addPoint(pPoint, pFealyr);
                    }

                    axMapControl1.ActiveView.Refresh();

3、创建线要素

添加线的函数

        private void addLine(IPolyline pLine, IFeatureLayer pFeatureLayer)
        {
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
            IFeature pFeature = pFeatureClass.CreateFeature();
            pFeature.Shape = pLine;
            pFeature.Store();
        }

添加axMapControl的点击事件

                    IPolyline polyline = axMapControl1.TrackLine() as IPolyline;
                    IFeatureLayer pFeatureLayer = getLayerByShape(esriGeometryType.esriGeometryPolyline);
                    if (pFeatureLayer != null)
                    {
                        addLine(polyline, pFeatureLayer);
                    }

                    axMapControl1.ActiveView.Refresh();

4、创建面要素

添加面的函数

        private void addPolygon(IPolygon pPolygon, IFeatureLayer pFeatureLayer)
        {
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
            IFeature pFeature = pFeatureClass.CreateFeature();
            pFeature.Shape = pPolygon;
            pFeature.Store();
        }

添加axMapControl的点击事件

                    IPolygon pPolygon = axMapControl1.TrackPolygon() as IPolygon;
                    IFeatureLayer pFeatureLayer2 = getLayerByShape(esriGeometryType.esriGeometryPolygon);
                    if (pFeatureLayer2 != null) {
                        addPolygon(pPolygon, pFeatureLayer2);
                    }   

                    axMapControl1.ActiveView.Refresh();

PS:网络对于创建线要素的影响

似乎由于效应网络的影响,有些线要素图层上,不能使用要素类的 Create方法直接创建,笔者直接使用 Create方法时,报错说:

不能在编辑会话之外更新此类中的对象

自己创建一个线要素图层,之后添加线要素成功。

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值