How to wait for AutoCAD to be ready?
If I run the "OpenDrawing" from a command prompt inside of AutoCAD (after AutoCAD is "ready") everything works perfectly. However, if I try to run the "OpenDrawing" subroutine as part of the "autoload" functionality I get most of the message boxes, but the drawing doesn't open. I feel this is because AutoCAD is not "all the way open" when the subroutine executes. Does anyone have an idea how to resolve this issue?
Code:
Imports System.Windows.Forms
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports System.IO
Public Class clsInitialize
Implements Autodesk.AutoCAD.Runtime.IExtensionApplication
Public Sub subInitialize() Implements IExtensionApplication.Initialize
MsgBox("Plugin loaded successfully . . .")
OpenDrawing() 'IF THIS LINE IS OMITTED,
End Sub
Public Sub subTerminate() Implements IExtensionApplication.Terminate
MsgBox("Plugin unloaded . . . ")
End Sub
<CommandMethod("OpenDrawing", CommandFlags.Session)> _
Public Sub OpenDrawing()
Dim strDrivePathNameExt As String
Dim colAcadDocs As DocumentCollection 'ApplicationServices
MsgBox("OpenDrawing Sub")
strDrivePathNameExt = "C:\Temp\Junk.dwg"
colAcadDocs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
MsgBox("Here1")
If (File.Exists(strDrivePathNameExt)) Then 'System.IO
MsgBox("Here2")
DocumentCollectionExtension.Open(colAcadDocs, strDrivePathNameExt, False)
MsgBox("File " & strDrivePathNameExt & " opened.")
Else
MsgBox("File " & strDrivePathNameExt & " does not exist.")
End If
MsgBox("Here3")
End Sub
End Class
Re: How to wait for AutoCAD to be ready?
You may want to provide some description on why you want to open a drawing when your add-in DLL is loaded (just remember, you may configure AutoCAD to load it on start-up, but it can also be loaded after AutoCAD started). IMO, opening a drawing in IExtensionApplication.Initialize() is not a good idea, even the DLL is designed to ONLY work against certain drawing. I'd never do it.
Having said that, you can add an event handler in Initialize() to handle Application.Idle event. That is, when loading your DLL, AutoCAD is obviously busy until the loading is done. As soon as the loading is done (and possible other activities AutoCAD may perform on startup, such as loading menus, AutoCAD will fire event Idle. So, you can place your opening file code in that event handler, and you also probably want to unhook the event handler there.
Again, while this is doable, you really need to be careful. At least, in the event handler you would have code to check the conditions why/which drawing you want to open after the DLL loading is done.