Author Topic: AutoCAD Automation  (Read 2438 times)

0 Members and 1 Guest are viewing this topic.

Bill Tillman

  • Guest
AutoCAD Automation
« on: August 21, 2015, 07:07:37 PM »
Using VS 2015 Community to do some more automation projects with AutoCAD and need advice on how to remedy this problem. The use of this project can take anywhere from 40-60 users in an 8 hour shift so the idea is for it to execute, get done and then wait for the next request. So keeping an instance of AutoCAD up all the time is important, but this block handles it either way. Trouble is AutoCAD can sometimes take 1-2 minutes to actually load so that's why we keep it at the ready, but that's for another post.

Okay, so AutoCAD is sitting there with it's start screen and when a request comes in via the web, the C# project goes into action. All works well. Except when I try it on my desktop. I have the same version of AutoCAD on my desktop as the server which runs this automation project. But on my computer, this same code opens up a new instance of AutoCAD every time. It never behaves like the other computers in the department and just use the existing open instance of AutoCAD. I've checked everything I know how to do but it's not looking good as for results.
Code: [Select]
class LaunchAutoCAD
    {
        private static IAcadApplication vAcadApp = null;
        private static IAcadDocument vAcadDoc = null;

        // CHANGE THIS TO AutoCAD.Application.20 FOR ACAD 2015/16
        private static string vAcadID = "AutoCAD.Application.20";

        public static void StartAutoCAD()
        {
            try
            {
                // Check for instance of AutoCAD
                vAcadApp = (IAcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject(vAcadID);
            }
            catch (Exception) // If none found start a new instance of AutoCAD
            {
                System.Type AcadProg = System.Type.GetTypeFromProgID(vAcadID);
                vAcadApp = (IAcadApplication)System.Activator.CreateInstance(AcadProg);
            }
            if (vAcadApp != null)
            {
                vAcadApp.Visible = true;
                .
                do a bunch of stuff with SendCommands
                .
            }
            else
            {
                Console.WriteLine("ERROR: The drawing file is missing.");
                Console.ReadKey();
            }
        }
   }
« Last Edit: August 21, 2015, 07:12:21 PM by Bill Tillman »

BlackBox

  • King Gator
  • Posts: 3770
Re: AutoCAD Automation
« Reply #1 on: August 21, 2015, 10:45:32 PM »
Sounds like an Exception is repeatedly raised in your Try Catch block, resulting in repeated calls to System.Activator.CreatInstance(), when you could instead use Process Class to effectively Get-or-Create the/an instance respectively.

What happens when you step through the code on your workstation?
"How we think determines what we do, and what we do determines what we get."

n.yuan

  • Bull Frog
  • Posts: 348
Re: AutoCAD Automation
« Reply #2 on: August 22, 2015, 09:40:44 AM »
Not really a direct answer to your question, but I thought it would be helpful:

Having an instance of AutoCAD running and waiting for respond multiple automation requests is a not reliable design, IMO: since AutoCAD can only do one thing at a time, how do you make sure there is no more than 2 requests coming in overlapped that crashes either the client app (user side) or even the AutoCAD instance? With 40-60 users who may send request from their client app, depending on the nature of the app, the chance of concurrent requests may be high enough the guarantee the failure of the process.

With this in mind, you may want to design the process in different way: have the AutoCAD instance starts on the computer (server computer)'s startup. and check the requests stored in a Que periodically. Users' app send request to the Que rather than directly to AutoCAD. This way, you effectively decouple the 2 apps, so that each works without bothering the other (so that the user app does not have to try to get existing AutoCAD instance, or have to create one. It simply does not care; just drop the request into the que).

Of course, we leave the issue of whether running AutoCAD this way violates the license or not out of the discussion. You may look into using AutoCAD console, which is designed for such "server type" use, and it is actually better to instance each AutoCAD console to process each single drawing.

BlackBox

  • King Gator
  • Posts: 3770
Re: AutoCAD Automation
« Reply #3 on: August 22, 2015, 01:23:57 PM »
While an incredibly apt point, regarding concurrent requests, I'm not a fan of concurrent instances on-demand as this can easily overwhelm the servers available resources as well.

Unless sufficient code logic exists to first determine a max number one bing instances, and then queues the residual incoming requests via dumping instructions to file, where a monitor then evaluates the instructions once one or more allowed instances has become available.

Even if only in one allowed instance, I think the queue procedure makes more sense from the outset, as this can also be used for reporting to the requesting user... 'There are 4 requests ahead of yours', etc
"How we think determines what we do, and what we do determines what we get."

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: AutoCAD Automation
« Reply #4 on: August 22, 2015, 05:56:51 PM »
I haven't looked into and don't really know that much about it but isn't that what AutoCAD IO was designed for?
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

BlackBox

  • King Gator
  • Posts: 3770
Re: AutoCAD Automation
« Reply #5 on: August 23, 2015, 09:38:27 AM »
... isn't that what AutoCAD IO was designed for?

Perhaps, but AIO API explicitly required the drawing(s) be accessible via HTTP, and I was under the impression the OP was using internal / domain server.

Not sure of OP security requirements, but no client of mine would be happy if I published their confidential data, even if only to Autodesk, setting aside unstated risks.

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

CADbloke

  • Bull Frog
  • Posts: 344
  • Crash Test Dummy
Re: AutoCAD Automation
« Reply #6 on: August 23, 2015, 07:45:53 PM »
You probably need a dispatcher in front of the AutoCAD process(es) to limit the AutoCAD instances and handle the jobs. Core Console would work better for this, mostly because it is much lighter. The legalities of using an AutoCAD client application are out of scope of this reply ;) RealDwg would be a solution if it supported Server Operating systems, which I don't think it does.

I believe AutoCAD can retrieve drawings from private places like a DropBox folder, AutoCAD 360 etc.

Um, (trying to be diplomatic here) what boat-anchor are you running AutoCAD on that it takes 1-2 minutes to load? My first load of the day on the laptop I'm on, including all the various plugins I have inflicted on it takes less than 15 seconds, subsequent (cached) loads are around 10 seconds. I think that's slower than other PCs I have.

Bill Tillman

  • Guest
Re: AutoCAD Automation
« Reply #7 on: August 24, 2015, 07:59:17 AM »
Thanks for all the replies, but it's not a question of overwhelming the AutoCAD server. And in reality, this is just a Windows 7 box with Xampp and AutoCAD running on it, with the C# executables being called when needed by the php code in Xampp. We do have a throttling method to keep from sending in too many requests at once. It still needs some refinement but that's not really the trouble I'm having. My desktop, is almost identical to the other one and it always launches a new instance of AutoCAD when I run that code on it. I can run the same code on the other Windows 7 desktop and all is well. I tested this just now on another Windows 7 box and it works fine. So there is something amiss with my installation which is causing it to call a new instance each time.

ChrisCarlson

  • Guest
Re: AutoCAD Automation
« Reply #8 on: August 24, 2015, 08:07:06 AM »
On your issue box, what is the value of "SDI"? If it's 1, try setting it to 0.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: AutoCAD Automation
« Reply #9 on: August 24, 2015, 08:16:17 PM »
If you are doing any printing in the background, check "BACKGROUNDPLOT"=0