Zoom to a Window in Editor using CommandASync

By Madhukar Moogala

In this post, I will illustrate a sample based on AutoCAD 2015 API Editor.CommandAsync.

Problem: Can I zoom to particular window in an Editor as long as I press esc or cancel to quit?

Answer: Yes, with help of CommandAsync, you can achieve this task.

#region "send Zoom command by callback function"
private static bool ZoomExit = false;
//declare the callback delegation
delegate void Del();
private static Del _actionCompletedDelegate;
// Exit function,check if Zoom command is esccancelled
static void
MdiActiveDocument_CommandCancelled(object sender,CommandEventArgs e)
{
    ZoomExit = true;
}
[CommandMethod("TestZoom")]
public static void TestZoom()
{
   var ed = Application.DocumentManager.MdiActiveDocument.Editor;
   var doc = Application.DocumentManager.MdiActiveDocument;
   //esc event
   doc.CommandCancelled += MdiActiveDocument_CommandCancelled;
   // start Zoom command
   Editor.CommandResult cmdResult1 =
   ed.CommandAsync(new object[]{
   "_.ZOOM", Editor.PauseToken, Editor.PauseToken});
   // delegate callback function, wait for interaction ends 
   _actionCompletedDelegate = new Del(CreateZoomAsyncCallback);
   cmdResult1.OnCompleted(new Action(_actionCompletedDelegate));
   ZoomExit = false;
}
// callback function 
public static void CreateZoomAsyncCallback()
{
    var ed = Application.DocumentManager.MdiActiveDocument.Editor;
    //if Zoom command is running 
    if (!ZoomExit)
    {
        // AutoCAD hands over to the callback function 
        Editor.CommandResult cmdResult1 =
        ed.CommandAsync(new object[]{
        "_.ZOOM", Editor.PauseToken, Editor.PauseToken});
        // delegate callback function, wait for interaction ends 
        _actionCompletedDelegate = new Del(CreateZoomAsyncCallback);
       cmdResult1.OnCompleted(new Action(_actionCompletedDelegate));
    }
    else
    {
        ed.WriteMessage("Zoom Exit");
        return;
    }
}
#endregion

Comments

4 responses to “Zoom to a Window in Editor using CommandASync”

  1. Hi Madhukar!
    All code is broken because of trimming spaces. Please repost it.

  2. Hi Alex,
    Sorry for the trouble, can you check now ?

  3. Now it is OK. Thanks!

  4. Vaidas Velutis Avatar
    Vaidas Velutis

    i am getting eNotAplicable with this code, mayby someone knows why :)

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading