Author Topic: Accessing AutoCAD from background thread  (Read 3031 times)

0 Members and 1 Guest are viewing this topic.

matt.hibb

  • Guest
Accessing AutoCAD from background thread
« on: November 27, 2015, 07:34:33 AM »
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:
Code - C#: [Select]
  1. Thread backgroundThread = new Thread(backgroundThreadNew);
  2. backgroundThread.Name = "MainThreadScheduler";
  3. backgroundThread.IsBackground = true;
  4. 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:
Code - C#: [Select]
  1. Dispatcher.Invoke(new MainThreadCallbackDelegate(mainThreadCallback));

- When we get the callback, we 1st try and check that AutoCAD is not busy:
Code - C#: [Select]
  1. private bool isCadQuiescent()
  2. {
  3.     bool quiescent = true;
  4.     if (document.LockMode() != DocumentLockMode.None && document.LockMode() != DocumentLockMode.NotLocked)
  5.     {
  6.         quiescent = false;
  7.     }
  8.    
  9.     if (!Application.IsQuiescent)
  10.     {
  11.         quiescent = false;
  12.     }
  13.     return quiescent;
  14. }

- 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:
Code - C#: [Select]
  1. 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:
Code: [Select]
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!

CADbloke

  • Bull Frog
  • Posts: 342
  • Crash Test Dummy
Re: Accessing AutoCAD from background thread
« Reply #1 on: November 29, 2015, 05:10:07 PM »
This sounds like a job for the Core Console. Do a search on "Core Console" here and check out these posts http://through-the-interface.typepad.com/through_the_interface/core-console/ as a start.

BlackBox

  • King Gator
  • Posts: 3770
Re: Accessing AutoCAD from background thread
« Reply #2 on: November 30, 2015, 12:54:36 AM »
Not sure we have enough information.

Jump into AutoCAD... In a client session, or on a service host?

Why load a plugin at Initialize(), to start a separate background thread, to ask for a callback, to check if it (the Application) is available... Just to 'do' something?

If running on a client session, why are you doing anything to user's opened DB without user authorization... Or is this just a sort of user automation?

If running on a service host, just monitor for file, folder, email, etc changes and act of the corresponding instructions to a given DB in queue without the need to check for access, as the service host is what initiatives the session.

If holding a service host session is too much load (even when not 'doing' anything?), or you find you need to batch process, then consider Core Console as aptly mentioned above, specifically in series (as opposed to in parallel).

Cheers
"How we think determines what we do, and what we do determines what we get."

matt.hibb

  • Guest
Re: Accessing AutoCAD from background thread
« Reply #3 on: November 30, 2015, 07:00:49 AM »
My apologies, I obviously didn't provide enough context for the problem.

We have our own stand alone drawing application. The requirement was to show geometry generated in our application in a live AutoCAD session, i.e. the user draws a line in our application, our application draws the same line in the users AutoCAD session.

To do this, we have an AutoCAD plugin as I described. The user starts our application and AutoCAD. Our application can then connect to the plugin. The background thread in the plugin receives geometry from our application that needs to be drawn in AutoCAD. The process to then get this geometry into AutoCAD is as I described in my original post.

Thanks