A4WaterUtilities.GeoNetTools.EstablishFlow C# (CSharp) Method

EstablishFlow() public static method

public static EstablishFlow ( Globals flowDirection, IApplication app ) : void
flowDirection Globals
app IApplication
return void
        public static void EstablishFlow(Globals.GNFlowDirection flowDirection, IApplication app)
        {
            IProgressDialog2 progressDialog = default(IProgressDialog2);
            IProgressDialogFactory progressDialogFactory = null;
            IEditor editor = null;
            IEditLayers eLayers = null;
            IMouseCursor appCursor = null;
            INetworkAnalysisExt netExt = null;
            UID pUID = null;
            IMap pMap = null;
            List<IGeometricNetwork> gnList = null;
            ITrackCancel trackCancel = null;
            Int32 int32_hWnd;
            IStepProgressor stepProgressor = null;
            IMxDocument mxdoc = null;
            try
            {

                int calcCount = 0;

                //Get editor

                editor = Globals.getEditor(ref app);

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

                eLayers = editor as IEditLayers;

                //Change mouse cursor to wait - automatically changes back (ArcGIS Desktop only)
                appCursor = new MouseCursorClass();
                appCursor.SetCursor(2);

                ESRI.ArcGIS.esriSystem.IStatusBar statusBar = app.StatusBar;
                statusBar.set_Message(0, A4LGSharedFunctions.Localizer.GetString("GeoNetToolsWait_1"));

                //Get NA Extension in order to update the current network with the first visible network
                pUID = new UIDClass();
                pUID.Value = "esriEditorExt.UtilityNetworkAnalysisExt";
                netExt = app.FindExtensionByCLSID(pUID) as INetworkAnalysisExt;

                //Get Visible geometric networks
                pMap = editor.Map;
                gnList = Globals.GetGeometricNetworksCurrentlyVisible(ref pMap);

                if (gnList.Count > 0)
                {

                    //ProgressBar
                    progressDialogFactory = new ProgressDialogFactoryClass();

                    // Create a CancelTracker
                    trackCancel = new CancelTrackerClass();

                    // Set the properties of the Step Progressor
                    int32_hWnd = app.hWnd;
                    stepProgressor = progressDialogFactory.Create(trackCancel, int32_hWnd);
                    stepProgressor.MinRange = 0;
                    stepProgressor.MaxRange = gnList.Count;
                    stepProgressor.StepValue = 1;
                    stepProgressor.Message = "";
                    stepProgressor.Hide();

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

                    // Set the properties of the ProgressDialog
                    progressDialog.CancelEnabled = false;
                    progressDialog.Description = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsDesc_1");
                    progressDialog.Title = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsTitle_1");
                    progressDialog.Animation = ESRI.ArcGIS.Framework.esriProgressAnimationTypes.esriProgressSpiral;

                    bool editStarted = false;
                    try
                    {// Create an edit operation enabling undo/redo
                        editor.StartOperation();
                        editStarted = true;
                    }
                    catch
                    {
                        editStarted = false;
                    }
                    IGeometricNetwork gn = null;
                    IEnumFeatureClass enumFC = null;
                    INetwork net = null;
                    IUtilityNetworkGEN unet = null;
                    IEnumNetEID edgeEIDs = null;
                    //IFeatureLayer fLayer = null;
                    try
                    {

                        for (int i = 0; i < gnList.Count; i++)
                        {

                            gn = gnList[i] as IGeometricNetwork;
                            // fLayer = Globals.FindLayerByFeatureClass(pMap, gn.OrphanJunctionFeatureClass, false);
                            //if (fLayer == null)
                            //{
                            //    MessageBox.Show("Unable to set flow direction for " + gn.FeatureDataset.Name + ".  Add the " + gn.OrphanJunctionFeatureClass.AliasName + " to your map and try again, if needed", "Establish Flow Direction");
                            //    stepProgressor.Step();
                            //    continue;
                            //}
                            //if (!eLayers.IsEditable(fLayer))
                            //{
                            //    MessageBox.Show("Unable to set flow direction for " + gn.FeatureDataset.Name + ".  It is visible but not editable.", "Establish Flow Direction");
                            //    stepProgressor.Step();
                            //    continue;
                            //}
                            stepProgressor.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_1");// +fLayer.Name;
                            //Establish flow using AncillaryRole values
                            if (flowDirection == Globals.GNFlowDirection.AncillaryRole)
                            {
                                if (editor.EditWorkspace.Equals(gn.FeatureDataset.Workspace))
                                {
                                    enumFC = gn.get_ClassesByNetworkAncillaryRole(esriNetworkClassAncillaryRole.esriNCARSourceSink);
                                    if (enumFC.Next() == null)
                                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_1a") + gn.FeatureDataset.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_1b") + Environment.NewLine +
                                                        A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_1c"), A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_2"));
                                    else
                                    {
                                        gn.EstablishFlowDirection();
                                        calcCount += 1;
                                    }
                                }

                            }

                            //Establish flow direction based on digitized direction.
                            else
                            {
                                net = gn.Network;
                                if (editor.EditWorkspace.Equals(gn.FeatureDataset.Workspace))
                                {

                                    unet = net as IUtilityNetworkGEN;
                                    edgeEIDs = net.CreateNetBrowser(esriElementType.esriETEdge);
                                    edgeEIDs.Reset(); int edgeEID;
                                    for (long j = 0; j < edgeEIDs.Count; j++)
                                    {
                                        edgeEID = edgeEIDs.Next();
                                        unet.SetFlowDirection(edgeEID, esriFlowDirection.esriFDWithFlow);
                                    }
                                    calcCount += 1;
                                }
                            }
                            stepProgressor.Step();

                        }
                    }

                    catch (Exception ex)
                    {
                        editor.AbortOperation();
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_2") + "\n" + ex.Message, ex.Source);
                    }
                    finally
                    {
                        if (enumFC != null)
                            Marshal.ReleaseComObject(enumFC);

                        gn = null;
                        enumFC = null;
                        net = null;
                        unet = null;
                        edgeEIDs = null;
                        //fLayer = null;
                    }
                    if (editStarted)
                    {   // Stop the edit operation
                        if (flowDirection == Globals.GNFlowDirection.AncillaryRole)
                            editor.StopOperation(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_2"));
                        else
                            editor.StopOperation(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_3"));
                    }
                    object Missing = Type.Missing;
                    mxdoc = app.Document as IMxDocument;
                    mxdoc.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Missing, mxdoc.ActiveView.Extent);

                    if (app != null)
                        app.StatusBar.set_Message(2, A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_1a") + calcCount + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_1b"));

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_2") + "\n" + ex.Message, ex.Source);
                return;

            }
            finally
            {
                // Cleanup
                if (progressDialog != null)
                    progressDialog.HideDialog();
                progressDialog = null;
                progressDialogFactory = null;
                editor = null;
                eLayers = null;
                appCursor = null;
                netExt = null;
                pUID = null;
                pMap = null;
                gnList = null;
                trackCancel = null;

                stepProgressor = null;
                mxdoc = null;
            }
        }