Exemplo n.º 1
0
 //解决路径
 public void SolvePath(string WeightName)
 {
     try
     {
         int    intEdgeUserClassID;
         int    intEdgeUserID;
         int    intEdgeUserSubID;
         int    intEdgeID;
         IPoint ipFoundEdgePoint;
         double dblEdgePercent;
         ITraceFlowSolverGEN ipTraceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
         INetSolver          ipNetSolver       = ipTraceFlowSolver as INetSolver;
         INetwork            ipNetwork         = m_ipGeometricNetwork.Network;
         ipNetSolver.SourceNetwork = ipNetwork;
         INetElements ipNetElements = ipNetwork as INetElements;
         int          intCount      = m_ipPoints.PointCount;//这里的points有值吗?
         //定义一个边线旗数组
         IEdgeFlag[] pEdgeFlagList = new EdgeFlagClass[intCount];
         for (int i = 0; i < intCount; i++)
         {
             INetFlag ipNetFlag   = new EdgeFlagClass() as INetFlag;
             IPoint   ipEdgePoint = m_ipPoints.get_Point(i);
             //查找输入点的最近的边线
             m_ipPointToEID.GetNearestEdge(ipEdgePoint, out intEdgeID, out ipFoundEdgePoint, out dblEdgePercent);
             ipNetElements.QueryIDs(intEdgeID, esriElementType.esriETEdge, out intEdgeUserClassID, out intEdgeUserID, out intEdgeUserSubID);
             ipNetFlag.UserClassID = intEdgeUserClassID;
             ipNetFlag.UserID      = intEdgeUserID;
             ipNetFlag.UserSubID   = intEdgeUserSubID;
             IEdgeFlag pTemp = (IEdgeFlag)(ipNetFlag as IEdgeFlag);
             pEdgeFlagList[i] = pTemp;
         }
         ipTraceFlowSolver.PutEdgeOrigins(ref pEdgeFlagList);
         INetSchema        ipNetSchema        = ipNetwork as INetSchema;
         INetWeight        ipNetWeight        = ipNetSchema.get_WeightByName(WeightName);
         INetSolverWeights ipNetSolverWeights = ipTraceFlowSolver as INetSolverWeights;
         ipNetSolverWeights.FromToEdgeWeight = ipNetWeight; //开始边线的权重
         ipNetSolverWeights.ToFromEdgeWeight = ipNetWeight; //终止边线的权重
         object[] vaRes = new object[intCount - 1];
         //通过findpath得到边线和交汇点的集合
         ipTraceFlowSolver.FindPath(esriFlowMethod.esriFMConnected,
                                    esriShortestPathObjFn.esriSPObjFnMinSum,
                                    out m_ipEnumNetEID_Junctions, out m_ipEnumNetEID_Edges, intCount - 1, ref vaRes);
         //计算元素成本
         m_dblPathCost = 0;
         for (int i = 0; i < vaRes.Length; i++)
         {
             double m_Va = (double)vaRes[i];//我修改过
             m_dblPathCost = m_dblPathCost + m_Va;
         }
         m_ipPolyline = null;
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
        /// <summary>
        /// 最短路径分析
        /// </summary>
        /// <param name="pMap"></param>
        /// <param name="pGeometricNet"></param>
        /// <param name="pCollection"></param>
        /// <param name="weightName"></param>
        /// <param name="pDisc"></param>
        /// <returns></returns>
        public static IPolyline DistanceFun(IMap pMap, IGeometricNetwork pGeometricNet, IPointCollection pCollection, string weightName, double pDisc)
        {
            //几何网络分析接口
            ITraceFlowSolverGEN pTraceFlowGen = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
            INetSolver          pNetSolver    = pTraceFlowGen as INetSolver;
            //获取网络
            INetwork pNetwork = pGeometricNet.Network;

            //设置该网络为几何分析处理网络
            pNetSolver.SourceNetwork = pNetwork;
            //网络元素
            INetElements pNetElements = pNetwork as INetElements;

            //根据输入点建立旗帜数组
            IJunctionFlag[] pJunctionFlags = GetJunctionFlags(pMap, pGeometricNet, pCollection, pDisc);
            //将旗帜数组添加到处理类中
            pTraceFlowGen.PutJunctionOrigins(ref pJunctionFlags);
            //设置边线的权重 可以考虑使用点权重
            SetWeight(weightName, pTraceFlowGen, pNetwork);
            //获取边线和交汇点的集合
            IEnumNetEID junctionEIDs;
            IEnumNetEID netEIDS;

            object[] pRec = new object[pCollection.PointCount - 1];
            pTraceFlowGen.FindPath(esriFlowMethod.esriFMConnected, esriShortestPathObjFn.esriSPObjFnMinSum, out junctionEIDs, out netEIDS, pCollection.PointCount - 1, ref pRec);
            //获取最短路径
            IPolyline           pPolyline           = new PolylineClass();
            IGeometryCollection pGeometryCollection = pPolyline as IGeometryCollection;
            ISpatialReference   pSpatialReference   = pMap.SpatialReference;
            IEIDHelper          pEIDHelper          = new EIDHelperClass();

            pEIDHelper.GeometricNetwork       = pGeometricNet;
            pEIDHelper.OutputSpatialReference = pSpatialReference;
            pEIDHelper.ReturnGeometries       = true;
            IEnumEIDInfo pEnumEIDInfo = pEIDHelper.CreateEnumEIDInfo(netEIDS);
            int          Count        = pEnumEIDInfo.Count;

            pEnumEIDInfo.Reset();
            for (int i = 0; i < Count; i++)
            {
                IEIDInfo  pEIDInfo  = pEnumEIDInfo.Next();
                IGeometry pGeometry = pEIDInfo.Geometry;
                pGeometryCollection.AddGeometryCollection(pGeometry as IGeometryCollection);
            }
            return(pPolyline);
        }
        public ITraceFlowSolverGEN UTIL_coreTraceSetup()
        {
            // get the current network's logical network
            INetworkAnalysisExt nax = m_utilNetExt as INetworkAnalysisExt;
            INetwork            net = nax.CurrentNetwork.Network;

            // create a new TraceFlowSolver object and
            // set the source network for the solve
            ITraceFlowSolverGEN tfs       = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
            INetSolver          netSolver = tfs as INetSolver;

            netSolver.SourceNetwork = net;

            // get the barriers for the network, using the element barriers and
            // selection barriers that have been added using the user interface
            INetworkAnalysisExtBarriers naxBarriers = m_utilNetExt as INetworkAnalysisExtBarriers;
            INetElementBarriers         juncElemBarriers, edgeElemBarriers;

            naxBarriers.CreateElementBarriers(out juncElemBarriers, out edgeElemBarriers);
            ISelectionSetBarriers selSetBarriers;

            naxBarriers.CreateSelectionBarriers(out selSetBarriers);
            netSolver.set_ElementBarriers(esriElementType.esriETJunction, juncElemBarriers);
            netSolver.set_ElementBarriers(esriElementType.esriETEdge, edgeElemBarriers);
            netSolver.SelectionSetBarriers = selSetBarriers;

            // set up the disabled layers for the network solver
            // for each feature layer belonging to this network, determine if it is
            // enabled or disabled; if it's disabled, then notify the network solver
            for (int i = 0; i < nax.FeatureLayerCount; i++)
            {
                IFeatureLayer featureLayer = nax.get_FeatureLayer(i);
                if (naxBarriers.GetDisabledLayer(featureLayer))
                {
                    netSolver.DisableElementClass(featureLayer.FeatureClass.FeatureClassID);
                }
            }

            INetworkAnalysisExtWeightFilter naxWeightFilter  = m_utilNetExt as INetworkAnalysisExtWeightFilter;
            INetSolverWeightsGEN            netSolverWeights = netSolver as INetSolverWeightsGEN;
            INetSchema netSchema = net as INetSchema;

            // create the junction weight filter
            int juncFilterRangeCount = naxWeightFilter.get_FilterRangeCount(esriElementType.esriETJunction);

            if (juncFilterRangeCount > 0)
            {
                INetWeight netWeight = netSchema.get_WeightByName(naxWeightFilter.JunctionWeightFilterName);
                netSolverWeights.JunctionFilterWeight = netWeight;

                esriWeightFilterType juncWeightFilterType;
                bool juncApplyNotOperator;
                naxWeightFilter.GetFilterType(esriElementType.esriETJunction,
                                              out juncWeightFilterType, out juncApplyNotOperator);
                netSolverWeights.SetFilterType(esriElementType.esriETJunction,
                                               juncWeightFilterType, juncApplyNotOperator);

                object[] juncFromValues = new object[juncFilterRangeCount];
                object[] juncToValues   = new object[juncFilterRangeCount];
                for (int i = 0; i < juncFilterRangeCount; i++)
                {
                    naxWeightFilter.GetFilterRange(esriElementType.esriETJunction, i,
                                                   out juncFromValues[i], out juncToValues[i]);
                }
                netSolverWeights.SetFilterRanges(esriElementType.esriETJunction,
                                                 ref juncFromValues, ref juncToValues);
            }

            // create the edge weight filters
            int edgeFilterRangeCount = naxWeightFilter.get_FilterRangeCount(esriElementType.esriETEdge);

            if (edgeFilterRangeCount > 0)
            {
                INetWeight fromToNetWeight = netSchema.get_WeightByName(naxWeightFilter.FromToEdgeWeightFilterName);
                netSolverWeights.FromToEdgeFilterWeight = fromToNetWeight;
                INetWeight toFromNetWeight = netSchema.get_WeightByName(naxWeightFilter.ToFromEdgeWeightFilterName);
                netSolverWeights.ToFromEdgeFilterWeight = toFromNetWeight;

                esriWeightFilterType edgeWeightFilterType;
                bool edgeApplyNotOperator;
                naxWeightFilter.GetFilterType(esriElementType.esriETEdge,
                                              out edgeWeightFilterType, out edgeApplyNotOperator);
                netSolverWeights.SetFilterType(esriElementType.esriETEdge,
                                               edgeWeightFilterType, edgeApplyNotOperator);

                object[] edgeFromValues = new object[edgeFilterRangeCount];
                object[] edgeToValues   = new object[edgeFilterRangeCount];
                for (int i = 0; i < edgeFilterRangeCount; i++)
                {
                    naxWeightFilter.GetFilterRange(esriElementType.esriETEdge, i,
                                                   out edgeFromValues[i], out edgeToValues[i]);
                }
                netSolverWeights.SetFilterRanges(esriElementType.esriETEdge,
                                                 ref edgeFromValues, ref edgeToValues);
            }

            INetworkAnalysisExtFlags naxFlags = m_utilNetExt as INetworkAnalysisExtFlags;

            // assign the edge flags to the network solver
            int edgeFlagCount = naxFlags.EdgeFlagCount;

            if (edgeFlagCount > 0)
            {
                IEdgeFlag[] edgeFlags = new IEdgeFlag[edgeFlagCount];
                for (int i = 0; i < edgeFlagCount; i++)
                {
                    IEdgeFlagDisplay edgeFlagDisplay = naxFlags.get_EdgeFlag(i);
                    IFlagDisplay     flagDisplay     = edgeFlagDisplay as IFlagDisplay;
                    IEdgeFlag        edgeFlag        = new EdgeFlagClass();
                    edgeFlag.Position = Convert.ToSingle(edgeFlagDisplay.Percentage);
                    INetFlag netFlag = edgeFlag as INetFlag;
                    netFlag.UserClassID = flagDisplay.FeatureClassID;
                    netFlag.UserID      = flagDisplay.FID;
                    netFlag.UserSubID   = flagDisplay.SubID;
                    edgeFlags[i]        = edgeFlag;
                }
                tfs.PutEdgeOrigins(ref edgeFlags);
            }

            // assign the junction flags to the network solver
            int juncFlagCount = naxFlags.JunctionFlagCount;

            if (juncFlagCount > 0)
            {
                IJunctionFlag[] juncFlags = new IJunctionFlag[juncFlagCount];
                for (int i = 0; i < juncFlagCount; i++)
                {
                    IJunctionFlagDisplay juncFlagDisplay = naxFlags.get_JunctionFlag(i);
                    IFlagDisplay         flagDisplay     = juncFlagDisplay as IFlagDisplay;
                    IJunctionFlag        juncFlag        = new JunctionFlagClass();
                    INetFlag             netFlag         = juncFlag as INetFlag;
                    netFlag.UserClassID = flagDisplay.FeatureClassID;
                    netFlag.UserID      = flagDisplay.FID;
                    netFlag.UserSubID   = flagDisplay.SubID;
                    juncFlags[i]        = juncFlag;
                }
                tfs.PutJunctionOrigins(ref juncFlags);
            }

            // set the option for tracing on indeterminate flow
            ITraceTasks traceTasks = m_utilNetExt as ITraceTasks;

            tfs.TraceIndeterminateFlow = traceTasks.TraceIndeterminateFlow;

            return(tfs);
        }
        public static string TracePath(double[] Xs, double[] Ys, string GeoNetName, IApplication app, IMap map, bool traceIndeterminate,
                                   double snapTol, bool selectEdges, out IEnumNetEID juncEIDs, out IEnumNetEID edgeEIDs, out IGeometricNetwork gn)
        {
            gn = null;

            juncEIDs = null;
            edgeEIDs = null;

            List<INetFlag> pNetFlags = null;
            IFlagDisplay pFlagDisplay = null;

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

            ITraceFlowSolverGEN traceFlowSolver = null;

            List<IEdgeFlag> pEdgeFlags = null;
            List<IJunctionFlag> pJunctionFlags = null;
            INetElementBarriers pEdgeElementBarriers;
            INetElementBarriers pJunctionElementBarriers;
            ISelectionSetBarriers pSelectionSetBarriers;
            List<IGeometricNetwork> gnList = null;
            INetworkAnalysisExt pNetAnalysisExt = null;
            UID pID = null;
            try
            {
                gnList = Globals.GetGeometricNetworks(ref map);
                if (gnList == null)
                {
                    MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_17a"));
                    return "";
                }

                int gnIdx = -1;

                gnIdx = Globals.GetGeometricNetwork(ref gnList, GeoNetName);
                if (gnIdx != -1)
                {
                    gn = (IGeometricNetwork)gnList[gnIdx];
                }

                pNetFlags = new List<INetFlag>();

                //  INetFlag[] netFlags = new INetFlag[Xs.Length];

                for (int i = 0; i < Xs.Length; i++)
                {
                    IPoint snappedPoint = null; int EID = -1;
                    if (gn == null)
                    {

                        pNetFlags.Add(Globals.GetJunctionFlag(Xs[i], Ys[i], ref map, ref gnList, snapTol, ref gnIdx,
                                    out snappedPoint, out EID, out  pFlagDisplay, true) as INetFlag);

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

                    }
                    else
                    {
                        pNetFlags.Add(Globals.GetJunctionFlagWithGN(Xs[i], Ys[i], ref map, ref gn, ref snapTol, out snappedPoint, out EID, out  pFlagDisplay, true) as INetFlag);

                    }
                }

                if (gn == null || pNetFlags.Count < 1)
                {
                    return (A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_17b"));

                }

                if (app != null)
                {

                    pID = new UID();

                    pID.Value = "esriEditorExt.UtilityNetworkAnalysisExt";
                    pNetAnalysisExt = (INetworkAnalysisExt)app.FindExtensionByCLSID(pID);
                    Globals.SetCurrentNetwork(ref pNetAnalysisExt, ref gn);
                    traceFlowSolver = Globals.CreateTraceFlowSolverFromToolbar(ref pNetAnalysisExt, out pEdgeFlags, out pJunctionFlags, out pEdgeElementBarriers, out pJunctionElementBarriers, out pSelectionSetBarriers) as ITraceFlowSolverGEN;

                }
                else
                {
                    traceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
                    INetSolver netSolver = traceFlowSolver as INetSolver;

                    netSolver.SourceNetwork = gn.Network;

                }

                traceFlowSolver.TraceIndeterminateFlow = traceIndeterminate;

                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)
                //{

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

                //}

                Globals.AddFlagsToTraceSolver(pNetFlags.ToArray(), ref traceFlowSolver, out junctionFlags, out edgeFlags);
                int intCount = 1;
                System.Object[] segCosts = new System.Object[1];
                segCosts[0] = new System.Object();

                traceFlowSolver.FindPath(esriFlowMethod.esriFMConnected, esriShortestPathObjFn.esriSPObjFnMinMax,
                    out juncEIDs, out edgeEIDs, intCount, ref segCosts);

                //Select junction features
                map.ClearSelection();
                IJunctionFlag[] junctionFlag = null;
                Globals.SelectJunctions(ref map, ref  gn, ref juncEIDs, ref junctionFlag, "", "", "", true);
                if (selectEdges)
                    Globals.SelectEdges(ref map, ref gn, ref edgeEIDs);
                edgeEIDs.Reset();

                //Draw edge graphics
                IEnvelope env = Globals.DrawEdges(ref map, ref gn, ref edgeEIDs);

                return edgeEIDs.Count.ToString();

            }
            catch (Exception Ex)
            {
                MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsLbl_17a") + "\n" + Ex.Message);
                return "";

            }
        }
        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);
        }
        public static void TraceFlow(ref IPoint point, IApplication app, esriFlowMethod flow, double snapTol, bool traceIndeterminate, bool selectEdges)
        {
            IMap map = null;
            IProgressDialogFactory pProDFact;
            IStepProgressor pStepPro;
            IProgressDialog2 pProDlg = null;
            ITrackCancel pTrkCan;

            List<IGeometricNetwork> gnList;
            IGeometricNetwork gn = null;
            IPoint snappedPoint = null;
            IFlagDisplay pFlagDisplay;
            INetFlag startNetFlag;
            ITraceFlowSolverGEN traceFlowSolver;
            List<IEdgeFlag> pEdgeFlags = null;
            List<IJunctionFlag> pJunctionFlags = null;
            //List<IEdgeFlag> pEdgeFlagsBar = null;
            //List<IJunctionFlag> pJunctionFlagsBar = null;
            INetElementBarriers pEdgeElementBarriers;
            INetElementBarriers pJunctionElementBarriers;
            ISelectionSetBarriers pSelectionSetBarriers;
            INetworkAnalysisExt pNetAnalysisExt = null;
            List<INetFlag> pNetFlags = new List<INetFlag>();
            IJunctionFlag[] junctionFlag;
            IEdgeFlag[] edgeFlag;
            IEnumNetEID juncEIDs;
            IEnumNetEID edgeEIDs;
            IEnvelope env;
            int EID = -1;
            double distanceAlong;

            UID pID = null;
            INetSolver netSolver = null;

            try
            {

                map = ((IMxDocument)app.Document).FocusMap;

                bool boolCont = true;
                // 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;
                if (flow == esriFlowMethod.esriFMConnected)
                {
                    pProDlg.Description = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsDesc_14a");
                    pProDlg.Title = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsTitle_14a");
                }
                else if (flow == esriFlowMethod.esriFMDownstream)
                {
                    pProDlg.Description = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsDesc_14b");
                    pProDlg.Title = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsTitle_14b");
                }
                else if (flow == esriFlowMethod.esriFMUpstream)
                {
                    pProDlg.Description = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsDesc_14c");
                    pProDlg.Title = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsTitle_14c");
                }

                pProDlg.Animation = esriProgressAnimationTypes.esriProgressGlobe;

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

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

                gnList = Globals.GetGeometricNetworksCurrentlyVisible(ref map);
                int gnIdx = -1;

                if (gnList == null || gnList.Count == 0)
                {
                    MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_2"), A4LGSharedFunctions.Localizer.GetString("GeoNetToolsErrorLbl_2"));
                    return;
                }

                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;
                }
                //Remove old trace graphics (flags and results)
                Globals.RemoveTraceGraphics(map, true);
                Globals.ClearSelected(map, true);

                // Create junction or edge flag at start of trace - also returns geometric network, snapped point, and EID of junction
                pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_5");
                pStepPro.Step();
                boolCont = pTrkCan.Continue();

                if (!boolCont)
                {

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

                startNetFlag = Globals.GetJunctionFlag(ref point, ref map, ref gnList, snapTol, ref gnIdx, out snappedPoint, out EID, out pFlagDisplay, true) as INetFlag;
                if (startNetFlag == null)
                {
                    startNetFlag = Globals.GetEdgeFlag(ref point, ref map, ref gnList, snapTol, ref gnIdx, out snappedPoint, out EID, out distanceAlong, out pFlagDisplay, true) as INetFlag;
                }

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

                // Stop if user point was not on a visible network feature, old trace results and selection are cleared
                if (gn == null || startNetFlag == null)
                {
                    return;
                }

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

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return;
                }
                //Setup trace solver
                //  ITraceFlowSolverGEN traceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
                // ITraceFlowSolver traceFlowSolver = Globals.CreateTraceFlowSolverFromToolbar(gn);

                if (app != null)
                {

                    pID = new UID();

                    pID.Value = "esriEditorExt.UtilityNetworkAnalysisExt";
                    pNetAnalysisExt = (INetworkAnalysisExt)app.FindExtensionByCLSID(pID);
                    Globals.SetCurrentNetwork(ref pNetAnalysisExt, ref gn);
                    traceFlowSolver = Globals.CreateTraceFlowSolverFromToolbar(ref pNetAnalysisExt, out pEdgeFlags, out pJunctionFlags, out pEdgeElementBarriers, out pJunctionElementBarriers, out pSelectionSetBarriers) as ITraceFlowSolverGEN;
                    pID = null;

                }
                else
                {
                    traceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
                    netSolver = traceFlowSolver as INetSolver;

                    netSolver.SourceNetwork = gn.Network;
                    netSolver = null;

                }

                traceFlowSolver.TraceIndeterminateFlow = traceIndeterminate;

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

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return;
                }
                //Add the flag to the trace solver

                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)
                {

                    pNetFlags.Add((INetFlag)startNetFlag);

                }

                Globals.AddFlagsToTraceSolver(pNetFlags.ToArray(), ref traceFlowSolver, out junctionFlag, out edgeFlag);

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

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return;
                }
                //Run the trace

                traceFlowSolver.FindFlowElements(flow, esriFlowElements.esriFEJunctionsAndEdges, out juncEIDs, out edgeEIDs);

                if (juncEIDs.Count == 0)
                {
                    if (flow == esriFlowMethod.esriFMDownstream)
                        MessageBox.Show(
                          A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_14e") + Environment.NewLine + Environment.NewLine +
                          A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_14f")
                        , A4LGSharedFunctions.Localizer.GetString("GeoNetToolsTitle_14b"));
                    else
                        MessageBox.Show(
                          A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_14g") + Environment.NewLine + Environment.NewLine +
                          A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_14f")
                          , A4LGSharedFunctions.Localizer.GetString("GeoNetToolsTitle_14c"));
                    return;
                }

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

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return;
                }
                //Select junction features
                Globals.SelectJunctions(ref map, ref gn, ref juncEIDs, ref junctionFlag, "", "", "", true);

                if (selectEdges)
                    Globals.SelectEdges(ref map, ref gn, ref edgeEIDs);
                else
                    Globals.DrawEdges(ref map, ref gn, ref edgeEIDs);

                //edgeEIDs.Reset();

                //Draw edge graphics

                //Draw graphic point at start location of trace
                if (pNetAnalysisExt != null)
                {
                    Globals.AddFlagToGN(ref pNetAnalysisExt, ref gn, ref pFlagDisplay);
                }
                else
                {
                    Globals.AddPointGraphic(map, snappedPoint, false);
                }
                Globals.GetCommand("esriArcMapUI.ZoomToSelectedCommand", app).Execute();

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

                // add set flow direction buttons
                pStepPro.Message = A4LGSharedFunctions.Localizer.GetString("Complete");
                pStepPro.Step();
                boolCont = pTrkCan.Continue();

                if (!boolCont)
                {

                    pStepPro.Hide();
                    pProDlg.HideDialog();
                    pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return;
                }
                ((IMxDocument)app.Document).ActiveView.Refresh();

                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ErrorInThe") + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsProc_14a") + ": " + ex.ToString());
                if (pProDlg != null)
                {

                    pProDlg.HideDialog();
                    //pStepPro = null;
                    pProDlg = null;
                    pProDFact = null;
                    return;

                }

            }
            finally
            {
                if (pProDlg != null)
                {

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

                gnList = null;
                gn = null;
                snappedPoint = null;
                pFlagDisplay = null;
                startNetFlag = null;
                traceFlowSolver = null;
                pEdgeFlags = null;
                pJunctionFlags = null;
                pNetAnalysisExt = null;
                pNetFlags = null;
                junctionFlag = null;
                edgeFlag = null;
                juncEIDs = null;
                edgeEIDs = null;
                env = null;
                map = null;

                pID = null;
                netSolver = null;
            }
        }
        public static string TraceFindClosest(double[] x, double[] y, IApplication app, string targetFLName, string sWeightName,
                                double snapTol, bool processEvent, bool traceIndeterminate)
        {
            IMap map = null;

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

            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

            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;

            List<BarClassIDS> barrierIds = null;
            Hashtable sourceDirectEIDInfoHT = null;
            INetFlag netFlag1 = null;
            INetFlag netFlag2 = null;

            Hashtable htClosestAsset = null;
            IFeatureLayer pClosestLayer = null;
            INetwork pNetwork = null;
            INetSchema pNetSchema = null;
            INetWeight pNetWeight = null;
            INetSolverWeights pNetSolverW = 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_15a");
                    pProDlg.Title = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsTitle_15a");

                    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");
                }

                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");
                }

                // 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);

                    }
                }

                //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");
                }

                bool fndAsLayer = false;
                pClosestLayer = Globals.FindLayer(app, targetFLName, ref fndAsLayer) as IFeatureLayer;
                if (pClosestLayer == null)
                {
                    return A4LGSharedFunctions.Localizer.GetString("LayerNotFoundReturnStatement");
                }
                if (processEvent)
                {

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

                try
                {
                    Globals.AddFlagsToTraceSolver(pNetFlags.ToArray(), ref traceFlowSolver, out junctionFlag, out edgeFlag);

                    traceFlowSolver.FindFlowElements(esriFlowMethod.esriFMConnected, esriFlowElements.esriFEJunctions, 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");
                }

                eidHelper = new EIDHelperClass();
                eidHelper.GeometricNetwork = gn;
                eidHelper.ReturnFeatures = true;

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

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

                if (!boolCont)
                {

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

                if (pClosestLayer.FeatureClass != null)
                {
                    htClosestAsset = Globals.GetEIDInfoListByFC(pClosestLayer.FeatureClass.FeatureClassID, juncEIDs, eidHelper);

                }

                if (htClosestAsset.Count == 0)
                {
                    if (processEvent)
                    {

                        pStepPro.Message = (A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15b") + targetFLName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_15c"));

                        pStepPro.Step();
                        boolCont = pTrkCan.Continue();
                        return A4LGSharedFunctions.Localizer.GetString("NoFeaturesReturnStatement");
                    }

                }
                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);
                pNetwork = gn.Network;
                pNetSchema = pNetwork as INetSchema;
                for (int i = 0; i < pNetSchema.WeightCount; i++)
                {
                    pNetWeight = pNetSchema.get_Weight(i);
                    if (pNetWeight.WeightName == sWeightName)
                        break;

                }
                if (pNetWeight != null)
                {
                    if (pNetWeight.WeightType == esriWeightType.esriWTBitGate || pNetWeight.WeightType == esriWeightType.esriWTNull)
                    {
                        pNetWeight = null;
                    }
                }
                //Get trace weights
                if (pNetWeight != null)
                {
                    pNetSolverW = traceFlowSolver as INetSolverWeights;
                    pNetSolverW.JunctionWeight = pNetWeight;
                    pNetSolverW.FromToEdgeWeight = pNetWeight;
                    pNetSolverW.ToFromEdgeWeight = pNetWeight;

                }
                double shortest = 9999999.9;
                int pntAlong = 0;
                foreach (DictionaryEntry entry in htClosestAsset)
                {
                    pntAlong++;
                    ////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);
                    // startNetFlag[0].Label = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsLbl_16a");
                    //    startNetFlag[0].UserSubID = 0;

                    //Set the second (and last) trace flag at this source
                    //netFlag2 = new JunctionFlagClass();
                    //eidInfo = entry.Value as IEIDInfo;

                    //netFlag2.UserClassID = eidInfo.Feature.Class.ObjectClassID;
                    //netFlag2.UserID = eidInfo.Feature.OID;
                    //netFlag2.UserSubID = 0;
                    //netFlag2.Label = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsLbl_16b");

                    eidInfo = entry.Value as IEIDInfo;
                    IPoint pTmpPnt = eidInfo.Feature.Shape as IPoint;

                    //ref point, ref map, ref gn, snapTol, out snappedPoint,
                    //                             out EID, out distanceAlong, out  pFlagDisplay, Flag);
                    bool Flag = true;
                    netFlag2 = Globals.GetEdgeFlagWithGN(ref pTmpPnt, ref map, ref gn, snapTol, out snappedPoint,
                                                 out EID, out distanceAlong, out  pFlagDisplay, Flag) as INetFlag;
                    // Globals.AddTwoJunctionFlagsToTraceSolver(ref traceFlowSolver, netFlag1, netFlag2);
                    Globals.AddTwoJunctionFlagsToTraceSolver(ref traceFlowSolver, startNetFlag[0], netFlag2);

                    //Run trace
                    segCosts = new System.Object[1];
                    segCosts[0] = new System.Object(); edgeEIDs = null;
                    object pTotalCost = null;
                    IEnumNetEID pJuncSel = null;
                    IEnumNetEID pEdgeSel = null;
                    //traceFlowSolver.FindAccumulation(esriFlowMethod.esriFMConnected, esriFlowElements.esriFEJunctionsAndEdges, out pJuncSel, out pEdgeSel,out pTotalCost);
                    traceFlowSolver.FindPath(esriFlowMethod.esriFMConnected, esriShortestPathObjFn.esriSPObjFnMinSum, out juncEIDs, out edgeEIDs, 1, ref segCosts);
                    if (Convert.ToDouble(segCosts[0]) < shortest)
                    {
                        shortest = Convert.ToDouble(segCosts[0]);
                    }
                    string test = "";

                    // if (edgeEIDs != null && edgeEIDs.Count > 0)
                    // {
                    //     // foundSource = true;
                    //     // break;
                    // }

                }
                MessageBox.Show(shortest.ToString());
                //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();

                //}
                string returnVal = "";
                return returnVal;
            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.ToString());
                return ex.Message.ToString();

            }
            finally
            {

                barrierIds = null;
                sourceDirectEIDInfoHT = null;
                netFlag1 = null;
                netFlag2 = null;
                junctionFlags = null;
                edgeFlags = null;
                pID = 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 (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;

                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);
        }
        /// <summary>
        /// operation solve trace
        /// </summary>
        /// <returns>solve trace</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));
                        if (this.barrierNotFound.Count > 0)
                        {
                            error.AddArray("barriersNotFound", Helper.GetListJsonObjects(this.barrierNotFound));
                        }

                        return(error);
                    }
                }

                ITraceFlowSolverGEN traceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
                INetSolver          netSolver       = traceFlowSolver as INetSolver;
                INetwork            network         = this.geometricNetwork.Network;
                netSolver.SourceNetwork = network;

                // flag origin
                this.SetFlagsOrigin(ref traceFlowSolver);

                // barrier
                this.SetBarriers(ref traceFlowSolver);

                // set disabled layers
                foreach (int i in this.DisableLayers)
                {
                    netSolver.DisableElementClass(i);
                }

                // set weight
                this.SetWeights(ref traceFlowSolver);

                if ((this.TraceSolverType != TraceSolverType.FindCircuits) && (this.TraceSolverType != TraceSolverType.FindCommonAncestors))
                {
                    // The TraceIndeterminateFlow property affects the trace results of trace solvers whose FlowMethod parameter is esriFMUpstream or esriFMDownstream
                    if ((this.FlowMethod == esriFlowMethod.esriFMDownstream) || (this.FlowMethod == esriFlowMethod.esriFMUpstream))
                    {
                        traceFlowSolver.TraceIndeterminateFlow = this.TraceIndeterminateFlow;
                    }
                }

                IEnumNetEID junctionEIDs = null;
                IEnumNetEID edgeEIDs     = null;
                object      totalCost;
                int         count;
                object[]    segmentCosts;

                JsonObject objectJson = new JsonObject();
                switch (this.TraceSolverType)
                {
                case TraceSolverType.FindAccumulation:
                    traceFlowSolver.FindAccumulation(this.FlowMethod, this.FlowElements, out junctionEIDs, out edgeEIDs, out totalCost);
                    objectJson.AddDouble("totalCost", (double)totalCost);
                    break;

                case TraceSolverType.FindCircuits:
                    traceFlowSolver.FindCircuits(this.FlowElements, out junctionEIDs, out edgeEIDs);
                    break;

                case TraceSolverType.FindCommonAncestors:
                    traceFlowSolver.FindCommonAncestors(this.FlowElements, out junctionEIDs, out edgeEIDs);
                    break;

                case TraceSolverType.FindFlowElements:
                    traceFlowSolver.FindFlowElements(this.FlowMethod, this.FlowElements, out junctionEIDs, out edgeEIDs);
                    break;

                case TraceSolverType.FindFlowEndElements:
                    traceFlowSolver.FindFlowEndElements(this.FlowMethod, this.FlowElements, out junctionEIDs, out edgeEIDs);
                    break;

                case TraceSolverType.FindFlowUnreachedElements:
                    traceFlowSolver.FindFlowUnreachedElements(this.FlowMethod, this.FlowElements, out junctionEIDs, out edgeEIDs);
                    break;

                case TraceSolverType.FindPath:
                    count = Math.Max(this.junctionFlags.Length, this.edgeFlags.Length);
                    if (count < 2)
                    {
                        throw new GeometricNetworkException("Edge or junction found < 2!");
                    }

                    --count;
                    segmentCosts = new object[count];
                    traceFlowSolver.FindPath(this.FlowMethod, this.ShortestPathObjFn, out junctionEIDs, out edgeEIDs, count, ref segmentCosts);
                    objectJson.AddArray("segmentCosts", segmentCosts);
                    break;

                case TraceSolverType.FindSource:
                    count        = this.junctionFlags.Length + this.edgeFlags.Length;
                    segmentCosts = new object[count];
                    traceFlowSolver.FindSource(this.FlowMethod, this.ShortestPathObjFn, out junctionEIDs, out edgeEIDs, count, ref segmentCosts);
                    objectJson.AddArray("segmentCosts", segmentCosts);
                    break;

                case TraceSolverType.FindLongest:
                    // get end junctions in upstream
                    IEnumNetEID junctionEIDsFindLongest = null;
                    IEnumNetEID edgeEIDsFindLongest     = null;
                    traceFlowSolver.FindFlowEndElements(esriFlowMethod.esriFMUpstream, esriFlowElements.esriFEJunctions, out junctionEIDsFindLongest, out edgeEIDsFindLongest);

                    int?   eidLongest       = null;
                    double eidLongestLenght = double.MinValue;

                    if (junctionEIDsFindLongest.Count > 0)
                    {
                        double eidLongestLenghtCurrent = double.NaN;
                        for (int i = 0; i < junctionEIDsFindLongest.Count; i++)
                        {
                            int         netEIDCurrent       = junctionEIDsFindLongest.Next();
                            object[]    segmentLenghts      = new object[1];
                            IEnumNetEID junctionEIDsLongest = null;
                            IEnumNetEID edgeEIDsLongest     = null;

                            INetFlag     netFlag = new JunctionFlagClass();
                            INetElements netElements = this.geometricNetwork.Network as INetElements;
                            int          featureClassID, featureID, subID;
                            netElements.QueryIDs(netEIDCurrent, esriElementType.esriETJunction, out featureClassID, out featureID, out subID);

                            netFlag.UserClassID = featureClassID;
                            netFlag.UserID      = featureID;
                            netFlag.UserSubID   = subID;

                            IJunctionFlag[] junctionFlags = new IJunctionFlag[] { this.junctionFlags[0], netFlag as IJunctionFlag };
                            traceFlowSolver.PutJunctionOrigins(ref junctionFlags);

                            traceFlowSolver.FindPath(esriFlowMethod.esriFMUpstream, esriShortestPathObjFn.esriSPObjFnMinMax, out junctionEIDsLongest, out edgeEIDsLongest, 1, ref segmentLenghts);
                            if (segmentLenghts[0] != null)
                            {
                                eidLongestLenghtCurrent = Convert.ToDouble(segmentLenghts[0]);
                                if (eidLongestLenghtCurrent > eidLongestLenght)
                                {
                                    eidLongestLenght = eidLongestLenghtCurrent;
                                    eidLongest       = netEIDCurrent;
                                    edgeEIDs         = edgeEIDsLongest;
                                    junctionEIDs     = junctionEIDsLongest;
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new GeometricNetworkException("Junction end not found!");
                    }

                    if (eidLongest.HasValue)
                    {
                        objectJson.AddDouble("totalCost", eidLongestLenght);
                    }
                    else
                    {
                        throw new GeometricNetworkException("EID longest not found!");
                    }

                    break;

                default:
                    throw new GeometricNetworkException("Trace solver type not found");
                }

                this.SetResults(ref objectJson, edgeEIDs, junctionEIDs);
                objectJson.AddArray("flagsNotFound", Helper.GetListJsonObjects(this.flagNotFound));
                objectJson.AddArray("barriersNotFound", Helper.GetListJsonObjects(this.barrierNotFound));
                return(objectJson);
            }
            catch (Exception e)
            {
                return((new ObjectError(e.Message)).ToJsonObject());
            }
        }
 //解决路径
 public void SolvePath(string WeightName)
 {
     try
     {
         int intEdgeUserClassID;
         int intEdgeUserID;
         int intEdgeUserSubID;
         int intEdgeID;
         IPoint ipFoundEdgePoint;
         double dblEdgePercent;
         //定义追踪线
         ITraceFlowSolverGEN ipTraceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
         INetSolver ipNetSolver = ipTraceFlowSolver as INetSolver;
         //ipNetSolver.SelectionSetBarriers
         INetwork ipNetwork = m_ipGeometricNetwork.Network;
         ipNetSolver.SourceNetwork = ipNetwork;
         INetElements ipNetElements = ipNetwork as INetElements;
         int intCount = m_ipPoints.PointCount;
         //定义一个边线旗数组,各边线与输入点最近
         IEdgeFlag[] pEdgeFlagList = new EdgeFlagClass[intCount];
         for (int i = 0; i < intCount; i++)
         {
             INetFlag ipNetFlag = new EdgeFlagClass() as INetFlag;
             IPoint ipEdgePoint = m_ipPoints.get_Point(i);
             //查找输入点的最近的边线
             m_ipPointToEID.GetNearestEdge(ipEdgePoint, out intEdgeID, out ipFoundEdgePoint, out dblEdgePercent);
             ipNetElements.QueryIDs(intEdgeID, esriElementType.esriETEdge, out intEdgeUserClassID, out intEdgeUserID, out intEdgeUserSubID);
             ipNetFlag.UserClassID = intEdgeUserClassID;
             ipNetFlag.UserID = intEdgeUserID;
             ipNetFlag.UserSubID = intEdgeUserSubID;
             IEdgeFlag pTemp = (IEdgeFlag)(ipNetFlag as IEdgeFlag);
             pEdgeFlagList[i] = pTemp;
         }
         IFeatureClassContainer ipFeatureClassContainer = m_ipGeometricNetwork as IFeatureClassContainer;
         IFeatureClass ipFeatureClass;
         int count = ipFeatureClassContainer.ClassCount;
         if (barrierflag == 1)
         {
             for (int i = 0; i < count; i++)
             {
                 ipFeatureClass = ipFeatureClassContainer.get_Class(i);
                 INetElementBarriers inetelementbarriers;
                 if (i == 0)
                 {
                     inetelementbarriers = setbarriers(ipFeatureClass, barrierarray1, ipNetwork);
                     if(inetelementbarriers!=null)
                         ipNetSolver.set_ElementBarriers(esriElementType.esriETEdge, inetelementbarriers);
                 }
                 if (i == 1)
                 {
                     inetelementbarriers = setbarriers(ipFeatureClass, barrierarray2, ipNetwork);
                     if(inetelementbarriers!=null)
                         ipNetSolver.set_ElementBarriers(esriElementType.esriETEdge, inetelementbarriers);
                 }
             }
         }
         ipTraceFlowSolver.PutEdgeOrigins(ref pEdgeFlagList);
         INetSchema ipNetSchema = ipNetwork as INetSchema;
         INetWeight ipNetWeight = ipNetSchema.get_WeightByName(WeightName);
         INetSolverWeights ipNetSolverWeights = ipTraceFlowSolver as INetSolverWeights;
         ipNetSolverWeights.FromToEdgeWeight = ipNetWeight;//开始边线的权重
         ipNetSolverWeights.ToFromEdgeWeight = ipNetWeight;//终止边线的权重
         object[] vaRes = new object[intCount - 1];
         //通过findpath得到边线和交汇点的集合
         ipTraceFlowSolver.FindPath(esriFlowMethod.esriFMConnected, esriShortestPathObjFn.esriSPObjFnMinSum, out m_ipEnumNetEID_Junctions, out m_ipEnumNetEID_Edges, intCount - 1, ref vaRes);
         //计算成本
         m_dblPathCost = 0;
         for (int i = 0; i < vaRes.Length; i++)
         {
             double m_Va = (double)vaRes[i];
             m_dblPathCost = m_dblPathCost + m_Va;
         }
         m_ipPolyline = null;
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
     }
 }
Exemplo n.º 10
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);

            }
        }
