A4WaterUtilities.GeoNetTools.ConnectSelected C# (CSharp) Method

ConnectSelected() public static method

public static ConnectSelected ( IApplication app ) : void
app IApplication
return void
        public static void ConnectSelected(IApplication app)
        {
            IEditor editor = null;
            IEditLayers eLayers = null;

            IMap pMap = null;
            IActiveView av = null;
            try
            {
                editor = Globals.getEditor(ref app);

                if (editor.EditState != esriEditState.esriStateEditing)
                {
                    MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("MustBEditg"), A4LGSharedFunctions.Localizer.GetString("GeoNetToolsLbl_2"));
                    return;
                }
                eLayers = (IEditLayers)editor;

                pMap = editor.Map;
                av = pMap as IActiveView;

                if (pMap.SelectionCount > 0)
                {
                    ////If above threshold, prompt to cancel
                    //if ((map.SelectionCount > 1) &&
                    //   (MessageBox.Show("Are you sure you wish to connect the selected " + map.SelectionCount + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAsk_9b"), A4LGSharedFunctions.Localizer.GetString("Confirm"), System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No))
                    //  return;

                    bool test = false;
                    ESRI.ArcGIS.esriSystem.IStatusBar statusBar = null;
                    ESRI.ArcGIS.esriSystem.IAnimationProgressor animationProgressor = null;
                    ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel = null;
                    ESRI.ArcGIS.Framework.IProgressDialogFactory progressDialogFactory = null;
                    ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = null;
                    ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog2 = null;
                    IEnumLayer enumLayer = null;
                    IFeatureLayer fLayer = null;
                    IFeatureSelection fSel = null;
                    ILayer layer = null;
                    ICursor cursor = null;
                    IFeatureCursor fCursor = null;
                    UID geoFeatureLayerID = null;
                    INetworkClass netClass = null;
                    INetworkFeature netFeature = null;
                    IFeature feat = null;
                    int lyrCnt = 0;
                    try
                    {
                        // Create an edit operation enabling undo/redo
                        editor.StartOperation();

                        //Get list of feature layers
                        geoFeatureLayerID = new UIDClass();
                        geoFeatureLayerID.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}";
                        enumLayer = pMap.get_Layers(((ESRI.ArcGIS.esriSystem.UID)geoFeatureLayerID), true);

                        while ((layer = enumLayer.Next()) != null)
                        {
                            lyrCnt = lyrCnt + 1;

                        }
                        enumLayer.Reset();

                        statusBar = app.StatusBar;
                        animationProgressor = statusBar.ProgressAnimation;

                        animationProgressor.Show();
                        animationProgressor.Play(0, -1, -1);

                        statusBar.set_Message(0, A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_7"));

                        // Create a CancelTracker
                        trackCancel = new ESRI.ArcGIS.Display.CancelTrackerClass();

                        progressDialogFactory = new ESRI.ArcGIS.Framework.ProgressDialogFactoryClass();

                        // Set the properties of the Step Progressor
                        System.Int32 int32_hWnd = app.hWnd;
                        stepProgressor = progressDialogFactory.Create(trackCancel, int32_hWnd);
                        stepProgressor.MinRange = 0;
                        stepProgressor.MaxRange = lyrCnt - 1;
                        stepProgressor.StepValue = 1;
                        stepProgressor.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_7");

                        // Create the ProgressDialog. This automatically displays the dialog
                        progressDialog2 = (ESRI.ArcGIS.Framework.IProgressDialog2)stepProgressor; // Explict Cast

                        // Set the properties of the ProgressDialog
                        progressDialog2.CancelEnabled = true;
                        progressDialog2.Description = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsDesc_7");
                        progressDialog2.Title = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsTitle_7");
                        progressDialog2.Animation = ESRI.ArcGIS.Framework.esriProgressAnimationTypes.esriProgressGlobe;

                        // Step. Do your big process here.
                        bool boolean_Continue = false;
                        boolean_Continue = true;

                        // Step through each geofeature layer in the map
                        enumLayer.Reset();
                        while ((layer = enumLayer.Next()) != null)
                        {

                            // Verify that this is a valid, visible point layer and that this layer is editable
                            fLayer = (IFeatureLayer)layer;
                            if (fLayer.Valid && fLayer.Visible && eLayers.IsEditable(fLayer))
                            {
                                stepProgressor.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_7") + fLayer.Name;
                                // Verify selected features and network
                                netClass = fLayer.FeatureClass as INetworkClass;
                                fSel = (IFeatureSelection)fLayer;
                                if ((netClass != null) && (fSel.SelectionSet.Count > 0))
                                {
                                    test = true;

                                    fSel.SelectionSet.Search(null, false, out cursor);
                                    fCursor = cursor as IFeatureCursor;

                                    while ((netFeature = (INetworkFeature)fCursor.NextFeature()) != null)
                                    {
                                        try
                                        {
                                            netFeature.Connect();
                                        }
                                        catch
                                        { }
                                        //feat = (IFeature)netFeature;
                                        //int fieldPos = feat.Fields.FindField("ENABLED");

                                        //if (fieldPos > -1)
                                        //{
                                        //    feat.set_Value(fieldPos, feat.get_Value(fieldPos));
                                        //    feat.Store();
                                        //}
                                    }

                                    Marshal.ReleaseComObject(cursor);
                                    Marshal.ReleaseComObject(fCursor);
                                }
                            }
                            stepProgressor.Step();

                            boolean_Continue = trackCancel.Continue();
                            if (!boolean_Continue)
                            {
                                break;
                            }

                        }

                        // Stop the edit operation
                        editor.StopOperation(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_8"));
                    }

                    catch (Exception ex)
                    {
                        editor.AbortOperation();
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_8") + "\n" + ex.Message, ex.Source);
                        return;
                    }
                    finally
                    {
                        if (animationProgressor != null)
                        {
                            animationProgressor.Stop();
                            animationProgressor.Hide();
                        }
                        // Done
                        trackCancel = null;
                        stepProgressor = null;
                        if (progressDialog2 != null)
                        {
                            progressDialog2.HideDialog();
                        }

                        progressDialog2 = null;

                        statusBar = null;
                        animationProgressor = null;

                        progressDialogFactory = null;

                        enumLayer = null;
                        fLayer = null;
                        fSel = null;
                        layer = null;
                        if (cursor != null)
                            Marshal.ReleaseComObject(cursor);
                        if (fCursor != null)
                            Marshal.ReleaseComObject(fCursor);
                        cursor = null;
                        fCursor = null;
                        geoFeatureLayerID = null;
                        netClass = null;
                        netFeature = null;
                        feat = null;

                    }

                    //Alert the user know if no work was performed
                    if (!(test))
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_8a"), A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_8b"));
                    else
                        av.Refresh();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_8") + "\n" + ex.Message, ex.Source);
                return;
            }
            finally
            {

                editor = null;
                eLayers = null;

                pMap = null;
                av = null;
            }
        }