This document is archived and information here might be outdated.  Recommended version.


How to set up an ArcGIS Network Analyst extension solve to work with the undo/redo stack (ArcObjects .NET 10.8 SDK)
ArcObjects Help for .NET developers > ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Spatial query and analysis > Network analysis > Programming with the ArcGIS Network Analyst extension > How to set up an ArcGIS Network Analyst extension solve to work with the undo/redo stack

How to set up an ArcGIS Network Analyst extension solve to work with the undo/redo stack


Setting up an ArcGIS Network Analyst extension solve to work with the undo/redo stack

This topic shows how to programmatically solve, keeping the NAWindow in sync, and properly utilize the undo/redo stack.
Use the following code example to set up an ArcGIS Network Analyst extension solve.
  • The code example is best used by adding this method to an add-in command or to a stand-alone ArcEngine application.
  • For the add-in usage in ArcMap, change the usage of IEngineNAWindow to INAWindow and IEngineNetworkAnalystEnvironment to INetworkAnalystExtension.
[C#]
public void SolveWithinUndoRedoOperation()
{
    // Get the extension and the NAWindow to have access to the undo/redo stack.
    ESRI.ArcGIS.Controls.IEngineNetworkAnalystEnvironment engineNAEnvironment=new
        ESRI.ArcGIS.Controls.EngineNetworkAnalystEnvironmentClass();
    ESRI.ArcGIS.Controls.IEngineNAWindow naWindow=engineNAEnvironment.NAWindow;

    // Get the context and solver on which you are doing analysis.
    ESRI.ArcGIS.NetworkAnalyst.INAContext naContext =
        naWindow.ActiveAnalysis.Context;
    ESRI.ArcGIS.NetworkAnalyst.INASolver naSolver=naContext.Solver;

    // Set up the track cancel to allow the user to stop the solve at any time.
    ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel=new
        ESRI.ArcGIS.Display.CancelTrackerClass();
    trackCancel.CancelOnClick=false;
    trackCancel.CancelOnKeyPress=true;

    // Use StartOperation to start an edit operation on items in an NAClass. It should be followed with a call to StopOperation.  
    // The combination of these two calls adds an edit operation to ArcMap that can be undone/redone using the undo/redo buttons in ArcMap.
    naWindow.StartOperation(naContext);

    // Turning on DelayEvents stops the NAWindow from responding to system events, until it is turned back on.
    //  This will hold off on updating NALocationObjects in the window, helping the system be more responsive during solve.
    naWindow.DelayEvents(null, true);

    try
    {
        naSolver.Solve(naContext, new ESRI.ArcGIS.Geodatabase.GPMessagesClass(),
            trackCancel);
        naWindow.Message="Solve completed";

        // Stop the operation. The message passed shows on the Undo button.
        naWindow.StopOperation(naContext, "Solve");
    }
    catch (System.Exception e)
    {
        // User aborted this by choice, roll it back.
        if (e.Message == "Solve operation cancelled by user." ||
            !trackCancel.Continue())
        {
            naWindow.Message=e.Message;

            // AbortOperation will stop and automatically undo the current operation.
            naWindow.AbortOperation(naContext);
        }
        else
        {
            naWindow.StopOperation(naContext, "Solve");
            naWindow.Message="Error: " + e.Message;
        }
    }
    // Resume listening to events.
    naWindow.DelayEvents(null, false);

    // Notify clients that the context has changed.
    var naContextEdit=naContext as ESRI.ArcGIS.NetworkAnalyst.INAContextEdit;
    naContextEdit.ContextChanged();

    // Update the NAWindow to have the latest contents.
    naWindow.UpdateContent(null);
}
[VB.NET]
Public Sub SolveWithinUndoRedoOperation()
    ' Get the extension and the NAWindow to have access to the undo/redo stack.
    Dim engineNAEnvironment As ESRI.ArcGIS.Controls.IEngineNetworkAnalystEnvironment=New ESRI.ArcGIS.Controls.EngineNetworkAnalystEnvironmentClass()
    Dim naWindow As ESRI.ArcGIS.Controls.IEngineNAWindow=engineNAEnvironment.NAWindow
    
    ' Get the context and solver on which you are doing analysis.
    Dim naContext As ESRI.ArcGIS.NetworkAnalyst.INAContext=naWindow.ActiveAnalysis.Context
    Dim naSolver As ESRI.ArcGIS.NetworkAnalyst.INASolver=naContext.Solver
    
    ' Set up the track cancel to allow the user to stop the solve at any time.
    Dim trackCancel As ESRI.ArcGIS.esriSystem.ITrackCancel=New ESRI.ArcGIS.Display.CancelTrackerClass()
    trackCancel.CancelOnClick=False
    trackCancel.CancelOnKeyPress=True
    
    ' Use StartOperation to start an edit operation on items in an NAClass. It should be followed with a call to StopOperation.
    ' The combination of these two calls adds an edit operation to ArcMap that can be undone/redone using the undo/redo buttons in ArcMap.
    naWindow.StartOperation(naContext)
    
    ' Turning on DelayEvents stops the NAWindow from responding to system events, until it is turned back on.
    ' This will hold off on updating NALocationObjects in the window, helping the system be more responsive during solve.
    naWindow.DelayEvents(Nothing, True)
    
    Try
    naSolver.Solve(naContext, New ESRI.ArcGIS.Geodatabase.GPMessagesClass(), trackCancel)
    naWindow.Message="Solve completed"
    
    ' Stop the operation. The message passed in shows on the Undo button.
    naWindow.StopOperation(naContext, "Solve")
    Catch e As System.Exception
    ' User aborted this by choice, roll it back.
    If e.Message="Solve operation cancelled by user." OrElse Not trackCancel.[Continue]() Then
        naWindow.Message=e.Message
        
        ' AbortOperation will stop and automatically undo the current operation.
        naWindow.AbortOperation(naContext)
    Else
        naWindow.StopOperation(naContext, "Solve")
        naWindow.Message="Error: " & e.Message
    End If
    End Try
    ' Resume listening to events.
    naWindow.DelayEvents(Nothing, False)
    
    ' Notify clients that the context has changed.
    Dim naContextEdit=TryCast(naContext, ESRI.ArcGIS.NetworkAnalyst.INAContextEdit)
    naContextEdit.ContextChanged()
    
    ' Update the NAWindow to have the latest contents.
    naWindow.UpdateContent(Nothing)
End Sub


See Also:

What is the ArcGIS Network Analyst extension?
About the ArcGIS Network Analyst extension tutorial
Essential ArcGIS Network Analyst extension vocabulary
NetworkAnalyst
ArcGIS Network Analyst extension Object Model Diagram




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS Desktop Advanced: Network Analyst ArcGIS Desktop Advanced: Network Analyst
ArcGIS Desktop Standard: Network Analyst ArcGIS Desktop Standard: Network Analyst
ArcGIS Desktop Basic: Network Analyst ArcGIS Desktop Basic: Network Analyst