Exemplo n.º 11
0
 //解决路径
 public void SolvePath(string WeightName)
 {
     try
     {
         int intEdgeUserClassID;
         int intEdgeUserID;
         int intEdgeUserSubID;
         int intEdgeID;
         IPoint ipFoundEdgePoint;
         double dblEdgePercent;
         ITraceFlowSolverGEN ipTraceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
         INetSolver ipNetSolver = ipTraceFlowSolver as INetSolver;
         INetwork ipNetwork = m_ipGeometricNetwork.Network;
         ipNetSolver.SourceNetwork = ipNetwork;
         INetElements ipNetElements = ipNetwork as INetElements;
         int intCount = m_ipPoints.PointCount;//这里的points有值吗?
         //定义一个边线旗数组
         IEdgeFlag[] pEdgeFlagList = new EdgeFlagClass[intCount];
         for (int i = 0; i < intCount; i++)
         {
             INetFlag ipNetFlag = new EdgeFlagClass() as INetFlag;
             IPoint ipEdgePoint = m_ipPoints.get_Point(i);
             //查找输入点的最近的边线
             m_ipPointToEID.GetNearestEdge(ipEdgePoint, out intEdgeID, out ipFoundEdgePoint, out dblEdgePercent);
             ipNetElements.QueryIDs(intEdgeID, esriElementType.esriETEdge, out intEdgeUserClassID, out intEdgeUserID, out intEdgeUserSubID);
             ipNetFlag.UserClassID = intEdgeUserClassID;
             ipNetFlag.UserID = intEdgeUserID;
             ipNetFlag.UserSubID = intEdgeUserSubID;
             IEdgeFlag pTemp = (IEdgeFlag)(ipNetFlag as IEdgeFlag);
             pEdgeFlagList[i] = pTemp;
         }
         ipTraceFlowSolver.PutEdgeOrigins(ref pEdgeFlagList);
         INetSchema ipNetSchema = ipNetwork as INetSchema;
         INetWeight ipNetWeight = ipNetSchema.get_WeightByName(WeightName);
         INetSolverWeights ipNetSolverWeights = ipTraceFlowSolver as INetSolverWeights;
         ipNetSolverWeights.FromToEdgeWeight = ipNetWeight;//开始边线的权重
         ipNetSolverWeights.ToFromEdgeWeight = ipNetWeight;//终止边线的权重
         object[] vaRes = new object[intCount - 1];
         //通过findpath得到边线和交汇点的集合
         ipTraceFlowSolver.FindPath(esriFlowMethod.esriFMConnected,
          esriShortestPathObjFn.esriSPObjFnMinSum,
          out m_ipEnumNetEID_Junctions, out m_ipEnumNetEID_Edges, intCount - 1, ref vaRes);
         //计算元素成本
         m_dblPathCost = 0;
         for (int i = 0; i < vaRes.Length; i++)
         {
             double m_Va = (double)vaRes[i];//我修改过
             m_dblPathCost = m_dblPathCost + m_Va;
         }
         m_ipPolyline = null;
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
		public int [] FindPath(IEdgeFlag [] edgeFlags,  out double length)
		{
			int [] roadIDs = {};
			int [] roadIDList = {};
			length = -1;
			try
			{
				ITraceFlowSolverGEN traceSolver = new TraceFlowSolverClass();
				INetSolver solver = traceSolver as INetSolver;
				solver.SourceNetwork = roadNet;
				traceSolver.PutEdgeOrigins(ref edgeFlags);
				INetSchema schema = roadNet as INetSchema;
				INetSolverWeights netWeights = traceSolver as INetSolverWeights;
				INetWeight weight = schema.get_WeightByName("RoadTypeWeight");
				netWeights.FromToEdgeWeight = weight;
				netWeights.ToFromEdgeWeight = weight;
			
				IEnumNetEID roadJunctions = new EnumNetEIDArrayClass();
				IEnumNetEID roadEdges = new EnumNetEIDArrayClass();
				object [] segmentCosts = new object [edgeFlags.Length];

	
				traceSolver.FindPath(esriFlowMethod.esriFMConnected, 
					esriShortestPathObjFn.esriSPObjFnMinSum,
					out roadJunctions, out roadEdges, edgeFlags.Length-1, ref segmentCosts);

				// arrays for storing the featureclass id, object id, and subclass id
				roadIDs = new int[roadEdges.Count];
				int [] FCIDList = new int[roadEdges.Count];
				int [] SCIDList = new int[roadEdges.Count];
			
					
				//index of feature fields of interest
				int lengthField = RoadEdge.FindField("SHAPE_Length");
				//int roadIDField = RoadEdge.FindField("roadid");

				//collect feature ids, roadids, and length by iterating through uid enumeration
				length = 0;
				roadIDList = new int[roadEdges.Count];
				for(int i=0; i < roadEdges.Count; i++)
				{
					RoadNetElements.QueryIDs(roadEdges.Next(),ESRI.ArcGIS.Geodatabase.esriElementType.esriETEdge,out FCIDList[i], out roadIDs[i],out SCIDList[i]);
					roadIDList[i] = RoadEdge.GetFeature(roadIDs[i]).OID;//get_Value(roadIDField).ToString());
					length += double.Parse(RoadEdge.GetFeature(roadIDs[i]).get_Value(lengthField).ToString());
				}
			}
			catch(Exception err)
			{
				throw err;
			}
			return roadIDList;//roadIDs;
		}
Exemplo n.º 13
0
        public static List <int> StreamTrace(IGeometricNetwork geometricNetwork, StartFlagEdge edge, List <int> disabledFeatureClassIds, StopperJunctions stoppers, bool isUpStream, int maxFeatureCount, ServerLogger logger)
        {
            esriFlowMethod direction = isUpStream ? esriFlowMethod.esriFMUpstream : esriFlowMethod.esriFMDownstream;

            if (null == geometricNetwork || null == edge || maxFeatureCount <= 0)
            {
                return(null);
            }
            ITraceFlowSolverGEN traceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
            INetSolver          netSolver       = traceFlowSolver as INetSolver;

            netSolver.SourceNetwork = geometricNetwork.Network;
            INetFlag netFlag = new EdgeFlagClass();

            netFlag.UserClassID = edge.FeatureClassID;
            netFlag.UserID      = edge.FeatureID;
            //no idea when to assign -1, when to do 0
            netFlag.UserSubID = -1;
            traceFlowSolver.PutEdgeOrigins(new IEdgeFlag[1] {
                netFlag as IEdgeFlag
            });
            if (null != disabledFeatureClassIds)
            {
                foreach (int il in disabledFeatureClassIds)
                {
                    if (il > 0)
                    {
                        netSolver.DisableElementClass(il);
                    }
                }
            }
            if (null != stoppers && null != stoppers.Stoppers && stoppers.Stoppers.Length > 0)
            {
                INetElementBarriersGEN netBarriersGEN = null;
                netBarriersGEN             = new NetElementBarriersClass() as INetElementBarriersGEN;
                netBarriersGEN.ElementType = esriElementType.esriETJunction;
                netBarriersGEN.Network     = geometricNetwork.Network;
                netBarriersGEN.SetBarriers(stoppers.FeatureClassID, stoppers.Stoppers);
                netSolver.set_ElementBarriers(esriElementType.esriETJunction, netBarriersGEN as INetElementBarriers);
            }

            IEnumNetEID junctionEIDs = null;
            IEnumNetEID edgeEIDs     = null;

            traceFlowSolver.TraceIndeterminateFlow = false;
            try
            {
                traceFlowSolver.FindFlowElements(direction, esriFlowElements.esriFEEdges, out junctionEIDs, out edgeEIDs);
                if (null != edgeEIDs)
                {
                    if (edgeEIDs.Count <= maxFeatureCount)
                    {
                        IEIDHelper eidHelper = new EIDHelperClass();
                        eidHelper.GeometricNetwork = geometricNetwork;
                        eidHelper.ReturnGeometries = false;
                        eidHelper.ReturnFeatures   = true;
                        //eidHelper.AddField("FType");
                        IEnumEIDInfo eidInfos = eidHelper.CreateEnumEIDInfo(edgeEIDs);
                        eidInfos.Reset();
                        IEIDInfo   eidInfo = null;
                        List <int> ftrs    = new List <int>();
                        //IFeature cadFtr = null;
                        //int ftype;
                        while ((eidInfo = eidInfos.Next()) != null)
                        {
                            ftrs.Add(eidInfo.Feature.OID);

                            /*cadFtr = eidInfo.Feature;
                             * if (null != cadFtr.get_Value(edgeTypeId) && int.TryParse(cadFtr.get_Value(edgeTypeId).ToString(), out ftype))
                             * {
                             *  if(460 == ftype || 558 == ftype)
                             *      ftrs.Add(cadFtr);
                             * }*/
                        }
                        return(ftrs);
                    }
                }
            }
            catch (Exception e)
            {
                if (null != logger)
                {
                    logger.LogMessage(ServerLogger.msgType.error, typeof(AOUtilities).Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, ErrorCode, e.Message);
                }
            }
            finally
            {
                ReleaseCOMObj(traceFlowSolver);
            }
            return(null);
        }
        /// <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());
            }
        }
