public static bool AddLocation(INALayer layer, string locationClassName, IPoint point, string locationName, StringObjectDictionary locationProperties, double tolerance) { INALocator locator = layer.Context.Locator; locator.SnapTolerance = tolerance; INALocation location = null; IPoint outPoint = null; double distanceFromPoint = 0.0; locator.QueryLocationByPoint(point, ref location, ref outPoint, ref distanceFromPoint); INAClass class2 = layer.Context.NAClasses.get_ItemByName(locationClassName) as INAClass; IFeatureClass class3 = class2 as IFeatureClass; IFeature feature = class3.CreateFeature(); feature.Shape = point; int num2 = class3.FeatureCount(null); int index = -1; IClass class4 = class3; INALocationObject obj2 = feature as INALocationObject; obj2.NALocation = location; index = class4.FindField(NetworkConstants.NAME_FIELD); if (locationName.Trim().Length > 0) { feature.set_Value(index, locationName); } index = class4.FindField(NetworkConstants.SEQUENCE_FIELD); if (index >= 0) { feature.set_Value(index, num2); } index = class4.FindField(NetworkConstants.STATUS_FIELD); if (location.IsLocated) { feature.set_Value(index, 0); } else { feature.set_Value(index, 1); } if ((locationProperties != null) && (locationProperties.Count > 0)) { IEnumerator enumerator = locationProperties.Keys.GetEnumerator(); while (enumerator.MoveNext()) { string current = enumerator.Current as string; if ((current != null) && (current.Length > 0)) { index = class4.FindField(current); if (index != -1) { feature.set_Value(index, locationProperties[current]); } } } } feature.Store(); return(location.IsLocated); }
/// <summary> /// Finds the positions of the created, modified and user fields, and verifies that /// the specified field has the correct data type. /// </summary> private void SetFieldIndexes() { // Get the base class from the class helper. IClass baseClass = classHelper.Class; // Find the indexes of the fields. createdFieldIndex = baseClass.FindField(createdFieldName); modifiedFieldIndex = baseClass.FindField(modifiedFieldName); userFieldIndex = baseClass.FindField(userFieldName); // Verify that the field data types are correct. IFields fields = baseClass.Fields; if (createdFieldIndex != -1) { IField createdField = fields.get_Field(createdFieldIndex); // If the "created" field is not a date field, do not use it. if (createdField.Type != esriFieldType.esriFieldTypeDate) { createdFieldIndex = -1; } } if (modifiedFieldIndex != -1) { IField modifiedField = fields.get_Field(modifiedFieldIndex); // If the "modified" field is not a date field, do not use it. if (modifiedField.Type != esriFieldType.esriFieldTypeDate) { modifiedFieldIndex = -1; } } if (userFieldIndex != -1) { IField userField = fields.get_Field(userFieldIndex); // If the "user" field is not a text field, do not use it. if (userField.Type != esriFieldType.esriFieldTypeString) { userFieldIndex = -1; } else { // Get the length of the text field. userFieldLength = userField.Length; } } }
/// <summary> /// 向已有表格添加新字段,若字段存在则不添加,并返回字段索引 /// (注意,向已有表格添加字段使用IClass.AddFiled,而在创建表格时设置字段使用IFieldsEdit.AddField) /// </summary> /// <param name="table">操作的表格</param> /// <param name="name">字段名</param> /// <param name="aliasName">字段别名</param> /// <param name="fieldType">字段类型</param> /// <param name="length">字段长度,若为0则不设置长度(使用默认长度)</param> /// <returns>添加的字段的索引</returns> public static int AddField(this ITable table, string name, string aliasName, esriFieldType fieldType, int length = 0) { //若存在,则不需添加 int index = -1; if ((index = table.Fields.FindField(name)) > -1) { return(index); } IField field = new FieldClass(); IFieldEdit fieldEdit = field as IFieldEdit; fieldEdit.Name_2 = name; fieldEdit.AliasName_2 = aliasName; fieldEdit.Type_2 = fieldType; if (length > 0) { fieldEdit.Length_2 = length; } IClass cls = table as IClass; cls.AddField(fieldEdit); //此处使用IClass.AddFiled方法,不使用IFieldsEdit.AddField方法 return(cls.FindField(name)); }
/// <summary> /// Creates fieldmapping object with the OBJECTID field to be extracted to be used with GP tools. /// </summary> /// <param name="classObj">FeatureClass or table.</param> /// <param name="newOIDFldName">Name of the object id field in the mapping.</param> /// <returns></returns> public static IGPFieldMapping CreateFieldMap(this IClass classObj, string newOIDFldName, bool newOidFldNullable = false, int newOidDefaultVal = -1) { IGPFieldMapping fieldmapping = null; if (classObj is ITable || classObj is IFeatureClass) { IGPUtilities gputilities = new GPUtilities(); IDETable inputTableA = (IDETable)gputilities.MakeDataElementFromNameObject((classObj as IDataset).FullName); IArray inputTables = new ESRI.ArcGIS.esriSystem.Array(); inputTables.Add(inputTableA); fieldmapping = new GPFieldMapping() as IGPFieldMapping; fieldmapping.Initialize(inputTables, null); IFieldEdit2 oidField = new Field() as IFieldEdit2; oidField.Name_2 = newOIDFldName; oidField.Type_2 = esriFieldType.esriFieldTypeInteger; oidField.IsNullable_2 = newOidFldNullable; oidField.DefaultValue_2 = newOidDefaultVal; IGPFieldMap orgOIDFldMap = new GPFieldMap { OutputField = oidField }; IField OIDFld = classObj.Fields.Field[classObj.FindField(classObj.OIDFieldName)]; orgOIDFldMap.AddInputField(inputTableA, OIDFld, 0, OIDFld.Length); fieldmapping.AddFieldMap(orgOIDFldMap); } return(fieldmapping); }
/// <summary> /// 向已有表格添加新字段,若字段存在则不添加,并返回字段索引 /// <para>注意:若数据源为shp,字段名长度不能超过10,否则报错</para> /// <para>注意:向已有表格添加字段使用IClass.AddFiled,而在创建表格时设置字段使用IFieldsEdit.AddField</para> /// </summary> /// <param name="table">操作的表格</param> /// <param name="name">字段名</param> /// <param name="aliasName">字段别名</param> /// <param name="fieldType">字段类型</param> /// <param name="length">字段长度,若为0则不设置长度(使用默认长度)</param> /// <param name="precision">字段精度,表示数值类型字段除小数点外的总长度,即整数位数加上小数位数</param> /// <param name="scale">小数位数</param> /// <returns>添加的字段的索引</returns> public static int AddField(this ITable table, string name, string aliasName, esriFieldType fieldType, int length = 0, int precision = 0, int scale = 0) { //若存在,则不需添加 int index; if ((index = table.Fields.FindField(name)) > -1) { return(index); } IField field = new FieldClass(); try { IFieldEdit fieldEdit = field as IFieldEdit; fieldEdit.Name_2 = name; fieldEdit.AliasName_2 = aliasName; fieldEdit.Type_2 = fieldType; if (length > 0) { fieldEdit.Length_2 = length; } if (precision > 0) { fieldEdit.Precision_2 = precision; } if (scale > 0) { fieldEdit.Scale_2 = scale; } IClass cls = table as IClass; cls.AddField(fieldEdit); //此处使用IClass.AddFiled方法,不使用IFieldsEdit.AddField方法 return(cls.FindField(name)); } catch (Exception ex) { throw new Exception($"添加字段{name}({aliasName})失败:{ex.Message}", ex); } }
protected static int AddNewField(IClass fclass, string Name) { IField nw = new FieldClass(); IFieldEdit new_field = (IFieldEdit)nw; new_field.Name_2 = Name; if (Name.IndexOf("UNIT", 0, StringComparison.CurrentCultureIgnoreCase) > -1) { new_field.Type_2 = esriFieldType.esriFieldTypeString; new_field.Length_2 = 20; } else { new_field.Type_2 = esriFieldType.esriFieldTypeDouble; new_field.DefaultValue_2 = double.NaN; } new_field.IsNullable_2 = true; fclass.AddField(new_field); return fclass.FindField(Name); }
void formAdvanced_doneFormEvent(object sender, AdvancedEvents e) { m_pSDS.DesignMode = true; formAdvanced.Cursor = System.Windows.Forms.Cursors.WaitCursor; //process the algorithm if there is one if (e.AlgorithmName != "") { ISchematicAlgoSmartTree a = new SchematicAlgoSmartTreeClass(); if (e.AlgorithmParams.Count > 0) { Dictionary <string, string> .KeyCollection keys = e.AlgorithmParams.Keys; string strValue = ""; foreach (string s in keys) { if (s == "Direction") { e.AlgorithmParams.TryGetValue(s, out strValue); if (strValue == "Top to Bottom") { a.Direction = esriSchematicAlgoDirection.esriSchematicAlgoTopDown; } else if (strValue == "Bottom to Top") { a.Direction = esriSchematicAlgoDirection.esriSchematicAlgoBottomUp; } else if (strValue == "Left to Right") { a.Direction = esriSchematicAlgoDirection.esriSchematicAlgoLeftRight; } else { a.Direction = esriSchematicAlgoDirection.esriSchematicAlgoRightLeft; } } } if (e.RootClass != "") { ISchematicElementClassContainer pECC = (ISchematicElementClassContainer)m_pSDS; ISchematicElementClass pEC = pECC.GetSchematicElementClass(e.RootClass); ESRI.ArcGIS.esriSystem.UID u = new ESRI.ArcGIS.esriSystem.UID(); u.Value = "{3AD9D8B8-0A1D-4F32-ABB5-54B848A46F85}"; ISchematicAttributeConstant pAttrConst = (ISchematicAttributeConstant)pEC.CreateSchematicAttribute("RootFlag", u); ISchematicAttributeManagement pAttrMgmt = (ISchematicAttributeManagement)pAttrConst; pAttrMgmt.StorageMode = esriSchematicAttributeStorageMode.esriSchematicAttributeFieldStorage; pAttrConst.ConstantValue = "-1"; } } m_pSDT.SchematicAlgorithm = (ISchematicAlgorithm)a; } //check to see if we need to add associated fields if (e.FieldsToCreate != null) { if (e.FieldsToCreate.Count > 0) { ISchematicElementClassContainer pECC = (ISchematicElementClassContainer)m_pSDS; //create the associated field attributes string[] keys = e.FieldsToCreate.AllKeys; foreach (string s in keys) { //get the feature class ISchematicElementClass pEC = pECC.GetSchematicElementClass(s); if (pEC != null) { string strName = ""; string[] values = e.FieldsToCreate.GetValues(s); foreach (string v in values) { //create the field ESRI.ArcGIS.esriSystem.UID u = new ESRI.ArcGIS.esriSystem.UID(); u.Value = "{7DE3A19D-32D0-41CD-B896-37CA3AFBD88A}"; IClass pClass = (IClass)pEC; //only handle names that don't already exist in the schematic tables if (pClass.FindField(v) == -1) { strName = v.ToString(); ISchematicAttributeAssociatedField pFieldAttr = (ISchematicAttributeAssociatedField)pEC.CreateSchematicAttribute(strName, u); pFieldAttr.AssociatedFieldName = v; ISchematicAttributeManagement pAttrMgmt = (ISchematicAttributeManagement)pFieldAttr; pAttrMgmt.StorageMode = esriSchematicAttributeStorageMode.esriSchematicAttributeFieldStorage; } } } } } } m_pSDS.Save(ESRI.ArcGIS.esriSystem.esriArcGISVersion.esriArcGISVersionCurrent, true); m_pSDS.DesignMode = false; formAdvanced.Cursor = System.Windows.Forms.Cursors.Default; formAdvanced.Close(); }