c# AE實現欄選功能
阿新 • • 發佈:2018-05-09
畫圖 add pla true sha eat return inf eos
1工具效果展示:畫線,選擇與該線相交的要素並高亮顯示
2代碼實現
開發環境: c# 2010、 arcengine 10.1、 win7
思路:在mapcontrol中畫一條線(INewLineFeedback接口),並顯示;用map的SelectByShape方法選擇要素。
步驟:
a 在項目中添加BaseTool類,命名T_Tool.cs
b 在T_Tool中添加:INewLineFeedback m_displayFeedback = null 用於追蹤鼠標所畫的線段。
c 在OnMouseDown、OnMouseMove方法下面,實現畫線操作
d 在OnDblClick()下結束畫線,並顯示要素。
代碼:(OnMouseDown)
pPoint = m_mapControl.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y); if (m_displayFeedback == null) { m_displayFeedback = new NewLineFeedbackClass(); ISimpleLineSymbol pLine = m_displayFeedback.Symbol as ISimpleLineSymbol; RgbColorClass pColor=new RgbColorClass(); pColor.Red=255; pColor.Green=0; pColor.Blue=0; pLine.Color = pColor; m_displayFeedback.Display = m_mapControl.ActiveView.ScreenDisplay; m_displayFeedback.Start(pPoint); } else { m_displayFeedback.AddPoint(pPoint); }
在OnMouseMove下:
pPoint = m_mapControl.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
m_displayFeedback.MoveTo(pPoint);
在OnDblClick()
//雙擊的時候,結束畫線
IPolyline pLine = m_displayFeedback.Stop();
//釋放m_displayFeedbackm_displayFeedback = null; //顯示所畫圖形元素 DrawGeometry(pLine as IGeometry); //選中高亮 ISelectionEnvironment pSelectionEnvironment = new SelectionEnvironmentClass(); m_mapControl.Map.SelectByShape(pLine as IGeometry, pSelectionEnvironment, false); m_mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null); //空間查詢 ISpatialFilter pSpatialFilter = new SpatialFilterClass(); pSpatialFilter.Geometry = pLine as IGeometry; pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; IFeatureLayer pFeatureLayer = m_mapControl.Map.get_Layer(0) as IFeatureLayer; IFeatureCursor pfeatureCursor= pFeatureLayer.Search(pSpatialFilter, false); IFeature pFeature = pfeatureCursor.NextFeature(); while (pFeature != null) { //code goes here IFields pFields = pFeature.Fields; IField pField = null; for (int i = 0; i < pFields.FieldCount; i++) {
//此處可以獲取要素類的字段名和每一個feature的字段值,還可以將數據存儲在Datatable中,
//方便與其他對象(gridtable等)進行聯動展示
pField= pFields.get_Field(i); string filedName = pField.Name; object fieldValue= pFeature.get_Value(i); string field=fieldValue.ToString(); } pFeature = pfeatureCursor.NextFeature(); }
private void DrawGeometry(IGeometry geometry) { IGraphicsContainer pGraphicsContainer; IActiveView pActiveView; pGraphicsContainer = m_mapControl.ActiveView as IGraphicsContainer; pActiveView = m_mapControl.ActiveView; pGraphicsContainer.DeleteAllElements(); if (geometry == null) return; try {
geometry.Project(m_mapControl.Map.SpatialReference); IElement m_element = new LineElementClass(); m_element.Geometry = geometry; IRgbColor pColor; pColor = new RgbColorClass(); pColor.Red = 200; pColor.Blue = 20; pColor.Green = 0; ILineSymbol pOutline; pOutline = new SimpleLineSymbolClass(); pOutline.Width = 1.0; pOutline.Color = pColor; ISimpleLineSymbol pLineSymble = pOutline as ISimpleLineSymbol; pLineSymble.Style = esriSimpleLineStyle.esriSLSDot; ILineElement pFillshapeEle; pFillshapeEle = m_element as ILineElement; pFillshapeEle.Symbol = pOutline; m_element = pFillshapeEle as IElement; pGraphicsContainer.AddElement(m_element, 0); pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } catch (Exception err) {
// } }
e 工具的調用:
在系統菜單欄添加一個點擊按鈕,雙擊進入Click方法下,並用一下代碼調用剛剛實現的工具。
T_Tool test = new T_Tool(); test.OnCreate(m_mapControl.Object); m_mapControl.CurrentTool = test;
c# AE實現欄選功能