Arcengine的工具中實現Snapping(捕捉)
阿新 • • 發佈:2019-02-09
在Engine的工具(ITool)裡:
OnClick事件處理函式中:
首先需要獲取一個圖層,作為Snapping的參照,
IFeatureLayer targetLayer
然後宣告一個IMovePointFeedBack作為滑鼠移動時捕捉點的顯示:
- IMovePointFeedback m_pMovePtFeed = new MovePointFeedback();
- mFeedback = (IDisplayFeedback)m_pMovePtFeed;
-
ISimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass();
- IRgbColor pRGBColor = new RgbColorClass();
- pRGBColor.Red = 0;
- pRGBColor.Green = 0;
- pRGBColor.Blue = 0;
- simpleMarkerSymbol.Color = pRGBColor;
- simpleMarkerSymbol.Size = 3;
- simpleMarkerSymbol.Style = ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSSquare;
-
ISymbol symbol = simpleMarkerSymbol as
- symbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen;
- //symbol.ROP2 = esriRasterOpCode.;
- m_pMovePtFeed.Symbol = (ISymbol)simpleMarkerSymbol;
然後開始Feedback的顯示(tmpPoint是指開始的點,其實影響不大,如果不想要源點在螢幕上的話,可以取一個在螢幕外的點):
- m_pMovePtFeed.Display = mMapControl.ActiveView.ScreenDisplay;
-
m_pMovePtFeed.Start(tmpPoint, tmpPoint);
在OnMouseMove事件中:
- IPoint pPoint2 = null;
- IPoint pPoint = pMap.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
- pPoint2 = Snapping(pPoint.X, pPoint.Y, targetLayer, pMap, 10);
- if (pPoint2 == null)
- pPoint2 = pPoint;
- ((IMovePointFeedback)mFeedback).MoveTo(pPoint2);
其中Snapping函式即為最主要的查詢函式,其功能為根據輸入的點座標在目標圖層上查詢最為相近的點(也即需要捕捉的點),返回該點,若沒有找到則返回NULL,最後一個引數的含義是,在地圖控制元件上,以多少個畫素為單位在周邊查詢捕捉點.
- public IPoint Snapping(double x, double y, IFeatureLayer iFeatureLyr, IMapControl3 axMapControl1,double snappingDis)
- {
- IPoint iHitPoint = null;
- IMap iMap = axMapControl1.Map;
- IActiveView iView = axMapControl1.ActiveView;
- IFeatureClass iFClss = iFeatureLyr.FeatureClass;
- IPoint point = new PointClass();
- point.PutCoords(x, y);
- double length = ConvertPixelsToMapUnits(axMapControl1.ActiveView, snappingDis);
- ITopologicalOperator pTopo = point as ITopologicalOperator;
- IGeometry pGeometry = pTopo.Buffer(length).Envelope as IGeometry;
- ISpatialFilter spatialFilter = new SpatialFilterClass();
- spatialFilter.GeometryField = iFeatureLyr.FeatureClass.ShapeFieldName;
- spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
- spatialFilter.Geometry = pGeometry;
- IFeatureCursor cursor = iFClss.Search(spatialFilter, false);
- IFeature iF = cursor.NextFeature();
- if (iF == null) returnnull;
- IPoint iHitPt = new ESRI.ArcGIS.Geometry.Point();
- IHitTest iHitTest = iF.Shape as IHitTest;
- double hitDist = 0;
- int partIndex = 0;
- int vertexIndex = 0;
- bool bVertexHit = false;
- // Tolerance in pixels for line hits
- double tol = ConvertPixelsToMapUnits(iView, snappingDis);
- if (iHitTest.HitTest(point, tol, esriGeometryHitPartType.esriGeometryPartBoundary,
- iHitPt, ref hitDist, ref partIndex, ref vertexIndex, ref bVertexHit))
- {
- iHitPoint = iHitPt;
- }
- //axMapControl1.ActiveView.Refresh();
- return iHitPoint;
- }
- publicdouble ConvertPixelsToMapUnits(IActiveView pActiveView, double pixelUnits)
- {
- double realWorldDisplayExtent;
- int pixelExtent;
- double sizeOfOnePixel;
- pixelExtent = pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().right - pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().left;
- realWorldDisplayExtent = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width;
- sizeOfOnePixel = realWorldDisplayExtent / pixelExtent;
- return pixelUnits * sizeOfOnePixel;
- }
此時即可實現滑鼠實時地捕捉目標圖層上的物件,若需要獲取當前位置的捕捉點時,則可以在相應事件(例如OnMouseDown或OnDbClick)中呼叫:
- IPoint pPoint = ((IMovePointFeedback)mFeedback).Stop();
這時實時捕捉將會停止,若需要重新開始捕捉,則在之後呼叫這些語句即可:
- //重新開始Snap
- IPoint tmpPoint = new PointClass();
- tmpPoint.PutCoords(pMap.Extent.XMin - 1, pMap.Extent.YMin - 1);
- IMovePointFeedback m_pMovePtFeed = (IMovePointFeedback)mFeedback;
- m_pMovePtFeed.Display = pMap.ActiveView.ScreenDisplay;
- m_pMovePtFeed.Start(tmpPoint, tmpPoint);