?? aoeditor.cs
字號:
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 PolygonMovePointFeedbackClass();
m_pFeedback.Display = pActiveView.ScreenDisplay;
IPolygonMovePointFeedback pPolyMove =(IPolygonMovePointFeedback) m_pFeedback;
pPolyMove.Start((IPolygon) pGeom, vertexIndex, pPoint);
}
else
{
return false;
}
break;
}
return true ;
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString());
return false;
}
}
/// <summary>
/// 編輯地圖對象過程中的鼠標(biāo)移動事件,
/// 如果為點(diǎn)對象,進(jìn)行位置移動
/// 如果為線對象或面對象,進(jìn)行節(jié)點(diǎn)移動
/// 建議在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 EditFeatureMouseMove(int x, int y)
{
try
{
if (m_pFeedback == null) return ;
IActiveView pActiveView =(IActiveView) m_pMap;
IPoint pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
m_pFeedback.MoveTo(pPoint);
// if (m_pSelectionTracker !=null) m_pSelectionTracker.OnMouseMove(1,0,x,y);
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
/// <summary>
/// 完成地圖對象編輯,取得編輯后的對象,并將其更新到圖層中
/// 建議在Map.MouseUp事件中調(diào)用本方法
/// </summary>
public void EditFeatureEnd()
{
IGeometry pGeometry;
try
{
if (m_pFeedback ==null) return;
if (m_pFeedback is IMovePointFeedback)
{
IMovePointFeedback pPointMove =(IMovePointFeedback) m_pFeedback;
pGeometry = pPointMove.Stop();
UpdateFeature(m_pEditFeature, pGeometry);
}
else if (m_pFeedback is ILineMovePointFeedback)
{
ILineMovePointFeedback pLineMove =(ILineMovePointFeedback) m_pFeedback;
pGeometry = pLineMove.Stop();
UpdateFeature(m_pEditFeature, pGeometry);
}
else if (m_pFeedback is IPolygonMovePointFeedback)
{
IPolygonMovePointFeedback pPolyMove =(IPolygonMovePointFeedback) m_pFeedback;
pGeometry = pPolyMove.Stop();
UpdateFeature(m_pEditFeature, pGeometry);
}
m_pFeedback = null;
// m_pSelectionTracker = null;
IActiveView pActiveView = (IActiveView) m_pMap;
pActiveView.Refresh();
}
catch( Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
/// <summary>
/// 移動當(dāng)前圖層中鼠標(biāo)擊中地圖對象的位置(開始移動)
/// 建議在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 MoveFeatureMouseDown(int x, int y)
{
try
{
m_pMap.ClearSelection();
SelectMouseDown(x,y);
IEnumFeature pSelected =(IEnumFeature) m_pMap.FeatureSelection;
IFeature pFeature = pSelected.Next();
if (pFeature ==null ) return false;
IActiveView pActiveView =(IActiveView) m_pMap;
IPoint pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
IGeometry pGeom = pFeature.Shape;
m_pEditFeature = pFeature;
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:
m_pFeedback = new MoveLineFeedbackClass() ;
m_pFeedback.Display = pActiveView.ScreenDisplay;
IMoveLineFeedback pLineMove =(IMoveLineFeedback) m_pFeedback;
pLineMove.Start((IPolyline)pGeom, pPoint);
break;
case esriGeometryType.esriGeometryPolygon:
m_pFeedback = new MovePolygonFeedbackClass();
m_pFeedback.Display = pActiveView.ScreenDisplay;
IMovePolygonFeedback pPolyMove =(IMovePolygonFeedback) m_pFeedback;
pPolyMove.Start((IPolygon) pGeom,pPoint);
break;
}
return true ;
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString());
return false;
}
}
/// <summary>
/// 移動地圖對象過程中的鼠標(biāo)移動事件
/// 建議在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 MoveFeatureMouseMove(int x, int y)
{
try
{
if (m_pFeedback == null) return ;
IActiveView pActiveView =(IActiveView) m_pMap;
IPoint pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
m_pFeedback.MoveTo(pPoint);
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
/// <summary>
/// 完成地圖對象移動,取得移動后的對象,并將其更新到圖層中
/// 建議在Map.MouseUp事件中調(diào)用本方法
/// </summary>
public void MoveFeatureEnd()
{
IGeometry pGeometry;
try
{
if (m_pFeedback ==null) return;
if (m_pFeedback is IMovePointFeedback)
{
IMovePointFeedback pPointMove =(IMovePointFeedback) m_pFeedback;
pGeometry = pPointMove.Stop();
UpdateFeature(m_pEditFeature, pGeometry);
}
else if (m_pFeedback is IMoveLineFeedback)
{
IMoveLineFeedback pLineMove =(IMoveLineFeedback) m_pFeedback;
pGeometry = pLineMove.Stop();
UpdateFeature(m_pEditFeature, pGeometry);
}
else if (m_pFeedback is IMovePolygonFeedback)
{
IMovePolygonFeedback pPolyMove =(IMovePolygonFeedback) m_pFeedback;
pGeometry = pPolyMove.Stop();
UpdateFeature(m_pEditFeature, pGeometry);
}
m_pFeedback = null;
IActiveView pActiveView = (IActiveView) m_pMap;
pActiveView.Refresh();
}
catch( Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
/// <summary>
/// 刪除當(dāng)前圖層中選中的地圖對象
/// </summary>
public void DeleteSelectedFeature()
{
try
{
if (m_pCurrentLayer == null) return ;
IFeatureCursor pFeatureCursor = GetSelectedFeatures();
if (pFeatureCursor ==null) return ;
m_pMap.ClearSelection();
IWorkspaceEdit pWorkspaceEdit = GetWorkspaceEdit();
pWorkspaceEdit.StartEditOperation();
IFeature pFeature = pFeatureCursor.NextFeature();
while (pFeature !=null)
{
pFeature.Delete();
pFeature = pFeatureCursor.NextFeature();
}
pWorkspaceEdit.StopEditOperation();
IActiveView pActiveView =(IActiveView) m_pMap;
pActiveView.Refresh();
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
/// <summary>
/// 撤消以前所做的編輯
/// </summary>
public void UndoEdit()
{
bool bHasUndos =false;
try
{
if (m_pCurrentLayer ==null) return ;
IFeatureLayer pFeatureLayer =(IFeatureLayer) m_pCurrentLayer;
IDataset pDataset =(IDataset) pFeatureLayer.FeatureClass;
if (pDataset ==null) return ;
IWorkspaceEdit pWorkspaceEdit =(IWorkspaceEdit) pDataset.Workspace;
pWorkspaceEdit.HasUndos(ref bHasUndos);
if (bHasUndos) pWorkspaceEdit.UndoEditOperation();
IActiveView pActiveView =(IActiveView) m_pMap;
pActiveView.Refresh();
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
/// <summary>
/// 重做已撤消的編輯
/// </summary>
public void RedoEdit()
{
bool bHasRedos =false;
try
{
if (m_pCurrentLayer ==null) return ;
IFeatureLayer pFeatureLayer = (IFeatureLayer) m_pCurrentLayer;
IDataset pDataset =(IDataset) pFeatureLayer.FeatureClass;
if (pDataset ==null) return ;
IWorkspaceEdit pWorkspaceEdit =(IWorkspaceEdit) pDataset.Workspace;
pWorkspaceEdit.HasRedos(ref bHasRedos);
if (bHasRedos) pWorkspaceEdit.RedoEditOperation();
IActiveView pActiveView =(IActiveView) m_pMap;
pActiveView.Refresh();
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
/// <summary>
/// 向圖層中添加新的地圖對象,并使之處于選中狀態(tài)
/// </summary>
/// <param name="pGeom">圖形對象</param>
private void CreateFeature(IGeometry pGeom)
{
try
{
if (pGeom ==null) return ;
if (m_pCurrentLayer ==null) return ;
IWorkspaceEdit pWorkspaceEdit = GetWorkspaceEdit();
IFeatureLayer pFeatureLayer = (IFeatureLayer) m_pCurrentLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
pWorkspaceEdit.StartEditOperation();
IFeature pFeature = pFeatureClass.CreateFeature();
pFeature.Shape = pGeom;
pFeature.Store();
pWorkspaceEdit.StopEditOperation();
m_pMap.SelectFeature(m_pCurrentLayer, pFeature);
IActiveView pActiveView =(IActiveView) m_pMap;
pActiveView.Refresh();
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
/// <summary>
/// 瘵屏幕坐標(biāo)轉(zhuǎn)換為地圖坐標(biāo)
/// </summary>
/// <param name="pActiveView">地圖</param>
/// <param name="pixelUnits">屏幕坐標(biāo)</param>
/// <returns>地圖坐標(biāo)</returns>
private double ConvertPixelsToMapUnits(IActiveView pActiveView , double pixelUnits)
{
tagRECT pRect = pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame();
int pixelExtent = pRect.right - pRect.left ;
double realWorldDisplayExtent = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width;
double sizeOfOnePixel = realWorldDisplayExtent / pixelExtent;
return pixelUnits * sizeOfOnePixel;
}
/// <summary>
/// 取得當(dāng)前圖層所在的工作空間
/// </summary>
/// <returns>工作空間</returns>
private IWorkspaceEdit GetWorkspaceEdit()
{
if (m_pCurrentLayer == null) return null ;
IFeatureLayer pFeatureLayer = (IFeatureLayer) m_pCurrentLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IDataset pDataset = (IDataset) pFeatureClass;
if (pDataset ==null)
return null;
else
return (IWorkspaceEdit)pDataset.Workspace;
}
/// <summary>
/// 取得選中的地圖對象集合
/// </summary>
/// <returns>地圖對象游標(biāo)</returns>
private IFeatureCursor GetSelectedFeatures()
{
if (m_pCurrentLayer == null) return null;
IFeatureSelection pFeatSel = (IFeatureSelection) m_pCurrentLayer;
ISelectionSet pSelectionSet = pFeatSel.SelectionSet;
if (pSelectionSet.Count == 0)
{
return null;
}
ICursor pCursor;
pSelectionSet.Search(null, false, out pCursor);
return (IFeatureCursor) pCursor;
}
/// <summary>
/// 測試是否擊中地圖對象或地圖對象上的節(jié)點(diǎn)
/// </summary>
/// <param name="tolerance">查詢?nèi)莶?lt;/param>
/// <param name="pPoint">點(diǎn)擊位置</param>
/// <param name="pFeature">測試對象</param>
/// <param name="pHitPoint">查詢目標(biāo)點(diǎn)</param>
/// <param name="hitDist">目標(biāo)點(diǎn)與點(diǎn)擊點(diǎn)距離</param>
/// <param name="partIndex">節(jié)索引</param>
/// <param name="vertexIndex">點(diǎn)索引</param>
/// <param name="vertexHit">是否擊中點(diǎn)</param>
/// <returns>是否擊中測試對象</returns>
private bool TestGeometryHit(double tolerance, IPoint pPoint, IFeature pFeature, IPoint pHitPoint,
double hitDist,out int partIndex,out int vertexIndex,out bool vertexHit)
{
try
{
IGeometry pGeom = pFeature.Shape;
IHitTest pHitTest =(IHitTest) pGeom;
pHitPoint = new PointClass();
bool bRes = true;
partIndex =0;
vertexIndex =0;
vertexHit = false;
// 檢查節(jié)點(diǎn)是否被擊中
if (pHitTest.HitTest(pPoint, tolerance, esriGeometryHitPartType.esriGeometryPartVertex, pHitPoint,
ref hitDist, ref partIndex, ref vertexIndex, ref bRes))
{
vertexHit = true;
return true;
}
// 檢邊界是否被擊中
else
{
if (pHitTest.HitTest(pPoint, tolerance,esriGeometryHitPartType.esriGeometryPartBoundary, pHitPoint,
ref hitDist, ref partIndex, ref vertexIndex, ref bRes))
{
vertexHit = false;
return true;
}
}
return false;
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString());
partIndex =0;
vertexIndex =0;
vertexHit = false;
return false;
}
}
/// <summary>
/// 向圖層中更新新的地圖對象,并使之處于選中狀態(tài)
/// </summary>
/// <param name="pFeature"></param>
/// <param name="pGeometry"></param>
private void UpdateFeature(IFeature pFeature, IGeometry pGeometry)
{
try
{
IDataset pDataset =(IDataset) pFeature.Class;
IWorkspaceEdit pWorkspaceEdit =(IWorkspaceEdit) pDataset.Workspace;
if (!pWorkspaceEdit.IsBeingEdited())
{
MessageBox.Show("當(dāng)前圖層不可編輯");
return ;
}
pWorkspaceEdit.StartEditOperation();
pFeature.Shape = pGeometry;
pFeature.Store();
pWorkspaceEdit.StopEditOperation();
}
catch(Exception e)
{
MessageBox.Show(e.Message.ToString());
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -