using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Controls;
namespace LGGIS.MapEdit
{
public class ArcgisEditor
{
///
/// 使用本类可以新建点、线、面
/// 移动点、线、面
/// 编辑线、面的节点
/// 使用时需设置 Map 和 CurrentLayer
///
private IMapControl3 m_pMapControl;
private ILayer m_pCurrentLayer;
private IMap m_pMap;
private IFeature m_pEditFeature;
private IPoint m_pPoint;
private IDisplayFeedback m_pFeedback;
//
private bool m_bInUse;
private IPointCollection m_pPointCollection;
private ISelection m_Selection = null;
private IGeometryCollection m_GeometryCollection = null;
public static IEnumFeature SelEnumFeature = null;
private ISelectionTracker m_pSelectionTracker;
private IArray m_ElementArray = new ArrayClass();
public IMapControl3 MapControl
{
get
{
}
set
{
}
}
return m_pMapControl;
m_pMapControl = value;
///
/// 当前图层
///
public ILayer CurrentLayer
{
get
{
}
set
{
}
return m_pCurrentLayer;
m_pCurrentLayer = (ILayer)value;
}
///
/// 地图对象
///
public IMap Map
{
get
{
}
set
{
}
return m_pMap;
m_pMap = (IMap)value;
}
///
/// 选中的要素
///
public ISelection Selection
{
get { return m_Selection; }
set { m_Selection = value; }
}
///
/// 构造函数
///
public ArcgisEditor()
{
}
public bool IsStartEdit()
{
IWorkspaceEdit pWorkspaceEdit;
IFeatureClass pFeatureCls;
IFeatureLayer pFeatureLyr;
bool pIsStart = false;
for (int i = 0; i < m_pMapControl.LayerCount; i++)
{
pFeatureLyr = m_pMapControl.Map.get_Layer(i) as IFeatureLayer;
if (pFeatureLyr == null) { continue; }
pFeatureCls = pFeatureLyr.FeatureClass;
pWorkspaceEdit = ((IDataset)pFeatureCls).Workspace as IWorkspaceEdit;
if (pWorkspaceEdit.IsBeingEdited() == true)
{
pIsStart = true;
break;
}
}
return pIsStart;
}
public ILayer getCurrentLayer()
{
ILayer pLayer = CheckEditingFeatureLayer() ;
return pLayer;
}
public ILayer CheckEditingFeatureLayer()
{
IWorkspaceEdit pWorkspaceEdit;
IFeatureClass pFeatureCls;
IFeatureLayer pFeatureLyr;
ILayer pLayer = null;
for (int i = 0; i < m_pMapControl.LayerCount; i++)
{
if (m_pMapControl.Map.get_Layer(i) is IFeatureLayer)
{
pFeatureLyr = m_pMapControl.Map.get_Layer(i) as IFeatureLayer;
if (pFeatureLyr == null) { continue; }
pFeatureCls = pFeatureLyr.FeatureClass;
pWorkspaceEdit = ((IDataset)pFeatureCls).Workspace as IWorkspaceEdit;
if (pWorkspaceEdit.IsBeingEdited() == true)
{
pLayer = pFeatureLyr as ILayer;
ILayer plyr = m_pMapControl.CustomProperty as ILayer;
if
(plyr
!=
null
&&
((IDataset)((IFeatureLayer)plyr).FeatureClass).Workspace == ((IDataset)pFeatureCls).Workspace)
{
}
pLayer = plyr;
break;
}
}
}
return pLayer;
}
///
/// 开始编辑,使工作空间处于可编辑状态
/// 在进行图层编辑前必须调用本方法
///
public void StartEditing()
{
try
{
if (m_pCurrentLayer == null) return;
if (!(m_pCurrentLayer is IGeoFeatureLayer)) return;
IFeatureLayer pFeatureLayer = (IFeatureLayer)m_pCurrentLayer;
IDataset pDataset = (IDataset)pFeatureLayer.FeatureClass;
if (pDataset == null) return;
// 开始编辑,并设置 Undo/Redo 为可用
IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pDataset.Workspace;
if (!pWorkspaceEdit.IsBeingEdited())
{
pWorkspaceEdit.StartEditing(true);
pWorkspaceEdit.EnableUndoRedo();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
///
/// 停止编辑,并将以前的编辑结果保存到数据文件中。
///
public void StopEditing()
{
bool bHasEdits = false;
bool bSave = false;
try
{
if (m_pCurrentLayer == null) return;
IFeatureLayer pFeatureLayer = (IFeatureLayer)m_pCurrentLayer;
if (pFeatureLayer.FeatureClass == null) return;
IDataset pDataset = (IDataset)pFeatureLayer.FeatureClass;
if (pDataset == null) return;
//如果数据已被修改,则提示用户是否保存
IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pDataset.Workspace;
if (pWorkspaceEdit.IsBeingEdited())
{
pWorkspaceEdit.HasEdits(ref bHasEdits);
if (bHasEdits)
{
DialogResult result;
result = MessageBox.Show(" 是 否 保 存 已 做 的 修 改 ?", " 提 示 ",
MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
bSave = true;
}
}
pWorkspaceEdit.StopEditing(bSave);
}
m_pMap.ClearSelection();
IActiveView pActiveView = (IActiveView)m_pMap;
pActiveView.Refresh();
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
public bool HasEdits()
{
IWorkspaceEdit pWorkspaceEdit;
IFeatureLayer pFeatureLayer = m_pCurrentLayer as IFeatureLayer;
pWorkspaceEdit
((IDataset)pFeatureLayer.FeatureClass).Workspace
=
as
IWorkspaceEdit;
bool blHasEdits = false;
pWorkspaceEdit.HasEdits(ref blHasEdits);
return blHasEdits;
}
///
/// 检查工作空间中是否有数据处于编辑状态
///
/// 是否正在编辑
public bool InEdit()
{
try
{
if (m_pCurrentLayer == null) return false;
IFeatureLayer pFeatureLayer = (IFeatureLayer)m_pCurrentLayer;
if (pFeatureLayer.FeatureClass == null) return false;
IDataset pDataset = (IDataset)pFeatureLayer.FeatureClass;
if (pDataset == null) return false;
IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pDataset.Workspace;
if (pWorkspaceEdit.IsBeingEdited()) return true;
return false;
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
return false;
}
}
///
/// 新建对象方法
/// 当前图层为点图层时,每调用一次就新点一个点对象
/// 当前图层为线图层或面图层时,第一次调用开始新建对象,并添加当前点,
/// 以后每调用一次,即向新对象中添加一个点,调用 NewFeatureEnd 方法完成对象
创建
/// 在 Map.MouseDown 事件中调用本方法
///
///
鼠标 X 坐标,屏幕坐标
///
鼠标 Y 坐标,屏幕坐标
public void NewFeatureMouseDown(int x, int y)
{
INewPolygonFeedback pPolyFeed;
INewLineFeedback pLineFeed;
try
{
if (IsStartEdit() == true)
{
if (m_pCurrentLayer == null) return;
if (!(m_pCurrentLayer is IGeoFeatureLayer)) return;
IFeatureLayer pFeatureLayer = (IFeatureLayer)m_pCurrentLayer;
if (pFeatureLayer.FeatureClass == null) return;
pPoint
pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
IActiveView pActiveView = (IActiveView)m_pMap;
IPoint
=
// 如果是新开始创建的对象,则相应的创建一个新的 Feedback 对象;
// 否则,向已存在的 Feedback 对象中加点
if (!m_bInUse)
{
m_pMap.ClearSelection();
//清除地图选中对象
switch (pFeatureLayer.FeatureClass.ShapeType)
{
case esriGeometryType.esriGeometryPoint:
CreateFeature(pPoint);
break;
case esriGeometryType.esriGeometryMultipoint:
m_bInUse = true;
m_pFeedback = new NewMultiPointFeedbackClass();
INewMultiPointFeedback
pMPFeed
=
(INewMultiPointFeedback)m_pFeedback;
m_pPointCollection = new MultipointClass();
pMPFeed.Start(m_pPointCollection, pPoint);
break;
case esriGeometryType.esriGeometryPolyline:
m_bInUse = true;
m_pFeedback = new NewLineFeedbackClass();
pLineFeed = (INewLineFeedback)m_pFeedback;
pLineFeed.Start(pPoint);
break;
case esriGeometryType.esriGeometryPolygon:
m_bInUse = true;
m_pFeedback = new NewPolygonFeedbackClass();
pPolyFeed = (INewPolygonFeedback)m_pFeedback;
pPolyFeed.Start(pPoint);
break;
}
if (m_pFeedback != null)
m_pFeedback.Display = pActiveView.ScreenDisplay;
}
else
{
if (m_pFeedback is INewMultiPointFeedback)