C#AE实现框选查询

mapControl.MousePointer = esriControlsMousePointer.esriPointerArrowQuestion;//设置鼠标样式

//添加鼠标事件
            mapControl.OnMouseDown += new AxESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEventHandler(EsriMapEvent.MapFrameSelection_OnMouseDown);

public static void MapFrameSelection_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            AxMapControl map = sender as AxMapControl;
            if (e.button == 1 && map.MousePointer == esriControlsMousePointer.esriPointerArrowQuestion)
            {
                Dictionary<string, DataTable> pairs = new Dictionary<string, DataTable>();//存储要素信息
                IEnvelope ipEnvfd = map.TrackRectangle();
                IGeometry pGeometry = ipEnvfd as IGeometry;
                //选中多个要素
                map.Map.SelectByShape(pGeometry, null, false);
                //
                map.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                //过滤器
                ISpatialFilter pSpatialFilter = new SpatialFilterClass();
                pSpatialFilter.Geometry = pGeometry;
                pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

                SelectionAttribute attribute = new SelectionAttribute();
                TreeList tree = attribute.GetTree();
                tree.Columns.Clear();
                TreeListColumn newColumn = tree.Columns.Add();
                newColumn.Caption = "图层";
                newColumn.Visible = true;
                //TreeList tree = new TreeList();
                for (int i = 0; i < map.LayerCount; i++)
                {
                    
                    if (map.get_Layer(i) is IGroupLayer)
                    {
                        TreeListNode treeNode = tree.Nodes.Add(new System.Object[] { map.get_Layer(i).Name, map.get_Layer(i) });
                        //查找图层组下的结点
                        GetGroupLayerNodes(map.get_Layer(i), treeNode, pSpatialFilter,ref pairs);

                    }
                    if (map.get_Layer(i) is IFeatureLayer)
                    {
                        IFeatureLayer pFeatureLayer = map.get_Layer(i) as IFeatureLayer;
                        IFeatureCursor pFeatureCursor = pFeatureLayer.Search(pSpatialFilter, false);
                        if (pFeatureCursor != null)
                        {
                            //添加图层节点
                            TreeListNode treeNode = tree.Nodes.Add(new System.Object[] { pFeatureLayer.Name, pFeatureLayer });
                            //获取该图层下的子节点
                            GetLayerNodes(pFeatureCursor, treeNode, ref pairs);
                        }
                    }
                }
                if (tree.Nodes.Count > 0)
                {
                    attribute.SetDictionary(pairs);
                    attribute.Expand();
                    attribute.Show();
                    //清除选择
                    map.Map.ClearSelection();
                }
            } 
        }

/// <summary>
        /// 查找图层组下所有子图层节点
        /// </summary>
        /// <param name="player"></param>
        /// <param name="midNode"></param>
        private static void GetGroupLayerNodes(ILayer player, TreeListNode midNode, ISpatialFilter pSpatialFilter,  ref Dictionary<string, DataTable> pairs)
        {
            ICompositeLayer midpos = player as ICompositeLayer;
            for (int i = 0; i < midpos.Count; i++)
            {
                if (midpos.get_Layer(i) is IGroupLayer)
                {
                    GetGroupLayerNodes(midpos.get_Layer(i), midNode, pSpatialFilter, ref pairs);
                }
                if(midpos.get_Layer(i) is IFeatureLayer)
                {
                    IFeatureLayer pFeatureLayer = midpos.get_Layer(i) as IFeatureLayer;
                    IFeatureCursor pFeatureCursor = pFeatureLayer.Search(pSpatialFilter, false);
                    if (pFeatureCursor != null)
                    {
                        //添加图层节点
                        TreeListNode treeNode = midNode.Nodes.Add(new System.Object[] { pFeatureLayer.Name, pFeatureLayer });
                        //获取该图层下的子节点
                        GetLayerNodes(pFeatureCursor, treeNode, ref pairs);
                    }
                }
            }
        }

/// <summary>
        /// 添加feature节点
        /// </summary>
        /// <param name="player"></param>
        /// <param name="midNode"></param>
        private static void GetLayerNodes(IFeatureCursor pFeatureCursor, TreeListNode midNode, ref Dictionary<string, DataTable> pairs)
        {
            IFeature pfeature = pFeatureCursor.NextFeature();
            while (pfeature != null)
            {
                //唯一值字段标识
                int OID =Convert.ToInt32(pfeature.Value[pfeature.Class.FindField(pfeature.Class.OIDFieldName)]);
                midNode.Nodes.Add(new System.Object[] { OID, pfeature });
                if (!pairs.ContainsKey(OID.ToString()))
                {
                    DataTable dt = GetFeatureDataTable(pfeature);
                    pfeature = pFeatureCursor.NextFeature();
                    string key = OID.ToString()+ midNode[0];
                    pairs.Add(key, dt);
                }
            }
        }

//SelectionAttribute为自己添加的窗体

 public partial class SelectionAttribute : Form
    {
        Dictionary<string, DataTable> pairs = new Dictionary<string, DataTable>();
        public SelectionAttribute()
        {
            InitializeComponent();
        }

        public void SetDataAttribute(string featureName)
        {
            DataTable dt = pairs[featureName];
            DataSelect.DataSource = dt;
        }

        public void SetDictionary(Dictionary<string, DataTable> pairs)
        {
            this.pairs = pairs;
        }

        public TreeList GetTree()
        {
            return SelectFeatureTree;
        }

        public void Expand()
        {
            SelectFeatureTree.ExpandAll();
        }

        private void SelectFeatureTree_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {

                //
                TreeList tree = sender as TreeList;
                //
                TreeListHitInfo hitInfo = tree.CalcHitInfo(e.Location);
                //获得点选的节点
                TreeListNode node = hitInfo.Node;
                string selectFeatureName = node[0].ToString();
                string key = selectFeatureName + node.ParentNode[0];
                node.TreeList.Appearance.BandPanel.BackColor = System.Drawing.Color.SteelBlue;
                if (pairs.ContainsKey(key))
                {
                    DataSelect.DataSource = pairs[key];
                }
            }
        }
    }

查询结果

猜你喜欢

转载自blog.csdn.net/qq_38370387/article/details/89145087
今日推荐