Exemplo n.º 15
0
        public static void ProfileFindPath(AxMapControl ppAxMapControl, IArray pNetFlagArray, IGeometricNetwork pGeoNetwork)
        {
            try
            {

                IEnumNetEID m_ipEnumNetEID_Junctions, m_ipEnumNetEID_Edges;//����������������Junctions��Edges
                IJunctionFlag[] pArrayJFlag = new IJunctionFlag[2];
                pArrayJFlag[0] = pNetFlagArray.get_Element(0) as IJunctionFlag;
                pArrayJFlag[1] = pNetFlagArray.get_Element(1) as IJunctionFlag;
                ITraceFlowSolverGEN ipTraceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;

                //get the inetsolver interface
                INetSolver ipNetSolver = ipTraceFlowSolver as INetSolver;
                ipNetSolver.SourceNetwork = pGeoNetwork.Network;

                ipTraceFlowSolver.PutJunctionOrigins(ref pArrayJFlag);

                ipTraceFlowSolver.TraceIndeterminateFlow = false;

                object[] totalCost = new object[1];

                //ipTraceFlowSolver.FindSource(esriFlowMethod.esriFMUpstream,esriShortestPathObjFn.esriSPObjFnMinSum,out m_ipEnumNetEID_Junctions,out m_ipEnumNetEID_Edges,1,ref totalCost);

                ipTraceFlowSolver.FindPath(esriFlowMethod.esriFMConnected,
                    esriShortestPathObjFn.esriSPObjFnMinMax,
                    out m_ipEnumNetEID_Junctions,
                    out m_ipEnumNetEID_Edges, 1, ref totalCost);
                //��������еĽ����Ԫ��
                pNetFlagArray.RemoveAll();
                //ma
                //              SpatialHelperFunction.pathToPolyline(pGeoNetwork, ppAxMapControl.ActiveView, m_ipEnumNetEID_Edges);
                //ProfileGetRelatedSewerElevData(ppAxMapControl,pGeoNetwork,m_ipEnumNetEID_Edges, m_ipEnumNetEID_Junctions  );
            }
            catch (Exception eX)
            {
                MessageBox.Show(eX.Message);

            }
        }
