Connecting to AutoCAD

Connecting to AutoCAD

Anonymous
Not applicable
3,511 Views
13 Replies
Message 1 of 14

Connecting to AutoCAD

Anonymous
Not applicable

I have a .NET app that I have created to run inside of AutoCAD.  It runs inside of AutoCAD and has been tested on both AutoCAD 2010 and 2012.  I have also built a standalone applicaiton that is intended to start AutoCAD and netload the in process dll.  I have a computer that has 2010 installed and if I compile the application on this machine everything works as it should.  If I then run the same standalone application on a computer that only has 2012 installed it doesn't connect.  It starts AutoCAD (acad.exe shows up in the task manager) but it can't connect to it.  I then tested the opposite scenario.  Built the standalone application on a computer that only has 2012 installed and it works with AutoCAD 2012.  I then ran that applicaiton on the computer with only AutoCAD 2010 installed and it can't connect.  Just like before, acad.exe is started but it doesn't connect and instead gives an error.

 

Obviously this would make supporting different versions of AutoCAD require a different executables for the standalone application which seems wrong.  I have created a applicaiton that communicates with Inventor and this problem did not occur.  Thus, I had assumed it wouldn't be an issue with AutoCAD.   Am I doing something wrong that is preventing the different versions from working?   Any help would be greatly appreciated.

0 Likes
3,512 Views
13 Replies
Replies (13)
Message 2 of 14

Anonymous
Not applicable

If you post the code from the stand-alone app that starts AutoCAD then netloads the in-process dll you might get a better response.

0 Likes
Message 3 of 14

Anonymous
Not applicable

Here is the code for connecting to AutoCAD.

 

public void connectAutoCad()

{

String progID = "Application.AutoCAD";

try

        {

               //try to connect an open instance of autoCad

               acApp = (AcadApplication)Marshal.GetActiveObject(progID);

        }

        catch

        {

        //open a new instance autoCad

                Type acType = Type.GetTypeFromProgID(progID);

                acApp =(AcadApplication)Activator.CreateInstance(acType,true);

}

}

0 Likes
Message 4 of 14

Balaji_Ram
Alumni
Alumni

Hi cvaught,

 

Have you tried setting the visibility using "acApp.Visible = true" after "CreateInstance" call ?

Does AutoCAD then show up ?

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 5 of 14

Anonymous
Not applicable

It throws an exception on that line.  Thus acApp is null.

0 Likes
Message 6 of 14

Balaji_Ram
Alumni
Alumni

Hi,

 

I have 2011 and 2012 installed and it works ok when i launch 2012.

 

Here are few things that you may try :

 

1) Check the following in the registry - HKEY_CLASSES_ROOT\AutoCAD.Application

    Use the CLSID mentioned to search and this should lead you to the acad.exe path that is supposed to get launched. Make sure you do this carefully and dont change anything by mistake.

 

2) Try invoking a specific version by providing a different progId such as "AutoCAD.Application.18" and "AutoCAD.Application.18.2".

 

3) See if the issue is specific to a system by trying it on other systems that only have 2012.

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 7 of 14

Anonymous
Not applicable

So just to confirm, a single excutable that is created on a computer using AutoCAD 2012 should be able to connect to AutoCAD when the application is installed on a computer that only contains AutoCAD 2010?

0 Likes
Message 8 of 14

Anonymous
Not applicable

I still haven't found a solution for my problem.  Does anyone have experience with connecting to multiple different versions of AutoCAD with a single executable?  Any help would be greatly appreciated.

0 Likes
Message 9 of 14

Hallex
Advisor
Advisor

Try to get the current version something like this

