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

4249
20
11-17-2011 03:45 AM
ReneeCammarere
Occasional Contributor
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
ReneeCammarere
Occasional Contributor
Thanks for all your help Patrick! 

I am using the following line to define naLayer:

INALayer naLayer = Layers.FindNetworkLayerInMap(map, "Line Barriers");

I'll see how this goes.
0 Kudos
ReneeCammarere
Occasional Contributor
When I got to the following line, I had a catastrophic failure (COM exception).  I will research to learn more about the object "IEngineNetworkAnalystEnvironment".

               IEngineNAWindow naWindow = naEnv.NAWindow;
0 Kudos
ReneeCammarere
Occasional Contributor
When I'm tracing down through the code, this is what the naEnv object looks like before the code crashes.  Do you know what could be going on with this?
0 Kudos
PatrickStevens
Esri Contributor
Just to make certain, your application is a stand-alone Engine application using Engine controls, correct?  This is not designed to work within ArcMap.

I need to look around and see if there is any further initialization to get the NA environment object to work correctly.
0 Kudos
PatrickStevens
Esri Contributor
Do you have access to the ArcObject .NET SDK?  If so install it and get one of the installed samples called NAEngine, also found here:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/0001000000pw000000.ht...

This app shows how to use the ToolbarControl, TOCControl, and MapControl to do network analysis work.  The Create Locations tool is already added to the toolbar, as is the button that opens the NAWindow.

Once there is an active analysis layer in the NAWindow and one of the barriers classes selected in the NAWindow, you can make sure the Create Location tool is selected on the toolbar.  At that point, you will be able to draw barriers appropriately.

Hopefully, this gives you everything you need.  At least, it is a great place to get reference code on how to program network analysis using the engine tools.
0 Kudos
ReneeCammarere
Occasional Contributor
Sorry, Patrick, it's been a while since I checked this.  Anyway, I resolved the issue with the code crashing.  I know (after a lot of trial and error) how to use this object.  I'm close to having an answer with this, but am still having problems with the code.  Since I can't insert screen shots here, it's hard to explain.  However, here is a small portion of the code . . .
       1         for (int j = 0; j < aCompLyr.Count; j++)
       2                     {
       3                        pLayer = aCompLyr.get_Layer(j);
       4                         aStr = pLayer.Name;
       5                         if (aStr == "Line Barriers")
       6                         {
       7                             naWindow.ActiveAnalysis = (INALayer)pLayer;
       8                            
       9                         }
       10                    }
I get an error on line #7 that reads as follows:
Unable to cast COM object of type 'System.__ComObject' to interface type 'ESRI.ArcGIS.NetworkAnalyst.INALayer'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{667B776B-5905-4450-9C94-18B214ECE8FB}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).


I would appreciate any suggestions.  Thanks!
0 Kudos
PatrickStevens
Esri Contributor
I think your problem is that you are casting one of the sublayers of the NALayer composite layer as an NALayer.  Using the Route layer as an example, a Route layer is a NALayer that can be cast as INALayer.  The Route layer contains 5 sublayers called Stops, Routes, Barriers, PolygonBarriers, and PolylineBarriers.  In your case, you are trying to cast the Lines Barriers class to an NALayer, when that class is only a feature layer that happens to be a sublayer of the Routes NALayer.

So, as you iterate through the layers in your map, be sure to assign the full analysis layer to the ActiveAnalysis, instead of assigning one of the sublayers.

Here is a bit of documentation about analysis layers:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//004700000034000000.htm
0 Kudos
ReneeCammarere
Occasional Contributor
Thanks Pat - I finally figured that out. Now, I'm just doing the following:

                IEngineNetworkAnalystEnvironment naEnv = new EngineNetworkAnalystEnvironmentClass();
                IEngineNAWindow naWindow = naEnv.NAWindow;
                naWindow.ActiveCategory = naWindow.get_CategoryByNAClassName("Barriers");
                naWindow.ActiveCategory.Layer.Name = "Line Barriers";
                naWindow.UpdateContent(naWindow.ActiveCategory);

This allows me to use the Location tool to put point barriers on the map. However, I really want to place line barriers (that's why the last 2 lines are in there). How can I place line or polygon barriers instead of point barriers? Thanks again!

Renee
0 Kudos
PatrickStevens
Esri Contributor
Try changing "Barriers" to "PolylineBarriers", like this:

    naWindow.ActiveCategory = naWindow.get_CategoryByNAClassName("PolylineBarriers");

Let me know if that does it.
0 Kudos
ReneeCammarere
Occasional Contributor
Yes, that did it.  Thanks so much!
0 Kudos