Posts: 12
Registered: ‎08-22-2012
Message 1 of 2 (343 Views)
Accepted Solution

Cannot set MdiActiveDocument

343 Views, 1 Replies
08-22-2013 02:04 PM

I cannot seem to set the MdiActiveDocument synchronously.

I have code inside a <CommandMethod> that creates a drawing and then writes any error information to the command line.  Since this is started in one document, if  a fatal error occurs and the drawing does not get created, that message goes to the original document's command line.

In the case where a drawing gets created with errors (say partial success), I would like the errors to appear in the new drawing (new active document's) command line.

The behavior when setting Core.Application.DocumentManager.MdiActiveDocument is peculiar.  The application can hang for a long time if you do not click anything.  If you click the document (from the taskbar or clicking on the window), it breaks out of the hang and continues to execute, but the active document is not changed.  The MdiActiveDocument still points to the original document.  You can also see that it does not change because doc.IsActive = False.  This state could be because when you clicked, you really did make the original document active (again? still?).  To break the hang, I think the click has to be on the original document.

On an example like http://www.theswamp.org/index.php?topic=44476.msg497357#msg497357 there is no action taken after MdiActiveDocument is set, so I assume that it is actually set when the command is completed, not synchronously.  I am not absolutely certain of this, though.


    <CommandMethod("AUTODRAWPN")> _
    Public Sub DrawPartNumber()
        Dim ed As Editor = Core.Application.DocumentManager.MdiActiveDocument.Editor

        If IsNothing(drwr) Then
            Catch ex As System.Exception
                'exit quietly if it is cancelled by the user
                'print the error if an exception happened
                If ex.GetType <> GetType(OperationCanceledException) Then
                    ed.WriteMessage("Error: " & ex.Message)
                End If
                Exit Sub
            End Try
        End If

            Dim pr As PromptResult = ed.GetString("Part number to draw: ")
            If pr.Status <> PromptStatus.OK Then Exit Sub

            Dim doc As Document = drwr.CreateByPartNumber(pr.StringResult)
            'Core.Application.DocumentManager.MdiActiveDocument = doc       'does not work

            If drwr.Errors.Count > 0 Then
                ed = Core.Application.DocumentManager.MdiActiveDocument.Editor
                ed.WriteMessage("Autodraw Error(s):" & vbCrLf & String.Join(vbCrLf, drwr.Errors))
            End If

        Catch ex As System.Exception
            ed.WriteMessage("Error: " & ex.Message)
        End Try

    End Sub


Is there a way to set the MDIActiveDocument?  Should it be hanging like this?  Should I be looking at an altogether different way to handle this?

Trying to use doc.Editor without setting the active causes a eNotApplicable error, so that is not a way out.

I am using AutoCAD 2013.  The following link implies that this could be a 2013 specific problem, but I do not know that for sure.

Thanks for whatever you guys can tell me!!


Posts: 12
Registered: ‎08-22-2012
Message 2 of 2 (295 Views)

Re: Cannot set MdiActiveDocument

08-23-2013 12:32 PM in reply to: mleslie00

I got it !!  :smileyvery-happy:

The key was to declare my function as

 <CommandMethod("AUTODRAWPN", CommandFlags.Session)>

rather than


This indicates that that the context is the entire AutoCAD session rather than running in only a specific document.  Now when I set MdiActiveDocument it takes and I can send my results to the correct Editor.

In fact, if I want, I don't even have to set the active document, since I now can write to doc.Editor without getting an eNotApplicable error.  This attribute definately opens up more cross-document abilities.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community