?? aoeditor.cs
字號(hào):
using System;
using System.IO ;
using System.Windows.Forms ;
using System.Reflection ;
using ESRI.ArcGIS.Carto ;
using ESRI.ArcGIS.Display ;
using ESRI.ArcGIS.Geometry ;
using ESRI.ArcGIS.Geodatabase ;
namespace AoTest
{
/// <summary>
/// 使用本類可以新建點(diǎn)、線、面
/// 移動(dòng)點(diǎn)、線、面
/// 編輯線、面的節(jié)點(diǎn)
/// 使用時(shí)需設(shè)置Map和CurrentLayer
/// </summary>
public class AoEditor
{
private ILayer m_pCurrentLayer;
private IMap m_pMap ;
private IFeature m_pEditFeature ;
private IPoint m_pPoint;
private IDisplayFeedback m_pFeedback;
// private ISelectionTracker m_pSelectionTracker;
private bool m_bInUse;
private IPointCollection m_pPointCollection;
/// <summary>
/// 當(dāng)前圖層,只寫
/// </summary>
public ILayer CurrentLayer
{
set
{
m_pCurrentLayer = (ILayer) value;
}
}
/// <summary>
/// 地圖對(duì)象,只寫
/// </summary>
public IMap Map
{
set
{
m_pMap = (IMap) value;
}
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
public AoEditor()
{
}
/// <summary>
/// 開始編輯,使工作空間處于可編輯狀態(tài)
/// 在進(jìn)行圖層編輯前必須調(diào)用本方法
/// </summary>
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 ;
// 開始編輯,并設(shè)置Undo/Redo 為可用
IWorkspaceEdit pWorkspaceEdit =(IWorkspaceEdit) pDataset.Workspace;
if (!pWorkspaceEdit.IsBeingEdited())
{
pWorkspaceEdit.StartEditing(true);
pWorkspaceEdit.EnableUndoRedo();
}
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
/// <summary>
/// 停止編輯,并將以前的編輯結(jié)果保存到數(shù)據(jù)文件中。
/// </summary>
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 ;
//如果數(shù)據(jù)已被修改,則提示用戶是否保存
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());
}
}
/// <summary>
/// 檢查工作空間中是否有數(shù)據(jù)處于編輯狀態(tài)
/// </summary>
/// <returns>是否正在編輯</returns>
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;
}
}
/// <summary>
/// 新建對(duì)象方法
/// 當(dāng)前圖層為點(diǎn)圖層時(shí),每調(diào)用一次就新點(diǎn)一個(gè)點(diǎn)對(duì)象
/// 當(dāng)前圖層為線圖層或面圖層時(shí),第一次調(diào)用開始新建對(duì)象,并添加當(dāng)前點(diǎn),
/// 以后每調(diào)用一次,即向新對(duì)象中添加一個(gè)點(diǎn),調(diào)用NewFeatureEnd方法完成對(duì)象創(chuàng)建
/// 在Map.MouseDown事件中調(diào)用本方法
/// </summary>
/// <param name="x">鼠標(biāo)X坐標(biāo),屏幕坐標(biāo)</param>
/// <param name="y">鼠標(biāo)Y坐標(biāo),屏幕坐標(biāo)</param>
public void NewFeatureMouseDown( int x, int y)
{
INewPolygonFeedback pPolyFeed ;
INewLineFeedback pLineFeed;
try
{
if (m_pCurrentLayer == null ) return ;
if (!(m_pCurrentLayer is IGeoFeatureLayer)) return ;
IFeatureLayer pFeatureLayer =(IFeatureLayer) m_pCurrentLayer;
if (pFeatureLayer.FeatureClass ==null ) return ;
IActiveView pActiveView =(IActiveView) m_pMap;
IPoint pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
// 如果是新開始創(chuàng)建的對(duì)象,則相應(yīng)的創(chuàng)建一個(gè)新的Feedback對(duì)象;
// 否則,向已存在的Feedback對(duì)象中加點(diǎn)
if (!m_bInUse)
{
m_pMap.ClearSelection(); //清除地圖選中對(duì)象
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)
{
object obj = Missing.Value ;
m_pPointCollection.AddPoint(pPoint,ref obj,ref obj);
}
else if (m_pFeedback is INewLineFeedback)
{
pLineFeed =(INewLineFeedback) m_pFeedback;
pLineFeed.AddPoint(pPoint);
}
else if (m_pFeedback is INewPolygonFeedback)
{
pPolyFeed = (INewPolygonFeedback) m_pFeedback;
pPolyFeed.AddPoint(pPoint);
}
}
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
/// <summary>
/// 新建對(duì)象過程中鼠標(biāo)移動(dòng)方法,產(chǎn)生Track效果
/// 在Map.MouseMove事件中調(diào)用本方法
/// </summary>
/// <param name="x">鼠標(biāo)X坐標(biāo),屏幕坐標(biāo)</param>
/// <param name="y">鼠標(biāo)Y坐標(biāo),屏幕坐標(biāo)</param>
public void NewFeatureMouseMove(int x, int y)
{
if ((!m_bInUse)||(m_pFeedback ==null)) return ;
IActiveView pActiveView = (IActiveView) m_pMap;
m_pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
m_pFeedback.MoveTo(m_pPoint);
}
/// <summary>
/// 完成新建對(duì)象,取得繪制的對(duì)象,并添加到圖層中
/// 建議在Map.DblClick或Map.MouseDown(Button = 2)事件中調(diào)用本方法
/// </summary>
public void NewFeatureEnd()
{
IGeometry pGeom = null;
IPointCollection pPointCollection;
try
{
if (m_pFeedback is INewMultiPointFeedback)
{
INewMultiPointFeedback pMPFeed =(INewMultiPointFeedback) m_pFeedback;
pMPFeed.Stop();
pGeom =(IGeometry) m_pPointCollection;
}
else if (m_pFeedback is INewLineFeedback)
{
INewLineFeedback pLineFeed =(INewLineFeedback) m_pFeedback;
pLineFeed.AddPoint(m_pPoint);
IPolyline pPolyLine = pLineFeed.Stop();
pPointCollection =(IPointCollection) pPolyLine;
if (pPointCollection.PointCount < 2)
MessageBox.Show("至少輸入兩個(gè)節(jié)點(diǎn)");
else
pGeom =(IGeometry) pPointCollection;
}
else if (m_pFeedback is INewPolygonFeedback)
{
INewPolygonFeedback pPolyFeed =(INewPolygonFeedback) m_pFeedback;
pPolyFeed.AddPoint(m_pPoint);
IPolygon pPolygon ;
pPolygon = pPolyFeed.Stop();
if (pPolygon !=null)
{
pPointCollection =(IPointCollection) pPolygon;
if (pPointCollection.PointCount < 3)
MessageBox.Show("至少輸入三個(gè)節(jié)點(diǎn)");
else
pGeom =(IGeometry) pPointCollection;
}
}
CreateFeature(pGeom);
m_pFeedback = null;
m_bInUse = false;
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
/// <summary>
/// 查詢當(dāng)前圖層中鼠標(biāo)位置處的地圖對(duì)象
/// 建議在Map.MouseDown事件中調(diào)用本方法
/// </summary>
/// <param name="x">鼠標(biāo)X坐標(biāo),屏幕坐標(biāo)</param>
/// <param name="y">鼠標(biāo)Y坐標(biāo),屏幕坐標(biāo)</param>
public void SelectMouseDown(int x, int y)
{
ISpatialFilter pSpatialFilter;
IQueryFilter pFilter ;
try
{
if (m_pCurrentLayer == null) return ;
if (!(m_pCurrentLayer is IGeoFeatureLayer)) return ;
IFeatureLayer pFeatureLayer =(IFeatureLayer) m_pCurrentLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
if (pFeatureClass == null) return;
IActiveView pActiveView =(IActiveView) m_pMap;
IPoint pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
IGeometry pGeometry = pPoint;
// 設(shè)置查詢緩沖區(qū)
double length = ConvertPixelsToMapUnits(pActiveView, 4.0);
ITopologicalOperator pTopo =(ITopologicalOperator) pGeometry;
IGeometry pBuffer = pTopo.Buffer(length);
pGeometry = pBuffer.Envelope;
//設(shè)置過濾器對(duì)象
pSpatialFilter = new SpatialFilterClass();
pSpatialFilter.Geometry = pGeometry;
switch (pFeatureClass.ShapeType)
{
case esriGeometryType.esriGeometryPoint:
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
break;
case esriGeometryType.esriGeometryPolyline:
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses;
break;
case esriGeometryType.esriGeometryPolygon:
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
break;
}
pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;
pFilter = pSpatialFilter;
// 查詢
IFeatureCursor pCursor = pFeatureLayer.Search(pFilter, false);
// 在地圖上高亮顯示查詢結(jié)果
IFeature pFeature = pCursor.NextFeature();
while (pFeature != null)
{
m_pMap.SelectFeature(m_pCurrentLayer, pFeature);
pFeature = pCursor.NextFeature();
}
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection ,null,null);
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
/// <summary>
/// 編輯當(dāng)前圖層中鼠標(biāo)擊中的地圖對(duì)象(開始編輯),
/// 如果為點(diǎn)對(duì)象,可進(jìn)行位置移動(dòng),如果為線對(duì)象或面對(duì)象,可進(jìn)行節(jié)點(diǎn)編輯
/// 建議在Map.MouseDown事件中調(diào)用本方法
/// </summary>
/// <param name="x">鼠標(biāo)X坐標(biāo),屏幕坐標(biāo)</param>
/// <param name="y">鼠標(biāo)Y坐標(biāo),屏幕坐標(biāo)</param>
/// <returns></returns>
public bool EditFeatureMouseDown(int x, int y)
{
IGeometryCollection pGeomColn ;
IPointCollection pPointColn;
IObjectClass pObjectClass ;
IFeature pFeature;
IGeometry pGeom ;
IPath pPath ;
IPoint pHitPoint =null ;
IPoint pPoint =null;
Double hitDist =0.0;
double tol;
int vertexIndex =0;
int numVertices;
int partIndex =0;
bool vertex = false;
try
{
m_pMap.ClearSelection();
// 取得鼠標(biāo)擊中的第一個(gè)對(duì)象
SelectMouseDown(x,y);
IEnumFeature pSelected =(IEnumFeature) m_pMap.FeatureSelection;
pFeature = pSelected.Next();
if (pFeature ==null ) return false;
IActiveView pActiveView =(IActiveView) m_pMap;
pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
// 節(jié)點(diǎn)空間查詢?nèi)莶? tol = ConvertPixelsToMapUnits(pActiveView, 4.0);
pGeom = pFeature.Shape;
pObjectClass = pFeature.Class;
m_pEditFeature = pFeature;
object objNull = Missing.Value ;
object objBefore, objAfter;
switch (pGeom.GeometryType)
{
case esriGeometryType.esriGeometryPoint:
m_pFeedback = new MovePointFeedbackClass();
m_pFeedback.Display = pActiveView.ScreenDisplay;
IMovePointFeedback pPointMove =(IMovePointFeedback) m_pFeedback;
pPointMove.Start((IPoint)pGeom, pPoint);
break;
case esriGeometryType.esriGeometryPolyline:
if (TestGeometryHit(tol, pPoint, pFeature, pHitPoint, hitDist,out partIndex,out vertexIndex,out vertex))
{
if (!vertex)
{
pGeomColn =(IGeometryCollection) pGeom;
pPath =(IPath) pGeomColn.get_Geometry(partIndex);
pPointColn =(IPointCollection) pPath;
numVertices = pPointColn.PointCount;
if (vertexIndex == 0)
{
objBefore = (object) (vertexIndex+1);
pPointColn.AddPoint(pPoint,ref objBefore,ref objNull);
}
else
{
objAfter = (object) vertexIndex;
pPointColn.AddPoint( pPoint,ref objNull , ref objAfter);
}
TestGeometryHit(tol, pPoint, pFeature, pHitPoint, hitDist,out partIndex,out vertexIndex,out vertex);
}
m_pFeedback = new LineMovePointFeedbackClass();
m_pFeedback.Display = pActiveView.ScreenDisplay;
ILineMovePointFeedback pLineMove =(ILineMovePointFeedback) m_pFeedback;
pLineMove.Start((IPolyline)pGeom, vertexIndex, pPoint);
// m_pSelectionTracker = new LineTrackerClass();
// m_pSelectionTracker.Display = pActiveView.ScreenDisplay ;
// m_pSelectionTracker.Geometry = pGeom;
// m_pSelectionTracker.ShowHandles = true;
// m_pSelectionTracker.QueryResizeFeedback(ref m_pFeedback);
// m_pSelectionTracker.OnMouseDown(1,0,x,y);
}
else
{
return false;
}
break;
case esriGeometryType.esriGeometryPolygon:
if (TestGeometryHit(tol, pPoint, pFeature, pHitPoint, hitDist,out partIndex,out vertexIndex,out vertex))
{
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -