/// <summary>
        /// 设置障碍点
        /// </summary>
        /// <param name="pNetSolver"></param>
        /// <param name="breakPoints"></param>
        /// <param name="pGeometricNet"></param>
        /// <param name="pDisc"></param>
        private void SetBarries(INetSolver pNetSolver, List <IPoint> breakPoints, IGeometricNetwork pGeometricNet, double pDisc)
        {
            INetwork     pNetwork     = pGeometricNet.Network;
            INetElements pNetElements = pNetwork as INetElements;
            IPointToEID  pPointToEID  = new PointToEIDClass();

            pPointToEID.GeometricNetwork = pGeometricNet;
            pPointToEID.SnapTolerance    = pDisc;
            ISelectionSetBarriers barriers = new SelectionSetBarriersClass();

            foreach (var point in breakPoints)
            {
                //寻找障碍点最近的线要素,从而添加爆管线
                int    nearEdgeID;
                IPoint outIpoint;
                double precent;
                pPointToEID.GetNearestEdge(point, out nearEdgeID, out outIpoint, out precent);
                int userClassID;
                int userID;
                int userSubID;
                //查询相关ID
                pNetElements.QueryIDs(nearEdgeID, esriElementType.esriETJunction, out userClassID, out userID, out userSubID);
                //添加障碍点
                barriers.Add(userClassID, userID);
            }
            pNetSolver.SelectionSetBarriers = barriers;
        }
        public static string TraceIsolation(double[] x, double[] y, IApplication app, string sourceFLName, string valveFLName, string operableFieldNameValves, string operableFieldNameSources,
                                  double snapTol, bool processEvent, string[] opValues, string addSQL, bool traceIndeterminate, bool ZeroSourceCont, bool selectEdges, string MeterName, string MeterCritField, string MeterCritVal)
        {
            IMap map = null;

            List<int> valveFCClassIDs = new List<int>();

            IProgressDialogFactory pProDFact = null;
            IStepProgressor pStepPro = null;
            IProgressDialog2 pProDlg = null;
            ITrackCancel pTrkCan = null;

            int pointAlong = 0;

            List<IGeometricNetwork> gnList = null;
            int gnIdx;
            IGeometricNetwork gn = null;
            IPoint snappedPoint = null;
            int EID = -1;
            double distanceAlong;
            List<INetFlag> startNetFlag = null;
            List<IFlagDisplay> pFlagsDisplay = null;
            //Find feature classes
            string[] sourceFLs;
            IFeatureClass[] sourceFC = null;
            IFeatureLayer[] sourceFL = null;
            List<int> sourceFCClassIDs = new List<int>();

            string[] strValveFLs;
            IFeatureLayer pTempLay = null;
            List<IFeatureLayer> valveFLs = null;
            List<IFeatureClass> valveFCs = null;
            //IFeatureLayer meterFL = null;
            // string meterDSName;
            IJunctionFlag[] junctionFlag = null;
            IEdgeFlag[] edgeFlag = null;
            ITraceFlowSolverGEN traceFlowSolver = null;
            INetSolver netSolver = null;
            INetElementBarriersGEN netElementBarriers = null;

            INetElementBarriers nb = null;
            IEnumNetEID juncEIDs = null;
            IEnumNetEID edgeEIDs = null;
            IEIDInfo eidInfo = null;
            IEIDInfo valveEIDInfo = null;
            IEIDInfo sourceEIDInfo = null;
            IEIDInfo vEIDInfo = null;
            List<int[]> userIds = null;

            IEIDHelper eidHelper = null;
            List<Hashtable> valveEIDInfoHT = null;
            Hashtable sourceEIDInfoHT = null;
            System.Object[] segCosts = null;
            ISelectionSetBarriers netElementBarrier = null;

            List<IEdgeFlag> pEdgeFlags = null;
            List<IJunctionFlag> pJunctionFlags = null;
            //List<IEdgeFlag> pEdgeFlagsBar = null;
            //List<IJunctionFlag> pJunctionFlagsBar = null;
            INetElementBarriers pEdgeElementBarriers = null;
            INetElementBarriers pJunctionElementBarriers = null;
            ISelectionSetBarriers pSelectionSetBarriers = null;
            List<INetFlag> pNetFlags;
            //ITraceResult traceRes = null;
            IFlagDisplay pFlagDisplay = null;
            INetworkAnalysisExt pNetAnalysisExt = null;
            UID pID = null;

            IJunctionFlag[] junctionFlags = null;
            IEdgeFlag[] edgeFlags = null;

            Hashtable noSourceValveHT = null;
            Hashtable hasSourceValveHT = null;
            List<BarClassIDS> barrierIds = null;
            Hashtable sourceDirectEIDInfoHT = null;
            INetFlag netFlag1 = null;
            INetFlag netFlag2 = null;
            try
            {
                map = ((app.Document as IMxDocument).FocusMap);

                bool boolCont = true;
                if (processEvent)
                {
                    // Create a CancelTracker
                    pTrkCan = new CancelTrackerClass();
                    // Create the ProgressDialog. This automatically displays the dialog
                    pProDFact = new ProgressDialogFactoryClass();
                    pProDlg = (IProgressDialog2)pProDFact.Create(pTrkCan, 0);

                    // Set the properties of the ProgressDialog
                    pProDlg.CancelEnabled = true;

                    pProDlg.Description = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsDesc_16a");
                    pProDlg.Title = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsTitle_16a");

                    pProDlg.Animation = esriProgressAnimationTypes.esriProgressGlobe;

                    // Set the properties of the Step Progressor
                    pStepPro = (IStepProgressor)pProDlg;

                    pStepPro.MinRange = 0;
                    pStepPro.MaxRange = 18;
                    pStepPro.StepValue = 1;
                    pStepPro.Position = 0;
                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_4");

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_14a");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();

                    if (!boolCont)
                    {

                        pStepPro.Hide();
                        pProDlg.HideDialog();
                        pStepPro = null;
                        pProDlg = null;
                        pProDFact = null;
                        return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                    }
                }
                if (processEvent)
                {
                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_4");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }
                pointAlong++;

                gnList = Globals.GetGeometricNetworksCurrentlyVisible(ref map);
                gnIdx = -1;
                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_14a");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }
                pointAlong++;

                // Create junction or edge flag at start of trace - also returns geometric network, snapped point, and EID of junction

                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15a");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }

                startNetFlag = new List<INetFlag>();// null;// Globals.GetJunctionFlag(x, y, map, ref gnList, snapTol, ref gnIdx, out snappedPoint, out EID) as INetFlag;
                //if (startNetFlag == null)
                pFlagsDisplay = new List<IFlagDisplay>();
                if (x != null)
                {
                    for (int l = 0; l < x.Length; l++)
                    {
                        startNetFlag.Add(Globals.GetEdgeFlag(x[l], y[l], ref  map, ref gnList, snapTol, ref gnIdx, out snappedPoint, out EID, out distanceAlong, out pFlagDisplay, true) as INetFlag);
                        pFlagsDisplay.Add(pFlagDisplay);

                    }
                }
                pointAlong++;

                //Set network to trace
                if (gnIdx > -1)
                    gn = gnList[gnIdx] as IGeometricNetwork;

                if (app != null)
                {

                    pID = new UID();

                    pID.Value = "esriEditorExt.UtilityNetworkAnalysisExt";
                    pNetAnalysisExt = (INetworkAnalysisExt)app.FindExtensionByCLSID(pID);
                    if (gn != null)
                    {
                        Globals.SetCurrentNetwork(ref pNetAnalysisExt, ref gn);
                    }

                    traceFlowSolver = Globals.CreateTraceFlowSolverFromToolbar(ref pNetAnalysisExt, out pEdgeFlags, out pJunctionFlags, out pEdgeElementBarriers, out pJunctionElementBarriers, out pSelectionSetBarriers) as ITraceFlowSolverGEN;

                    gn = pNetAnalysisExt.CurrentNetwork;
                    netSolver = traceFlowSolver as INetSolver;

                }
                else
                {
                    if (gn == null || startNetFlag.Count == 0)
                    {

                        return A4LGSharedFunctions.Localizer.GetString("NoFlagReturnStatement");
                    }
                    traceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
                    netSolver = traceFlowSolver as INetSolver;

                    netSolver.SourceNetwork = gn.Network;

                }
                traceFlowSolver.TraceIndeterminateFlow = traceIndeterminate;
                pNetFlags = new List<INetFlag>();

                if (pEdgeFlags != null)
                {
                    foreach (IEdgeFlag pEdFl in pEdgeFlags)
                    {
                        pNetFlags.Add((INetFlag)pEdFl);

                    }
                }
                if (pJunctionFlags != null)
                {

                    foreach (IJunctionFlag pJcFl in pJunctionFlags)
                    {
                        pNetFlags.Add((INetFlag)pJcFl);
                    }
                }
                if (startNetFlag != null)
                {
                    if (startNetFlag.Count > 0)
                    {
                        foreach (INetFlag pNF in startNetFlag)
                        {
                            if (pNF != null)
                            {
                                pNetFlags.Add((INetFlag)pNF);
                            }
                        }
                    }

                    // pNetFlags.Add((INetFlag)startNetFlag);

                }
                if (pNetFlags.Count == 0)
                {
                    return A4LGSharedFunctions.Localizer.GetString("AddFlagOrClickReturnStatement");

                }

                // Stop if user point was not on a visible network feature, old trace results and selection are cleared
                if (gn == null || pNetFlags.Count == 0)
                {

                    return A4LGSharedFunctions.Localizer.GetString("NotIntersectReturnStatement");
                }
                pointAlong++;
                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16a");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }
                //Find feature classes
                sourceFLs = sourceFLName.Split('|');
                sourceFC = new IFeatureClass[sourceFLs.Length];
                sourceFL = new IFeatureLayer[sourceFLs.Length];

                for (int i = 0; i < sourceFLs.Length; i++)
                {
                    bool FCorLayerTemp = true;
                    sourceFL[i] = (IFeatureLayer)Globals.FindLayer(map, sourceFLs[i], ref FCorLayerTemp);
                    if (sourceFL[i] != null)
                    {
                        sourceFC[i] = sourceFL[i].FeatureClass;
                        sourceFCClassIDs.Add(sourceFL[i].FeatureClass.FeatureClassID);
                    }

                }
                pointAlong++;
                //                IFeatureClass sourceFC = (IFeatureClass)Globals.GetFeatureClassFromGeometricNetwork(sourceFCName, gn, esriFeatureType.esriFTSimpleJunction);
                // IFeatureClass valveFC = (IFeatureClass)Globals.GetFeatureClassFromGeometricNetwork(valveFCName, gn, esriFeatureType.esriFTSimpleJunction);
                strValveFLs = valveFLName.Split('|');

                valveFLs = new List<IFeatureLayer>();//(IFeatureLayer)Globals.FindLayer(map, valveFLName);
                valveFCs = new List<IFeatureClass>();//[strValveFLs.Length];

                for (int i = 0; i < strValveFLs.Length; i++)
                {
                    bool FCorLayerTemp = true;
                    pTempLay = (IFeatureLayer)Globals.FindLayer(map, strValveFLs[i], ref FCorLayerTemp);
                    if (pTempLay != null)
                    {

                        if (pTempLay.FeatureClass != null)
                        {
                            valveFLs.Add(pTempLay);
                            valveFCs.Add(pTempLay.FeatureClass);
                            valveFCClassIDs.Add(pTempLay.FeatureClass.FeatureClassID);
                        }
                    }

                }
                //  string strMeterFL = meterFLName;

                //meterFL = (IFeatureLayer)Globals.FindLayer(map, meterFLName);
                //meterDSName = "";
                //if (meterFL != null)
                //{
                //    if (meterFL is IDataset)
                //    {
                //        IDataset pTempDataset = (IDataset)meterFL;
                //        meterDSName = pTempDataset.BrowseName;
                //        if (meterDSName.Contains("."))
                //        {
                //            meterDSName = meterDSName.Substring(meterDSName.LastIndexOf(".") + 1);

                //        }
                //        Marshal.ReleaseComObject(pTempDataset);

                //        pTempDataset = null;
                //    }

                //}
                //meterFL = null;

                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16a");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }
                pointAlong++;
                if (valveFCs == null)
                {
                    if (processEvent)
                    {

                        pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15d") + valveFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15e") + Environment.NewLine +
                            A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15f");
                        pStepPro.Step();
                        boolCont = pTrkCan.Continue();
                    }

                    if (!boolCont)
                    {

                        pStepPro.Hide();
                        pProDlg.HideDialog();
                        pStepPro = null;
                        pProDlg = null;
                        pProDFact = null;
                        return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement"); ;
                    }
                    return A4LGSharedFunctions.Localizer.GetString("LambdaReturnStatement") + valveFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15e") + Environment.NewLine +
                            A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15f");
                }
                pointAlong++;
                if (valveFCs.Count == 0)
                {
                    if (processEvent)
                    {

                        pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15d") + valveFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15e") + Environment.NewLine +
                            A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15f");
                        pStepPro.Step();
                        boolCont = pTrkCan.Continue();
                    }

                    if (!boolCont)
                    {

                        pStepPro.Hide();
                        pProDlg.HideDialog();
                        pStepPro = null;
                        pProDlg = null;
                        pProDFact = null;
                        return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                    }
                    return A4LGSharedFunctions.Localizer.GetString("LambdaReturnStatement") + valveFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15e") + Environment.NewLine +
                            A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15f");
                }

                if (sourceFC == null)
                {
                    if (processEvent)
                    {

                        pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15d") + sourceFLs + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15e") + Environment.NewLine +
                            A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15f");
                        pStepPro.Step();
                        boolCont = pTrkCan.Continue();
                    }

                    if (!boolCont)
                    {

                        pStepPro.Hide();
                        pProDlg.HideDialog();
                        pStepPro = null;
                        pProDlg = null;
                        pProDFact = null;
                        return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                    }
                    return A4LGSharedFunctions.Localizer.GetString("LambdaReturnStatement") + sourceFLs + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15e") + Environment.NewLine +
                            A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15f");
                }
                pointAlong++;
                if (sourceFC.Length == 0)
                {
                    if (processEvent)
                    {

                        pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15d") + sourceFLs + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15e") + Environment.NewLine +
                            A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15f");
                        pStepPro.Step();
                        boolCont = pTrkCan.Continue();
                    }

                    if (!boolCont)
                    {

                        pStepPro.Hide();
                        pProDlg.HideDialog();
                        pStepPro = null;
                        pProDlg = null;
                        pProDFact = null;
                        return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                    }
                    return A4LGSharedFunctions.Localizer.GetString("LambdaReturnStatement") + sourceFLs + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15e") + Environment.NewLine +
                            A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15f");
                }

                pointAlong++;

                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16b");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }

                Globals.AddFlagsToTraceSolver(pNetFlags.ToArray(), ref traceFlowSolver, out junctionFlags, out edgeFlags);

                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16c");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }

                //Create barriers based on all operable valves
                pointAlong++;
                netElementBarriers = new NetElementBarriersClass() as INetElementBarriersGEN;
                netElementBarriers.ElementType = esriElementType.esriETJunction;
                netElementBarriers.Network = gn.Network;

                userIds = Globals.GetOperableValveOIDs(valveFCs.ToArray(), operableFieldNameValves, opValues, addSQL);
                if (userIds == null)
                {
                    if (processEvent)
                    {

                        pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16d");
                        pStepPro.Step();
                        boolCont = pTrkCan.Continue();
                    }

                    if (!boolCont)
                    {

                        pStepPro.Hide();
                        pProDlg.HideDialog();
                        pStepPro = null;
                        pProDlg = null;
                        pProDFact = null;
                        return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                    }
                }

                else
                {
                    if (userIds.Count == 0)
                    {
                        if (processEvent)
                        {

                            pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16d");
                            pStepPro.Step();
                            boolCont = pTrkCan.Continue();
                        }

                        if (!boolCont)
                        {

                            pStepPro.Hide();
                            pProDlg.HideDialog();
                            pStepPro = null;
                            pProDlg = null;
                            pProDFact = null;
                            return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                        }
                    }
                    else
                    {
                        try
                        {
                            int idxUser = 0;
                            if (processEvent)
                            {

                                pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16e");
                                pStepPro.Step();
                                boolCont = pTrkCan.Continue();
                            }

                            if (!boolCont)
                            {

                                pStepPro.Hide();
                                pProDlg.HideDialog();
                                pStepPro = null;
                                pProDlg = null;
                                pProDFact = null;
                                return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                            }
                            foreach (IFeatureClass valveFC in valveFCs)
                            {
                                int[] usrid = userIds[idxUser];
                                if (usrid.Length > 0)
                                {
                                    netElementBarriers.SetBarriers(valveFC.FeatureClassID, ref usrid);  //error here after sum
                                    nb = netElementBarriers as INetElementBarriers;
                                    netSolver.set_ElementBarriers(esriElementType.esriETJunction, nb);
                                }
                                idxUser++;
                            }
                        }
                        catch (Exception Ex)
                        {
                            if (processEvent)
                            {

                                pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_16a") + Ex.Message;
                                pStepPro.Step();
                                boolCont = pTrkCan.Continue();
                                return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                            }

                            if (!boolCont)
                            {

                                pStepPro.Hide();
                                pProDlg.HideDialog();
                                pStepPro = null;
                                pProDlg = null;
                                pProDFact = null;
                                return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                            }
                        }
                    }
                }
                pointAlong++;
                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16f");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }

                try
                {
                    traceFlowSolver.FindFlowEndElements(esriFlowMethod.esriFMConnected, esriFlowElements.esriFEJunctionsAndEdges, out juncEIDs, out edgeEIDs);
                }
                catch
                {
                    juncEIDs = null;
                    edgeEIDs = null;
                    if (processEvent)
                    {

                        pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("ErrorInThe") + "FindFlowEndElements";
                        pStepPro.Step();
                        boolCont = pTrkCan.Continue();
                    }

                    if (!boolCont)
                    {

                        pStepPro.Hide();
                        pProDlg.HideDialog();
                        pStepPro = null;
                        pProDlg = null;
                        pProDFact = null;
                        return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                    }
                    return A4LGSharedFunctions.Localizer.GetString("DontFindFlowEltReturnStatement");
                    //MessageBox.Show("Error in the FindFlowEndElements");
                }
                pointAlong++;
                eidHelper = new EIDHelperClass();
                eidHelper.GeometricNetwork = gn;
                eidHelper.ReturnFeatures = true;

                //Save valves which stopped the trace
                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16g");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }
                pointAlong++;

                valveEIDInfoHT = new List<Hashtable>();
                int totalreachcount = 0;
                foreach (IFeatureClass valveFC in valveFCs)
                {
                    if (valveFC != null)
                    {
                        Hashtable valveEIDInfoHTtemp = Globals.GetEIDInfoListByFC(valveFC.FeatureClassID, juncEIDs, eidHelper);
                        totalreachcount = totalreachcount + valveEIDInfoHTtemp.Count;
                        valveEIDInfoHT.Add(valveEIDInfoHTtemp);
                    }

                }
                if (totalreachcount == 0)
                {
                    if (processEvent)
                    {

                        pStepPro.Message = (A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16a") + valveFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15c") + Environment.NewLine +
                            Environment.NewLine +
                            A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16b") + valveFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16c") + Environment.NewLine +
                            A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16d") + Environment.NewLine +
                            A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16e"));
                        pStepPro.Step();
                        boolCont = pTrkCan.Continue();
                    }

                    if (!boolCont)
                    {

                        pStepPro.Hide();
                        pProDlg.HideDialog();
                        pStepPro = null;
                        pProDlg = null;
                        pProDFact = null;
                        return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                    }

                    return A4LGSharedFunctions.Localizer.GetString("Error") + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16a") + valveFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15c") + Environment.NewLine +
                            Environment.NewLine +
                            A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16b") + valveFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16c") + Environment.NewLine +
                            A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16d") + Environment.NewLine +
                            A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16e");
                }
                pointAlong++;
                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16h");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }

                traceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
                traceFlowSolver.TraceIndeterminateFlow = traceIndeterminate;
                netSolver = traceFlowSolver as INetSolver;

                netSolver.SourceNetwork = gn.Network;
                //Globals.AddFlagsToTraceSolver(startNetFlag.ToArray(), ref traceFlowSolver, out junctionFlag, out edgeFlag);
                Globals.AddFlagsToTraceSolver(pNetFlags.ToArray(), ref traceFlowSolver, out junctionFlag, out edgeFlag);
                Globals.AddBarriersToSolver(ref traceFlowSolver, ref pEdgeElementBarriers, ref pJunctionElementBarriers, ref pSelectionSetBarriers);

                pointAlong++;
                foreach (int sFC in sourceFCClassIDs)
                {
                    netSolver.DisableElementClass(sFC);
                }

                traceFlowSolver.FindFlowEndElements(esriFlowMethod.esriFMConnected, esriFlowElements.esriFEJunctions, out juncEIDs, out edgeEIDs);
                pointAlong++;
                //Save sources which are reachable
                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16i");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }

                sourceEIDInfoHT = new Hashtable();

                //foreach (IFeatureClass sFC in sourceFC)
                //{
                Globals.GetEIDInfoListByFCWithHT(ref sourceEIDInfoHT, sourceFCClassIDs, operableFieldNameSources, opValues, juncEIDs, eidHelper);

                //}
                pointAlong++;
                if (sourceEIDInfoHT.Count == 0)
                {
                    if (processEvent)
                    {

                        pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("No") + sourceFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15c") + Environment.NewLine +
                         Environment.NewLine +
                         A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16b") + sourceFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16c") + Environment.NewLine +
                         A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16j");
                        pStepPro.Step();
                        boolCont = pTrkCan.Continue();
                    }

                    if (!boolCont)
                    {

                        pStepPro.Hide();
                        pProDlg.HideDialog();
                        pStepPro = null;
                        pProDlg = null;
                        pProDFact = null;
                        return null;
                    }
                    return A4LGSharedFunctions.Localizer.GetString("Error") + A4LGSharedFunctions.Localizer.GetString("No") + sourceFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15c") + Environment.NewLine +
                         Environment.NewLine +
                         A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16b") + sourceFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16c") + Environment.NewLine +
                         A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16j");
                }
                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16k");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return null;
                }

                traceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
                traceFlowSolver.TraceIndeterminateFlow = traceIndeterminate;
                netSolver = traceFlowSolver as INetSolver;
                netSolver.SourceNetwork = gn.Network;
                //Globals.AddFlagsToTraceSolver(startNetFlag.ToArray(), ref traceFlowSolver, out junctionFlag, out edgeFlag);
                Globals.AddFlagsToTraceSolver(pNetFlags.ToArray(), ref traceFlowSolver, out junctionFlag, out edgeFlag);
                Globals.AddBarriersToSolver(ref traceFlowSolver, ref pEdgeElementBarriers, ref pJunctionElementBarriers, ref pSelectionSetBarriers);

                pointAlong++;
                //Set the barriers in the network based on the saved valves
                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16l");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }

                //Run the trace to find directly reachable sources (without passing valve)
                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16m");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }
                netElementBarrier = new SelectionSetBarriersClass();
                totalreachcount = 0;
                foreach (Hashtable HTentry in valveEIDInfoHT)
                {
                    foreach (DictionaryEntry entry in HTentry)
                    {
                        eidInfo = entry.Value as IEIDInfo;
                        netElementBarrier.Add(eidInfo.Feature.Class.ObjectClassID, eidInfo.Feature.OID);
                    }
                    totalreachcount++;
                }
                pointAlong++;
                netSolver.SelectionSetBarriers = netElementBarrier;
                pointAlong++;
                Globals.AddBarriersToSolver(ref traceFlowSolver, ref pEdgeElementBarriers, ref pJunctionElementBarriers, ref pSelectionSetBarriers);

                traceFlowSolver.FindFlowElements(esriFlowMethod.esriFMConnected, esriFlowElements.esriFEJunctionsAndEdges, out juncEIDs, out edgeEIDs);
                // Hashtable sourceDirectEIDInfoHT = Globals.GetEIDInfoListByFC(sourceFC.FeatureClassID, juncEIDs, eidHelper);
                sourceDirectEIDInfoHT = new Hashtable();

                Globals.GetEIDInfoListByFCWithHT(ref sourceDirectEIDInfoHT, sourceFCClassIDs, operableFieldNameSources, opValues, juncEIDs, eidHelper);

                //Remove directly reachable sources from source array
                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16n");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }
                pointAlong++;
                foreach (DictionaryEntry entry in sourceDirectEIDInfoHT)
                {
                    eidInfo = entry.Value as IEIDInfo;
                    sourceEIDInfoHT.Remove(eidInfo.Feature.OID);
                }
                pointAlong++;//21
                if (sourceEIDInfoHT.Count == 0)
                {
                    if (processEvent)
                    {

                        pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("No") + sourceFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16o") + Environment.NewLine +
                         Environment.NewLine +
                         A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16b") + sourceFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16c") + Environment.NewLine +
                         A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16j");
                        pStepPro.Step();
                        boolCont = pTrkCan.Continue();
                    }

                    if (!boolCont)
                    {

                        pStepPro.Hide();
                        pProDlg.HideDialog();
                        pStepPro = null;
                        pProDlg = null;
                        pProDFact = null;
                        return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                    }
                    return A4LGSharedFunctions.Localizer.GetString("Error") + A4LGSharedFunctions.Localizer.GetString("No") + sourceFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16o") + Environment.NewLine +
                         Environment.NewLine +
                         A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16b") + sourceFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_16c") + Environment.NewLine +
                         A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16j");
                }
                pointAlong++;//22
                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16p");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }

                noSourceValveHT = new Hashtable();
                hasSourceValveHT = new Hashtable();

                // IEIDInfo vEIDInfo;
                barrierIds = null;

                bool foundSource;
                // ArrayList barrierArrayList;
                pointAlong++;
                totalreachcount = 0;
                foreach (Hashtable valveHT in valveEIDInfoHT)
                {
                    foreach (DictionaryEntry valveEntry in valveHT)
                    {

                        foundSource = false;
                        valveEIDInfo = valveEntry.Value as IEIDInfo;

                        //Create array of all isolation valves excluding the current one
                        // barrierArrayList = new ArrayList();
                        barrierIds = new List<BarClassIDS>();

                        foreach (Hashtable valveHTBar in valveEIDInfoHT)
                        {
                            BarClassIDS barClID = new BarClassIDS();
                            List<int> tempIntArr = new List<int>();

                            foreach (DictionaryEntry valveEntryBar in valveHTBar)
                            {
                                vEIDInfo = valveEntryBar.Value as IEIDInfo;

                                barClID.ClassID = vEIDInfo.Feature.Class.ObjectClassID;

                                vEIDInfo = valveEntryBar.Value as IEIDInfo;
                                if (vEIDInfo.Feature.OID == valveEIDInfo.Feature.OID && vEIDInfo.Feature.Class.ObjectClassID == valveEIDInfo.Feature.Class.ObjectClassID)
                                {

                                }
                                else
                                {

                                    //  barrierArrayList.Add(vEIDInfo.Feature.OID);
                                    tempIntArr.Add(vEIDInfo.Feature.OID);
                                    //barrierIds.Add(vEIDInfo.Feature.OID);
                                }

                            }
                            barClID.IDs = tempIntArr.ToArray();
                            barrierIds.Add(barClID);
                        }
                        //if (valveHT.Count > 1)
                        //{

                        //    barrierArrayList = new ArrayList();
                        //    barrierIds = new int[valveHT.Count - 1];
                        //    foreach (DictionaryEntry v in valveHT)
                        //    {
                        //        vEIDInfo = v.Value as IEIDInfo;
                        //        if (vEIDInfo.Feature.OID != valveEIDInfo.Feature.OID)
                        //        {
                        //            barrierArrayList.Add(vEIDInfo.Feature.OID);
                        //        }
                        //    }
                        //    barrierArrayList.CopyTo(barrierIds);
                        //}
                        //else
                        //    barrierArrayList = null;

                        pointAlong++;
                        //For each source, attempt to trace

                        foreach (DictionaryEntry sourceEntry in sourceEIDInfoHT)
                        {

                            sourceEIDInfo = sourceEntry.Value as IEIDInfo;

                            //Setup trace to test each valve
                            traceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
                            traceFlowSolver.TraceIndeterminateFlow = traceIndeterminate;
                            netSolver = traceFlowSolver as INetSolver;
                            netSolver.SourceNetwork = gn.Network;
                            Globals.AddBarriersToSolver(ref traceFlowSolver, ref pEdgeElementBarriers, ref pJunctionElementBarriers, ref pSelectionSetBarriers);

                            //Set the first junction flag for path finding based this current valve
                            netFlag1 = new JunctionFlagClass();
                            netFlag1.UserClassID = valveEIDInfo.Feature.Class.ObjectClassID;
                            netFlag1.UserID = valveEIDInfo.Feature.OID;
                            netFlag1.UserSubID = 0;
                            netFlag1.Label = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsLbl_16a");
                            //AddFlagToTraceSolver(netFlag1, ref traceFlowSolver, out junctionFlag, out edgeFlag);

                            //Set the second (and last) trace flag at this source
                            netFlag2 = new JunctionFlagClass();
                            netFlag2.UserClassID = sourceEIDInfo.Feature.Class.ObjectClassID;
                            netFlag2.UserID = sourceEIDInfo.Feature.OID;
                            netFlag2.UserSubID = 0;
                            netFlag2.Label = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsLbl_16b");

                            Globals.AddTwoJunctionFlagsToTraceSolver(ref traceFlowSolver, netFlag1, netFlag2);

                            //Set as isolation valves (except the current one) as barriers
                            if (barrierIds != null && barrierIds.Count > 0)
                            {
                                netElementBarriers = new NetElementBarriersClass() as INetElementBarriersGEN;
                                netElementBarriers.ElementType = esriElementType.esriETJunction;
                                netElementBarriers.Network = gn.Network;
                                bool setBar = false;
                                foreach (BarClassIDS tempBarIDS in barrierIds)
                                {
                                    if (tempBarIDS.IDs.Length > 0)
                                    {
                                        int[] barIDs = tempBarIDS.IDs;

                                        netElementBarriers.SetBarriers(tempBarIDS.ClassID, ref barIDs);
                                        setBar = true;
                                    }
                                }
                                if (setBar)//required, it would produce an error if there where no other barriers
                                {
                                    nb = netElementBarriers as INetElementBarriers;
                                    netSolver.set_ElementBarriers(esriElementType.esriETJunction, nb);
                                }
                            }

                            //Run trace
                            segCosts = new System.Object[1];
                            segCosts[0] = new System.Object(); edgeEIDs = null;
                            traceFlowSolver.FindPath(esriFlowMethod.esriFMConnected, esriShortestPathObjFn.esriSPObjFnMinSum, out juncEIDs, out edgeEIDs, 1, ref segCosts);
                            if (edgeEIDs != null && edgeEIDs.Count > 0)
                            {
                                foundSource = true;
                                break;
                            }

                        } // End of source loop
                        pointAlong++;//25 -30ish
                        if (foundSource)
                        {
                            hasSourceValveHT.Add(valveEIDInfo.Feature.OID, valveEIDInfo);//valveEIDInfo.Feature.Class.ObjectClassID +":" +
                        }
                        else
                        {
                            noSourceValveHT.Add(valveEIDInfo.Feature.OID, valveEIDInfo);
                        }

                    } // End of valve loop
                    totalreachcount++;
                }
                //Setup last trace with correct valve barriers

                if (hasSourceValveHT.Count == 0)
                {
                    if (ZeroSourceCont)
                        hasSourceValveHT = noSourceValveHT;
                    else
                        return A4LGSharedFunctions.Localizer.GetString("NoWaterSourceIdentifiedReturnStatement");

                }
                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_16q");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }
                pointAlong++;
                traceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
                traceFlowSolver.TraceIndeterminateFlow = traceIndeterminate;
                netSolver = traceFlowSolver as INetSolver;
                netSolver.SourceNetwork = gn.Network;

                //Globals.AddFlagsToTraceSolver(startNetFlag.ToArray(), ref traceFlowSolver, out junctionFlag, out edgeFlag);
                Globals.AddFlagsToTraceSolver(pNetFlags.ToArray(), ref traceFlowSolver, out junctionFlag, out edgeFlag);
                Globals.AddBarriersToSolver(ref traceFlowSolver, ref pEdgeElementBarriers, ref pJunctionElementBarriers, ref pSelectionSetBarriers);

                //Set the barriers in the network based on the saved valves
                netElementBarrier = new SelectionSetBarriersClass();
                foreach (DictionaryEntry entry in hasSourceValveHT)
                {
                    eidInfo = entry.Value as IEIDInfo;

                    //netElementBarrier.Add(valveFC.FeatureClassID, eidInfo.Feature.OID);
                    netElementBarrier.Add(((IFeatureClass)eidInfo.Feature.Class).FeatureClassID, eidInfo.Feature.OID);
                }
                netSolver.SelectionSetBarriers = netElementBarrier;

                pointAlong++;
                //Run last trace
                traceFlowSolver.FindFlowElements(esriFlowMethod.esriFMConnected, esriFlowElements.esriFEJunctionsAndEdges, out juncEIDs, out edgeEIDs);
                //skipped valves =>  //Hashtable skippedValvesEIDInfoHT = GetEIDInfoListByFC(valveFC.FeatureClassID, juncEIDs, eidHelper);

                //Select junction features
                pointAlong++; //51,44

                //Open identify dialog with selected features
                //IdentifySelected(map);
                if (processEvent)
                {

                    pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("Complete");
                    pStepPro.Step();
                    boolCont = pTrkCan.Continue();
                }

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return A4LGSharedFunctions.Localizer.GetString("CanceledReturnStatement");
                }

                if (snappedPoint != null)
                {
                    snappedPoint.Project(map.SpatialReference);
                    // traceRes.TracePoint = snappedPoint;

                }
                // return "Post1";

                //Globals.LoadJunctions(ref traceRes, ref gn, ref map, ref juncEIDs, ref meterDSName);
                // Globals.LoadValves(ref traceRes, ref gn, ref map, ref hasSourceValveHT);

                //Globals.LoadEdges(ref traceRes, ref gn, ref map, ref edgeEIDs);
                //((IMxDocument)app.Document).FocusMap.ClearSelection();
                //Globals.RemoveGraphics(((IMxDocument)app.Document).FocusMap, false);
                string returnVal = "";
                returnVal = Globals.SelectJunctions(ref map, ref gn, ref juncEIDs, ref junctionFlag, MeterName, MeterCritField, MeterCritVal, processEvent);
                if (processEvent)
                {
                    if (selectEdges)
                        Globals.SelectEdges(ref map, ref  gn, ref edgeEIDs);
                    else
                        Globals.DrawEdges(ref map, ref  gn, ref edgeEIDs);
                }
                returnVal = Globals.SelectValveJunctions(ref map, ref hasSourceValveHT, ref valveFLs, processEvent) + "_" + returnVal;

                if (processEvent)
                {
                    if (pNetAnalysisExt != null)
                    {
                        foreach (IFlagDisplay pFgDi in pFlagsDisplay)
                        {
                            Globals.AddFlagToGN(ref pNetAnalysisExt, ref gn, pFgDi);

                            // Globals.AddPointGraphic(map, pFgDi.Geometry as IPoint, false);
                        }
                    }
                    else
                    {
                        foreach (IFlagDisplay pFgDi in pFlagsDisplay)
                        {
                            //  Globals.AddFlagToGN(ref pNetAnalysisExt, ref gn,  pFgDi);

                            Globals.AddPointGraphic(map, pFgDi.Geometry as IPoint, false);
                        }
                    }
                    Globals.GetCommand("esriArcMapUI.ZoomToSelectedCommand", app).Execute();

                }
                return returnVal;
            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.ToString());
                return ex.Message.ToString() + "\n" + pointAlong;

            }
            finally
            {

                barrierIds = null;
                sourceDirectEIDInfoHT = null;
                netFlag1 = null;
                netFlag2 = null;
                junctionFlags = null;
                edgeFlags = null;
                pID = null;
                noSourceValveHT = null;
                hasSourceValveHT = null;

                if (pProDlg != null)
                {

                    pProDlg.HideDialog();
                }
                pStepPro = null;
                pProDlg = null;
                pProDFact = null;

                if (gnList != null)
                {
                    //   Marshal.ReleaseComObject(gnList);
                }
                if (gn != null)
                {
                    Marshal.ReleaseComObject(gn);
                }
                if (snappedPoint != null)
                {
                    Marshal.ReleaseComObject(snappedPoint);
                }
                if (startNetFlag != null)
                {
                    //   Marshal.ReleaseComObject(startNetFlag);
                }
                if (sourceFC != null)
                {

                    //  Marshal.ReleaseComObject(sourceFC);
                }
                if (sourceFL != null)
                {
                    //  Marshal.ReleaseComObject(sourceFL);
                }
                if (pTempLay != null)
                {
                    Marshal.ReleaseComObject(pTempLay);
                }
                if (valveFLs != null)
                {
                    // Marshal.ReleaseComObject(valveFLs);
                }
                if (valveFCs != null)
                {
                    // Marshal.ReleaseComObject(valveFCs);
                }
                //if (meterFL != null)
                //{
                //    Marshal.ReleaseComObject(meterFL);
                //}
                if (junctionFlag != null)
                {
                   // Marshal.ReleaseComObject(junctionFlag);
                }
                if (edgeFlag != null)
                {
                    // Marshal.ReleaseComObject(edgeFlag);
                }
                if (traceFlowSolver != null)
                {
                    Marshal.ReleaseComObject(traceFlowSolver);
                }
                if (netSolver != null)
                {
                    Marshal.ReleaseComObject(netSolver);
                }
                if (netElementBarriers != null)
                {
                    Marshal.ReleaseComObject(netElementBarriers);
                }
                if (nb != null)
                {
                    Marshal.ReleaseComObject(nb);
                }
                if (juncEIDs != null)
                {
                    Marshal.ReleaseComObject(juncEIDs);
                }
                if (edgeEIDs != null)
                {
                    Marshal.ReleaseComObject(edgeEIDs);
                }
                if (eidInfo != null)
                {
                    Marshal.ReleaseComObject(eidInfo);
                }
                if (valveEIDInfo != null)
                {
                    Marshal.ReleaseComObject(valveEIDInfo);
                }
                if (sourceEIDInfo != null)
                {
                    Marshal.ReleaseComObject(sourceEIDInfo);
                }
                if (vEIDInfo != null)
                {
                    Marshal.ReleaseComObject(vEIDInfo);
                }
                if (userIds != null)
                {
                    // Marshal.ReleaseComObject(userIds);
                }
                if (eidHelper != null)
                {
                    Marshal.ReleaseComObject(eidHelper);
                }
                if (valveEIDInfoHT != null)
                {
                    //  Marshal.ReleaseComObject(valveEIDInfoHT);
                }
                if (sourceEIDInfoHT != null)
                {
                    //  Marshal.ReleaseComObject(sourceEIDInfoHT);
                }
                if (segCosts != null)
                {
                    //  Marshal.ReleaseComObject(segCosts);
                }
                if (netElementBarrier != null)
                {
                    Marshal.ReleaseComObject(netElementBarrier);
                }
                //if (traceRes != null)
                //{
                //    traceRes.Dispose();
                //    // Marshal.ReleaseComObject(traceRes);
                //}

                pNetAnalysisExt = null;
                pFlagsDisplay = null;
                pEdgeFlags = null;
                pJunctionFlags = null;
                pNetFlags = null;
                pFlagDisplay = null;
                //pEdgeFlagsBar = null;
                //pJunctionFlagsBar = null;

                gnList = null;

                gn = null;
                snappedPoint = null;

                startNetFlag = null;

                sourceFC = null;
                sourceFL = null;

                pTempLay = null;
                valveFLs = null;
                valveFCs = null;
                //meterFL = null;

                junctionFlag = null;
                edgeFlag = null;
                traceFlowSolver = null;
                netSolver = null;
                netElementBarriers = null;
                nb = null;
                juncEIDs = null;
                edgeEIDs = null;
                eidInfo = null;
                valveEIDInfo = null;
                sourceEIDInfo = null;
                vEIDInfo = null;
                userIds = null;

                eidHelper = null;
                valveEIDInfoHT = null;
                sourceEIDInfoHT = null;
                segCosts = null;
                netElementBarrier = null;

                //traceRes = null;
            }
            GC.Collect();
            GC.WaitForFullGCComplete(300);
        }
