using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geodatabase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Kingo.Plugin.ShapeToKOApp.KoDataBase
{
public class LayerComm
{
///
/// 设置分组显示
///
///
///
///
public void SetLayerGroupVisble(IMap map, string groupName, bool isVisble)
{
for (int i = 0; i < map.LayerCount; i++)
{
if (map.get_Layer(i) is IGroupLayer)
{
if ((map.get_Layer(i) as ILayerGeneralProperties).LayerDescription == groupName)
{
map.get_Layer(i).Visible = isVisble;
break;
}
}
}
}
///
/// 判断是否存在分组
///
///
///
///
public ILayer ExistLayerGroupByName(IMap map, string groupName)
{
ILayer layer = null;
for (int i = 0; i < map.LayerCount; i++)
{
if (map.get_Layer(i) is IGroupLayer)
{
if (map.get_Layer(i).Name == groupName)
{
layer = map.get_Layer(i);
break;
}
}
}
return layer;
}
///
/// 找到顶层节点
///
///
///
private TreeNode FindTopNode(TreeNode tn)
{
if (tn.Parent == null)
{
return tn;
}
else
{
return FindTopNode(tn.Parent);
}
}
#region 属性信息转换
private List GetListField(ILayerFields fields)
{
List lstRst = new List();
IAttributeTable attrTable = fields as IAttributeTable;
for (int i = 0; i < attrTable.AttributeTable.Fields.FieldCount; i++)
{
IField field = attrTable.AttributeTable.Fields.get_Field(i);
int index = fields.FindField(field.Name);
IFieldInfo fieldInfo = fields.get_FieldInfo(index);
if (!fieldInfo.Visible)
continue;
if (field.Name.ToUpper() == "SHAPE") continue;
lstRst.Add(new XSDClass.FieldNomal()
{
Alias = fieldInfo.Alias,
Name = field.Name
});
}
return lstRst;
}
public XSDClass.ItemInfo FeatureLayerToEntity(IFeatureLayer Layer)
{
XSDClass.ItemInfo result = new XSDClass.ItemInfo();
//if (selectLayerEntity == null)
// selectLayerEntity = new Kingo.Mobile.Common.Entity.LayerEntity();
try
{
ESRI.ArcGIS.Carto.FeatureLayerClass layerFeatureClass = Layer as ESRI.ArcGIS.Carto.FeatureLayerClass;
//selectLayerEntity.LayerName = Layer.Name;
//selectLayerEntity.LayerCaption = (Layer as ILayerGeneralProperties).LayerDescription;
//selectLayerEntity.LayerVisible = Layer.Visible;
//selectLayerEntity.LayerSource = (Layer as IDataset).Workspace.PathName + "\\" + (Layer as IDataset).BrowseName;
LabelStyleConvert styleConvert = new LabelStyleConvert();
result.DrawingInfo.LabelingInfo = styleConvert.EsriLabelToCustomLabels((Layer as IGeoFeatureLayer).AnnotationProperties);
result.DisplayField = Layer.DisplayField;
IGeoDataset geoLayer = Layer as IGeoDataset;
result.Extent = new XSDClass.Extent()
{
SpatialReference = new XSDClass.SpatialReference()
{
LatestWkid = geoLayer.SpatialReference.FactoryCode,
Wkid = geoLayer.SpatialReference.FactoryCode
},
Xmax = geoLayer.Extent.XMax,
Xmin = geoLayer.Extent.XMin,
Ymax = geoLayer.Extent.YMax,
Ymin = geoLayer.Extent.YMin
};
result.Fields = GetListField(Layer as ILayerFields);
result.GeometryType = (XSDClass.esriGeometryType)((int)geoLayer.Extent.GeometryType);
result.MaxScale = (int)Layer.MaximumScale;
result.MinScale = (int)Layer.MinimumScale;
result.Name = Layer.Name;
//selectLayerEntity.LayerWhere = (Layer as IFeatureLayerDefinition).DefinitionExpression;
result.DrawingInfo.Transparency = (Layer as ILayerEffects).Transparency;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
#endregion
#region 打开ShapeFile数据源
public IDatasetName GetShapeFileDatasetName(string FileFullPath)
{
try
{
IEnumDatasetName name = this.OpenShapeFileWorkSpace(System.IO.Path.GetDirectoryName(FileFullPath)).get_DatasetNames(esriDatasetType.esriDTFeatureClass);
for (IDatasetName name2 = name.Next(); name2 != null; name2 = name.Next())
{
if (name2.Name.ToUpper() == System.IO.Path.GetFileNameWithoutExtension(FileFullPath).ToUpper())
{
return name2;
}
}
}
catch (Exception exception)
{
throw exception;
}
return null;
}
public IWorkspace OpenShapeFileWorkSpace(string filePath)
{
IWorkspaceFactory factory = null;
try
{
factory = null;
factory = new ShapefileWorkspaceFactoryClass();
}
catch (Exception exception)
{
throw exception;
}
return factory.OpenFromFile(filePath, 0);
}
#endregion
#region 打开mdb数据源
public IWorkspace OpenMDBWorkspace(string PersonalDBFile)
{
IWorkspaceFactory factory = null;
IWorkspace workspace = null;
try
{
factory = null;
factory = new AccessWorkspaceFactoryClass();
workspace = factory.OpenFromFile(PersonalDBFile, 0);
}
catch (Exception exception)
{
throw exception;
}
return workspace;
}
public IFeatureClass GetFeatureClass(IWorkspace pWorkspace, string FeaClsName)
{
IFeatureWorkspace workspace = (IFeatureWorkspace)pWorkspace;
return workspace.OpenFeatureClass(FeaClsName);
}
public IFeatureClass GetFeatureClass(string PersonalDBFile, string FeaClsName)
{
PersonalDBFile = PersonalDBFile.Substring(0, PersonalDBFile.LastIndexOf("\\"));
return GetFeatureClass(OpenMDBWorkspace(PersonalDBFile), FeaClsName);
}
#endregion
#region 处理信息的方法
///
/// 找最顶层的节点名字
///
///
///
private object FindTopParentName(TreeNode tn)
{
if (tn.Parent == null)
return tn.Text;
else
{
return FindTopParentName(tn.Parent);
}
}
//设置子节点状态
private void SetChildNodeCheckedState(TreeNode currNode, bool isCheckedOrNot)
{
if (currNode.Nodes == null) return; //没有子节点返回
foreach (TreeNode tmpNode in currNode.Nodes)
{
tmpNode.Checked = isCheckedOrNot;
SetChildNodeCheckedState(tmpNode, isCheckedOrNot);
}
}
//设置父节点状态
private void SetParentNodeCheckedState(TreeNode currNode, bool isCheckedOrNot)
{
if (currNode.Parent == null) return; //没有父节点返回
if (isCheckedOrNot) //如果当前节点被选中,则设置所有父节点都被选中
{
currNode.Parent.Checked = isCheckedOrNot;
SetParentNodeCheckedState(currNode.Parent, isCheckedOrNot);
}
else //如果当前节点没有被选中,则当其父节点的子节点有一个被选中时,父节点被选中,否则父节点不被选中
{
bool checkedFlag = false;
foreach (TreeNode tmpNode in currNode.Parent.Nodes)
{
if (tmpNode.Checked)
{
checkedFlag = true;
break;
}
}
currNode.Parent.Checked = checkedFlag;
SetParentNodeCheckedState(currNode.Parent, checkedFlag);
}
}
#endregion
}
}