Hello All,
Thanks for all the information provided in this forum, I have found it useful on numerous occasions.
However, I now have a problem that I cannot find an answer.
My company has an application that needs to routinely jump into AutoCAD and do some work (usually CRUD operations on DB objects). The way this is currently implemented is as follows:
- We have an AutoCAD plugin. In the main IExtensionApplication class of the plugin, we start a back ground thread in the Initialize() method as follows:
Thread backgroundThread
= new Thread
(backgroundThreadNew
);backgroundThread.Name = "MainThreadScheduler";
backgroundThread.IsBackground = true;
backgroundThread.Start(Dispatcher.CurrentDispatcher);
- When our application wants to access AutoCAD, we ask AutoCAD for a callback to our application from its main thread by doing:
Dispatcher
.Invoke(new MainThreadCallbackDelegate
(mainThreadCallback
));
- When we get the callback, we 1st try and check that AutoCAD is not busy:
private bool isCadQuiescent()
{
bool quiescent = true;
if (document.LockMode() != DocumentLockMode.None && document.LockMode() != DocumentLockMode.NotLocked)
{
quiescent = false;
}
if (!Application.IsQuiescent)
{
quiescent = false;
}
return quiescent;
}
- If AutoCAD is busy, we do nothing and wait for another callback. If it is not busy, we ask AutoCAD to invoke a command we have defined using:
Application.DocumentManager.MdiActiveDocument.SendStringToExecute("MY_COMMAND\n", false, false, false);
- Inside the method that defines 'MY_COMMAND' we start a transaction and work with the required DB objects as normal
I would say that 99% of the time, everything works very well. However, the other 1% of the time I receive the following exception:
eInvalidInput
at Autodesk.AutoCAD.ApplicationServices.Document.SendStringToExecute(String command, Boolean activate, Boolean wrapUpInactiveDoc, Boolean echoCommand)
My questions are:
1. Is this a valid method of attempting to control AutoCAD?
2. Are we doing enough checks to ensure AutoCAD really is quiescent before attempting to send the command?
3. Under what circumstances would I be receiving the error I am? So far, from user reports and our own logging I cannot determine this.
4. Are there any other tips when trying to control AutoCAD like this?
Many thanks!