public override void Setup() { base.Setup(); // The code to create a new coded value domain. _CodedValueDomain = new CodedValueDomainClass(); // Value and name pairs. _CodedValueDomain.AddCode("RES", "Residential"); _CodedValueDomain.AddCode("COM", "Commercial"); _CodedValueDomain.AddCode("IND", "Industrial"); _CodedValueDomain.AddCode("BLD", "Building"); }
private static void UpdateCodedValueDomain(IWorkspace theWorkspace, string DomainName, string SourceClassName, string CodeFieldName, string ValueFieldName) { // Get reference to the table to read codes and values from ITable theTable = commonFunctions.OpenTable(theWorkspace, SourceClassName); // Get reference to the domain itself IWorkspaceDomains wsDomains = (IWorkspaceDomains)theWorkspace; ICodedValueDomain theDomain = (ICodedValueDomain)wsDomains.get_DomainByName(DomainName); // Requires exclusive schema lock ISchemaLock schemaLock = (ISchemaLock)theDomain; try { // Get an exclusive lock schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); // Clear everything out of the domain first int codedValues = theDomain.CodeCount; for (int i = 0; i <= codedValues - 1; i++) { theDomain.DeleteCode(theDomain.Value[0]); } // Sort the table ITableSort tableSorter = new TableSortClass(); tableSorter.Fields = ValueFieldName; tableSorter.set_Ascending(ValueFieldName, true); tableSorter.Table = theTable; tableSorter.Sort(null); // Loop through the sorted rows, add to the domain int codeFld = theTable.FindField(CodeFieldName); int valueFld = theTable.FindField(ValueFieldName); ICursor theCursor = tableSorter.Rows; IRow theRow = theCursor.NextRow(); while (theRow != null) { theDomain.AddCode(theRow.get_Value(codeFld), theRow.get_Value(valueFld).ToString()); theRow = theCursor.NextRow(); } } catch (Exception e) { MessageBox.Show(DomainName + " was not updated. This is likely because an exclusive schema lock could not be obtained.", "NCGMP Tools"); } finally { // Release the exclusive lock, if it was acquired. // This block of code (finally) is called whether or not there is a problem schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }
protected override void OnClick() { m_pApp = (IApplication)ArcMap.Application; if (m_pApp == null) { //if the app is null then could be running from ArcCatalog m_pApp = (IApplication)ArcCatalog.Application; } if (m_pApp == null) { MessageBox.Show("Could not access the application.", "No Application found"); return; } IGxApplication pGXApp = (IGxApplication)m_pApp; stdole.IUnknown pUnk = null; try { pUnk = (stdole.IUnknown)pGXApp.SelectedObject.InternalObjectName.Open(); } catch (COMException ex) { if (ex.ErrorCode == (int)fdoError.FDO_E_DATASET_TYPE_NOT_SUPPORTED_IN_RELEASE || ex.ErrorCode == -2147220944) { MessageBox.Show("The dataset is not supported in this release.", "Could not open the dataset"); } else { MessageBox.Show(ex.ErrorCode.ToString(), "Could not open the dataset"); } return; } if (pUnk is ICadastralFabric) { m_pCadaFab = (ICadastralFabric)pUnk; } else { MessageBox.Show("Please select a parcel fabric and try again.", "Not a parcel fabric"); return; } Utils FabricUTILS = new Utils(); ITable pTable = m_pCadaFab.get_CadastralTable(esriCadastralFabricTable.esriCFTPlans); IDataset pDS = (IDataset)pTable; IWorkspace pWS = pDS.Workspace; //Do a Start and Stop editing to make sure we're not running in an edit session if (!FabricUTILS.StartEditing(pWS, true)) {//if start editing fails then bail if (pUnk != null) { Marshal.ReleaseComObject(pUnk); } Cleanup(pTable, pWS); FabricUTILS = null; return; } FabricUTILS.StopEditing(pWS); bool bAddedField = false; if (FabricUTILS.GetFabricVersion((ICadastralFabric2)m_pCadaFab) < 2) { bAddedField = FabricUTILS.CadastralTableAddFieldV1(m_pCadaFab, esriCadastralFabricTable.esriCFTPlans, esriFieldType.esriFieldTypeInteger, "KeepOnMerge", "KeepOnMerge", 1); } else { bAddedField = FabricUTILS.CadastralTableAddField(m_pCadaFab, esriCadastralFabricTable.esriCFTPlans, esriFieldType.esriFieldTypeInteger, "KeepOnMerge", "KeepOnMerge", 1); } if (bAddedField) { MessageBox.Show("Plan-merge helper field 'KeepOnMerge' added.", "Add Field"); } else { MessageBox.Show("Field 'KeepOnMerge' could not be added." + Environment.NewLine + "The field may already exist.", "Add Field"); } if (bAddedField) { //if the field was added succesfully, add the Yes/No domain IDomain pDom = new CodedValueDomainClass(); try { IWorkspaceDomains2 pWSDoms = (IWorkspaceDomains2)pWS; pDom.FieldType = esriFieldType.esriFieldTypeInteger; pDom.Name = "Flag for Keep on Plan Merge"; pDom.Description = "Flag for Keep on Plan Merge"; ICodedValueDomain pCVDom = (ICodedValueDomain)pDom; //pCVDom.AddCode(0, "No"); pCVDom.AddCode(1, "Keep On Merge"); pWSDoms.AddDomain(pDom); } catch (COMException ex) { MessageBox.Show(ex.ErrorCode.ToString()); } //Get the field int iFld = pTable.FindField("KeepOnMerge"); if (iFld >= 0) { IField pFld = pTable.Fields.get_Field(iFld); // Check that the field and domain have the same field type. if (pFld.Type == pDom.FieldType) { // Cast the feature class to the ISchemaLock and IClassSchemaEdit interfaces. ISchemaLock schemaLock = (ISchemaLock)pTable; IClassSchemaEdit classSchemaEdit = (IClassSchemaEdit)pTable; // Attempt to get an exclusive schema lock. try { // Lock the class and alter the domain. schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); classSchemaEdit.AlterDomain("KeepOnMerge", pDom); Console.WriteLine("The domain was successfully assigned."); } catch (COMException 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); } } } if (pDom != null) { Marshal.ReleaseComObject(pDom); } } Cleanup(pTable, pWS); FabricUTILS = null; }
private void btn_Save_Click(object sender, EventArgs e) { IDomain domain = null; IRangeDomain rd = null; ICodedValueDomain cv = null; IDomainFactory df = new DomainFactory(); DataTable table = null; try { //保存时,删除操作才生效 foreach (string s in deletedomains) { ds.DeleteDomain(s); } for (int i = 0; i < this.dgv_DomainAttr.Rows.Count; i++) { DataGridViewRow myRow = this.dgv_DomainAttr.Rows[i]; string name = myRow.Cells["name"].Value == null ? "" : myRow.Cells["name"].Value.ToString(); string description = myRow.Cells["description"].Value == null ? "" : myRow.Cells["description"].Value.ToString(); string ft = myRow.Cells["fieldtype"].Value == null ? "" : myRow.Cells["fieldtype"].Value.ToString(); string dt = myRow.Cells["domaintype"].Value == null ? "" : myRow.Cells["domaintype"].Value.ToString(); if (name.Equals("")) { continue; //名称为空,不创建 } if (myRow.Tag == null) { continue; } if (ft.Equals("String") && dt.Equals("值域型")) { continue; } table = myRow.Tag as DataTable; if (!HasDomain(name, ds)) { if (dt == "值域型") { rd = df.CreateRangeDomain(name, GetFDEFieldTypeByString(ft)); rd.Description = description; rd.MinValue = table.Rows[0].IsNull(sMinFieldName) ? GetColumnDefaultValue(ft) : table.Rows[0][sMinFieldName]; rd.MaxValue = table.Rows[0].IsNull(sMaxFieldName) ? GetColumnDefaultValue(ft) : table.Rows[0][sMaxFieldName]; ds.AddDomain(rd); } else if (dt == "枚举型") { cv = df.CreateCodedValueDomain(name, GetFDEFieldTypeByString(ft)); cv.Description = description; for (int j = 0; j < table.Rows.Count; j++) { if (table.Rows[j][sDscribFieldName].ToString() != "") { if (table.Rows[j].IsNull(sEmunFieldName)) { continue; } cv.AddCode(table.Rows[j][sEmunFieldName], table.Rows[j][sDscribFieldName].ToString()); } } ds.AddDomain(cv); } } else { domain = ds.GetDomainByName(name); if (dt == "值域型") { rd = domain as IRangeDomain; rd.Description = description; rd.MaxValue = table.Rows[0].IsNull(sMaxFieldName) ? GetColumnDefaultValue(ft) : table.Rows[0][sMaxFieldName]; rd.MinValue = table.Rows[0].IsNull(sMinFieldName) ? GetColumnDefaultValue(ft) : table.Rows[0][sMinFieldName]; ds.ModifyDomain(rd); } else { cv = domain as ICodedValueDomain; cv.Description = description; int codecount = cv.CodeCount; for (int a = 0; a < codecount; a++) { cv.DeleteCode(cv.GetCodeValue(0)); } for (int l = 0; l < table.Rows.Count; l++) { if (table.Rows[l][sDscribFieldName].ToString() != "") { if (table.Rows[l].IsNull(sEmunFieldName)) { continue; } cv.AddCode(table.Rows[l][sEmunFieldName], table.Rows[l][sDscribFieldName].ToString()); } } ds.ModifyDomain(cv); } } } MessageBox.Show("保存成功!"); } catch (COMException comEx) { MessageBox.Show(comEx.Message); this.DialogResult = DialogResult.None; } catch (System.Exception ex) { MessageBox.Show(ex.Message); this.DialogResult = DialogResult.None; } finally { if (cv != null) { //Marshal.ReleaseComObject(cv); cv = null; } if (rd != null) { //Marshal.ReleaseComObject(rd); rd = null; } if (domain != null) { //Marshal.ReleaseComObject(domain); domain = null; } } }