ArcEngine Domain(待補充......)
阿新 • • 發佈:2018-12-18
基礎知識
ArcEngine操作
常用介面
IDomain、ICodedValueDomain、ICodedValueDomain2、IRangeDomain
建立編碼域(coded value domains)
//將Workspace物件轉換到IworkspaceDomains介面
IWorkspaceDomains workspaceDomains = (IWorkspaceDomains)workspace;
// 建立編碼域
ICodedValueDomain codedValueDomain = new CodedValueDomainClass();
// 新增編碼
codedValueDomain.AddCode("RES", "Residential");
codedValueDomain.AddCode("COM", "Commercial");
codedValueDomain.AddCode("IND", "Industrial");
codedValueDomain.AddCode("BLD", "Building");
// 移除編碼
codedValueDomain.DeleteCode("BLD"); // The "Building" code is removed.
// 將ICodedValueDomain物件轉換到IDomain物件上,設定通用屬性
IDomain domain = (IDomain)codedValueDomain;
domain.Name = "Building Types";
domain.FieldType = esriFieldType.esriFieldTypeString;
domain.SplitPolicy = esriSplitPolicyType.esriSPTDuplicate;
domain.MergePolicy = esriMergePolicyType.esriMPTDefaultValue;
// 新增Domain到工作空間中
workspaceDomains.AddDomain(domain);
建立範圍域(range domains)
// 將Workspace物件轉換到IworkspaceDomains介面
IWorkspaceDomains workspaceDomains = (IWorkspaceDomains)workspace;
// 建立範圍域
IRangeDomain rangeDomain = new RangeDomainClass();
rangeDomain.MinValue = 0;//設定最小值
rangeDomain.MaxValue = 255;//設定最大值
//將IRangeDomain物件轉換到IDomain物件上,設定通用屬性
IDomain domain = (IDomain)rangeDomain;
domain.Name = "EightBitUnsignedInt";//設定Domain名稱
domain.FieldType = esriFieldType.esriFieldTypeInteger;//設定Domain的欄位型別
domain.SplitPolicy = esriSplitPolicyType.esriSPTDuplicate;//設定Domain的分割策略
domain.MergePolicy = esriMergePolicyType.esriMPTAreaWeighted;//設定Domain的合併策略
//新增Domain到工作空間中
workspaceDomains.AddDomain(domain);
開啟值域
IWorkspaceDomains workspaceDomains = (IWorkspaceDomains)workspace;
IDomain requestedDomain = workspaceDomains.get_DomainByName(domainName);
刪除值域
IWorkspaceDomains workspaceDomains = (IWorkspaceDomains)workspace;
workspaceDomains.DeleteDomain(domainName);
#
修改值域
//修改Domain的屬性
......程式碼略
//修改完,呼叫AlterDmoain方法儲存修改
IWorkspaceDomains2.AlterDomain(pDomain);
在欄位上應用值域
// For example: domainName = "Material".
// fieldName = "CP_MATERIAL".
public void AssignDomainToField(IFeatureClass featureClass, String domainName,String fieldName)
{
// Cast the feature class to IDataset to get a reference to the workspace.
IDataset dataset = (IDataset)featureClass;
// Get the workspace and cast it to the IWorkspaceDomains interface and get the requested domain.
IWorkspace workspace = dataset.Workspace;
IWorkspaceDomains workspaceDomains = (IWorkspaceDomains)workspace;
IDomain domain = workspaceDomains.get_DomainByName(domainName);
// Get the field to assign the domain to.
IFields fields = featureClass.Fields;
int fieldIndex = featureClass.FindField(fieldName);
IField field = fields.get_Field(fieldIndex);
// Check that the field and domain have the same field type.
if (field.Type == domain.FieldType)
{
// Cast the feature class to the ISchemaLock and IClassSchemaEdit interfaces.
ISchemaLock schemaLock = (ISchemaLock)featureClass;
IClassSchemaEdit classSchemaEdit = (IClassSchemaEdit)featureClass;
// Attempt to get an exclusive schema lock.
try
{
// Lock the class and alter the domain.
schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
classSchemaEdit.AlterDomain(fieldName, domain);
Console.WriteLine("The domain was successfully assigned.");
}
catch (Exception exc)
{
// Handle the exception in a way appropriate for the application.
Console.WriteLine(exc.Message);
}
finally
{
// Set the schema lock to be a shared lock.
schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
}
}
else
{
Console.WriteLine("The field and the domain have different field types: " +
"Field = {0}, Domain = {1}", field.Type, domain.FieldType);
}
}