Select to view content in your preferred language

Working on C# code that will allow user to create "Barriers" on a map

5062
20
11-17-2011 03:45 AM
ReneeCammarere
Deactivated User
Right now, I have code that allows the user to indicate on a map (that contains a Network Dataset layer) "Stops" that are needed to create a "Route". I need to add the functionality to the interface that allows the user to also create "Barriers" as well as "Stops".

If I do this in ArcMap, after the "Route" layer is created by clicking on "Network Analyst" and "New Route", I would just make the Network Analyst window visible, and then highlight the "Restriction" layer inside the "Line Barriers" layer, and then click on the button for "Create Network Location Tool" which allows me to draw the barrier on the map. I already have implemented the ability to create Stops by using ControlsNetworkAnalystCreateLocationToolClass().

My question is, how can I also implement the capability of creating "Barriers" as well as "Stops" into my code? Thanks, Renee
Tags (2)
0 Kudos
20 Replies
PatrickStevens
Esri Contributor
Hello, Renee!

To be clear, are you interested in loading barriers programmatically from already existing polygon/polyline features?  Or are you attempting to have users draw them manually in a stand-alone application?
0 Kudos
ReneeCammarere
Deactivated User
I want the user to be able to draw the barriers using the "Create Network Location Tool"
0 Kudos
ReneeCammarere
Deactivated User
I'm not sure how, but I must have inadvertently created a new user login for myself with that last thread.  Sorry about that.
0 Kudos
ReneeCammarere
Deactivated User
Right now I am looking into using the INAWindow object for what I need to do:
http://edndoc.esri.com/arcobjects/9.2/ComponentHelp/esriNetworkAnalystUI/INAWindow_ActiveCategory.ht...
I need to somehow select one of the barrier layers in the N.A. Window.
0 Kudos
PatrickStevens
Esri Contributor
Nice detective work.  That is where I was going to send you.

The create locations tool should work appropriately depending on the type of class that is active.  For stops and point barriers, it will place points.  For line barriers, it will draw polylines.  For polygon barriers, it will draw polygons.
0 Kudos
ReneeCammarere
Deactivated User
I may be on the right track, but I've never used an IApplication object, so I'm not sure what to do about that.
0 Kudos
PatrickStevens
Esri Contributor
So, you need to set the active category on the active analysis shown in the NAWindow.

To start, here is a bit of code to get the EngineNetworkAnalystEnvironment:

public static IEngineNetworkAnalystEnvironment GetTheEngineNetworkAnalystEnvironment()       
{           
// The network analyst environment is a singleton, and must be accessed using the System.Activator           
System.Type t = System.Type.GetTypeFromProgID("esriControls.EngineNetworkAnalystEnvironment");           
var naEnv = System.Activator.CreateInstance(t) as IEngineNetworkAnalystEnvironment;           
return naEnv;       
}

Use that (along with a NALayer) to set your active analysis layer:

// Set the active Analysis layer
IEngineNAWindow naWindow = naEnv.NAWindow;
if (naWindow.ActiveAnalysis != naLayer)   naWindow.ActiveAnalysis = naLayer; 

At that point, you should be able to set the active category by clicking on it in the NAWindow during application runtime. 

Does this work for you?  You can set the category programmatically, but I'm not sure it is necessary in your case.  Once the naWindow is open, has an active analysis in it, and has an active category selected, the add location tool should work as you expect it to.
0 Kudos
ReneeCammarere
Deactivated User
I was just looking at the AppRef class:
"Since AppRef is a singleton, you should use the Activator class to create an instance of it.
// Get the actual underlying COM type
Type t = Type.GetTypeFromCLSID(typeof(AppRefClass).GUID);
// Or if ProgID or CLSID is known, use it directly
//Type t = Type.GetTypeFromProgID("esriFramework.AppRef");
//Type t = Type.GetTypeFromCLSID(new Guid("e1740ec5-9513-11d2-a2df-0000f8774fb5"));
System.Object obj = Activator.CreateInstance(t);
IApplication app = obj as IApplication;"

What you sent me looks more friendly.  I'll try out the code that you sent and let you know.  Thanks!
0 Kudos
ReneeCammarere
Deactivated User
Is naLayer an INALayer2 object?  As in . . .
var naLayer = (INALayer2)Layers.FindNetworkLayerInMap(map, "Route");

Is the Route layer that shows up in the table of contents something different from the layers in the Network Analyst Window?  Thanks!
0 Kudos