Select features

630
2
05-16-2012 03:52 AM
YimanSong
New Contributor III
I have select tool that goes through each TOC layer to find features matching the search criteria. It's very slow when there are a lot of layers in the TOC. Can anyone suggest a way to optimize the speed?

        private void cmdSelect_Click(object sender, EventArgs e)
        {
            IMxDocument pDoc = m_application.Document as IMxDocument;
            IMap pMap = pDoc.FocusMap;
            IActiveView pActiveView = pMap as IActiveView;

            IEnumLayer pEnumLayer = pDoc.FocusMap.Layers;
            ILayer currentLayer = pEnumLayer.Next();
            IFeatureLayer pFeatureLayer = null;

            //loop through layers in TOC and select features
            //Written to accomodate grouped layers 2 levels deep
            int numLayerSelected = 0;

            while (currentLayer != null)
            {
                if (currentLayer is IFeatureLayer)
                {
                    pFeatureLayer = currentLayer as IFeatureLayer;
                    SelectByAtt(pFeatureLayer, pActiveView);
                    numLayerSelected = numLayerSelected + IsSelectionEmpty(pActiveView, pFeatureLayer);
                    currentLayer = pEnumLayer.Next();
                    pFeatureLayer = null;
                }

                if (currentLayer is ICompositeLayer)
                {
                    ICompositeLayer level1Layer = currentLayer as ICompositeLayer;
                    int i;
                    for (i = 0; i < level1Layer.Count; i++)
                    {
                        if (level1Layer.get_Layer(i) is IFeatureLayer)
                        {
                            pFeatureLayer = level1Layer.get_Layer(i) as IFeatureLayer;
                            SelectByAtt(pFeatureLayer, pActiveView);
                            numLayerSelected = numLayerSelected + IsSelectionEmpty(pActiveView, pFeatureLayer);
                            currentLayer = pEnumLayer.Next();
                            pFeatureLayer = null;
                        }

                        if (level1Layer.get_Layer(i) is ICompositeLayer)
                        {
                            ICompositeLayer level2layers = level1Layer.get_Layer(i) as ICompositeLayer;
                            int j;
                            for (j = 0; j < level2layers.Count; j++)
                            {
                                if (level2layers.get_Layer(j) is IFeatureLayer)
                                {
                                    pFeatureLayer = level2layers.get_Layer(j) as IFeatureLayer;
                                    SelectByAtt(pFeatureLayer, pActiveView);
                                    numLayerSelected = numLayerSelected + IsSelectionEmpty(pActiveView, pFeatureLayer);
                                    currentLayer = pEnumLayer.Next();
                                    pFeatureLayer = null;
                                }
                            }
                        }
                    }
                }
            }


            pDoc.CurrentContentsView.Refresh(0);

            //Refresh attributes dialog
            ISelectionEvents pSelEvents = pMap as ISelectionEvents;
            if (pSelEvents != null)
            {
                pSelEvents.SelectionChanged();

                if (numLayerSelected != 0)
                {
                    if (chkZoom.Checked)
                    {
                        ZoomToSelected(pActiveView);
                    }
                    else
                    {
                        // do nothing
                    }
                }
                else
                {
                    MessageBox.Show("No feature is selected");
                }
            }

            else
            {
                MessageBox.Show("No features selected.");
            }
        }
0 Kudos
2 Replies
DuncanHornby
MVP Notable Contributor
Yiman,

Next time you upload your code please place it in the code (#) tags as it makes it easier to read. You've lost all your indentation... This is particularly important if the code is Python.
0 Kudos
DerekLoi
New Contributor III
I ran into this problem too and the way i did it was tun off the visibility of the map control temporarily so that it doesn't have to redraw anything... which in turn makes the process go a lot faster.
i'm not sure if this is the best way of going about it but it works 🙂
0 Kudos