Author Topic: Best ways to launch AutoCAD and run a command?  (Read 14664 times)

0 Members and 1 Guest are viewing this topic.

LE3

  • Guest
Best ways to launch AutoCAD and run a command?
« on: August 18, 2010, 02:35:27 PM »
Hi All.

I need to be able to launch AutoCAD from a shortcut and to run an specific command, this is how I am doing that:
Any pros-cons about it, or better coding ways ?

Was trying first the mfc-com route but from C# it is much simpler.

Thanks.

Code: [Select]
        private void launchAndRunCmd_Click(object sender, EventArgs e)
        {
            try
            {
                // invoke AutoCAD
                System.Type acad = System.Type.GetTypeFromProgID(progID);
                acadApp = System.Activator.CreateInstance(acad, true) as iAcadApp;
            }
            catch (COMException ce)
            {
                MessageBox.Show("\nCOMException occurred: " + ce.Message);
            }
            finally
            {
                if (acadApp != null)
                {
                    acadApp.Visible = true;
                    acadApp.WindowState = Autodesk.AutoCAD.Interop.Common.AcWindowState.acMax;
                    //MessageBox.Show("Hi! - I am here!"); // debug
                    acadApp.ActiveDocument.SendCommand("CSREPORT ");
                }
            }
        }

Glenn R

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #1 on: August 18, 2010, 05:08:49 PM »
Luis,

It depends. Do you want your program to control autocad before, during and after your startup command executes, or do you just want to start autocad and run a command?

If you just want to start and 'fire and forget' a command, you could do this:
Code: [Select]
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace AcadStartupScript
{
    class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo acadStartParams = new ProcessStartInfo();

            acadStartParams.FileName = @"C:\Program Files\AutoCAD 2010\acad.exe";
            acadStartParams.Arguments = @"/b C:\Temp\LeStartupScriptOMatic.scr";

            Process acadProc = Process.Start(acadStartParams);
        }
    }
}

...and the script file alluded to above is thus:
Code: [Select]
(alert "G'day Luis!")

If, however, you want control, then I would suggest the COM route, as you've already mentioned. I believe there are a few examples here and on the adesk ng's, but I've not done it.

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #2 on: August 18, 2010, 05:37:16 PM »
want to start autocad and run a command?

Thank you Glenn.

That code will become handy one of these days.

I will be using a .reg file to allow my assembly to register and autoload it (still looking for more options here - maybe it can be done from innosetup have not use it in a looong time), then will have the option to run my application from a shortcut, that will launch autocad and run the command to show a modeless dialog form. Or if the user opens AutoCAD the normal way to simple load the command and run it and display a splash screen (or welcome form).

Glenn R

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #3 on: August 18, 2010, 06:33:39 PM »
I would use the reg file file to register dll for load at startup myself. You didn't mention that to begin with...

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #4 on: August 18, 2010, 06:41:43 PM »
I would use the reg file to register dll for load at startup myself. You didn't mention that to begin with...
I was thinking while I was typing... that's why :)

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #5 on: August 19, 2010, 02:41:59 PM »
Since this it is going to be also an assembly installer...

Anyone knows if AutoCAD 2007 and 2008 was/is available for x64 platforms?

(I see that the ARX SDK's started to include the x64 files in AutoCAD 2009 arx sdk)


Thanks!

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #6 on: August 19, 2010, 09:50:55 PM »
and it is going to be something like this:

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Best ways to launch AutoCAD and run a command?
« Reply #7 on: August 20, 2010, 08:47:41 AM »
Since this it is going to be also an assembly installer...

Anyone knows if AutoCAD 2007 and 2008 was/is available for x64 platforms?

(I see that the ARX SDK's started to include the x64 files in AutoCAD 2009 arx sdk)


Thanks!
They would install on X64, only after hacking the MSI with Orca.  IIRC
Be your Best


Michael Farrell
http://primeservicesglobal.com/

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Best ways to launch AutoCAD and run a command?
« Reply #8 on: August 23, 2010, 02:34:39 AM »
You can right click on the toolbox in Visual Studio and add item and on th Com tab is the AcCtrl

In the docs under you can drill down to

Using COM for ObjectARX Development
COM and AtiveX Automation
Run Autocad in a Host Application
AutoCad as an In-Place Server

Is an example on using the Acctrl but I tried it 2 and got error each time

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #9 on: August 25, 2010, 11:44:06 AM »
Going back now to the launching of AutoCAD...

Found out after installing A2011 this does not work (going the interop.com route) I started to get a com exception (see image)

So, will going to take Glenn code route - by somehow extract the paths where AutoCAD's are installed and use the .SCR to open my application (command)

Glenn R

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #10 on: August 25, 2010, 11:53:21 AM »
IIRC, CreateInstance can take a string, so try "AutoCAD.Application" and see what happens...

Glenn R

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #11 on: August 25, 2010, 11:54:21 AM »
What's your 'progID' variable and what's it set to?

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #12 on: August 25, 2010, 12:00:18 PM »
What's your 'progID' variable and what's it set to?
#if _ACADTARGET_18
            const string progID = "AutoCAD.Application.18"; // 2010

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #13 on: August 25, 2010, 12:03:46 PM »
IIRC, CreateInstance can take a string, so try "AutoCAD.Application" and see what happens...
I went this route too, but this will launch the last one that was used.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Best ways to launch AutoCAD and run a command?
« Reply #14 on: August 25, 2010, 12:08:26 PM »
Quote from ADN

 

Unfortunately, the AutoCAD 2011 installer does not install the AutoCAD 2010 version (18.0.0.0) of the Autodesk.AutoCAD.Interop.dll. This has already been logged and is supposed to be remedied in the oncoming Update 1 for AutoCAD 2011.