• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    Re: Connecting to AutoCAD

    03-03-2012 02:44 PM in reply to: cvaught

    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
    Please use plain text.
    Contributor
    cvaught
    Posts: 11
    Registered: ‎01-25-2012

    Re: Connecting to AutoCAD

    03-03-2012 07:56 PM in reply to: Hallex

    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?  

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    Re: Connecting to AutoCAD

    03-04-2012 03:45 AM in reply to: cvaught

    I'm working nowhere, sorry

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

     

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Active Member
    Posts: 8
    Registered: ‎05-10-2010

    Re: Connecting to AutoCAD

    10-07-2012 10:24 PM in reply to: cvaught

    cvaught 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

     

    Please use plain text.