using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
namespace Kingo.RuleCheck.AEHelper
{
public class WorkspaceFactory
{
public static IWorkspaceFactory2 GetWorkspaceFactory(WorkspaceTypeEnum c)
{
//return hashtable[c] as IWorkspaceFactory2;
switch (c)
{
case WorkspaceTypeEnum.SDEConn:
return new SdeWorkspaceFactoryClass();
case WorkspaceTypeEnum.ShapeFile:
return new ShapefileWorkspaceFactoryClass();
case WorkspaceTypeEnum.CADFile:
return new CadWorkspaceFactoryClass();
case WorkspaceTypeEnum.MDBFile:
return new AccessWorkspaceFactoryClass();
case WorkspaceTypeEnum.GDBFile:
return new FileGDBWorkspaceFactoryClass();
case WorkspaceTypeEnum.SDEFile:
return new SdeWorkspaceFactoryClass();
case WorkspaceTypeEnum.SQLite:
return new SqlWorkspaceFactoryClass();
}
return null;
}
///
/// 在指定工作空间内,创建要素类:
/// 若还指定了要素集,则新要素类创建在该要素集内;
/// 若没有指定要素,则直接在工作空间下创建该要素类
///
/// 工作空间
/// 要素集
/// 字段集
/// 要素类名
///
public IFeatureClass CreateFeatureClass(IFeatureWorkspace pFeatureWorkspace, IFeatureDataset pFeatureDataSet, IFields pFields, string sFeatureClassName, enumDataOutputFormat format)
{
//判断必须的参数是否都有效
if (pFeatureWorkspace == null || pFields == null)
{
return null;
}
//判断FeatureClass是否已经存在,若存在则删除以前的FeatureClas
IWorkspace2 pWorkspace2 = pFeatureWorkspace as IWorkspace2;
//判断数据库类型
IFeatureClass pFC = null;
if (pWorkspace2 != null && IsExistFeatureClass(pWorkspace2, sFeatureClassName, out pFC))
{
IDataset pDataset = pFC as IDataset;
pDataset.Delete();
}
//获得要素UID
IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass();
IObjectClassDescription ocDescription = (IObjectClassDescription)fcDescription;
//获得Fields
IFieldChecker fieldChecker = new FieldCheckerClass();
IFields validatedFields = null;
fieldChecker.ValidateWorkspace = pFeatureWorkspace as IWorkspace;
validatedFields = pFields;
IFields newpFields = ocDescription.RequiredFields;
IFieldsEdit pFieldsEdit = newpFields as IFieldsEdit;
List fieldNames = new List();
var gfil = newpFields.Field[1];
if (gfil.Type == esriFieldType.esriFieldTypeGeometry)
{
var ofield = pFields.Field[pFields.FindField("Shape")] as IFieldEdit;
IFieldEdit gFieldEdit = gfil as IFieldEdit;
gFieldEdit.Name_2 = "Shape";
gFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
IGeometryDef pGeometryDef = new GeometryDef();
IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
pGeometryDefEdit.GeometryType_2 = ofield.GeometryDef.GeometryType;
pGeometryDefEdit.SpatialReference_2 = ofield.GeometryDef.SpatialReference;
gFieldEdit.GeometryDef_2 = pGeometryDef;
}
for (int i = 0; i < validatedFields.FieldCount; i++)
{
IField field = validatedFields.get_Field(i);
if (IsExist(field.Name, newpFields))
{
continue;
}
if (field.AliasName.Contains("."))
{
continue;
}
if (format ==enumDataOutputFormat.Access || format == enumDataOutputFormat.FGDB)
{
if (field.Name.ToUpper().Contains("SHAPE"))
{
continue;
}
}
if (field.Name.ToUpper().Contains("FID"))
{
continue;
}
if (field.Name.ToUpper().Contains("SE_ANNO_CAD_DATA"))
{
continue;
}
if (field.Type == esriFieldType.esriFieldTypeGeometry)
{
continue;
}
if (field.Name.ToUpper() == "LAST_EDITED_USER")
{
continue;
}
IFieldEdit pFieldEdit = field as IFieldEdit;
pFieldEdit.Length_2 = field.Length;
if (field.Name.Length > 10)
{
string fname = field.Name.Substring(0, 10);
int n = 1;
while (fieldNames.Contains(fname))
{
string end = n.ToString();
fname = fname.Remove(10 - end.Length) + end;
n++;
}
pFieldEdit.Name_2 = fname;
}
else
{
pFieldEdit.Name_2 = field.Name;
}
if (pFieldEdit.Name.Contains("st_area") || pFieldEdit.Name.Contains("st_length"))
{
continue;
}
fieldNames.Add(pFieldEdit.Name);
pFieldsEdit.AddField(field);
}
//如果pFeatureDataSet不为空,在pFeatureDataSet下创建要素层;否则,直接在pFeatureWorkspace下创建要素层
IFeatureClass pFeatClass = null;
if (pFeatureDataSet != null)
{
pFeatClass = pFeatureDataSet.CreateFeatureClass(sFeatureClassName, newpFields, ocDescription.InstanceCLSID,
ocDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, fcDescription.ShapeFieldName, "");
}
else if (pFeatureDataSet == null && pFeatureWorkspace != null)
{
pFeatClass = pFeatureWorkspace.CreateFeatureClass(sFeatureClassName, newpFields, ocDescription.InstanceCLSID,
ocDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, fcDescription.ShapeFieldName, "");
}
return pFeatClass;
}
///
/// 判断一个工作空间中,要素层是否已经存在
/// 若存在,将其返回
///
///
///
///
///
private Boolean IsExistFeatureClass(IWorkspace2 pWorkspace, string sFeatureClassName, out IFeatureClass pFeatureClass)
{
bool IsExist = false;
IsExist = pWorkspace.get_NameExists(esriDatasetType.esriDTFeatureClass, sFeatureClassName);
if (IsExist)
{
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
pFeatureClass = pFeatureWorkspace.OpenFeatureClass(sFeatureClassName);
}
else
pFeatureClass = null;
return IsExist;
}
///
///
///
///
///
///
private bool IsExist(string name, IFields fields)
{
for (var i = 0; i < fields.FieldCount; i++)
{
if (name.Equals(fields.get_Field(i).Name, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
///
/// 创建注记要素类
///
///
/// 要素集
/// 关联的要素类
/// 字段集
/// 空间参考
/// 显示比例尺
/// 注记要素类名称
/// 注记属性集合
/// 符号集合
public IFeatureClass CreateFeatureClassAnno(IWorkspace pWorkspace, IFeatureDataset pFeatureDataSet, IFeatureClass pFeatureClassRelated, IFields pFields,
ISpatialReference pSpatialReference, double pScale, string pAnnoFeatureClassName, ISymbolCollection symbolCollection, IAnnotateLayerPropertiesCollection annoPropsColl)
{
if (pWorkspace == null || string.IsNullOrEmpty(pAnnoFeatureClassName)) return null;
//获得工作空间
IFeatureWorkspaceAnno featureWorkspaceAnno = (IFeatureWorkspaceAnno)pWorkspace;
//比例尺
ESRI.ArcGIS.Carto.IGraphicsLayerScale graphicLayerScale = new ESRI.ArcGIS.Carto.GraphicsLayerScaleClass();
IGeoDataset geoDataset = (IGeoDataset)pFeatureDataSet;
graphicLayerScale.Units = ESRI.ArcGIS.esriSystem.esriUnits.esriMeters;
graphicLayerScale.ReferenceScale = pScale;
//Symbol设置
if (symbolCollection == null)
{
symbolCollection = new SymbolCollectionClass();
symbolCollection.set_Symbol(0, (ISymbol)CreateDefaultTextSymbol());
}
#region 注记类及其属性
if (annoPropsColl == null)
{
annoPropsColl = new AnnotateLayerPropertiesCollectionClass();
annoPropsColl.Add(CreateDefaultAnnotationLayerPro(graphicLayerScale));
}
#endregion
//获得要素UID
IFeatureClassDescription fcDescription = new ESRI.ArcGIS.Carto.AnnotationFeatureClassDescriptionClass();
IObjectClassDescription ocDescription = (IObjectClassDescription)fcDescription;
//验证Fields
IFieldChecker fieldChecker = new FieldCheckerClass();
IEnumFieldError enumFieldError = null;
IFields validatedFields = null;
if (pFeatureDataSet != null)
{
fieldChecker.ValidateWorkspace = pFeatureDataSet.Workspace;
}
else
{
fieldChecker.ValidateWorkspace = pWorkspace;
}
fieldChecker.Validate(pFields, out enumFieldError, out validatedFields);
if (validatedFields == null) return null;
//执行创建
IFeatureClass pFeatureClassRes = null;
try
{
pFeatureClassRes = featureWorkspaceAnno.CreateAnnotationClass(pAnnoFeatureClassName, validatedFields, ocDescription.InstanceCLSID, ocDescription.ClassExtensionCLSID, fcDescription.ShapeFieldName,
"", pFeatureDataSet, pFeatureClassRelated, annoPropsColl, graphicLayerScale, symbolCollection, true);
}
catch (Exception ex)
{
return null;
}
return pFeatureClassRes;
}
private ITextSymbol CreateDefaultTextSymbol()
{
IFormattedTextSymbol myTextSymbol = new TextSymbolClass();
stdole.IFontDisp myFont = new stdole.StdFontClass() as stdole.IFontDisp;
myFont.Name = "Courier New";
myFont.Size = 9;
myTextSymbol.Font = myFont;
IRgbColor rgbColor = new ESRI.ArcGIS.Display.RgbColorClass();
rgbColor.Red = 150;
rgbColor.Green = 0;
rgbColor.Blue = 0;
myTextSymbol.Color = (IColor)rgbColor;
myTextSymbol.Angle = 0;
myTextSymbol.RightToLeft = false;
myTextSymbol.VerticalAlignment = esriTextVerticalAlignment.esriTVABaseline;
myTextSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHAFull;
myTextSymbol.CharacterSpacing = 200;
myTextSymbol.Case = esriTextCase.esriTCNormal;
return myTextSymbol;
}
private IAnnotateLayerProperties CreateDefaultAnnotationLayerPro(ESRI.ArcGIS.Carto.IGraphicsLayerScale graphicLayerScale)
{
IAnnotateLayerProperties annoProps = new LabelEngineLayerPropertiesClass();
annoProps.FeatureLinked = true;
annoProps.AddUnplacedToGraphicsContainer = false;
annoProps.CreateUnplacedElements = true;
annoProps.DisplayAnnotation = true;
annoProps.UseOutput = true;
annoProps.Class = "Annotation Class 1";
ILabelEngineLayerProperties layerEngineLayerProps = (ILabelEngineLayerProperties)annoProps;
IAnnotationExpressionEngine annoExpressionEngine = new AnnotationVBScriptEngineClass();
layerEngineLayerProps.ExpressionParser = annoExpressionEngine;
layerEngineLayerProps.Expression = "[DESCRIPTION]";
layerEngineLayerProps.IsExpressionSimple = true;
layerEngineLayerProps.Offset = 0;
layerEngineLayerProps.SymbolID = 0;
layerEngineLayerProps.Symbol = CreateDefaultTextSymbol();
IAnnotateLayerTransformationProperties annoLayerTransProp = (IAnnotateLayerTransformationProperties)annoProps;
annoLayerTransProp.ReferenceScale = graphicLayerScale.ReferenceScale;
annoLayerTransProp.Units = graphicLayerScale.Units;
annoLayerTransProp.ScaleRatio = 1;
return annoProps;
}
///
/// 开始编辑
///
/// 数据集
public static bool BeginEdit(IDataset pDataset)
{
IWorkspace pWorkspace = pDataset.Workspace;
if (pWorkspace == null) return false;
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IWorkspaceEdit pWE = pFeatureWorkspace as IWorkspaceEdit;
if (!pWE.IsBeingEdited())
{
IVersionedObject pVersioned = pDataset as IVersionedObject;
if (pVersioned == null || !pVersioned.IsRegisteredAsVersioned)
{
IMultiuserWorkspaceEdit pMulti = pFeatureWorkspace as IMultiuserWorkspaceEdit;
if (pMulti != null)
{
if (pMulti.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMNonVersioned))
{
try
{
pMulti.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMNonVersioned);
}
catch (Exception ex)
{
pWE.StartEditing(false);
}
}
else
{
pWE.StartEditing(false);
}
}
else
{
pWE.StartEditing(false);
}
}
else
{
pWE.StartEditing(false);
}
if (pWE.IsBeingEdited() == false) return false;
}
pWE.StartEditOperation();
return true;
}
///
/// 结束编辑
///
/// 数据集
public static bool EndEdit(IDataset pDataset)
{
IWorkspace pWorkspace = pDataset.Workspace;
if (pWorkspace == null) return false;
IWorkspaceEdit pWE = null;
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
pWE = pFeatureWorkspace as IWorkspaceEdit;
if (pWE.IsBeingEdited())
{
try
{
pWE.StopEditOperation();
pWE.StopEditing(true);
return true;
}
catch (Exception ex)
{
return false;
}
}
else
{
return true;
}
}
///
/// 结束编辑
///
/// 数据集
/// 是否保存编辑
///
public static bool EndEdit(IDataset pDataset, bool isSave)
{
IWorkspace pWorkspace = pDataset.Workspace;
if (pWorkspace == null) return false;
IWorkspaceEdit pWE = null;
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
pWE = pFeatureWorkspace as IWorkspaceEdit;
if (pWE.IsBeingEdited())
{
try
{
pWE.StopEditOperation();
pWE.StopEditing(isSave);
return true;
}
catch (Exception ex)
{
return false;
}
}
else
{
return true;
}
}
}
}