using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using KGIS.Framework.AE;
using System;
using System.Collections.Generic;
namespace KGIS.Plugin.DataChanging.Utility
{
public static class FeatureOperator
{
///
/// 属性变更历史记录
///
///
///
///
///
public static bool SaveEditFeatureToHis(List features, IEnvelope pEnvelope, string ActionName)
{
if (features == null || features.Count == 0)
return false;
IDataset dataset = features[0].Class as IDataset;
IFeatureClass hisFeatureClass = GetHisFeatureClass(dataset);
if (hisFeatureClass != null)
{
IFeatureClassAPI featureAPI = new FeatureClassAPI(hisFeatureClass);
List Fields = featureAPI.GetFields();
int OID = 0;
//记录变更动作
OID = InsertAction(dataset.Workspace, EnumActionType.PropertyEdited, features[0].Class.AliasName, (features[0].Class as FeatureClass).BrowseName, "", pEnvelope, ActionName);
if (OID == 0)
return false;
foreach (var feature in features)
{
foreach (K_Field item in Fields)
{
if (item.Name.ToUpper() == "ACTIONID")
{
item.Value = OID;
continue;
}
int index = feature.Fields.FindField(item.Name);
if (index == -1)
continue;
item.Value = feature.get_Value(index);
}
featureAPI.AddFeature(Fields);
}
return true;
}
return false;
}
public static IFeatureClass GetHisFeatureClass(IDataset dataset)
{
if (dataset != null)
{
IWorkspace2 workSpace = dataset.Workspace as IWorkspace2;
string hisDatasetName = dataset.Name + "_HIS";
if (workSpace.get_NameExists(esriDatasetType.esriDTFeatureClass, hisDatasetName))
{
IFeatureWorkspace pFeatureWorkspace = workSpace as IFeatureWorkspace;
IFeatureClass his = pFeatureWorkspace.OpenFeatureClass(hisDatasetName);
//检测是否有历史纪录相关字段,没有则创建
AddFiledToExistFeatureClass(his, "OPTIME", esriFieldType.esriFieldTypeString);
AddFiledToExistFeatureClass(his, "OPGUID", esriFieldType.esriFieldTypeString);
return his;
}
}
return null;
}
public static void AddFiledToExistFeatureClass(IFeatureClass featureClass, string fieldName, esriFieldType fieldType)
{
if (featureClass.FindField(fieldName) > -1)
return;
IClass pClass = featureClass as IClass;
IFieldsEdit fldsE = featureClass.Fields as IFieldsEdit;
IField fld = new FieldClass();
IFieldEdit2 fldE = fld as IFieldEdit2;
fldE.Type_2 = fieldType;
fldE.Name_2 = fieldName;
pClass.AddField(fld);
}
///
/// 插入变更动作
///
public static int InsertAction(IWorkspace pWorkSpace, EnumActionType pActionType, string pOperationLayerName, string layerTableName, string pOIDs, IEnvelope pEnvelope, string ActionName = "", IGeometry geo = null)
{
EngineEditorClass editor = new EngineEditorClass();
ITableAPI tableAPI = null;
try
{
IWorkspaceAPI wkAPI = new WorkspaceAPI(pWorkSpace);
tableAPI = wkAPI.OpenTable("BGDZB");
Dictionary data = InsertAction(pActionType, pOperationLayerName, layerTableName, pOIDs, pEnvelope, ActionName, geo);
if (data == null)
return 0;
object OID = tableAPI.AddRow(data);
return (int)OID;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (tableAPI != null)
tableAPI.CloseTable();
}
}
///
/// 插入变更动作
///
private static Dictionary InsertAction(EnumActionType pActionType, string pOperationLayerName, string layerTableName, string pOIDs, IEnvelope pEnvelope, string ActionName = "", IGeometry geo = null)
{
//if (Env.Instance.CurrentActiveBGXM == 0)
//{
// MessageHelper.Show("在进行变更之前,请先创建变更项目或者打开现有变更项目!");
// return null;
//}
Dictionary dic = new Dictionary();
dic.Add("ProjectID", "1");
if (pActionType == EnumActionType.SplitFeature)
dic.Add("ActionName", "土地利用-图斑分割");
if (pActionType == EnumActionType.MerageFeature)
dic.Add("ActionName", "土地利用-图斑合并");
if (pActionType == EnumActionType.AddFeature)
dic.Add("ActionName", "土地利用-新增图斑");
if (pActionType == EnumActionType.PropertyEdited)
dic.Add("ActionName", "土地利用-图斑属性修改");
//判断传入的值是否存在
if (pActionType == EnumActionType.None)
{
dic.Add("ActionName", ActionName);
}
dic.Add("OperationLayerName", pOperationLayerName);
dic.Add("OperationLayerTableName", layerTableName);
dic.Add("OperationFeatureOIDs", pOIDs);
dic.Add("XMax", pEnvelope.XMax);
dic.Add("XMin", pEnvelope.XMin);
dic.Add("YMax", pEnvelope.YMax);
dic.Add("YMin", pEnvelope.YMin);
dic.Add("CreateTime", DateTime.Now);
dic.Add("OperationUser", "Admin");
dic.Add("State", 1);
dic.Add("IsShow", 1);
if (geo != null)
{
dic.Add("SHAPE", geo);
}
return dic;
}
}
///
/// 变更动作类型
///
public enum EnumActionType
{
None = 0,
AddFeature = 1,
SplitFeature = 2,
MerageFeature = 3,
PropertyEdited = 4
}
}