How do you wire up the DocumentActivated event?
Following code works for me:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
[assembly: CommandClass(typeof(DocumentActevatedEvent.MyCommands))]
[assembly: ExtensionApplication(typeof(DocumentActevatedEvent.MyCommands))]
namespace DocumentActevatedEvent
{
public class MyCommands : IExtensionApplication
{
#region IExtensionApplication Members
public void Initialize()
{
Document dwg = Application.DocumentManager.MdiActiveDocument;
Editor ed = dwg.Editor;
try
{
ed.WriteMessage("\nInitializing...");
Application.DocumentManager.DocumentActivated += DocumentManager_DocumentActivated;
ed.WriteMessage("completed.");
}
catch (System.Exception ex)
{
ed.WriteMessage("failed:\n");
ed.WriteMessage(ex.Message);
}
Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
}
private void DocumentManager_DocumentActivated(object sender, DocumentCollectionEventArgs e)
{
e.Document.Editor.WriteMessage("\n{0} activated.", e.Document.Name);
Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
}
public void Terminate()
{
}
#endregion
[CommandMethod("AddNewDoc", CommandFlags.Session)]
public static void RunMyCommand()
{
Document dwg = Application.DocumentManager.Add(null);
Application.DocumentManager.MdiActiveDocument=dwg;
}
}
} I use AutoCAD 2012 and VS2010.
Actually, the second line of code in the CommandMethod:
Application.Documentmanager.MdiActiveDocument=dwg;
is not needed. When a new document is added, AutoCAD automatically set it as the active document. That is, the DocumentActivated event is triggered at the end of Add() method, not by the code App...MdiActiveDocument=dwg;