示例#3
0
        //���ҷ���
        private void BurstFindValves(IFlagDisplay pFlagDisplay)
        {
            try
            {
                INetwork pNetwork = pGeoNetwork.Network;
                IFeatureLayer pValveFeatLayer = Utility.FindFeatLayer("Water Fixtures", this);
                IFeatureLayer pWaterLineFeatLyr = Utility.FindFeatLayer("Water Lines", this);
                //�õ�EdgeFlag
                IEdgeFlag pEdgeFlag = new EdgeFlagClass();
                INetFlag pNetFlag = pEdgeFlag as INetFlag;
                pNetFlag.UserClassID = pFlagDisplay.FeatureClassID;
                pNetFlag.UserID = pFlagDisplay.FID;
                //��÷��ŵĹ��
                IQueryFilter pQueryFilter = new QueryFilterClass();
                pQueryFilter.WhereClause = "PWF_TYPE_CODE = 1";
                IFeatureSelection pValveFeatSelection = pValveFeatLayer as IFeatureSelection;
                pValveFeatSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
                //������ѡ��ķ���ΪSelectionSetBarriers
                ISelectionSetBarriers pSelSetBarriers = new SelectionSetBarriersClass();
                int FeatClassID = pValveFeatLayer.FeatureClass.FeatureClassID;
                IEnumIDs pEnumIDs = pValveFeatSelection.SelectionSet.IDs;
                pEnumIDs.Reset();
                int FeatID = pEnumIDs.Next();
                while (FeatID > 0)
                {
                    pSelSetBarriers.Add(FeatClassID, FeatID);
                    FeatID = pEnumIDs.Next();
                }
                //����TraceFlowSolver
                ITraceFlowSolver pTraceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolver;

                INetSolver pNetSolver = pTraceFlowSolver as INetSolver;
                pNetSolver.SourceNetwork = pNetwork;
                pNetSolver.SelectionSetBarriers = pSelSetBarriers;
                pTraceFlowSolver.PutEdgeOrigins(1, ref pEdgeFlag);
                //�Ӹ��ٹ����л�ý��
                IEnumNetEID pResultJenction;
                IEnumNetEID pResultsEdges;
                IEnumNetEID ptmpEnumNetEID = null;
                pTraceFlowSolver.FindFlowEndElements(esriFlowMethod.esriFMConnected, esriFlowElements.esriFEJunctions, out pResultJenction, out ptmpEnumNetEID);
                pTraceFlowSolver.FindFlowElements(esriFlowMethod.esriFMConnected, esriFlowElements.esriFEEdges, out ptmpEnumNetEID, out pResultsEdges);
                int lEID;
                int iUserClassID;
                int iUserID;
                int iUserSubID;
                INetElements pNetElements = pGeoNetwork.Network as INetElements;
                pResultJenction.Reset();
                IFeature pFeature;
                pValveFeatSelection.Clear();
                for (int j = 0; j <= pResultJenction.Count - 1; j++)
                {
                    lEID = pResultJenction.Next();
                    pNetElements.QueryIDs(lEID, esriElementType.esriETJunction, out iUserClassID, out iUserID, out iUserSubID);
                    pFeature = pValveFeatLayer.FeatureClass.GetFeature(iUserID);
                    pValveFeatSelection.Add(pFeature);
                    // MessageBox.Show(iUserClassID.ToString()+","+iUserID.ToString()+","+iUserSubID.ToString());
                }
                IFeatureSelection pLinesFeatSelection = pWaterLineFeatLyr as IFeatureSelection;
                pLinesFeatSelection.Clear();
                for (int i = 0; i <= pResultsEdges.Count - 1; i++)
                {
                    lEID = pResultsEdges.Next();
                    pNetElements.QueryIDs(lEID, esriElementType.esriETEdge, out iUserClassID, out iUserID, out iUserSubID);
                    pFeature = pWaterLineFeatLyr.FeatureClass.GetFeature(iUserID);
                    pLinesFeatSelection.Add(pFeature);
                }

                //�����µķ���������Ⱦѡȡ�Ľ��
                IMarkerSymbol pMarkerSymbol = new SimpleMarkerSymbolClass();
                IRgbColor pRGBColor = new RgbColorClass();
                pRGBColor.Red = 12;
                pRGBColor.Green = 250;
                pRGBColor.Blue = 233;
                pMarkerSymbol.Size = 14;
                pMarkerSymbol.Color = pRGBColor;
                pValveFeatSelection.SelectionSymbol = pMarkerSymbol as ISymbol;
                pValveFeatSelection.SelectionColor = pRGBColor as IColor;
                pValveFeatSelection.SelectionChanged();

                IRgbColor pRGBColor2 = new RgbColorClass();
                pRGBColor2.Red = 230;
                pRGBColor2.Green = 230;
                pRGBColor2.Blue = 0;
                pLinesFeatSelection.SelectionColor = pRGBColor2;
                pLinesFeatSelection.SelectionChanged();
                axMapControl1.ActiveView.Refresh();
                ICursor pCursor = null;
                pValveFeatSelection.SelectionSet.Search(null, false, out pCursor);
                IFeatureCursor pFeatCursor = pCursor as IFeatureCursor;
                IArray pArray = new ArrayClass();
                pFeature = pFeatCursor.NextFeature();
                while (pFeature != null)
                {
                    pArray.Add(pFeature);
                    pFeature = pFeatCursor.NextFeature();
                }
                pFeature = pWaterLineFeatLyr.FeatureClass.GetFeature(pFlagDisplay.FID);
                frmBurstReport frmBurstReport1 = new frmBurstReport(this, pValveFeatLayer, pWaterLineFeatLyr, pArray, pFeature);
                frmBurstReport1.Show();

            }
            catch (Exception eX)
            {
                MessageBox.Show(eX.Message);

            }
        }
        /// <summary>
        /// solve isolation
        /// </summary>
        /// <returns>object JsonObject</returns>
        public override JsonObject Solve()
        {
            try
            {
                if ((this.edgeFlags.Length == 0) && (this.junctionFlags.Length == 0))
                {
                    string message = "No input valid flags found";
                    if (this.flagNotFound.Count == 0)
                    {
                        throw new GeometricNetworkException(message);
                    }
                    else
                    {
                        JsonObject error = (new ObjectError(message)).ToJsonObject();
                        error.AddArray("flagsNotFound", Helper.GetListJsonObjects(this.flagNotFound));

                        return(error);
                    }
                }

                INetwork network = this.geometricNetwork.Network;

                ITraceFlowSolverGEN traceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
                INetSolver          netSolver       = traceFlowSolver as INetSolver;
                netSolver.SourceNetwork = network;

                this.SetFlagsOrigin(ref traceFlowSolver);

                ////now create barries based on valves
                INetElementBarriersGEN netElementBarriersGEN = new NetElementBarriersClass() as INetElementBarriersGEN;
                netElementBarriersGEN.ElementType = esriElementType.esriETJunction;
                netElementBarriersGEN.Network     = network;
                int[] userIds = Helper.GetOIDs(this.Valve);
                netElementBarriersGEN.SetBarriers(this.Valve.FeatureClassID, ref userIds);
                INetElementBarriers netElementBarriers = netElementBarriersGEN as INetElementBarriers;
                netSolver.set_ElementBarriers(esriElementType.esriETJunction, netElementBarriers);

                IEnumNetEID junctionEIDs;
                IEnumNetEID edgeEIDs;
                traceFlowSolver.FindFlowEndElements(esriFlowMethod.esriFMConnected, esriFlowElements.esriFEJunctionsAndEdges, out junctionEIDs, out edgeEIDs);

                IEIDHelper eidHelper = new EIDHelperClass();
                eidHelper.GeometricNetwork = this.geometricNetwork;
                eidHelper.ReturnFeatures   = true;

                List <IEIDInfo> valveEIDInfoHT = Helper.GetEIDInfoListByFeatureClass(this.Valve.FeatureClassID, junctionEIDs, eidHelper);

                traceFlowSolver         = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
                netSolver               = traceFlowSolver as INetSolver;
                netSolver.SourceNetwork = network;

                this.SetFlagsOrigin(ref traceFlowSolver);

                netSolver.DisableElementClass(this.Station.FeatureClassID);
                traceFlowSolver.FindFlowEndElements(esriFlowMethod.esriFMConnected, esriFlowElements.esriFEJunctions, out junctionEIDs, out edgeEIDs);

                List <IEIDInfo> sourceEIDInfoHT = Helper.GetEIDInfoListByFeatureClass(this.Station.FeatureClassID, junctionEIDs, eidHelper);

                ////set up trace to find directly reachable sources
                traceFlowSolver         = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
                netSolver               = traceFlowSolver as INetSolver;
                netSolver.SourceNetwork = network;

                this.SetFlagsOrigin(ref traceFlowSolver);

                ////set barriers in the network based on the saved valves
                ISelectionSetBarriers netElementBarrier = new SelectionSetBarriersClass();
                foreach (IEIDInfo eidInfo in valveEIDInfoHT)
                {
                    netElementBarrier.Add(this.Valve.FeatureClassID, eidInfo.Feature.OID);
                }

                netSolver.SelectionSetBarriers = netElementBarrier;

                ////run trace to find directly reachable sources(without passing valve)
                traceFlowSolver.FindFlowElements(esriFlowMethod.esriFMConnected, esriFlowElements.esriFEJunctionsAndEdges, out junctionEIDs, out edgeEIDs);
                List <IEIDInfo> sourceDirectEIDInfoHT = Helper.GetEIDInfoListByFeatureClass(this.Station.FeatureClassID, junctionEIDs, eidHelper);

                ////remove directly reachable sources from source array
                foreach (IEIDInfo eidInfo in sourceDirectEIDInfoHT)
                {
                    sourceEIDInfoHT.RemoveAll(x => x.Feature.OID == eidInfo.Feature.OID);
                }

                List <IEIDInfo> noSourceValveHT  = new List <IEIDInfo>();
                List <IEIDInfo> hasSourceValveHT = new List <IEIDInfo>();

                List <int> listBarrierIds;
                bool       foundSource;

                foreach (IEIDInfo valve in valveEIDInfoHT)
                {
                    foundSource = false;

                    ////create array of all isolation valve excluding the current one
                    listBarrierIds = new List <int>();
                    if (valveEIDInfoHT.Count > 1)
                    {
                        listBarrierIds.AddRange(valveEIDInfoHT.ConvertAll <int>(i => i.Feature.OID));
                        if (listBarrierIds.Contains(valve.Feature.OID))
                        {
                            listBarrierIds.Remove(valve.Feature.OID);
                        }
                    }

                    ////for each source attempt to trace
                    INetFlag netFlag1;
                    INetFlag netFlag2;
                    foreach (IEIDInfo source in sourceEIDInfoHT)
                    {
                        ////setup trace to test each valve
                        traceFlowSolver         = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
                        netSolver               = traceFlowSolver as INetSolver;
                        netSolver.SourceNetwork = network;

                        ////set the first junction flag for path finding based this current valve
                        netFlag1             = new JunctionFlagClass();
                        netFlag1.UserClassID = valve.Feature.Class.ObjectClassID;
                        netFlag1.UserID      = valve.Feature.OID;
                        netFlag1.UserSubID   = 0;
                        netFlag1.Label       = "Origin";

                        ////set the second and last trace flag at this source
                        netFlag2             = new JunctionFlagClass();
                        netFlag2.UserClassID = source.Feature.Class.ObjectClassID;
                        netFlag2.UserID      = source.Feature.OID;
                        netFlag2.UserSubID   = 0;
                        netFlag2.Label       = "Destination";

                        Helper.AddTwoJunctionFlagsToTraceSolver(ref traceFlowSolver, netFlag1, netFlag2);

                        if (listBarrierIds.Count > 0)
                        {
                            netElementBarriersGEN             = new NetElementBarriersClass() as INetElementBarriersGEN;
                            netElementBarriersGEN.ElementType = esriElementType.esriETJunction;
                            netElementBarriersGEN.Network     = network;
                            int[] barrierIds = listBarrierIds.ToArray();
                            netElementBarriersGEN.SetBarriers(this.Valve.FeatureClassID, ref barrierIds);
                            netElementBarriers = netElementBarriersGEN as INetElementBarriers;
                            netSolver.set_ElementBarriers(esriElementType.esriETJunction, netElementBarriers);
                        }

                        ////run trace
                        object[] segCosts = new object[1];
                        segCosts[0] = new object();
                        edgeEIDs    = null;
                        traceFlowSolver.FindPath(esriFlowMethod.esriFMConnected, esriShortestPathObjFn.esriSPObjFnMinSum, out junctionEIDs, out edgeEIDs, 1, ref segCosts);
                        if (edgeEIDs != null && edgeEIDs.Count > 0)
                        {
                            foundSource = true;
                            break;
                        }
                    }

                    if (foundSource)
                    {
                        hasSourceValveHT.Add(valve);
                    }
                    else
                    {
                        noSourceValveHT.Add(valve);
                    }
                }

                traceFlowSolver         = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
                netSolver               = traceFlowSolver as INetSolver;
                netSolver.SourceNetwork = network;

                this.SetFlagsOrigin(ref traceFlowSolver);

                ////set the barrier in the network based on the saved valves
                netElementBarrier = new SelectionSetBarriersClass();
                foreach (IEIDInfo eidInfo in hasSourceValveHT)
                {
                    netElementBarrier.Add(this.Valve.FeatureClassID, eidInfo.Feature.OID);
                }

                netSolver.SelectionSetBarriers = netElementBarrier;

                ////run last trace
                traceFlowSolver.FindFlowElements(esriFlowMethod.esriFMConnected, this.FlowElements, out junctionEIDs, out edgeEIDs);

                ////deal with out put juncEIDs and edgeEIDs
                JsonObject objectJson = new JsonObject();
                this.SetResults(ref objectJson, edgeEIDs, junctionEIDs);
                objectJson.AddArray("flagsNotFound", Helper.GetListJsonObjects(this.flagNotFound));

                ////valves
                if (hasSourceValveHT.Count > 0)
                {
                    JsonObject[] featureSetValves;
                    this.GetTrace(Helper.GetEnumNetEID(hasSourceValveHT, esriElementType.esriETJunction), out featureSetValves);
                    objectJson.AddJsonObject("valves", featureSetValves[0]);
                }

                return(objectJson);
            }
            catch (Exception e)
            {
                return((new ObjectError(e.Message)).ToJsonObject());
            }
        }