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

0 Members and 1 Guest are viewing this topic.

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #15 on: August 25, 2010, 12:10:24 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.

Yep... have access to there too. :(

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #16 on: August 25, 2010, 12:16:56 PM »
IIRC, CreateInstance can take a string, so try "AutoCAD.Application" and see what happens...
Glenn,
If I try that with the code below and obvious the references are for A2010 (ARX SDK) and I ran AutoCAD 2009 for example the code won't work and will leave the acad.exe process running, so I have to end the process on the task manager:
Code: [Select]
#if _ACADTARGET_18
            const string progID = "AutoCAD.Application"; //.18"; // 2010

            AcadApplication acApp = null;

            //
            //Guid clsid = new Guid("6D7AE628-FF41-4CD3-91DD-34825BB1A251");

            //try
            //{
            //    acApp = (AcadApplication)Marshal.GetActiveObject(progID);
            //}
            //catch
            //{
                try
                {
                    Type acType = Type.GetTypeFromProgID(progID);
                    acApp = (AcadApplication)Activator.CreateInstance(acType, true);

                    //Type acadtype = Type.GetTypeFromCLSID(clsid, true);
                    //acApp = (AcadApplication)System.Activator.CreateInstance(acadtype, true);
                }
                catch
                {
                    MessageBox.Show("Cannot create object of type \"" + progID + "\"");
                }
           // }

            if (acApp != null)
            {
                // By the time this is reached AutoCAD is fully
                // functional and can be interacted with through code
                acApp.Visible = true;
                //acApp.ActiveDocument.SendCommand("_MYCOMMAND ");
            }
#endif

Glenn R

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #17 on: August 25, 2010, 02:26:58 PM »
I believe you can just use "AutoCAD.Application" and let it figure it out as to latest version.

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Best ways to launch AutoCAD and run a command?
« Reply #18 on: August 31, 2010, 01:17:51 PM »
Ran across this It might help

Code: [Select]

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput

   End Class


 

Public Class Class2

    Public Function StartAutoCADSession() As Object

        '*********************************************
        '*********Code from VisibleVisual.com*********
        '*********************************************

        On Error Resume Next

        Dim AcadApp As Object
        'Check if a AutoCad session Exists
        AcadApp = GetObject(, "AutoCAD.Application")
        If Err.Number <> 0Then
            Err.Clear()
            'If there is no AutoCad session active then there will be one created
            AcadApp = CreateObject("AutoCAD.Application")
        End If

        If Err.Number <> 0 Then
            StartAutoCADSession = Nothing
            Exit Function
        End If

        'if successful set visibility status
        AcadApp.Visible = 1
        'opened OK
        Return AcadApp

    End Function


Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Best ways to launch AutoCAD and run a command?
« Reply #19 on: September 01, 2010, 04:20:43 AM »
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 ?        
For my users I do it via loader (.net-plagin, which place on server). It plagin load other plagins according xml-settings files, which content are filters for selecting and loading files. Full code here.

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #20 on: September 02, 2010, 10:52:04 PM »
And....  :x

If you have installed AutoCAD 2010 and 2011 - it simple way to hard to launch any of both AutoCAD's.... my last resort is to go to the registry... all these just for a simple access to a single command, but they asked for it - so I have to provide it....

Even from the com side - once you manually launch any lower version, this function does not work:
Code: [Select]
#import "C:/Program Files/Common Files/Autodesk Shared/acax18enu.tlb" no_namespace

void AutoCADLauncher::InvokeAutoCAD()
{
TRY
{
HRESULT hr = S_OK;
hr = CoInitialize ( NULL );
CLSID clsid;
hr = ::CLSIDFromProgID ( L"AutoCAD.Application.18", &clsid );
if ( SUCCEEDED ( hr ))
{
IAcadApplication *pApp = NULL;
hr = CoCreateInstance ( clsid, NULL, CLSCTX_ALL, __uuidof( IAcadApplication ), (void**) &pApp );
if( SUCCEEDED ( hr ))
{
pApp->Visible = VARIANT_TRUE;

//...

pApp->Release();
}
}
CoUninitialize();
}
CATCH(COleDispatchException,e)
{
e->ReportError();
e->Delete();
}
END_CATCH;
}
« Last Edit: September 02, 2010, 11:11:10 PM by le »

pkohut

  • Bull Frog
  • Posts: 483
Re: Best ways to launch AutoCAD and run a command?
« Reply #21 on: September 02, 2010, 11:18:45 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:

Is this a special shortcut on the desktop or start menu the user would push to launch Autocad "and" run the command?
New tread (not retired) - public repo at https://github.com/pkohut

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #22 on: September 02, 2010, 11:19:12 PM »
And for example for AutoCAD 2009 - I can use these simple lines of code and works (apart of all my testings) and by reference the Autodesk.AutoCAD.Interop and Autodesk.AutoCAD.Interop.Common from the A2009 ARX SDK:

Code: [Select]
#if _ACADTARGET_17x // win32
            AcadApplication acadApp = new AcadApplication();
            if (acadApp == null) return;
            acadApp.Visible = true;
            acadApp.WindowState = Autodesk.AutoCAD.Interop.Common.AcWindowState.acMax;
            acadApp.ActiveDocument.SendCommand("_CSREPORT ");
            return;
#endif

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #23 on: September 02, 2010, 11:19:40 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:

Is this a special shortcut on the desktop or start menu the user would push to launch Autocad "and" run the command?
Yes a shortcut will be made from the executable (called from innosetup btw).

pkohut

  • Bull Frog
  • Posts: 483
Re: Best ways to launch AutoCAD and run a command?
« Reply #24 on: September 02, 2010, 11:45:54 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:

Is this a special shortcut on the desktop or start menu the user would push to launch Autocad "and" run the command?
Yes a shortcut will be made from the executable (called from innosetup btw).

Bare with me as I ask questions that may seem obvious.
The executable you want to launch is Autocad?
From and icon on the desktop?
You need a command to run automatically.

If all these are true then -
1. Copy the existing Autocad shortcut.
2. Paste somewhere.
3. Edit the new shortcut properties.
  3a. In the target section of the shortcut append
       /b "C:\Temp\LeStartupScriptOMatic.scr"
4. Follow the last part of Glenn's initial suggestion.
New tread (not retired) - public repo at https://github.com/pkohut

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #25 on: September 02, 2010, 11:53:28 PM »
Thank you Paul,

Will look into this.

Bare with me as I ask questions that may seem obvious.
The executable you want to launch is Autocad?
From and icon on the desktop?
You need a command to run automatically.

If all these are true then -
1. Copy the existing Autocad shortcut.
2. Paste somewhere.
3. Edit the new shortcut properties.
  3a. In the target section of the shortcut append
       /b "C:\Temp\LeStartupScriptOMatic.scr"
4. Follow the last part of Glenn's initial suggestion.

pkohut

  • Bull Frog
  • Posts: 483
Re: Best ways to launch AutoCAD and run a command?
« Reply #26 on: September 03, 2010, 12:28:12 AM »
If you want to do something a little more dynamic than just fire a script, then maybe -

If in Autolisp/Vlisp you are able to read the Autocad command line switches, then you could add custom switches (Autocad would ignore any switches it doesn't understand).

So lets say you have 3 switches you'd like to be able to supply at the command line
/RunCommand1 /RunCommand2 /RunCommand3
They can be appended to the Acad shortcut target section, their entry order is the order they will be run.

The lisp function will basically -
Code: [Select]
Function ParseCommandLine
  Get the Autocad startup command line string // Win32 API GetCommandLine, can something like that be done in Auto/Vlisp?
  Split the string on the ' ' space character
  For each split value
     Is split value == "/RunCommand1"
         Do the RunCommand1 stuff
     Is split value == "/RunCommand2"
         Do the RunCommand2 stuff
     Is split value == "/RunCommand3"
         Do the RunCommand3 stuff
End of Function

The function can be run from one of the lisp files that is automatically started with Autocad. Depending on where it is place, it can be run once per Acad session, or once for each drawing of an Acad session.
New tread (not retired) - public repo at https://github.com/pkohut

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #27 on: September 03, 2010, 10:57:56 PM »
After all the hoops and jumps... here it is something that is working, using a MFC application:
edit: by adding this line Sleep(5000); was able to make it work.

Code: [Select]
#import "C:/Program Files/Common Files/Autodesk Shared/acax17enu.tlb" no_namespace

// launch AutoCAD 2009
void AutoCADLauncher::InvokeAutoCAD()
{
TRY
{
HRESULT hr = S_OK;
hr = CoInitialize(NULL);
CLSID clsid;
hr = ::CLSIDFromProgID(L"AutoCAD.Application.17.2", &clsid);
if (SUCCEEDED(hr))
{
IAcadApplication *pApp = NULL;
hr = CoCreateInstance(clsid, NULL, CLSCTX_ALL, __uuidof(IAcadApplication), (void**) &pApp);
Sleep(5000); // required!
if (SUCCEEDED(hr))
{
pApp->Visible = VARIANT_TRUE;
pApp->PutWindowState(::acMax);
pApp->ActiveDocument->SendCommand(_bstr_t("_.CSREPORT ")); // call the command
pApp->Release();
}
}
CoUninitialize();
}
CATCH(COleDispatchException,e)
{
e->ReportError();
e->Delete();
}
END_CATCH;
}

#import "C:/Program Files/Common Files/Autodesk Shared/acax18enu.tlb" no_namespace

// launch AutoCAD 2010 or 2011
void AutoCADLauncher::InvokeAutoCAD()
{
TRY
{
HRESULT hr = S_OK;
hr = CoInitialize(NULL);
CLSID clsid;
// depending if both AutoCAD 2010 and 2011 are in the same machine
// it will be launched the most recently used from any of the two
hr = ::CLSIDFromProgID(L"AutoCAD.Application.18", &clsid);
if (SUCCEEDED(hr))
{
IAcadApplication *pApp = NULL;
hr = CoCreateInstance(clsid, NULL, CLSCTX_ALL, __uuidof(IAcadApplication), (void**) &pApp);
Sleep(5000); // required!
if (SUCCEEDED(hr))
{
pApp->Visible = VARIANT_TRUE;
pApp->PutWindowState(::acMax);
pApp->ActiveDocument->SendCommand(_bstr_t("_.CSREPORT ")); // call the command
pApp->Release();
}
}
CoUninitialize();
}
CATCH(COleDispatchException,e)
{
e->ReportError();
e->Delete();
}
END_CATCH;
}

// for the initialization

AutoCADLauncher dlg;
m_pMainWnd = &dlg;
dlg.InvokeAutoCAD(); // launch AutoCAD
m_pMainWnd = NULL;
« Last Edit: September 04, 2010, 04:03:01 PM by le »

LE3

  • Guest
Re: Best ways to launch AutoCAD and run a command?
« Reply #28 on: September 08, 2010, 03:41:37 PM »
at the end it is programming so this is an image output of the final release after all the homework :)

- it does a search of the autodesk (autocad-based) installed applications.
- and extract the product, language, and location path.
- and are displayed on the dialog interface.
- the user selects from the list on where it is going to run the command (in this case for an application in particular).
- depending on the selection, it will save the data into the registry for demand-loading on command invoke.
- also will generate a new shortcut on the desktop to launch the autocad-based product and appending a call to an script (as Glenn and Paul recommended previously here).
- once the autocad-based product it is open, the command it is called.
- all of these avoids the need to depend or to add more additional external executables to simple open/launch the autocad-based product.
- also it provides the ability to do the setup outside of the autocad-based product and on multiple seats at the same time.

It is working :)