{code}

        Dim acadver As Object
        acadver = My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\AutoCAD.Application\CurVer", _
                                              String.Empty, String.Empty)

        If typeOf acadver Is String Then
            MessageBox.Show("String acadver: " & acadver.ToString
end if

{code}

 

Then pull this string to CreateInstance method from

System.Reflection namespace

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 10 of 14

Anonymous
Not applicable

Same problem still exists.  It starts acad.exe but cannot connect to it.  Any other opitons?  This seems like it should be pretty straight forward.  How else can a stand along application support multiple versions of AutoCAD?

0 Likes
Message 11 of 14

Hallex
Advisor
Advisor

Both ways is woking for me with A2009 Win7

Try again

{code}

   ''1st way   
  Private Function GetAcadVer() As String
        Dim acadver As Object
        Try
            acadver = My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\AutoCAD.Application\CurVer", _
                                                  String.Empty, String.Empty)

            If TypeOf acadver Is String Then
                'MessageBox.Show("String acadver: " & acadver.ToString())

                Return acadver.ToString()
            Else
                Return Nothing
            End If

        Catch
            Return Nothing
        End Try
    End Function
    Public Sub connectAutoCad()

        Dim acApp As Object = Nothing
        Dim progID As String = GetAcadVer()

        Try
            'try to get a current instance of AutoCAD
            acApp = Marshal.GetActiveObject(progID)

        Catch
            'try to create a new instance of AutoCAD
            Dim acType As Type = Type.GetTypeFromProgID(progID)
            acApp = Activator.CreateInstance(acType, True)
        End Try
        acApp.GetType().InvokeMember("visible", Reflection.BindingFlags.SetProperty + Reflection.BindingFlags.IgnoreCase, Nothing, acApp, New Object() {True})
        acApp.GetType().InvokeMember("eval", Reflection.BindingFlags.InvokeMethod + Reflection.BindingFlags.IgnoreCase, Nothing, acApp, New Object() {"MsgBox(" + """Hello Discussion group""" + ")"})
        acApp.GetType().InvokeMember("quit", Reflection.BindingFlags.InvokeMethod + Reflection.BindingFlags.IgnoreCase, Nothing, acApp, Nothing)

        Marshal.FinalReleaseComObject(acApp)
        acApp = Nothing
    End Sub

''2nd way
   Private Sub GetAcadApp(ByVal AcadString As String)
        Dim AcadApp As Object = Nothing
        Try
            AcadApp = GetObject(, "AutoCAD.Application." & AcadString)

        Catch

            AcadApp = CreateObject("AutoCAD.Application." & AcadString)

            AcadApp.GetType().InvokeMember("Visible", Reflection.BindingFlags.SetProperty + Reflection.BindingFlags.IgnoreCase, Nothing, AcadApp, New Object() {True})
            MessageBox.Show(AcadApp.GetType().InvokeMember("Name", Reflection.BindingFlags.GetProperty + Reflection.BindingFlags.IgnoreCase, Nothing, AcadApp, Nothing).ToString)


        Finally
            AcadApp.GetType().InvokeMember("Quit", Reflection.BindingFlags.InvokeMethod + Reflection.BindingFlags.IgnoreCase, Nothing, AcadApp, Nothing)
            Marshal.FinalReleaseComObject(AcadApp)
            AcadApp = Nothing
        End Try

    End Sub
''example:
''on button click call
'' GetAcadApp("17")

 

{code}

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 12 of 14

Anonymous
Not applicable

Still doesn't work.  Just to confirm, did the code you use work with just one version of AutoCAD or have you been able to use it to connect to different versions on different machines using the same executable?  

0 Likes
Message 13 of 14

Hallex
Advisor
Advisor

I'm working nowhere, sorry

just tested this code on my home machine - A2009(eng) release

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 14 of 14

Hui-lung.Chen
Participant
Participant

@Anonymous wrote:

Same problem still exists.  It starts acad.exe but cannot connect to it.  Any other opitons?  This seems like it should be pretty straight forward.  How else can a stand along application support multiple versions of AutoCAD?


 

There is no way that the same exe can support multiple versions of AutoCAD.

Different version or OS (X64 ,X86) of AutoCAD has different library.

The best way is to recomplie another project (the same code but has different reference librayies).


Huilung Chen

 

0 Likes