MDIActiveDocument and DocumentActivated

MDIActiveDocument and DocumentActivated

Anonymous
Not applicable
10,137 Views
4 Replies
Message 1 of 5

MDIActiveDocument and DocumentActivated

Anonymous
Not applicable

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

0 Likes
Accepted solutions (1)
10,138 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor
Accepted solution

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;

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 5

Anonymous
Not applicable

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

0 Likes
Message 4 of 5

Anonymous
Not applicable

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.

 

0 Likes
Message 5 of 5

Anonymous
Not applicable

"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."

 

Hi,

 

I know this message as been plusblished 2 years ago but I just found the solution of the same problem

 

Use a "CommandFlags.Session" parameters when you declare your CommandMethod.

 

Exemple:

 

    <CommandMethod("ExportSHP", "EXPSHP", CommandFlags.Session + CommandFlags.UsePickSet)> _
    Public Sub Export_Shape()

          ......

    End sub

 

For more informations see :

 

Command Definition (.NET)

 

http://adndevblog.typepad.com/autocad/2012/05/when-to-lock-the-document.html

 

 

 

0 Likes