A4WaterUtilities.GeoNetTools.CheckConnections C# (CSharp) Method

CheckConnections() public static method

public static CheckConnections ( IApplication app, bool CheckVisibleOnly ) : void
app IApplication
CheckVisibleOnly bool
return void
        public static void CheckConnections(IApplication app, bool CheckVisibleOnly)
        {
            IProgressDialog2 progressDialog = default(IProgressDialog2);
            IGeometricNetwork geometricNetwork = null;
            ISelectionEvents selEvents = null;
            IMxDocument mxDoc = null;
            IActiveView activeView = null;
            IMap map = null;
            List<IGeometricNetwork> gnList = null;
            IMouseCursor appCursor = null;
            IEnumFeatureClass enumClass = null;
            IFeatureClass featureClass = null;
            IFeatureLayer featureLayer = null;
            IProgressDialogFactory progressDialogFactory = null;
            ITrackCancel trackCancel = null;
            IStepProgressor stepProgressor = null;
            IFeatureSelection fSel = null;
            IEnumFeature enumFeatures = null;
            IEditor editor = null;

            try
            {
                editor = Globals.getEditor(ref app);
                if (editor == null)
                    return;

                if (editor.EditState == esriEditState.esriStateNotEditing)
                {
                    MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("MustBEditg"), A4LGSharedFunctions.Localizer.GetString("GeoNetToolsLbl_2"));
                    return;
                }
                mxDoc = (IMxDocument)app.Document;
                activeView = (IActiveView)mxDoc.FocusMap;
                map = activeView.FocusMap;
                int countDeleted = 0;

                if (activeView == null) return;
                if (map.LayerCount == 0) return;
                long total = Globals.GetTotalVisibleNetworkFeatures(map);
                if (total > 1000)
                {
                    if (MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAsk_11a") + total + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAsk_11b"), A4LGSharedFunctions.Localizer.GetString("Proceed"), System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                    {
                        return;
                    }

                }
                string resultMessage = "";
                string resultMessage2 = "";

                //Get visible networks
                gnList = Globals.GetGeometricNetworksCheckedVisible(ref map);

                if (gnList.Count == 0)
                {
                    MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_11a"), A4LGSharedFunctions.Localizer.GetString("GeoNetToolsLbl_2"));
                    return;
                }

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

                //This step is required to avoid accidently deleting features
                if (map.SelectionCount > 0)
                {
                    activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                    map.ClearSelection();
                }

                int itotal = Convert.ToInt32(total);

                //ProgressBar
                progressDialogFactory = new ProgressDialogFactoryClass();

                // Create a CancelTracker

                trackCancel = new CancelTrackerClass();

                // Set the properties of the Step Progressor
                Int32 int32_hWnd = editor.Parent.hWnd;
                stepProgressor = progressDialogFactory.Create(trackCancel, int32_hWnd);
                stepProgressor.MinRange = 0;
                // stepProgressor.MaxRange = itotal
                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_11");
                progressDialog.Title = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsTitle_11");
                progressDialog.Animation = ESRI.ArcGIS.Framework.esriProgressAnimationTypes.esriProgressGlobe;

                for (int i = 0; i < gnList.Count; i++)
                {
                    geometricNetwork = gnList[i] as IGeometricNetwork;
                    if (geometricNetwork == null)
                    {
                        continue;
                    }
                    enumClass = null;
                    featureClass = null;
                    featureLayer = null;

                    int count = 0;

                    enumClass = geometricNetwork.get_ClassesByType(esriFeatureType.esriFTSimpleJunction);

                    //Find all 'disconnected junctions' in orphan junction layer (edges=0)
                    //and also junctions in orphan junction that are unneed because they connect only 2 edges of the same feature
                    // Do this first so we can safely delete them(if editing)
                    string junFCName = "";
                    featureClass = geometricNetwork.OrphanJunctionFeatureClass;
                    junFCName = featureClass.AliasName;
                    bool FCorLayer = true;
                    featureLayer = Globals.FindLayer((IMap)mxDoc.FocusMap, ((IDataset)featureClass).Name, ref FCorLayer) as IFeatureLayer;

                    if (featureLayer != null &&
                            (
                                (Globals.isVisible((ILayer)featureLayer, (IMap)mxDoc.FocusMap) && CheckVisibleOnly) ||
                                (CheckVisibleOnly == false)
                            )
                        )
                    {
                        count = Globals.SelectJunctions(featureLayer, (IGeometry)activeView.Extent, 0, "ORPHAN", ref progressDialog, ref stepProgressor, ref trackCancel);
                        if ((count > 0) && (editor != null) && (editor.EditState == esriEditState.esriStateEditing))
                        {
                            try
                            {
                                editor.StartOperation();
                                fSel = (IFeatureSelection)featureLayer;
                                enumFeatures = editor.EditSelection as IEnumFeature;
                                Globals.DeleteFeatures(enumFeatures);
                                editor.StopOperation(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsDone_11a") + featureLayer.Name);
                            }
                            catch (Exception ex)
                            {
                                editor.AbortOperation();
                                MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsLbl_11") + "\n" + ex.Message, ex.Source);
                            }
                            countDeleted = +count;
                            if (count == 1)
                            {
                                resultMessage2 += +count + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11a") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11b") + Environment.NewLine;
                            }
                            else if (count > 1)
                            {
                                resultMessage2 += count + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11c") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11d") + Environment.NewLine;
                            }
                        }
                        else
                        {
                            if (count == 1)
                            {
                                resultMessage2 += +count + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11a") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11e") + Environment.NewLine;
                            }
                            else if (count > 1)
                            {
                                resultMessage2 += count + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11c") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11f") + Environment.NewLine;
                            }
                        }
                    }

                    //Step through each feature layer
                    enumClass.Reset();
                    while ((featureClass = (IFeatureClass)enumClass.Next()) != null)
                    {

                        int numberJunctions;
                        count = 0;
                        // FCorLayer = true;
                        featureLayer = Globals.FindLayer((IMap)mxDoc.FocusMap, ((IDataset)featureClass).Name, ref FCorLayer) as IFeatureLayer;
                        if (featureLayer != null)
                        {

                            //Handle all non-orphan junction feature layers
                            if (junFCName != featureClass.AliasName && featureLayer != null && (
                                    (Globals.isVisible((ILayer)featureLayer, (IMap)mxDoc.FocusMap) && CheckVisibleOnly) ||
                                    (CheckVisibleOnly == false)
                                ))
                            {
                                numberJunctions = 1;
                                count = Globals.SelectJunctions(featureLayer, (IGeometry)activeView.Extent, numberJunctions, "LT", ref progressDialog, ref stepProgressor, ref trackCancel);

                                if (count == 1)
                                {
                                    //Add lookup to config to see how many junctions a FC should connect to
                                    resultMessage += count + A4LGSharedFunctions.Localizer.GetString("AssetFromThe") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11g") + Environment.NewLine;// "\r\n";
                                }

                                if (count > 1)
                                {
                                    //Add lookup to config to see how many junctions a FC should connect to
                                    resultMessage += count + A4LGSharedFunctions.Localizer.GetString("AssetsFromThe") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11h") + Environment.NewLine;// "\r\n";
                                }

                            }
                            //Handle orphan junction feature layers
                            else if (featureLayer != null && (
                                    (Globals.isVisible((ILayer)featureLayer, (IMap)mxDoc.FocusMap) && CheckVisibleOnly) ||
                                    (CheckVisibleOnly == false)
                                ))
                            {
                                numberJunctions = 0;
                                count = Globals.SelectJunctions(featureLayer, (IGeometry)activeView.Extent, numberJunctions, "EQ", ref progressDialog, ref stepProgressor, ref trackCancel);

                                if (count == 1)
                                {
                                    resultMessage += count + A4LGSharedFunctions.Localizer.GetString("JunctionInThe") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11i") + Environment.NewLine;// "\r\n";
                                }

                                if (count > 1)
                                {
                                    resultMessage += count + A4LGSharedFunctions.Localizer.GetString("JunctionsInThe") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11j") + Environment.NewLine;// "\r\n";
                                }

                                numberJunctions = 1;
                                count = Globals.SelectJunctions(featureLayer, (IGeometry)activeView.Extent, numberJunctions, "EQ", ref progressDialog, ref stepProgressor, ref trackCancel);

                                if (count == 1)
                                {
                                    resultMessage += count + A4LGSharedFunctions.Localizer.GetString("JunctionInThe") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11k") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAsk_11b") + Environment.NewLine;// "\r\n";
                                }

                                if (count > 1)
                                {
                                    resultMessage += count + A4LGSharedFunctions.Localizer.GetString("JunctionsInThe") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11l") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAsk_11b") + Environment.NewLine;// "\r\n";
                                }

                            }
                        }

                    }

                    //****

                    enumClass = geometricNetwork.get_ClassesByType(esriFeatureType.esriFTComplexEdge);

                    //Step through each feature layer
                    enumClass.Reset();
                    while ((featureClass = (IFeatureClass)enumClass.Next()) != null)
                    {
                        count = 0;
                        FCorLayer = true;
                        featureLayer = Globals.FindLayer((IMap)mxDoc.FocusMap, ((IDataset)featureClass).Name, ref  FCorLayer) as IFeatureLayer;
                        if (featureLayer != null)
                        {
                            //Handle all non-orphan junction feature layers
                            if ((Globals.isVisible((ILayer)featureLayer, (IMap)mxDoc.FocusMap) && CheckVisibleOnly) ||
                                    (CheckVisibleOnly == false))
                            {

                                count = Globals.SelectEdges(featureLayer, (IGeometry)activeView.Extent, ref progressDialog, ref stepProgressor, ref trackCancel, geometricNetwork.OrphanJunctionFeatureClass.ObjectClassID);
                                if (count == 1)
                                {
                                    //Add lookup to config to see how many junctions a FC should connect to
                                    resultMessage += count + A4LGSharedFunctions.Localizer.GetString("AssetFromThe") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11g") + Environment.NewLine;// "\r\n";
                                }

                                if (count > 1)
                                {
                                    //Add lookup to config to see how many junctions a FC should connect to
                                    resultMessage += count + A4LGSharedFunctions.Localizer.GetString("AssetsFromThe") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11h") + Environment.NewLine;// "\r\n";
                                }

                            }
                        }

                    }

                    enumClass = geometricNetwork.get_ClassesByType(esriFeatureType.esriFTSimpleEdge);

                    //Step through each feature layer
                    enumClass.Reset();
                    while ((featureClass = (IFeatureClass)enumClass.Next()) != null)
                    {

                        count = 0;
                        bool FCorLayerFeat = true;
                        featureLayer = Globals.FindLayer((IMap)mxDoc.FocusMap, ((IDataset)featureClass).Name, ref FCorLayerFeat) as IFeatureLayer;
                        if (featureLayer != null)
                        {

                            //Handle all non-orphan junction feature layers
                            if ((Globals.isVisible((ILayer)featureLayer, (IMap)mxDoc.FocusMap) && CheckVisibleOnly) ||
                                    (CheckVisibleOnly == false))
                            {

                                count = Globals.SelectEdges(featureLayer, (IGeometry)activeView.Extent, ref progressDialog, ref stepProgressor, ref trackCancel, geometricNetwork.OrphanJunctionFeatureClass.ObjectClassID);

                                if (count == 1)
                                {
                                    //Add lookup to config to see how many junctions a FC should connect to
                                    resultMessage += count + A4LGSharedFunctions.Localizer.GetString("AssetFromThe") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11g") + Environment.NewLine;// "\r\n";
                                }

                                if (count > 1)
                                {
                                    //Add lookup to config to see how many junctions a FC should connect to
                                    resultMessage += count + A4LGSharedFunctions.Localizer.GetString("AssetsFromThe") + featureLayer.Name + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_11h") + Environment.NewLine;// "\r\n";
                                }

                            }
                        }

                    }
                }

                //**

                selEvents = (ISelectionEvents)mxDoc.FocusMap;

                if (selEvents != null)
                {
                    selEvents.SelectionChanged();

                }

                if (countDeleted == 0)
                    activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                else
                    activeView.Refresh();

                resultMessage += resultMessage2;

                if (resultMessage == "")
                    resultMessage = A4LGSharedFunctions.Localizer.GetString("NoError");

                if (progressDialog != null)
                    progressDialog.HideDialog();
                //Report results
                System.Windows.Forms.MessageBox.Show(resultMessage, A4LGSharedFunctions.Localizer.GetString("GeoNetToolsTitle_11"), System.Windows.Forms.MessageBoxButtons.OK);

            }

            catch (Exception ex)
            {

                System.Windows.Forms.MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsTitle_11") + "\n" + ex.Message);
            }
            finally
            {
                // Cleanup
                if (progressDialog != null)
                {
                    progressDialog.HideDialog();

                    //progressDialog = null;
                    Marshal.ReleaseComObject(progressDialog);
                    //progressDialogFactory = null;
                    Marshal.ReleaseComObject(progressDialogFactory);
                    //trackCancel = null;
                    Marshal.ReleaseComObject(trackCancel);
                    //stepProgressor = null;
                    Marshal.ReleaseComObject(stepProgressor);
                    //appCursor = null;
                }
                if (appCursor != null)
                    Marshal.ReleaseComObject(appCursor);
                gnList = null;
                // Marshal.ReleaseComObject(gnList);
                //enumClass = null;
                if (enumClass != null)
                    Marshal.ReleaseComObject(enumClass);
                fSel = null;
                //Marshal.ReleaseComObject(fSel);
                enumFeatures = null;
                //Marshal.ReleaseComObject(enumFeatures);
                geometricNetwork = null;
                selEvents = null;
                mxDoc = null;
                activeView = null;
                map = null;

                featureClass = null;
                featureLayer = null;
                editor = null;

            }
        }