Exemplo n.º 16
0
        private void SolvePath(string weightName)
        {
            //创建ITraceFlowSolverGEN
            ITraceFlowSolverGEN pTraceFlowSolverGEN = new TraceFlowSolverClass();
            INetSolver          pNetSolver          = pTraceFlowSolverGEN as INetSolver;
            //初始化用于路径计算的Network
            INetwork pNetWork = mGeometricNetwork.Network;

            pNetSolver.SourceNetwork = pNetWork;

            //获取分析经过的点的个数
            int intCount = mPointCollectionNet.PointCount;

            if (intCount < 1)
            {
                return;
            }


            INetFlag pNetFlag;

            //用于存储路径计算得到的边
            IEdgeFlag[] pEdgeFlags = new IEdgeFlag[intCount];


            IPoint pEdgePoint = new PointClass();
            int    intEdgeEID;
            IPoint pFoundEdgePoint;
            double dblEdgePercent;

            //用于获取几何网络元素的UserID, UserClassID,UserSubID
            INetElements pNetElements = pNetWork as INetElements;
            int          intEdgeUserClassID;
            int          intEdgeUserID;
            int          intEdgeUserSubID;

            for (int i = 0; i < intCount; i++)
            {
                pNetFlag = new EdgeFlagClass();
                //获取用户点击点
                pEdgePoint = mPointCollectionNet.get_Point(i);
                //获取距离用户点击点最近的边
                mPointToEID.GetNearestEdge(pEdgePoint, out intEdgeEID, out pFoundEdgePoint, out dblEdgePercent);
                if (intEdgeEID <= 0)
                {
                    continue;
                }
                //根据得到的边查询对应的几何网络中的元素UserID, UserClassID,UserSubID
                pNetElements.QueryIDs(intEdgeEID, esriElementType.esriETEdge,
                                      out intEdgeUserClassID, out intEdgeUserID, out intEdgeUserSubID);
                if (intEdgeUserClassID <= 0 || intEdgeUserID <= 0)
                {
                    continue;
                }

                pNetFlag.UserClassID = intEdgeUserClassID;
                pNetFlag.UserID      = intEdgeUserID;
                pNetFlag.UserSubID   = intEdgeUserSubID;
                pEdgeFlags[i]        = pNetFlag as IEdgeFlag;
            }
            //设置路径求解的边
            pTraceFlowSolverGEN.PutEdgeOrigins(ref pEdgeFlags);

            //路径计算权重
            INetSchema pNetSchema = pNetWork as INetSchema;
            INetWeight pNetWeight = pNetSchema.get_WeightByName(weightName);

            if (pNetWeight == null)
            {
                return;
            }

            //设置权重,这里双向的权重设为一致
            INetSolverWeights pNetSolverWeights = pTraceFlowSolverGEN as INetSolverWeights;

            pNetSolverWeights.ToFromEdgeWeight = pNetWeight;
            pNetSolverWeights.FromToEdgeWeight = pNetWeight;

            object[] arrResults = new object[intCount - 1];
            //执行路径计算
            pTraceFlowSolverGEN.FindPath(esriFlowMethod.esriFMConnected, esriShortestPathObjFn.esriSPObjFnMinSum,
                                         out mEnumNetEID_Junctions, out mEnumNetEID_Edges, intCount - 1, ref arrResults);

            //获取路径计算总代价(cost)
            mdblPathCost = 0;
            for (int i = 0; i < intCount - 1; i++)
            {
                mdblPathCost += (double)arrResults[i];
            }
        }
