Yiman

Select features

Discussion created by Yiman on May 16, 2012
Latest reply on May 21, 2012 by SnoBrdinRTiste
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.");
            }
        }

Outcomes