ArcEngine添加字段

  • 获取所有字段
                //ITable iTable = ifc as ITable;
                //int fieldCount = iTable.Fields.FieldCount;     
                int num = ifc.Fields.FieldCount;//字段的个数    
                for (int i = 0; i < num; i++)
                {
                    string target = ifc.Fields.get_Field(i).AliasName;
                    IField field = ifc.Fields.get_Field(i);//iTable.Fields.get_Field(i);
                    if ((field.Type == esriFieldType.esriFieldTypeOID) || field.Type == esriFieldType.esriFieldTypeGeometry ||
                                field.Name.ToUpper() == "SHAPE_LENGTH" || field.Name.ToUpper() == "SHAPE_LENGTH()" ||
                                field.Name.ToUpper() == "SHAPE_AREA()" || field.Name.ToUpper() == "SHAPE_AREA" ||
                                field.Name.ToUpper() == "SHAPE.STAREA()" || field.Name.ToUpper() == "SHAPE.STLENGTH()")
                        continue;
                    else
                    {
                        string Name = ified.Name;
                        string AliasName = ified.AliasName;
                        esriFieldType Type = ified.Type;
                        int Length = ified.Length;
                        object DefaultValue = ified.DefaultValue;
                        bool IsNullable = ified.IsNullable;
                        int Precision = ified.Precision;
                        int Scale = ified.Scale;
                    }
                }
    • 添加一个字段
                                  IField addFied = new Field();
                                  IFieldEdit editField = (IFieldEdit)addFied;
                                  editField.Name_2 = fieldName;
                                  //editField.AliasName_2 = "";
                                  editField.Type_2 = esriFieldType.esriFieldTypeString;//esriFieldType.esriFieldTypeString
                                  editField.Length_2 = length;
                                  editField.Precision_2 = precision;
                                  editField.DefaultValue_2 = "";
                                  editField.IsNullable_2 = true;
                                  ifc.AddField(addFied);
    • 删除字段
                          int fieldIndex = m_feacls.Fields.FindField(name);//需要删除的字段名称
                          IField delField = iFields.get_Field(fieldIndex);
                          ifc.DeleteField(delField);
    • 添加多个字段
                  ESRI.ArcGIS.Geodatabase.IObjectClassDescription objectClassDescription = new ESRI.ArcGIS.Geodatabase.FeatureClassDescriptionClass();
                  ESRI.ArcGIS.Geodatabase.IFields fields = null;
                  // if a fields collection is not passed in then supply our own
                  if (fields == null)
                  {
                      // create the fields using the required fields method
                      fields = objectClassDescription.RequiredFields;
                      ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields; // Explicit Cast
      
                      ESRI.ArcGIS.Geodatabase.IField fieldBSM = new ESRI.ArcGIS.Geodatabase.FieldClass();
                      ESRI.ArcGIS.Geodatabase.IFieldEdit fieldEditBSM = (ESRI.ArcGIS.Geodatabase.IFieldEdit)fieldBSM; // Explicit Cast
                      fieldEditBSM.Name_2 = "BSM";
                      fieldEditBSM.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger;
                      fieldEditBSM.IsNullable_2 = true;
                      fieldEditBSM.AliasName_2 = "标识码";
                      fieldEditBSM.Editable_2 = true;
                      fieldsEdit.AddField(fieldBSM);
      
                      ESRI.ArcGIS.Geodatabase.IField fieldYSDM = new ESRI.ArcGIS.Geodatabase.FieldClass();
                      ESRI.ArcGIS.Geodatabase.IFieldEdit fieldEditYSDM = (ESRI.ArcGIS.Geodatabase.IFieldEdit)fieldYSDM; // Explicit Cast
                      fieldEditYSDM.Name_2 = "YSDM";
                      fieldEditYSDM.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString;
                      fieldEditYSDM.IsNullable_2 = true;
                      fieldEditYSDM.AliasName_2 = "要素代码";
                      fieldEditYSDM.DefaultValue_2 = "0";
                      fieldEditYSDM.Editable_2 = true;
                      fieldEditYSDM.Length_2 = 10;
                      fieldsEdit.AddField(fieldYSDM);
                  }
                  ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker = new ESRI.ArcGIS.Geodatabase.FieldCheckerClass();
                  ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError = null;
                  ESRI.ArcGIS.Geodatabase.IFields validatedFields = null;
                  fieldChecker.ValidateWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)workspace;
                  fieldChecker.Validate(fields, out enumFieldError, out validatedFields);//检查字段的有效性,最终结果用validatedFields
  • 字段计算器
                Geoprocessor gp = new Geoprocessor();
                try
                {
                    CalculateField c = new CalculateField();
                    c.in_table = m_ifc as ITable;
                    c.field = name;//需要计算的字段名
                    c.expression = richTextBox1.Text;//"!shape.area!"或"!ZLDWDM!+!QSDWDM!"或"!JBNTMJ!"注意感叹号的运用
                    c.expression_type = "PYTHON";
                    gp.Execute(c, null);
                }
                catch
                {
                    label7.Text = gp.GetMessage(3);
                    MessageBox.Show(gp.GetMessage(3));
                }

     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值