Exemplo n.º 17
0
        //����׷�ٵĽ��
        public static void FindStreamTraceResult(AxMapControl ppAxMapControl, INetFlag ipNetFlag, IGeometricNetwork pGeoNetwork)
        {
            try
            {

                IEnumNetEID m_ipEnumNetEID_Junctions, m_ipEnumNetEID_Edges;//����������������Junctions��Edges
                IJunctionFlag[] pArrayJFlag = new IJunctionFlag[1];
                pArrayJFlag[0] = ipNetFlag as IJunctionFlag;
                ITraceFlowSolverGEN ipTraceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;

                //get the inetsolver interface
                INetSolver ipNetSolver = ipTraceFlowSolver as INetSolver;
                ipNetSolver.SourceNetwork = pGeoNetwork.Network;

                ipTraceFlowSolver.PutJunctionOrigins(ref pArrayJFlag);

                ipTraceFlowSolver.TraceIndeterminateFlow = false;

                object[] totalCost = new object[1];

                //ipTraceFlowSolver.FindSource(esriFlowMethod.esriFMUpstream,esriShortestPathObjFn.esriSPObjFnMinSum,out m_ipEnumNetEID_Junctions,out m_ipEnumNetEID_Edges,1,ref totalCost);

                ipTraceFlowSolver.FindFlowElements(esriFlowMethod.esriFMUpstream,
                    esriFlowElements.esriFEEdges,
                    out m_ipEnumNetEID_Junctions,
                    out m_ipEnumNetEID_Edges);

                //ma
                //SpatialHelperFunction.pathToPolyline(pGeoNetwork, ppAxMapControl.ActiveView, m_ipEnumNetEID_Edges);
                //UpStreamFindParcels(ppAxMapControl, m_ipEnumNetEID_Edges,pGeoNetwork);
            }
            catch (Exception eX)
            {
                MessageBox.Show(eX.Message);

            }
        }
		public ITraceFlowSolverGEN UTIL_coreTraceSetup()
		{
			// get the current network's logical network
			INetworkAnalysisExt nax = m_utilNetExt as INetworkAnalysisExt;
			INetwork net = nax.CurrentNetwork.Network;

			// create a new TraceFlowSolver object and
			// set the source network for the solve
			ITraceFlowSolverGEN tfs = new TraceFlowSolverClass() as ITraceFlowSolverGEN;
			INetSolver netSolver = tfs as INetSolver;
			netSolver.SourceNetwork = net;

			// get the barriers for the network, using the element barriers and 
			// selection barriers that have been added using the user interface
			INetworkAnalysisExtBarriers naxBarriers = m_utilNetExt as INetworkAnalysisExtBarriers;
			INetElementBarriers juncElemBarriers, edgeElemBarriers;
			naxBarriers.CreateElementBarriers(out juncElemBarriers, out edgeElemBarriers);
			ISelectionSetBarriers selSetBarriers;
			naxBarriers.CreateSelectionBarriers(out selSetBarriers);
			netSolver.set_ElementBarriers(esriElementType.esriETJunction, juncElemBarriers);
			netSolver.set_ElementBarriers(esriElementType.esriETEdge, edgeElemBarriers);
			netSolver.SelectionSetBarriers = selSetBarriers;

			// set up the disabled layers for the network solver
			// for each feature layer belonging to this network, determine if it is
			// enabled or disabled; if it's disabled, then notify the network solver
			for (int i = 0; i < nax.FeatureLayerCount; i++)
			{
				IFeatureLayer featureLayer = nax.get_FeatureLayer(i);
				if (naxBarriers.GetDisabledLayer(featureLayer))
					netSolver.DisableElementClass(featureLayer.FeatureClass.FeatureClassID);
			}

			INetworkAnalysisExtWeightFilter naxWeightFilter = m_utilNetExt as INetworkAnalysisExtWeightFilter;
			INetSolverWeightsGEN netSolverWeights = netSolver as INetSolverWeightsGEN;
			INetSchema netSchema = net as INetSchema;
			
			// create the junction weight filter
			int juncFilterRangeCount = naxWeightFilter.get_FilterRangeCount(esriElementType.esriETJunction);
			if (juncFilterRangeCount > 0)
			{
				INetWeight netWeight = netSchema.get_WeightByName(naxWeightFilter.JunctionWeightFilterName);
				netSolverWeights.JunctionFilterWeight = netWeight;

				esriWeightFilterType juncWeightFilterType;
				bool juncApplyNotOperator;
				naxWeightFilter.GetFilterType(esriElementType.esriETJunction,
					out juncWeightFilterType, out juncApplyNotOperator);
				netSolverWeights.SetFilterType(esriElementType.esriETJunction,
					juncWeightFilterType, juncApplyNotOperator);

				object[] juncFromValues = new object[juncFilterRangeCount];
				object[] juncToValues = new object[juncFilterRangeCount];
				for (int i = 0; i < juncFilterRangeCount; i++)
				{
					naxWeightFilter.GetFilterRange(esriElementType.esriETJunction, i,
						out juncFromValues[i], out juncToValues[i]);
				}
				netSolverWeights.SetFilterRanges(esriElementType.esriETJunction,
					ref juncFromValues, ref juncToValues);
			}

			// create the edge weight filters
			int edgeFilterRangeCount = naxWeightFilter.get_FilterRangeCount(esriElementType.esriETEdge);
			if (edgeFilterRangeCount > 0)
			{
				INetWeight fromToNetWeight = netSchema.get_WeightByName(naxWeightFilter.FromToEdgeWeightFilterName);
				netSolverWeights.FromToEdgeFilterWeight = fromToNetWeight;
				INetWeight toFromNetWeight = netSchema.get_WeightByName(naxWeightFilter.ToFromEdgeWeightFilterName);
				netSolverWeights.ToFromEdgeFilterWeight = toFromNetWeight;

				esriWeightFilterType edgeWeightFilterType;
				bool edgeApplyNotOperator;
				naxWeightFilter.GetFilterType(esriElementType.esriETEdge,
					out edgeWeightFilterType, out edgeApplyNotOperator);
				netSolverWeights.SetFilterType(esriElementType.esriETEdge,
					edgeWeightFilterType, edgeApplyNotOperator);

				object[] edgeFromValues = new object[edgeFilterRangeCount];
				object[] edgeToValues = new object[edgeFilterRangeCount];
				for (int i = 0; i < edgeFilterRangeCount; i++)
				{
					naxWeightFilter.GetFilterRange(esriElementType.esriETEdge, i,
						out edgeFromValues[i], out edgeToValues[i]);
				}
				netSolverWeights.SetFilterRanges(esriElementType.esriETEdge,
					ref edgeFromValues, ref edgeToValues);
			}

			INetworkAnalysisExtFlags naxFlags = m_utilNetExt as INetworkAnalysisExtFlags;

			// assign the edge flags to the network solver
			int edgeFlagCount = naxFlags.EdgeFlagCount;
			if (edgeFlagCount > 0)
			{
				IEdgeFlag[] edgeFlags = new IEdgeFlag[edgeFlagCount];
				for (int i = 0; i < edgeFlagCount; i++)
				{
					IEdgeFlagDisplay edgeFlagDisplay = naxFlags.get_EdgeFlag(i);
					IFlagDisplay flagDisplay = edgeFlagDisplay as IFlagDisplay;
					IEdgeFlag edgeFlag = new EdgeFlagClass();
					edgeFlag.Position = Convert.ToSingle(edgeFlagDisplay.Percentage);
					INetFlag netFlag = edgeFlag as INetFlag;
					netFlag.UserClassID = flagDisplay.FeatureClassID;
					netFlag.UserID = flagDisplay.FID;
					netFlag.UserSubID = flagDisplay.SubID;
					edgeFlags[i] = edgeFlag;
				}
				tfs.PutEdgeOrigins(ref edgeFlags);
			}

			// assign the junction flags to the network solver
			int juncFlagCount = naxFlags.JunctionFlagCount;
			if (juncFlagCount > 0)
			{
				IJunctionFlag[] juncFlags = new IJunctionFlag[juncFlagCount];
				for (int i = 0; i < juncFlagCount; i++)
				{
					IJunctionFlagDisplay juncFlagDisplay = naxFlags.get_JunctionFlag(i);
					IFlagDisplay flagDisplay = juncFlagDisplay as IFlagDisplay;
					IJunctionFlag juncFlag = new JunctionFlagClass();
					INetFlag netFlag = juncFlag as INetFlag;
					netFlag.UserClassID = flagDisplay.FeatureClassID;
					netFlag.UserID = flagDisplay.FID;
					netFlag.UserSubID = flagDisplay.SubID;
					juncFlags[i] = juncFlag;
				}
				tfs.PutJunctionOrigins(ref juncFlags);
			}

			// set the option for tracing on indeterminate flow
			ITraceTasks traceTasks = m_utilNetExt as ITraceTasks;
			tfs.TraceIndeterminateFlow = traceTasks.TraceIndeterminateFlow;

			return tfs;
		}
