• C# AE 实现点选框选/点击选择要素




    选择一个要素或者一个要素集(FeatureSelection)的方法很多,如IMap::SelectByShape、ILayer::search、IFeatureSection::SelectFeature等方法

    主要用到的方法

    方法一:

    IMap接口的SelectFeature(Layer, Feature) (方法,从一个Layer中选择一个Feature);

    方法二:

    IMap接口SelectByShape(Shape, env, justOne) (方法,从Layer中依靠一个图形的范围shape和一个选择的环境env来选择要素,而在所有图层中只从IFeatureLayer的图层中进行选择)

    方法三:

    IFeatureSelection接口SelectFeatures (Filter, Method, justOne ) (方法,根据指定的标准过滤器filter和方法,选择要素,第一个参数为QueryFilter类型的变量,第二个参数为esriSelectionResultEnum类型的变量,第三个参数为布尔型变量,通常为false)

    方法四:

    IFeatureLayer接口Search (IqueryFilter, book ) (方法,创建一个游标去查询相应设置的过滤器的查询)

    方法五:

    IGraphicsContainer 可以m_GraphicsContainer = m_MapControl.ActiveView.GraphicsContainer;然后重置遍历循环IElement element = m_GraphicsContainer.Next();

    具体代码

    方法二:

    IMap pMap = axMapControl1.Map;
    IActiveView pActiveView = pMap as IActiveView;
    IFeatureLayer pFeatureLayer = pMap.get_Layer(0) as IFeatureLayer;
    IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
    IFeatureSelection pFSelection = pFeatureLayer as IFeatureSelection;
    
    //设置点击点的位置
    IPoint point = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y);
    ITopologicalOperator pTOpo = point as ITopologicalOperator;
    double length;
    length = ConvertPixelsToMapUnits(pActiveView, 4);
    IGeometry pBuffer = pTOpo.Buffer(length);
    IGeometry pGeomentry = pBuffer.Envelope;
    ISelectionEnvironment pSelEnv = new SelectionEnvironment();//新建选择环境
    IRgbColor pColor = new RgbColor();
    pColor.Red = 255;
    pSelEnv.DefaultColor = pColor;//设置高亮颜色
    pMap.SelectByShape(pBuffer, pSelEnv, false);//选择图形
    _map.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);?

    -----------------------------------------------  分割线 --------------------------------------------------------------

     另一种使用方式

    看到的一篇文章转载过来:【原文传送门

     要素选择包括点选和拉框选择,拉框选择比较简单,直接用IMap.SelectByShape就可以了;

    点选则是根据鼠标点创建一个缓冲区,然后再SelectByShape方法。如果不想建缓冲区的话,直接根据鼠标点扩展成一个正方形框也可以。其实两者差别不是很大,但后者更简便。

    public override void OnMouseDown(int Button, int Shift, int X, int Y)
    {
      IEnvelope pEnv;
      IActiveView pActiveView = m_MapCtl.ActiveView;
      IMap pMap = m_MapCtl.Map;
      pEnv = m_MapCtl.TrackRectangle();
             //ISelectionEnvironment pSelectionEnv = new SelectionEnvironment(); //新建选择环境 补充
             //pSelectionEnv.DefaultColor = new RgbColor() { Red = 255, Green = 0, Blue = 0 };
    
      if ( pEnv.IsEmpty == true )                    //点选
      {
          tagRECT r;
          r.bottom = Y + 5;
          r.top = Y - 5;
          r.left = X - 5;
          r.right = X + 5;
         pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnv, ref r, 4);
          pEnv.SpatialReference = pActiveView.FocusMap.SpatialReference;               
      }
      pMap.SelectByShape(pEnv, m_SelectEnvir, false);
      pActiveView.Refresh();
      pEnv = null;
    }

    其实主要利用了DisplayTransformation.TransformRect方法,将屏幕范围转换成地图范围。

    上面这个方法可以用于点选和框选。

    方法三:

    可以用这个方法来放大图层。

    IFeatureCursor featureCursor = pFeatureLayer.Search(pQueryFilter, false);
    IFeature pFeature;
    IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
    pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
    axMapControl.ActiveView.Refresh();
    if ((pFeature = featureCursor.NextFeature()) != null)
    {
    ...
    }

    方法五:

    m_GraphicsContainer.Reset();
    IElement element = m_GraphicsContainer.Next();
    while (element != null)
    {
        //If the polygon contains the point
        if (relationalOperator.Contains(element.Geometry) == true)
        {
            //QI for IMarkerElement interface through IElement interface
            IMarkerElement markerElement = (IMarkerElement) element;
            markerElement.Symbol = GetMarkerSymbol(true);
            //QI for the IElementProperties interface through IElement interface
            IElementProperties elementProperties = (IElementProperties) element;
            elementProperties.Name = true.ToString();
        }
        element = m_GraphicsContainer.Next();
    }
    if (chkTracking.CheckState == CheckState.Unchecked)
        //Refresh the graphics
        m_MapControl.Refresh(esriViewDrawPhase.esriViewGraphics, Type.Missing, Type.Missing);

    ----------------------------------------------------   没错,我就是分割线~~ ------------------------------------------

    下面附上点选框选之“经典”的博客,嗬!【传送门啊啊啊】【开吧!远古....咳咳

    PS.

    不论是  pMap.SelectByShape(pEnv, pSelectionEnv, false);

    还是   pMap.SelectByShape(g, null, false);  (PEnv转Geometry)

    在多层要素重叠情况下,点选似乎都会遇到pEnv.depth引发异常,暂时不知道如何处理,但是可以忽略


  • 相关阅读:
    链式二叉树遍历具体程序
    非线性结构
    函数调用
    递归
    队列的操作
    MFC多线程详细讲解(转)
    PreTranslateMessage()函数捕获键盘按键消息
    vc和halcon数据的相互赋值
    VC6.0加载lib文件的三种方法
    MFC中按钮控件的用法笔记(转)
  • 原文地址:https://www.cnblogs.com/marvelousone/p/7234464.html
Copyright © 2020-2023  润新知