基于C#的ArcEngine二次开发49:修改图层名称和别名、字段名称

 

目录

1 修改图层名称和别名

2 修改字段名称


1 修改图层名称和别名

Object classes in a Geodatabase can have between one and three names. The name of the object class, which is the same as the name of the table in the DBMS in which the objects in the object class are stored, the alias name which the user can set for display purposes in end user applications. The third name is the model name which is a tool for developers of custom objects to use to guarantee the names of objects independent of the true name or alias name.

代码:以gdb中图层为例修改图层名称(name)和别名(alias name)

public void AlterName()
{
    Type type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
    IWorkspaceFactory pWorksapcFactory = (IWorkspaceFactory)Activator.CreateInstance(type);
    IWorkspace pWorkspace = pWorksapcFactory.OpenFromFile(@"F:\GIS测试数据\测试.gdb", 0);
 
    IFeatureWorkspace pFWK = pWorkspace as IFeatureWorkspace;
    IFeatureClass pFeatureClass = pFWK.OpenFeatureClass("SSS");
 
    //修改图层名称
    IDataset dataset = pFeatureClass as IDataset;
    dataset.Rename("MC");
    //修改图层别名
    AlterAliasName(pFeatureClass);
}
 
/// 
/// 修改数据集别名
/// 
/// 对象类
public void AlterAliasName(IObjectClass objectClass)
{
    //cast for the IClassSchemaEdit
    IClassSchemaEdit pOcSchemaEdit = objectClass as IClassSchemaEdit;
 
    //set and exclusive lock on the class 设置并独占锁
    ISchemaLock schemaLock = (ISchemaLock)objectClass;
    schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
 
    //alter the class extension for the class
    pOcSchemaEdit.AlterAliasName("修改别名");
 
    //release the exclusive lock 释放锁
    schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
}

2 修改字段名称

修改字段名称需注意设置锁模式为执行模式锁(esriExclusiveSchemaLock),当其他应用已经锁定要素类,不管是(esriExclusiveSchemaLock,还是esriSharedSchemaLock),都无法修改当前要素类锁类型,即无法修改字段名称。当字段修改完成后应该将当前的执行模式锁(esriExclusiveSchemaLock)改为共享锁(esriExclusiveSchemaLock)。另外IClassSchemaEdit4接口还可以修改属性域、字段缺省值(即默认值)等等,但是不支持字段长度修改。

/// 
        /// 修改字段名称
        /// 
        /// 目标要素类
        /// 目标字段名称
        /// 目标字段新名称
        /// 目标字段新别名        
        private bool ModifyFieldName(IFeatureClass pFeatureClass, string oldFieldName, string newFieldName, string aliasName)
        {
            bool isModified = false;
            ISchemaLock pSchemaLock = null;
            try
            {
                IFields pFields = pFeatureClass.Fields;
                int fIndex = pFields.FindField(oldFieldName);
                if (fIndex == -1) return isModified;
                pSchemaLock = pFeatureClass as ISchemaLock;
                pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);//设置编辑锁
                IClassSchemaEdit4 pClassSchemaEdit = pFeatureClass as IClassSchemaEdit4;
                pClassSchemaEdit.AlterFieldAliasName(oldFieldName, aliasName);
                pClassSchemaEdit.AlterFieldName(oldFieldName, newFieldName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
               //释放编辑锁
pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); 
                isModified = true;
            }
            return isModified;
        }

 

你可能感兴趣的:(基于C#的ArcEngine二次开发49:修改图层名称和别名、字段名称)