How to connect AutoCAD (user specified version) using VB.Net via COM Library?

How to connect AutoCAD (user specified version) using VB.Net via COM Library?

muthukumar.kVVK9E
Contributor Contributor
487 Views
1 Reply
Message 1 of 2

How to connect AutoCAD (user specified version) using VB.Net via COM Library?

muthukumar.kVVK9E
Contributor
Contributor

Dear Team,

 

In my Vb.Net Project,  connecting AutoCAD 2020 using the COM type Library by selecting the Reference  (AutoCAD Version 2020) manually & Internally code modified the version 2020. Its working fine.

(Cannot change to Net Library, built lot of Standard functions based on this COM)

 

Suppose if user want to connect AutoCAD 2024, once again i have to modify the COM Reference (AutoCAD Version - 2024)  then will modify the connection portion inside the code based on version.

For Each user we can't built Project, not good Automation.

 

Query : Based on User Input (Version2024), connect AutoCAD 2024 via COM Library, along with reference base version 2020?  (Is it possible Deselect 2020 reference and Select 2024 Reference inside code?)

 

I tried this code, not working - results in Error.

Please give Suggestions to achieve this, Thanks by advance.

 

 

Code Snippet:

 

Need to open AutoCAD 2024:

 

 ' -Manually  added AutoCAD 2020 reference (below picture) - for built up the project

 

Public Shared Sub connect_cad()

 

        'CAD_Version  -- user input 
      'Dim CAD_Version As String = "AutoCAD.Application.23.1"          'Autocad 2020 user input  (working fine)

       Dim CAD_version As String = "AutoCAD.Application.24.3"          ' AutoCAD 2024 user input  (getting error)

 

Try

            Cadapp = CType(System.Runtime.InteropServices.Marshal.GetActiveObject(CAD_version), AcadApplication) ''                GetObject(, "AutoCAD.Application")
            If  Cadapp.Documents.Count = 0 Then Cadapp.Documents.Add()
                Caddoc = Cadapp.ActiveDocument

                Cadapp.WindowState = AcWindowState.acMax
                Cadapp.Visible = True

Catch Ex As Exception

       If Err.Number <> 0 Then
                 CADOPEN = False
                Err.Clear()
               Cadapp = CType(CreateObject(CAD_version), AcadApplication)   'Error coming here
          If Err.Number <> 0 Then
               CADOPEN = False
               MsgBox(Err.Description)
                Exit Sub
          End If

              Cadapp.WindowState = AcWindowState.acMax
               Cadapp.Visible = True
              Caddoc = Cadapp.ActiveDocument

    Else

           If Cadapp.Documents.Count = 0 Then Cadapp.Documents.Add()
           Caddoc = Cadapp.ActiveDocument
           conect_cad_option = True
            Cadapp.WindowState = AcWindowState.acMax
           Cadapp.Visible = True

    End If

 

Finally

End Try

End Sub

 

Error Msg:

System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to interface type 'AutoCAD.AcadApplication'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{6323190E-C6ED-434C-A368-E9314A8D7597}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'

 

 

This Picture - Manually added Reference for AutoCAD Version 2020

 

muthukumarkVVK9E_0-1709011243810.png

 

0 Likes
488 Views
1 Reply
Reply (1)
Message 2 of 2

norman.yuan
Mentor
Mentor

Since you set reference to AutoCAD's COM type library, and you declared the variable "Cadapp" as AcadApplication (you DID not show the code where the Cadapp variable is declared, but I assume you did so because of the question you post here), that means you use EARLY BINDING, that is, the the types (AcadXxxxx) of AutoCAD object models are bound to certain version of AutoCAD COM Classes when the code is compiled. Therefore, the code you showed here in attempt to start different version of AutoCAD is meaningless, as you have already experienced.

 

If you want your COM API code works with multiple versions of AutoCAD, you need to use LATE BINDING, so that the code is not compiled against specific version of AutoCAD COM API.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes