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