Creating a Flag in ArcMap using ArcObjects .NET
1. Add a new add-in component to the CreatingOutputs solution and name the component
AddFlagTool. Select Tool as the type of add-in, set the confi guration of the tool as shown in
2. Enter the following using directives at the top of the AddFlagTool.cs fi le’s code window:
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Carto;
3. You are going to write the code for handling the OnMouseDown event. In other words, you want
your code to be executed when a user clicks somewhere in the main window of the software application. Because all the events and their handlers are defined in the base class of all tools(ESRI.ArcGIS.Desktop.AddIns.Tool), you have to override the necessary handlers. So write
the following code in the AddflagTool.cs file inside the definition for the AddflagTool class and outside of any method: protected override
As soon as you type these two keywords and press the spacebar, you will see the list of all available handlers.
Find OnMouseDown in the list as shown in below Figure and press Enter.
4.On the Mouse down event call the add flag function in the another library
5.For the Add flag function add the following code in the other library addflag function
public static void AddFlag(IPoint pPnt, IApplication app, double snapTol)
{
//IProgressDialogFactory pProDFact = null;
//IStepProgressor pStepPro = null;
//IProgressDialog2 pProDlg = null;
//ITrackCancel pTrkCan = null;
IGeometricNetwork gn = null;
IPoint snappedPoint = null;
IFlagDisplay pFlagDisplay = null;
INetFlag startNetFlag = null;
INetworkAnalysisExt pNetAnalysisExt = null;
IMap pMap = null;
UID pID = null;
int EID = -1;
try
{
pID = new UID();
pID.Value = "esriEditorExt.UtilityNetworkAnalysisExt";
pNetAnalysisExt = (INetworkAnalysisExt)app.FindExtensionByCLSID(pID);
gn = pNetAnalysisExt.CurrentNetwork;
pMap = (app.Document as IMxDocument).FocusMap;
startNetFlag = Globals.GetJunctionFlagWithGN(ref pPnt, ref pMap, ref gn, snapTol,out snappedPoint, out EID, out pFlagDisplay, true) as INetFlag;
if (startNetFlag == null)
{
//startNetFlag = Globals.GetEdgeFlag(ref pPnt, ref pMap, ref gnList, snapTol, ref gnIdx, out snappedPoint, out EID, out distanceAlong, out pFlagDisplay, true) as INetFlag;
}
if (app != null)
{
Globals.AddFlagToGN(ref pNetAnalysisExt, ref gn, ref pFlagDisplay);
// pFlagDisplay
pNetAnalysisExt = null;
pID = null;
}
}
catch (Exception)
{
throw;
}
}
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;
}
}
}
}
6. Run code and set the default debug application as ArcMap
7. Once the ArcMap is open go to customise code and in the command tab add the tool to your window
8.click on the tool will add an flag symbol to the map(use f5 to refresh the map)
No comments:
Post a Comment