Exemplo n.º 19
0
 public NetAnalysisFrm()
 {
     InitializeComponent();
     traceFlowsolverGen = new TraceFlowSolverClass();
     netsolver          = traceFlowsolverGen as INetSolver;
 }
Exemplo n.º 20
0
        /// <summary>
        /// 求解最短路径
        /// </summary>
        /// <param name="_pMap"></param>
        /// <param name="_pGeometricNetwork"></param>
        /// <param name="_pWeightName"></param>
        /// <param name="_pPoints"></param>
        /// <param name="_pDist"></param>
        /// <param name="_pPolyline"></param>
        /// <param name="_pPathCost"></param>
        public void SolvePath(IMap _pMap, IGeometricNetwork _pGeometricNetwork, string _pWeightName, IPointCollection _pPoints, double _pDist, ref IPolyline _pPolyline, ref double _pPathCost)
        {
            try
            { // 这4个参数其实就是一个定位Element的指标
                int intEdgeUserClassID;

                int intEdgeUserID;

                int intEdgeUserSubID;

                int intEdgeID;

                IPoint pFoundEdgePoint;

                double dblEdgePercent;

                ITraceFlowSolverGEN pTraceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;

                INetSolver pNetSolver = pTraceFlowSolver as INetSolver;

                //操作是针对逻辑网络的,INetwork是逻辑网络

                INetwork pNetwork = _pGeometricNetwork.Network;

                pNetSolver.SourceNetwork = pNetwork;

                INetElements pNetElements = pNetwork as INetElements;

                int pCount = _pPoints.PointCount;
                //定义一个边线旗数组
                IEdgeFlag[] pEdgeFlagList = new EdgeFlagClass[pCount];

                IPointToEID pPointToEID = new PointToEIDClass();

                pPointToEID.SourceMap = _pMap;

                pPointToEID.GeometricNetwork = _pGeometricNetwork;

                pPointToEID.SnapTolerance = _pDist;

                for (int i = 0; i < pCount; i++)
                {
                    INetFlag pNetFlag = new EdgeFlagClass() as INetFlag;

                    IPoint pEdgePoint = _pPoints.get_Point(i);
                    //查找输入点的最近的边线
                    pPointToEID.GetNearestEdge(pEdgePoint, out intEdgeID, out pFoundEdgePoint, out dblEdgePercent);

                    pNetElements.QueryIDs(intEdgeID, esriElementType.esriETEdge, out intEdgeUserClassID, out intEdgeUserID, out intEdgeUserSubID);

                    pNetFlag.UserClassID = intEdgeUserClassID;

                    pNetFlag.UserID = intEdgeUserID;

                    pNetFlag.UserSubID = intEdgeUserSubID;

                    IEdgeFlag pTemp = (IEdgeFlag)(pNetFlag as IEdgeFlag);
                    pEdgeFlagList[i] = pTemp;
                }
                pTraceFlowSolver.PutEdgeOrigins(ref pEdgeFlagList);

                INetSchema pNetSchema = pNetwork as INetSchema;

                INetWeight pNetWeight = pNetSchema.get_WeightByName(_pWeightName);

                INetSolverWeightsGEN pNetSolverWeights = pTraceFlowSolver as INetSolverWeightsGEN;

                pNetSolverWeights.FromToEdgeWeight = pNetWeight;//开始边线的权重

                pNetSolverWeights.ToFromEdgeWeight = pNetWeight;//终止边线的权重

                object[] pRes = new object[pCount - 1];

                //通过FindPath得到边线和交汇点的集合
                IEnumNetEID  pEnumNetEID_Junctions;

                IEnumNetEID pEnumNetEID_Edges;

                pTraceFlowSolver.FindPath(esriFlowMethod.esriFMConnected,
                 esriShortestPathObjFn.esriSPObjFnMinSum,
                 out pEnumNetEID_Junctions, out pEnumNetEID_Edges, pCount - 1, ref pRes);
                //计算元素成本
                _pPathCost = 0;
                for (int i = 0; i < pRes.Length; i++)
                {
                    double m_Va = (double)pRes[i];

                    _pPathCost = _pPathCost + m_Va;
                }

                IGeometryCollection pNewGeometryColl = _pPolyline as IGeometryCollection;//QI

                ISpatialReference pSpatialReference = _pMap.SpatialReference;

                IEIDHelper pEIDHelper = new EIDHelperClass();

                pEIDHelper.GeometricNetwork = _pGeometricNetwork;

                pEIDHelper.OutputSpatialReference = pSpatialReference;

                pEIDHelper.ReturnGeometries = true;

                IEnumEIDInfo pEnumEIDInfo = pEIDHelper.CreateEnumEIDInfo(pEnumNetEID_Edges);

                int Count = pEnumEIDInfo.Count;

                pEnumEIDInfo.Reset();

                for (int i = 0; i < Count; i++)
                {
                    IEIDInfo pEIDInfo = pEnumEIDInfo.Next();

                    IGeometry pGeometry = pEIDInfo.Geometry;

                    pNewGeometryColl.AddGeometryCollection(pGeometry as IGeometryCollection);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }