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_displayFeedback m_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;