public static bool UpdateExtent(IFeatureClass fc) { bool rbc = false; ISchemaLock schemaLock = (ISchemaLock)fc; try { IFeatureClassManage fcManage = (IFeatureClassManage)fc; if (fcManage != null) { //schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); fcManage.UpdateExtent(); schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); //Console.WriteLine("Update finished"); rbc = true; } } catch (Exception e) { //error occured schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); //Console.WriteLine(e.Message); Log.WriteLine(e); } return(rbc); }
private bool RemoveClsInstance(IClass classInstance, UID uid) { ISchemaLock schemaLock = (ISchemaLock)classInstance; bool result = false; try { // Attempt to get an exclusive schema lock. schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); // Cast the object class to the IClassSchemaEdit2 interface. IClassSchemaEdit2 classSchemaEdit = (IClassSchemaEdit2)classInstance; classSchemaEdit.AlterInstanceCLSID(uid); result = true; } catch { result = false; } finally { schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } return(result); }
} // doWork method protected void RegisterWithGeodatabase( IObjectClass objectClass, String oidFieldName) { if (oidFieldName == "") { oidFieldName = "OBJECTID"; } // Attempt to acquire an exclusive schema lock for the object class. ISchemaLock schemaLock = (ISchemaLock)objectClass; try { schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); // If this point is reached, the exclusive lock was acquired. We can cast the object // class to IClassSchemaEdit and call RegisterAsObjectClass. IClassSchemaEdit classSchemaEdit = (IClassSchemaEdit)objectClass; classSchemaEdit.RegisterAsObjectClass(oidFieldName, ""); } catch (COMException comExc) { // Re-throw the exception. throw comExc; } finally { // Reset the lock on the object class to a shared lock. schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } } // RegisterWithGeodatabase method
// 删除 private void DeleteAttriToolStripMenuItem_Click(object sender, EventArgs e) { string fieldName = dataGridView1.Columns[_index].Name; var featureLayer = axMapControl1.get_Layer(0) as IFeatureLayer; if (featureLayer != null) { var featureClass = featureLayer.FeatureClass; int n = featureClass.FindField(fieldName); if (n != -1) { ISchemaLock schemaLock = featureClass as ISchemaLock; if (schemaLock != null) { schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); IField field = featureClass.Fields.Field[n]; featureClass.DeleteField(field); schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } } } ShowFeatureLayerAttrib(featureLayer); ShowFeatures(featureLayer, null, false); }
/// <summary> /// Register the newly created table with the Geodatabase. /// </summary> /// <param name="pTable"></param> /// <param name="oidFieldName"></param> private static void RegisterWithGeodatabase(ITable pTable, string oidFieldName) { if (!(IsArcInfoLicense())) { return; } IObjectClass oc = (IObjectClass)pTable; if (oidFieldName == "") { oidFieldName = "OBJECTID"; } // Attempt to acquire an exclusive schema lock for the object class. ISchemaLock schemaLock = (ISchemaLock)oc; try { schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); // If this point is reached, the exclusive lock was acquired. We can cast the object // class to IClassSchemaEdit and call RegisterAsObjectClass. IClassSchemaEdit classSchemaEdit = (IClassSchemaEdit)oc; classSchemaEdit.RegisterAsObjectClass(oidFieldName, ""); } catch (COMException comExc) { // Re-throw the exception. throw comExc; } finally { // Reset the lock on the object class to a shared lock. schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } } // method RegisterWithGeodatabase
public bool DeleteSpatialIndex(IFeatureClass featureClass) { bool result = true; ISchemaLock pSchemaLock = featureClass as ISchemaLock; try { pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); IIndexes indexes = featureClass.Indexes; String shapeFieldName = featureClass.ShapeFieldName; IEnumIndex enumIndex = indexes.FindIndexesByFieldName(shapeFieldName); enumIndex.Reset(); IIndex index = enumIndex.Next(); if (index != null) { featureClass.DeleteIndex(index); } } catch (Exception ex) { ErrorMessage = ex.ToString(); result = false; } finally { pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } return(result); }
public static void InsertLoadOnly(IFeatureClass featureCls, IList <IGeometry> geoList) { IFeatureClassLoad featureClsLoad = (IFeatureClassLoad)featureCls; ISchemaLock schemaLock = (ISchemaLock)featureCls; try { schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); featureClsLoad.LoadOnlyMode = true; using (var comReleaser = new ComReleaser()) { IFeatureBuffer featureBuffer = featureCls.CreateFeatureBuffer(); IFeatureCursor featureCursor = featureCls.Insert(true); comReleaser.ManageLifetime(featureCursor); featureBuffer.set_Value(featureBuffer.Fields.FindField("InstBy"), "B Pierce"); for (int ic = 0; ic < geoList.Count; ic++) { featureBuffer.Shape = geoList[0]; var featureOID = featureCursor.InsertFeature(featureBuffer); if (ic % 10 == 0) { featureCursor.Flush(); } } featureCursor.Flush(); } } finally { featureClsLoad.LoadOnlyMode = false; schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }
private bool RemoveClsExt(IClass classExtension) { ISchemaLock schemaLock = (ISchemaLock)classExtension; bool result = false; try { // Attempt to get an exclusive schema lock. schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); // Cast the object class to the IClassSchemaEdit2 interface. IClassSchemaEdit2 classSchemaEdit = (IClassSchemaEdit2)classExtension; // Clear the class extension. classSchemaEdit.AlterClassExtensionCLSID(null, null); IObjectClassDescription featureClassDescription = new FeatureClassDescriptionClass(); classSchemaEdit.AlterInstanceCLSID(featureClassDescription.InstanceCLSID); result = true; } catch { result = false; } finally { schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } return(result); }
/// <summary> /// Add new field. /// </summary> /// <param name="featureClass"></param> /// <param name="fieldName"></param> /// <param name="fieldType"></param> /// <param name="fldLength"></param> /// <param name="isNullable"></param> /// <param name="defaultValue"></param> public static void AddNewField(this IFeatureClass featureClass, string fieldName, esriFieldType fieldType, int fldLength = 1, bool isNullable = true, object defaultValue = null) { ISchemaLock schemaLock = (ISchemaLock)featureClass; try { schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); IFieldEdit2 field = new Field() as IFieldEdit2; field.Name_2 = fieldName; field.Type_2 = fieldType; field.IsNullable_2 = isNullable; field.DefaultValue_2 = defaultValue; if (fieldType == esriFieldType.esriFieldTypeString) { field.Length_2 = fldLength; } featureClass.AddField(field); } finally { schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }
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); } }
public override void OnClick() { try { ITable table = (((IGxSelection)_context.GxSelection).FirstObject as IGxDataset).Dataset as ITable; if (table is IFeatureClass) { Geoprocessor geoprocessor_ = new Geoprocessor(); string text = (table as IDataset).Workspace.PathName; bool flag = false; if (string.IsNullOrEmpty(text)) { string startupPath = System.Windows.Forms.Application.StartupPath; string text2 = Guid.NewGuid().ToString() + ".sde"; (table as IDataset).Workspace.WorkspaceFactory.Create(startupPath, text2, (table as IDataset).Workspace.ConnectionProperties, 0); text = System.IO.Path.Combine(startupPath, text2); flag = true; } string text3 = text; if ((table as IFeatureClass).FeatureDataset != null) { text3 = System.IO.Path.Combine(text3, (table as IFeatureClass).FeatureDataset.Name); } text3 = System.IO.Path.Combine(text3, (table as IDataset).Name); if ((table as IFeatureClass).ShapeType != esriGeometryType.esriGeometryAny) { CommonHelper.RunTool(geoprocessor_, new RegisterWithGeodatabase { in_dataset = text3 }, null); if (flag) { File.Delete(text); } } } else { ISchemaLock schemaLock = (ISchemaLock)table; schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); (table as IClassSchemaEdit).RegisterAsObjectClass("ObjectID", ""); schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } } catch (Exception ex) { ex.ToString(); } }
/// <summary> /// 创建拓扑集成方法 /// </summary> /// <param name="TopoName"></param> /// <param name="pFeatureDS"></param> /// <param name="FeatureClassList"></param> /// <param name="TopoDT"></param> /// <returns></returns> public ITopology2 CreateToplolgy(string TopoName, IFeatureDataset pFeatureDS, List <IFeatureClass> FeatureClassList, DataTable TopoDT) { ISchemaLock pSchemaLock = (ISchemaLock)pFeatureDS; try { pFeatureClassList = FeatureClassList; pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); //创建拓扑 ITopologyContainer2 pTopoContainer = pFeatureDS as ITopologyContainer2; ITopology2 pTopology = pTopoContainer.CreateTopology(TopoName, pTopoContainer.DefaultClusterTolerance, -1, "") as ITopology2; //添加要素 foreach (IFeatureClass pFeatureClass in FeatureClassList) { pTopology.AddClass(pFeatureClass, 5, 1, 1, false); } //添加规则 for (int i = 0; i < TopoDT.Rows.Count; i++) { string s = TopoDT.Rows[i][0].ToString(); if (TopoDT.Rows[i][2].ToString() == "") { AddTopoRules(pTopology, TopoDT.Rows[i][0].ToString(), TopoDT.Rows[i][1].ToString()); } else { AddTopoRules(pTopology, TopoDT.Rows[i][0].ToString(), TopoDT.Rows[i][1].ToString(), TopoDT.Rows[i][2].ToString()); } } //验证拓扑 IGeoDataset pGeoDataset = pTopology as IGeoDataset; IEnvelope pEnvelope = pGeoDataset.Extent; VaildateTopo(pTopology, pEnvelope); return(pTopology); } catch (COMException COMEx) { MessageBox.Show(COMEx.Message); return(null); } finally { pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }
/// <summary> /// Deletes the table. You must have exlusive rights to the table in order to delete it. /// Otherwise an error will be thrown. /// </summary> public static void Delete(this ITable source) { IDataset ds = (IDataset)source; ISchemaLock schemaLock = (ISchemaLock)ds; try { schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); ds.Delete(); } finally { schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }
private void RenameClassObject(IDataset dsCurrent, int standardID) { if (dsCurrent == null) { return; } Hy.Check.Define.StandardLayer standardLayer = LayerReader.GetLayerByName(dsCurrent.Name, standardID); //string strAliasName = Hy.Check.Rule.Helper.LayerReader.GetAliasName(dsCurrent.Name, standardID); if (standardLayer != null)// dsCurrent.Name != strAliasName) { ISchemaLock schemaLock = dsCurrent as ISchemaLock; schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); IClassSchemaEdit classSchemaEdit = dsCurrent as IClassSchemaEdit; if (classSchemaEdit != null) { classSchemaEdit.AlterAliasName(standardLayer.AliasName); ITable tCurrent = dsCurrent as ITable; if (tCurrent != null) { List <Hy.Check.Define.StandardField> fields = FieldReader.GetFieldsByLayer(standardLayer.ID); for (int i = 0; i < fields.Count; i++) { if (tCurrent.Fields.FindField(fields[i].Name) > -1) { classSchemaEdit.AlterFieldAliasName(fields[i].Name, fields[i].AliasName); } } } } } }
private void Progarm() { IWorkspace workspace = ArcClass.GetmdbWorkspace(mdbFilePath); if (workspace == null) { return; } IFeatureDataset featureDataset = ArcClass.GetFeatureDataset(workspace, "WJL"); ISchemaLock schemalock = featureDataset as ISchemaLock; try { schemalock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); ITopologyContainer topologyContainer = featureDataset as ITopologyContainer; ITopology topology = topologyContainer.CreateTopology("ttt", topologyContainer.DefaultClusterTolerance, -1, ""); IFeatureClass featureClass = ArcClass.GetFeatureClass(workspace, "PARK"); AddRuleToTopology(topology, esriTopologyRuleType.esriTRTAreaNoOverlap, "NO Block OverLap", featureClass); IGeoDataset geoDataset = topology as IGeoDataset; IEnvelope envelop = geoDataset.Extent; ValidateTopology(topology, envelop); }catch (COMException) { } }
public void IClassSchemaEdit_Example(IObjectClass objectClass) { //This function shows how you can use the IClassSchemaEdit //interface to alter the COM class extension for an object class. //cast for the IClassSchemaEdit IClassSchemaEdit classSchemaEdit = (IClassSchemaEdit)objectClass; //set and exclusive lock on the class ISchemaLock schemaLock = (ISchemaLock)objectClass; schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); ESRI.ArcGIS.esriSystem.UID classUID = new ESRI.ArcGIS.esriSystem.UIDClass(); //GUID for the C# project. classUID.Value = "{65a43962-8cc0-49c0-bfa3-015d0ff8350e}"; classSchemaEdit.AlterClassExtensionCLSID(classUID, null); //release the exclusive lock schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); }
//更改表模型名 函数 public static bool AlterModelName(ITable table, string NewModelName) { bool rbc = false; ISchemaLock sLock = table as ISchemaLock; IClassSchemaEdit csEdit = table as IClassSchemaEdit; if (sLock != null && csEdit != null) { sLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); //改变要素模型名 csEdit.AlterModelName(NewModelName); //释放锁定 sLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); rbc = true; } return(rbc); }
//更改要素类中字段默认值 函数 public static bool AlterFieldDefaultValue(IFeatureClass fc, string FieldName, object defaultvalue) { bool rbc = false; ISchemaLock sLock = fc as ISchemaLock; IClassSchemaEdit csEdit = fc as IClassSchemaEdit; if (sLock != null && csEdit != null) { sLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); //改变要素类中字段别名 csEdit.AlterDefaultValue(FieldName, defaultvalue); //释放锁定 sLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); rbc = true; } return(rbc); }
//更改要素类中字段模型名 函数 public static bool AlterFieldModelName(IFeatureClass fc, string FieldName, string NewModelName) { bool rbc = false; ISchemaLock sLock = fc as ISchemaLock; IClassSchemaEdit csEdit = fc as IClassSchemaEdit; if (sLock != null && csEdit != null) { sLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); //改变要素类中字段模型名 csEdit.AlterFieldModelName(FieldName, NewModelName); //释放锁定 sLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); rbc = true; } return(rbc); }
/// <summary> /// Perform the work contained in the delegate inside an exclusive schema lock. /// </summary> /// <param name="fc">The feature class whose schema is to be exclusively locked.</param> /// <param name="work">The work to be performed.</param> static void DoInSchemaLock(IFeatureClass fc, MethodInvoker work) { ISchemaLock schemaLock = (ISchemaLock)fc; try { // Exclusively lock the class schema. schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); // Do the work inside the schema lock work(); } finally { // Release the exclusive lock on the featureclass' schema. schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }
/// <summary>Method to perform the actual schema lock</summary> /// <remarks> /// - If the schema is already locked by a different user, an exception is thrown with lock info (name / table) /// - If the schema is already locked by the current user, no lock is performed /// - When an exclusive lock is successfully established, the lock is added to the list of managed locks /// for demotion at disposal /// </remarks> public void LockDatasetSchema(ITable fc) { ResourceManager resourceManager = new ResourceManager( "ESRI.ArcGIS.OSM.OSMClassExtension.OSMClassExtensionStrings", this.GetType().Assembly); ISchemaLock schemaLock = fc as ISchemaLock; if (schemaLock == null) { throw new ArgumentNullException("schemaLock"); } // make sure that are not any existing exclusive locks IEnumSchemaLockInfo currentlyExistingLocks; try { schemaLock.GetCurrentSchemaLocks(out currentlyExistingLocks); } catch (Exception ex) { throw new ApplicationException(resourceManager.GetString("OSMClassExtensionManager_Reading_Lock_Exception")); } bool gdbAlreadyLockedbyUser = false; ISchemaLockInfo schemaLockInfo = null; while ((schemaLockInfo = currentlyExistingLocks.Next()) != null) { if (schemaLockInfo.SchemaLockType == esriSchemaLock.esriExclusiveSchemaLock && !String.IsNullOrEmpty(schemaLockInfo.UserName)) { throw new ApplicationException(string.Format( resourceManager.GetString("OSMClassExtensionManager_Exclusive_Lock_Exception"), schemaLockInfo.TableName, schemaLockInfo.UserName)); } else if (schemaLockInfo.SchemaLockType == esriSchemaLock.esriExclusiveSchemaLock) { gdbAlreadyLockedbyUser = true; break; } } if (!gdbAlreadyLockedbyUser) { schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); } _managedLocks.Add(schemaLock); }
/// <summary> /// 检查数据集是否有被锁定 /// </summary> /// <param name="ipFeatClass">被检查是否有表的图层</param> /// <returns>是否有锁</returns> public static bool CheckFeatureClassHasLock(IFeatureClass ipFeatClass) { ISchemaLock ipSchemaLock = (ISchemaLock)ipFeatClass; if (ipSchemaLock == null) { return(false); } //通过设置锁成功与否判断该图层是否被其他人使用 ipSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); return(false); }
/// <summary> /// Changes the member variables and extension properties to store the provided field names /// as the created, modified and user fields (positions are also refreshed). Empty strings /// indicate the values should not be saved in a field. /// </summary> /// <param name="createdField">The name of the "created" field.</param> /// <param name="modifiedField">The name of the "modified" field.</param> /// <param name="userField">The name of the "user" field.</param> public void SetTimestampFields(String createdField, String modifiedField, String userField) { IClass baseClass = classHelper.Class; ISchemaLock schemaLock = (ISchemaLock)baseClass; try { // Get an exclusive lock. We want to do this prior to making any changes // to ensure the member variables and extension properties remain synchronized. schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); // Set the name member variables. createdFieldName = createdField; modifiedFieldName = modifiedField; userFieldName = userField; // Set the positions of the fields. SetFieldIndexes(); // Modify the extension properties. extensionProperties.SetProperty(Resources.CreatedFieldKey, createdFieldName); extensionProperties.SetProperty(Resources.ModifiedFieldKey, modifiedFieldName); extensionProperties.SetProperty(Resources.UserFieldKey, userFieldName); // Change the properties. IClassSchemaEdit2 classSchemaEdit = (IClassSchemaEdit2)baseClass; classSchemaEdit.AlterClassExtensionProperties(extensionProperties); } catch (COMException comExc) { throw new Exception(Resources.FailedToSavePropertiesMsg, comExc); } finally { schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }
public bool ChangeSchemaLockFeatureClass(IFeatureClass checkFeatureClass, bool exclusiveLock) { bool success = false; try { IObjectClass objectClass = checkFeatureClass as IObjectClass; if (objectClass == null) { return(false); } ISchemaLock schemaLock = (ISchemaLock)objectClass; if (schemaLock == null) { return(false); } if (exclusiveLock) { schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); } else { schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } success = true; } catch (Exception ex) { Trace.WriteLine(ex.Message); } return(success); }
public static void AddFeildToMeery(IFeatureClass featureCls) { ISchemaLock schemaLock = featureCls as ISchemaLock; schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); // var field = CreateStringField("Name", "", 8); IClass addFeild = (IClass)featureCls; addFeild.AddField(field); //加载到内存,上下文中 IFields fields = featureCls.Fields; IFieldEdit fieldsEdit = fields as IFieldEdit; featureCls.AddField(field); //fieldsEdit.AddField(field); }
/// <summary> /// Creates vessel type coded domain. /// </summary> /// <param name="pWorkspace">FileGeodatabase workspace.</param> private void CreateTypeDomain(IWorkspace pWorkspace) { try { if (!(IsArcInfoLicense())) { return; } IFeatureWorkspace pFWS = (IFeatureWorkspace)pWorkspace; ITable pTable = pFWS.OpenTable("Vessel"); IWorkspaceDomains pWSD = (IWorkspaceDomains)pWorkspace; ICodedValueDomain codedValueDomain = new CodedValueDomainClass(); int i; //VESSEL TYPE DOMAIN // Value and name pairs. for (i = 0; i <= 9; i++) { codedValueDomain.AddCode(i, "Not Available"); } for (i = 10; i <= 19; i++) { codedValueDomain.AddCode(i, "Reserved for future use"); } for (i = 20; i <= 29; i++) { codedValueDomain.AddCode(i, "WIG"); } codedValueDomain.AddCode(30, "Fishing"); codedValueDomain.AddCode(31, "Towing"); codedValueDomain.AddCode(32, "Towing and length of the tow exceeds 200 m or breadth exceeds 25 m"); codedValueDomain.AddCode(33, "Engaged in dredging or underwater operations"); codedValueDomain.AddCode(34, "Engaged in diving operations"); codedValueDomain.AddCode(35, "Engaged in military operations"); codedValueDomain.AddCode(36, "Sailing"); codedValueDomain.AddCode(37, "Pleasure craft"); codedValueDomain.AddCode(38, "Reserved for future use"); codedValueDomain.AddCode(39, "Reserved for future use"); for (i = 40; i <= 49; i++) { codedValueDomain.AddCode(i, "HSC"); } codedValueDomain.AddCode(50, "Pilot vessel"); codedValueDomain.AddCode(51, "Search and rescue vessels"); codedValueDomain.AddCode(52, "Tugs"); codedValueDomain.AddCode(53, "Port tenders"); codedValueDomain.AddCode(54, "Vessels with anti-pollution facilities or equipment"); codedValueDomain.AddCode(55, "Law enforcement vessels"); codedValueDomain.AddCode(56, "Spare - for assignments to local vessels"); codedValueDomain.AddCode(57, "Spare - for assignments to local vessels"); codedValueDomain.AddCode(58, "Medical transports (as defined in the 1949 Geneva Conventions and Additional Protocols)"); codedValueDomain.AddCode(59, "Ships according to RR Resolution No. 18 (Mob-83)"); for (i = 60; i <= 69; i++) { codedValueDomain.AddCode(i, "Passenger ships"); } for (i = 70; i <= 79; i++) { codedValueDomain.AddCode(i, "Cargo ships"); } for (i = 80; i <= 89; i++) { codedValueDomain.AddCode(i, "Tanker(s)"); } for (i = 90; i <= 99; i++) { codedValueDomain.AddCode(i, "Other types of ship"); } for (i = 100; i <= 199; i++) { codedValueDomain.AddCode(i, "Reserved, for regional use"); } for (i = 200; i <= 255; i++) { codedValueDomain.AddCode(i, "Reserved, for future use"); } // The code to set the common properties for the new coded value domain. IDomain domain = (IDomain)codedValueDomain; domain.Name = "VesselType"; domain.FieldType = esriFieldType.esriFieldTypeInteger; domain.SplitPolicy = esriSplitPolicyType.esriSPTDefaultValue; domain.MergePolicy = esriMergePolicyType.esriMPTDefaultValue; // Add the new domain to the workspace. pWSD.AddDomain(domain); //assign the domain to the vessel table VesselType field IFields pFields = pTable.Fields; int iFieldIndex = pTable.FindField("VesselType"); IField pField = pFields.get_Field(iFieldIndex); // Check that the field and domain have the same field type. if (pField.Type == domain.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("VesselType", domain); } catch (Exception exc) { // Handle the exception in a way appropriate for the application. MessageBox.Show(exc.Message); } finally { // Set the schema lock to be a shared lock. schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } } } catch (Exception ex) { MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "AIS_DataModel:CodedDomain:CreateTypeDomain"); } }
/// <summary> /// Creates Broadcast feature class Status coded domain. /// </summary> /// <param name="pWorkspace">FileGeodatabase workspace.</param> private void CreateStatusDomain(IWorkspace pWorkspace) { try { if (!(IsArcInfoLicense())) { return; } IFeatureWorkspace pFWS = (IFeatureWorkspace)pWorkspace; IFeatureClass pFC = pFWS.OpenFeatureClass("Broadcast"); IWorkspaceDomains pWSD = (IWorkspaceDomains)pWorkspace; ICodedValueDomain codedValueDomain = new CodedValueDomainClass(); //VESSEL STATUS DOMAIN // Value and name pairs. codedValueDomain.AddCode(0, "Under way using engine"); codedValueDomain.AddCode(1, "At anchor"); codedValueDomain.AddCode(2, "Not under command"); codedValueDomain.AddCode(3, "Restricted maneuverability"); codedValueDomain.AddCode(4, "Constrained by her draught"); codedValueDomain.AddCode(5, "Moored"); codedValueDomain.AddCode(6, "Aground"); codedValueDomain.AddCode(7, "Fishing"); codedValueDomain.AddCode(8, "Under way sailing"); codedValueDomain.AddCode(9, "Reserved for future amendment of navigational status for ships carrying DG, HS, or MP, or IMO hazard or pollutant category C, high speed craft (HSC)"); codedValueDomain.AddCode(10, "Reserved for future amendment of navigational status for ships carrying dangerous goods (DG), harmful substances (HS) or marine pollutants (MP), or IMO hazard or pollutant category A, wing in grand (WIG)"); codedValueDomain.AddCode(11, "Reserved for future use"); codedValueDomain.AddCode(12, "Reserved for future use"); codedValueDomain.AddCode(13, "Reserved for future use"); codedValueDomain.AddCode(14, "AIS-START (active)"); codedValueDomain.AddCode(15, "Not-Defined"); // The code to set the common properties for the new coded value domain. IDomain domain = (IDomain)codedValueDomain; domain.Name = "Status"; domain.FieldType = esriFieldType.esriFieldTypeInteger; domain.SplitPolicy = esriSplitPolicyType.esriSPTDefaultValue; domain.MergePolicy = esriMergePolicyType.esriMPTDefaultValue; // Add the new domain to the workspace. pWSD.AddDomain(domain); //assign the domain to the broadcast feature class status field IFields pFields = pFC.Fields; int iFieldIndex = pFC.FindField("Status"); IField pField = pFields.get_Field(iFieldIndex); // Check that the field and domain have the same field type. if (pField.Type == domain.FieldType) { // Cast the feature class to the ISchemaLock and IClassSchemaEdit interfaces. ISchemaLock schemaLock = (ISchemaLock)pFC; IClassSchemaEdit classSchemaEdit = (IClassSchemaEdit)pFC; // Attempt to get an exclusive schema lock. try { // Lock the class and alter the domain. schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); classSchemaEdit.AlterDomain("Status", domain); } catch (Exception exc) { // Handle the exception in a way appropriate for the application. MessageBox.Show(exc.Message); } finally { // Set the schema lock to be a shared lock. schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } } } catch (Exception ex) { MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "AIS_DataModel:CodedDomain:CreateStatusDomain"); } }
/// <summary> /// Creates Cargo type Voyage workspace. /// </summary> /// <param name="pWorkspace">FileGeodatabase workspace.</param> private void CreateCargoDomain(IWorkspace pWorkspace) { try { if (!(IsArcInfoLicense())) { return; } IFeatureWorkspace pFWS = (IFeatureWorkspace)pWorkspace; ITable pTable = pFWS.OpenTable("Voyage"); IWorkspaceDomains pWSD = (IWorkspaceDomains)pWorkspace; ICodedValueDomain codedValueDomain = new CodedValueDomainClass(); int i; //VESSEL CARGO DOMAIN // Value and name pairs. for (i = 0; i <= 9; i++) { codedValueDomain.AddCode(i, "Not Available"); } for (i = 10; i <= 19; i++) { codedValueDomain.AddCode(i, "Reserved for future use"); } for (i = 20; i <= 90; i += 10) { codedValueDomain.AddCode(i, "Not Available"); } for (i = 21; i <= 81; i += 20) { codedValueDomain.AddCode(i, "Carrying DG, HS, or MP, IMO hazard or pollutant category A"); } for (i = 22; i <= 82; i += 20) { codedValueDomain.AddCode(i, "Carrying DG, HS, or MP, IMO hazard or pollutant category B"); } for (i = 23; i <= 83; i += 20) { codedValueDomain.AddCode(i, "Carrying DG, HS, or MP, IMO hazard or pollutant category C"); } for (i = 24; i <= 84; i += 20) { codedValueDomain.AddCode(i, "Carrying DG, HS, or MP, IMO hazard or pollutant category D"); } for (i = 29; i <= 89; i += 20) { codedValueDomain.AddCode(i, "No additional information"); } for (i = 25; i <= 28; i++) { codedValueDomain.AddCode(i, "Reserved for future use"); } for (i = 31; i <= 39; i++) { codedValueDomain.AddCode(i, "Not Available"); } for (i = 45; i <= 48; i++) { codedValueDomain.AddCode(i, "Reserved for future use"); } for (i = 51; i <= 59; i++) { codedValueDomain.AddCode(i, "Not Available"); } for (i = 65; i <= 68; i++) { codedValueDomain.AddCode(i, "Reserved for future use"); } for (i = 71; i <= 91; i += 20) { codedValueDomain.AddCode(i, "Carrying DG, HS, or MP, IMO hazard or pollutant category A"); } for (i = 72; i <= 92; i += 20) { codedValueDomain.AddCode(i, "Carrying DG, HS, or MP, IMO hazard or pollutant category B"); } for (i = 73; i <= 93; i += 20) { codedValueDomain.AddCode(i, "Carrying DG, HS, or MP, IMO hazard or pollutant category C"); } for (i = 74; i <= 94; i += 20) { codedValueDomain.AddCode(i, "Carrying DG, HS, or MP, IMO hazard or pollutant category D"); } for (i = 79; i <= 99; i += 20) { codedValueDomain.AddCode(i, "No additional information"); } for (i = 75; i <= 78; i++) { codedValueDomain.AddCode(i, "Reserved for future use"); } for (i = 85; i <= 88; i++) { codedValueDomain.AddCode(i, "Reserved for future use"); } for (i = 95; i <= 98; i++) { codedValueDomain.AddCode(i, "Reserved for future use"); } for (i = 100; i <= 199; i++) { codedValueDomain.AddCode(i, "Reserved, for regional use"); } for (i = 200; i <= 255; i++) { codedValueDomain.AddCode(i, "Reserved, for future use"); } // The code to set the common properties for the new coded value domain. IDomain domain = (IDomain)codedValueDomain; domain.Name = "Cargo"; domain.FieldType = esriFieldType.esriFieldTypeInteger; domain.SplitPolicy = esriSplitPolicyType.esriSPTDefaultValue; domain.MergePolicy = esriMergePolicyType.esriMPTDefaultValue; // Add the new domain to the workspace. pWSD.AddDomain(domain); //assign the domain to the voyage table cargo field IFields pFields = pTable.Fields; int iFieldIndex = pTable.FindField("Cargo"); IField pField = pFields.get_Field(iFieldIndex); // Check that the field and domain have the same field type. if (pField.Type == domain.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("Cargo", domain); } catch (Exception exc) { // Handle the exception in a way appropriate for the application. MessageBox.Show(exc.Message); } finally { // Set the schema lock to be a shared lock. schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } } } catch (Exception ex) { MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "AIS_DataModel:CodedDomain:CreateCargoDomain"); } }
/// <summary> /// 创建拓扑 /// </summary> /// <param name="workspaceName">被检查文件的路径名称</param> public void CreateTopology(string workspaceName) { Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"); IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); IWorkspace workspace = workspaceFactory.OpenFromFile(Application.StartupPath + @"\Convert\GeoDataBase.gdb", 0); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset("ContourData"); ITopologyContainer2 topologyContainer = (ITopologyContainer2)featureDataset; IFeatureClass CurrContour = null; try { CurrContour = featureWorkspace.OpenFeatureClass("CurrContour"); ITopology topology = topologyContainer.get_TopologyByName("eeee"); IDataset fDataset = (IDataset)topology; fDataset.Delete(); } catch (Exception) { // throw; } if (CurrContour != null) { IDataset fDataset = (IDataset)CurrContour; fDataset.Delete(); } Geoprocessor gp = new Geoprocessor(); FeatureClassToFeatureClass pFFeatureClassToFeatureClass = new FeatureClassToFeatureClass(); pFFeatureClassToFeatureClass.in_features = Application.StartupPath + "\\Convert\\ContourResult\\" + workspaceName + "\\Contour.shp"; pFFeatureClassToFeatureClass.out_path = Application.StartupPath + "\\Convert\\GeoDataBase.gdb\\ContourData"; pFFeatureClassToFeatureClass.out_name = "CurrContour"; IGeoProcessorResult geoProcessorResult = (IGeoProcessorResult)gp.Execute(pFFeatureClassToFeatureClass, null); CurrContour = featureWorkspace.OpenFeatureClass("CurrContour"); ISchemaLock schemaLock = (ISchemaLock)featureDataset; try { schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); // Create the topology. ITopology topology = topologyContainer.CreateTopology("eeee", topologyContainer.DefaultClusterTolerance, -1, ""); // Add feature classes and rules to the topology. topology.AddClass((IClass)CurrContour, 5, 1, 1, false); AddRuleToTopology(topology, esriTopologyRuleType.esriTRTLineNoIntersection, "must not intersect", CurrContour); int ErrorCount = 0; // Get an envelope with the topology's extents and validate the topology. IGeoDataset geoDataset = (IGeoDataset)topology; ESRI.ArcGIS.Geometry.IEnvelope envelope = geoDataset.Extent; ValidateTopology(topology, envelope); IGeoDataset geoDS = topology as IGeoDataset; IErrorFeatureContainer errorContainer = topology as IErrorFeatureContainer; IEnumTopologyErrorFeature eErrorFeat; eErrorFeat = errorContainer.get_ErrorFeaturesByRuleType(geoDS.SpatialReference, esriTopologyRuleType.esriTRTAreaNoGaps, null, true, false); ITopologyErrorFeature topoError; topoError = eErrorFeat.Next(); while (topoError != null) { ErrorCount++; topoError = eErrorFeat.Next(); } if (ErrorCount == 0) { MessageBox.Show("无拓扑错误!", "Sunz", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("拓扑错误" + "" + ErrorCount + "" + "个!"); } } catch (COMException comExc) { throw new Exception(String.Format( "Error creating topology: {0} Message: {1}", comExc.ErrorCode, comExc.Message), comExc); } finally { schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }
private void btnTest_Click(object sender, EventArgs e) { if (txtBoxExcelPath.Text == "") { MessageBox.Show("请选择导入的外业调查表!"); return; } string strExcelPath = this.txtBoxExcelPath.Text; ExcelHelper elh = new ExcelHelper(strExcelPath); Microsoft.Office.Interop.Excel.Worksheet sheet = elh.excelApp.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet; sheet.Columns.EntireColumn.AutoFit(); //自动调整列宽 added by chulili 2012-11-29 IFeatureClass pFeatureClass = m_pCurFeaCls; IWorkspace pws = (pFeatureClass as IDataset).Workspace; string strKeyChineseName = cmbKeyField.Text; string strKeyFieldName = ""; int indexKeyField = -1; if (_DicFieds.ContainsKey(strKeyChineseName)) { indexKeyField = _DicFieds[strKeyChineseName]; strKeyFieldName = m_pCurFeaCls.Fields.get_Field(indexKeyField).Name; } IField pKeyField = pFeatureClass.Fields.get_Field(indexKeyField); IWorkspaceDomains pWorkSpaceDomains = pws as IWorkspaceDomains; ICodedValueDomain pCodedDomain = new CodedValueDomainClass(); int iNullRowCnt = 0; int iNullCnt = 0; for (int i = 2; i <= sheet.UsedRange.Rows.Count; i++) { Exception er = null; string sql = ""; iNullCnt = 0; object objcode = ((Microsoft.Office.Interop.Excel.Range)sheet.UsedRange.Cells[i, 1]).Text; //对应行的该列的值 string strcode = objcode.ToString(); object objvalue = ((Microsoft.Office.Interop.Excel.Range)sheet.UsedRange.Cells[i, 2]).Text; //对应行的该列的值 string strvalue = objvalue.ToString(); if (strcode == "") { iNullRowCnt++; if (iNullRowCnt > 10) { break; } } else { iNullRowCnt = 0; } pCodedDomain.AddCode(strcode, strvalue); } //pCodedDomain.AddCode("111", "纯林"); //pCodedDomain.AddCode("112", "混交林"); IDomain pDomain = pCodedDomain as IDomain; pDomain.Name = pKeyField.Name + "domain2"; pDomain.FieldType = pKeyField.Type; pDomain.SplitPolicy = esriSplitPolicyType.esriSPTDuplicate; pDomain.MergePolicy = esriMergePolicyType.esriMPTDefaultValue; pWorkSpaceDomains.AddDomain(pDomain); ISchemaLock schemaLock = (ISchemaLock)pFeatureClass; IClassSchemaEdit classSchemaEdit = (IClassSchemaEdit)pFeatureClass; try { schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); classSchemaEdit.AlterDomain(pKeyField.Name, pDomain); } catch (Exception exc) { } finally { schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } sheet = null; elh.Close(); }