+ Reply to Thread
Results 1 to 21 of 21

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

  1. #1
    Renee Cammarere
    Join Date
    Feb 2011
    Posts
    50
    Points
    3
    Answers Provided
    5


    0

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

    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

  2. #2
    Patrick Stevens
    Join Date
    Oct 2009
    Posts
    118
    Points
    72
    Answers Provided
    10


    0

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

    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?

  3. #3
    Renee Cammarere
    Join Date
    Nov 2011
    Posts
    19
    Points
    1
    Answers Provided
    1


    0

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

    I want the user to be able to draw the barriers using the "Create Network Location Tool"

  4. #4
    Renee Cammarere
    Join Date
    Feb 2011
    Posts
    50
    Points
    3
    Answers Provided
    5


    0

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

    I'm not sure how, but I must have inadvertently created a new user login for myself with that last thread. Sorry about that.

  5. #5
    Renee Cammarere
    Join Date
    Feb 2011
    Posts
    50
    Points
    3
    Answers Provided
    5


    0

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

    Right now I am looking into using the INAWindow object for what I need to do:
    http://edndoc.esri.com/arcobjects/9....veCategory.htm
    I need to somehow select one of the barrier layers in the N.A. Window.

  6. #6
    Patrick Stevens
    Join Date
    Oct 2009
    Posts
    118
    Points
    72
    Answers Provided
    10


    0

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

    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.

  7. #7
    Renee Cammarere
    Join Date
    Feb 2011
    Posts
    50
    Points
    3
    Answers Provided
    5


    0

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

    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.

  8. #8
    Patrick Stevens
    Join Date
    Oct 2009
    Posts
    118
    Points
    72
    Answers Provided
    10


    0

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

    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.

  9. #9
    Renee Cammarere
    Join Date
    Feb 2011
    Posts
    50
    Points
    3
    Answers Provided
    5


    0

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

    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!

  10. #10
    Renee Cammarere
    Join Date
    Feb 2011
    Posts
    50
    Points
    3
    Answers Provided
    5


    0

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

    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!

  11. #11
    Renee Cammarere
    Join Date
    Feb 2011
    Posts
    50
    Points
    3
    Answers Provided
    5


    0

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

    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.

  12. #12
    Renee Cammarere
    Join Date
    Feb 2011
    Posts
    50
    Points
    3
    Answers Provided
    5


    0

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

    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;

  13. #13
    Renee Cammarere
    Join Date
    Feb 2011
    Posts
    50
    Points
    3
    Answers Provided
    5


    0

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

    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?
    Attached Thumbnails Attached Thumbnails Click image for larger version

Name:	ForumBarriersError.jpg‎
Views:	41
Size:	44.5 KB
ID:	10233  

  14. #14
    Patrick Stevens
    Join Date
    Oct 2009
    Posts
    118
    Points
    72
    Answers Provided
    10


    0

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

    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.

  15. #15
    Patrick Stevens
    Join Date
    Oct 2009
    Posts
    118
    Points
    72
    Answers Provided
    10


    0

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

    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/a...00pw000000.htm

    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.

  16. #16
    Renee Cammarere
    Join Date
    Feb 2011
    Posts
    50
    Points
    3
    Answers Provided
    5


    0

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

    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!

  17. #17
    Patrick Stevens
    Join Date
    Oct 2009
    Posts
    118
    Points
    72
    Answers Provided
    10


    0

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

    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/arcgisdesk...0034000000.htm
    Last edited by PatrickStevens; 11-23-2011 at 06:35 AM.

  18. #18
    Renee Cammarere
    Join Date
    Feb 2011
    Posts
    50
    Points
    3
    Answers Provided
    5


    0

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

    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

  19. #19
    Patrick Stevens
    Join Date
    Oct 2009
    Posts
    118
    Points
    72
    Answers Provided
    10


    0

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

    Try changing "Barriers" to "PolylineBarriers", like this:

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

    Let me know if that does it.

  20. #20
    Renee Cammarere
    Join Date
    Feb 2011
    Posts
    50
    Points
    3
    Answers Provided
    5


    0

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

    Yes, that did it. Thanks so much!

  21. #21
    Declan Qian
    Join Date
    Jun 2012
    Posts
    1
    Points
    0
    Answers Provided
    0


    0

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

    Quote Originally Posted by PatrickStevens View Post
    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/a...00pw000000.htm

    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.

    Thank you. This helped me.

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts