public static void AddFlag(IPoint pPnt, IApplication app, double snapTol)
pNetAnalysisExt = (INetworkAnalysisExt)app.FindExtensionByCLSID(pID);
startNetFlag = Globals.GetJunctionFlagWithGN(ref pPnt, ref pMap, ref gn, snapTol,out snappedPoint, out EID, out pFlagDisplay, true) as INetFlag;
//startNetFlag = Globals.GetEdgeFlag(ref pPnt, ref pMap, ref gnList, snapTol, ref gnIdx, out snappedPoint, out EID, out distanceAlong, out pFlagDisplay, true) as INetFlag;
Globals.AddFlagToGN(ref pNetAnalysisExt, ref gn, ref pFlagDisplay);
5. The above function will make a call to the getJunctionflag code.create a function block called "getjunctionflag" with the class library Global and place the below code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.GeoDatabaseUI;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.CartoUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.ArcMap;
using ESRI.ArcGIS.NetworkAnalysis;
using ESRI.ArcGIS.NetworkAnalyst;
using ESRI.ArcGIS.EditorExt;
namespace Toolkit
{
class Globals
{
public enum GNTypes
{
Flags = 1, Barries = 2, Results = 3
};
public enum flagType { EdgeFlag, JunctionFlag, EdgeBarrier, JunctionBarrier };
public static IJunctionFlag GetJunctionFlagWithGN(ref IPoint point, ref IMap map, ref IGeometricNetwork gn, double snapTol, out IPoint snappedPoint, out int EID, out IFlagDisplay pFlagDisplay, bool Flag)
{
//Initialize output variables
snappedPoint = null;
EID = -1;
pFlagDisplay = null;
int FCID = -1, FID = -1, subID = -1;
IGeoDataset pDS = null;
IPointToEID pointToEID = null;
INetElements netElements = null;
INetFlag junctionFlag = null;
try
{
pFlagDisplay = null;
pDS = gn.FeatureDataset as IGeoDataset;
point.Project(pDS.SpatialReference);
pointToEID = new PointToEIDClass() as IPointToEID;
// find the nearest junction element to this Point
pointToEID.GeometricNetwork = gn as IGeometricNetwork;
pointToEID.SourceMap = map;
pointToEID.SnapTolerance = snapTol;
try
{
pointToEID.GetNearestJunction(point, out EID, out snappedPoint);
}
catch (Exception ex)
{
}
if (snappedPoint == null)
return null;
// convert the EID to a feature class ID, feature ID, and sub ID
netElements = gn.Network as INetElements;
try
{
netElements.QueryIDs(EID, esriElementType.esriETJunction, out FCID, out FID, out subID);
}
catch (Exception ex)
{
return null;
}
//Create flag for start of trace
junctionFlag = new JunctionFlagClass() as INetFlag;
junctionFlag.UserClassID = FCID;
junctionFlag.UserID = FID;
junctionFlag.UserSubID = subID;
if (junctionFlag is IEdgeFlag)
{
pFlagDisplay = new EdgeFlagDisplayClass();
if (Flag)
pFlagDisplay.Symbol = CreateNetworkFlagBarrierSymbol(flagType.EdgeFlag) as ISymbol;
else
pFlagDisplay.Symbol = CreateNetworkFlagBarrierSymbol(flagType.EdgeBarrier) as ISymbol;
}
else
{
pFlagDisplay = new JunctionFlagDisplayClass();
if (Flag)
pFlagDisplay.Symbol = CreateNetworkFlagBarrierSymbol(flagType.JunctionFlag) as ISymbol;
else
pFlagDisplay.Symbol = CreateNetworkFlagBarrierSymbol(flagType.JunctionBarrier) as ISymbol;
}
pFlagDisplay.ClientClassID = FCID;
pFlagDisplay.FeatureClassID = FID;
pFlagDisplay.SubID = subID;
pFlagDisplay.Geometry = snappedPoint;
return junctionFlag as IJunctionFlag;
}
catch
{
return null;
}
finally
{
pDS = null;
pointToEID = null;
netElements = null;
}
}
public static ISimpleMarkerSymbol CreateNetworkFlagBarrierSymbol(flagType flgType)
{
ISimpleMarkerSymbol pSymbolFlag = null;
switch (flgType)
{
case flagType.EdgeFlag:
pSymbolFlag = new SimpleMarkerSymbolClass();
pSymbolFlag.Style = esriSimpleMarkerStyle.esriSMSSquare;
pSymbolFlag.Angle = 0;
//pSymbolFlag.Color =
pSymbolFlag.Outline = true;
pSymbolFlag.OutlineSize = 1;
//pSymbolFlag.OutlineColor = GetColor(0, 0, 0);
pSymbolFlag.Size = 10; //TODO: UserConfig
break;
case flagType.JunctionFlag:
pSymbolFlag = new SimpleMarkerSymbolClass();
pSymbolFlag.Style = esriSimpleMarkerStyle.esriSMSCircle;
pSymbolFlag.Angle = 0;
//pSymbolFlag.Color = GetColor(0, 255, 0);
pSymbolFlag.Outline = true;
pSymbolFlag.OutlineSize = 1;
//pSymbolFlag.OutlineColor = GetColor(0, 0, 0);
pSymbolFlag.Size = 10; //TODO: UserConfig
break;
case flagType.EdgeBarrier:
pSymbolFlag = new SimpleMarkerSymbolClass();
pSymbolFlag.Style = esriSimpleMarkerStyle.esriSMSDiamond;
pSymbolFlag.Angle = 0;
//pSymbolFlag.Color = GetColor(255, 0, 0);
pSymbolFlag.Outline = true;
pSymbolFlag.OutlineSize = 1;
//pSymbolFlag.OutlineColor = GetColor(0, 0, 0);
pSymbolFlag.Size = 10; //TODO: UserConfig
break;
case flagType.JunctionBarrier:
pSymbolFlag = new SimpleMarkerSymbolClass();
pSymbolFlag.Style = esriSimpleMarkerStyle.esriSMSX;
pSymbolFlag.Angle = 0;
//pSymbolFlag.Color = GetColor(255, 0, 0);
pSymbolFlag.Outline = true;
pSymbolFlag.OutlineSize = 1;
//pSymbolFlag.OutlineColor = GetColor(0, 0, 0);
pSymbolFlag.Size = 10; //TODO: UserConfig
break;
default:
pSymbolFlag = new SimpleMarkerSymbolClass();
pSymbolFlag.Style = esriSimpleMarkerStyle.esriSMSCircle;
pSymbolFlag.Angle = 0;
//pSymbolFlag.Color = GetColor(0, 255, 0);
pSymbolFlag.Outline = true;
pSymbolFlag.OutlineSize = 1;
//pSymbolFlag.OutlineColor = GetColor(0, 0, 0);
pSymbolFlag.Size = 10; //TODO: UserConfig
break;
}
return pSymbolFlag;
}
public static void AddFlagToGN(ref INetworkAnalysisExt pNetworkAnalysisExt, ref ESRI.ArcGIS.Geodatabase.IGeometricNetwork pGeomNet, ref IFlagDisplay pFlagDsiplay)
{
INetworkAnalysisExtFlags pNetworkAnalysisExtFlags = null;
try
{
if (pNetworkAnalysisExt.CurrentNetwork != pGeomNet)
{
pNetworkAnalysisExt.CurrentNetwork = pGeomNet;
}
pNetworkAnalysisExtFlags = (INetworkAnalysisExtFlags)pNetworkAnalysisExt;
if (pFlagDsiplay is IEdgeFlagDisplay)
{
pNetworkAnalysisExtFlags.AddEdgeFlag(pFlagDsiplay as IEdgeFlagDisplay);
}
else
{
pNetworkAnalysisExtFlags.AddJunctionFlag(pFlagDsiplay as IJunctionFlagDisplay);
}
}
catch
{
}
finally
{
pNetworkAnalysisExtFlags = null;
}
}
}
}