.NET

Reply
Valued Contributor
mbujak
Posts: 53
Registered: ‎07-07-2004
Message 1 of 4 (1,526 Views)
Accepted Solution

MDIActiveDocument and DocumentActivated

1526 Views, 3 Replies
09-21-2012 01:16 PM

I have set a handler for the DocumentActivated event message but it doesn't get called when I set the MDIActiveDocument on the document manager object. It gets called when I swich windows, but not when the active document is initially set. Any ideas?

 

[CommandMethod("IFX_NEW_ASSEMBLY", CommandFlags.Session)]

void NewAssembly()

{

     Document doc = Application.DocumentManagerr.Add("");

     Application.DocumentManager.MDIActiveDocument = doc;

}

 

Should the DocumentActivated event be fired with the above code. P.S. I am positive the event handler is hooked up correctly.

 

- Mike B

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;

 

*Expert Elite*
norman.yuan
Posts: 1,017
Registered: ‎04-27-2009
Message 2 of 4 (1,514 Views)

Re: MDIActiveDocument and DocumentActivated

09-21-2012 02:31 PM in reply to: mbujak

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;

 

Valued Contributor
mbujak
Posts: 53
Registered: ‎07-07-2004
Message 3 of 4 (1,493 Views)

Re: MDIActiveDocument and DocumentActivated

09-22-2012 06:52 AM in reply to: norman.yuan

No different than your code except I have an AppData class that manages custom DocData class collection. The AppData has an Initialization method where the wiring is done. This gets called by the extension class initialization.

 

I removed the assignment and it seems to fire now.

 

Thanks

 

-Mike

New Member
cad.people
Posts: 2
Registered: ‎03-07-2013
Message 4 of 4 (1,294 Views)

Re: MDIActiveDocument and DocumentActivated

03-13-2013 09:17 PM in reply to: norman.yuan

Is anyone else having this same dwg activate problem, with R2013? I have a large project that has stopped working since the 2013 upgrade.

 

I have substituted the new call DocumentCollectionExtension.Add(acDocMgr,templatefile) - and I am using CommandFlags.Session - but the document is still not activated until the command has finished.

 

I've distilled it down to a couple of simple test commands:

 

    <CommandMethod("NewDrawing", CommandFlags.Session)> _
    Public Sub NewDrawing()
        'Create a new drawing and activate it

        Dim strTemplatePath As String = "acad-test.dwt" 'from support path

        Dim acDocMgr As DocumentCollection = Application.DocumentManager
        Dim acDoc As Document = DocumentCollectionExtension.Add(acDocMgr, _
                                            strTemplatePath)
        'the above line should activate the new added document but doesn't
        'acDocMgr.MdiActiveDocument = acDoc  'THIS LINE DOES NOT MAKE THE DWG ACTIVE
        Application.ShowAlertDialog("Active document is now " & acDocMgr.MdiActiveDocument.Name)
        'this stills shows drawing1 until after the command finishes

    End Sub


    <CommandMethod("ActiveDrawing", CommandFlags.Session)> _
    Public Sub ActiveDrawing()
        'Show which drawing is currently active

        Dim acDocMgr As DocumentCollection = Application.DocumentManager

        Application.ShowAlertDialog("Active document is now " & acDocMgr.MdiActiveDocument.Name)
        'this shows Drawing2 if this is run after the NewDrawing command has finished

    End Sub

 

Is there any updated sample code for 2013 anywhere? I could not get the code in the 2013 Dev Guide (here, search for "LockDoc") to compile as it has not been updated to use the new DocumentCollectionExtension methods. I have substituted the new method calls into that code but still have the activation problem.

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.