AE开发常用经验汇集

一、AE 向已存在的要素类中添加字段

 链接:  AE 向已存在的要素类中添加字段

在向已存在的要素类中添加字段的时候,需要用到ICLASS接口。于是,进一步的调整代码如下,问题得以解决

static void AddFiled(IFeatureClass pFC,string name)
{
DeleteFile(pFC, name);
IFields pFields = pFC.Fields;
IClass pClass = pFC as IClass;
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = name;
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
pClass.AddField(pField);
}

直接添加到Ifields是不成功的

static void AddFiled(IFeatureClass pFC,string name)
{

IFields pFields = pFC.Fields;
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = name;
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
pFields.AddField(pField);

}

二、AE高效处理

2.1属性高效处理

 链接:属性的更新 

运用IFeature和IRow的Store方法更新速度都很慢,用ICursor 的UpdateRow方法速度很快,分别是前两者效率的184倍、159倍!!

 ICursor pCursor =pTable.Update(null, false);
    pRow = pCursor.NextRow();
      for (int i = 0; i < pTable.RowCount(null); i++)
        {
            pRow.set_Value(2, i + 6);
            pCursor.UpdateRow(pRow);
            pRow = pCursor.NextRow();
         }

大量数据时不建议使用以下方式

//方法一: 
feature.set_Value(2, i);
feature.Store();

//方法二: 
ITable pTable = pLayer.FeatureClass as ITable;
IRow pRow = pTable.GetRow(i);
pRow.set_Value(2, i + 6);

 

2.2批量删除要素

        ///<summary>
        ///快速删除某featurelayer中所有feature
        ///</summary>
        ///<param name="pLayer">操作的涂层</param>
        ///<remarks>该方法可以给一个queryfilter,进行删除符合条件的features</remarks>
        public static void DeleteAllFeatures(IFeatureClass featCls, IQueryFilter queryFilter)
        {
            ITable pTable = featCls as ITable;
            pTable.DeleteSearchedRows(queryFilter);
        }

 

2.3快速批量插入要素

使用IFeatureBuffer 
        public static void InsertFeaturesUsingCursor(IFeatureClass featureClass, List<
            IGeometry> geometryList)
        {

            //using (ComReleaser comReleaser = new ComReleaser())
            //{
            // Create a feature buffer.
            IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
            //comReleaser.ManageLifetime(featureBuffer);

            // Create an insert cursor.
            IFeatureCursor insertCursor = featureClass.Insert(true);
            //comReleaser.ManageLifetime(insertCursor);

            // All of the features to be created are classified as Primary Highways.
            int typeFieldIndex = featureClass.FindField("TYPE");
            featureBuffer.set_Value(typeFieldIndex, "Primary Highway");
            foreach (IGeometry geometry in geometryList)
            {
                // Set the feature buffer's shape and insert it.
                featureBuffer.Shape = geometry;
                insertCursor.InsertFeature(featureBuffer);
            }

            // Flush the buffer to the geodatabase.
            insertCursor.Flush();
            // }
        }

 

三、AE ArcEngine 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

链接: 【转】.NET+AE开发中常见几种非托管对象的释放

今天在开发时遇到一个问题:" 未处理 System.AccessViolationException Message="尝试读取或写入受保护的内存。这通常指示其他内存已损坏。"      

这通常指示其他内存已损坏。不知大家遇到过没有,这种错误是由于用户反复写入或打开文件,

      最终使得内存资源没有及时释放导致此错误。这种错误是无法跟踪的,必须强制释放!

      没有强制的释放方法,底层的com组件根据你进程调用组件的次数来控制对象的增加和释放(对象释放会延迟,

      com自   身的问题)。一般是调用强制垃圾回收或ao自带的回收对象的方法,效果不明显。解决方法如下:

         1.将IIS重新启动。(我是按此方法解决的。)

         2.关闭VS进程,退出VS2005.

         3.上述方法若不行的话,可以自己在程序里强制释放COM资源,调用Marshal.ReleaseComObject()方法将不再使用的对象释放掉。

 

四、ArcEngine开发中“错误类型"****"未定义构造函数”

问题:在ArcEngine开发的时候,在编译时,发现出现这样的错误,出错的地方在实例化一个对象的时候。

比如:ISpatialReference a = new UnknownCoordinateSystemClass();

_hookHelper = new HookHelperClass();等等

 

解决办法:

选中实例化的类所在的程序集,如UnknownCoordinateSystemClass在ESRI.ArcGIS.Geometry中,在引用中选中,查看属性,把“嵌入互操作类型”由True改成False

 

 

 

-

posted @ 2017-03-02 09:05  AllenRobin  阅读(1074)  评论(0编辑  收藏  举报