Trouble connecting to ACAD with VB.NET

Trouble connecting to ACAD with VB.NET

Anonymous
Not applicable
2,194 Views
4 Replies
Message 1 of 5

Trouble connecting to ACAD with VB.NET

Anonymous
Not applicable

I have an existing EXE program in VS 2017, and I want to add the capacity to create a polyline in Autocad 2018.

 

Usually, I would use VBA, but now I need to use VB.NET, because my exe is in vb.net

 

I'm confused. Should I use ObjectARX SDK? Is compatible with VS 2017? or should I keep the COM interfaces?

 

I installed ObjectARX, and it copied AcCoreMgd, AcDbMgd And AcMgd dlls to the autocad folder, but it decompressed files in C:\Autodesk\Autodesk_ObjectARX_2018_Win_64_and_32_Bit

 

Can I delete the folder C:\Autodesk\Autodesk_ObjectARX_2018_Win_64_and_32_Bit?

 

I tried this code to connect to autocad

 

(I added the references to AcCoreMgd, AcDbMgd And AcMgd dlls)

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry

Module ModuleAutocad
    Public AutocadApp As Document
    Public AutocadDatabase As Database

    Public Sub ConnectToAutocad()
        ' Get the current document and database
        AutocadApp  = Application.DocumentManager.MdiActiveDocument
        AutocadDatabase  = AutocadApp.Database
    End Sub
End Module

but it causes this error:

System.IO.FileNotFoundException: 'Could not load file or assembly 'accoremgd, Version=22.0.0.0, 
0 Likes
Accepted solutions (1)
2,195 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor
Accepted solution

You CANNOT use AutoCAD .NET API assenblies (accoremgd/acdbmgd/acmgd.dll) in stand-alone EXE app. If you have to use external EXE to automate AutoCAD, you use AutoCAD's COM API, much similar to what you do in AutoCAD VBA, except your code is resposible to create/get AcadApplication/AcadDocument object that your code works against.

 

To use AutoCAD COM API with .NET development tool (VB.NET/C#), unless you use late binding, in the EXE project you set reference to AutoCAD tyle library and acad objectDBX common library in 2 ways:

 

1. if you downloaded AutoCAD ObjectARX SDK, you set reference to Autodesk.AutoCAD.Interop.dll and Autodesk.AutoCAD.Interop.common.dll coming in the SDK. Or you can set reeferences to these 2 files in AutoCAD installation folder. Make sure to set "Embed interop types" and "Copy Local" to false.

 

2. Or, you can open "add reference" dialog box, select "COM" tab, in the availabel COM library list, select "AutoCAD 20xx Type Library" and "AutoCAD/ObjectDBX Common 2x.x Type Libarary". After you click OK, VS will generate .NET wrapper assemblies (AutoCAD.Interop.dll and AXDBLib.dll). Make sure for these 2 DLLs "Copy Local" are set to "True"

 

Or, if you know AutoCAD COM API very well (object names, available methods...), you can use late binding without adding references to COM API type library at all.

 

As you can see, ObjectARX SDK is not required at all.

 

By the way, you do not "install" AutoCAD ObjectARX SDK. When you run the downloaded SDK EXE, the exe is simply an self-compressed EXE. When "run" it, it only extract all the filed of the SDK to a given location, default to "C:\Autodesk\AutoCAD_ObjectARX_20xx...." nothing really installed (for execution). So, yes, you can delete the folder, or move it to anywhere you choose.

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 5

Anonymous
Not applicable

What is the point of it if it cannot be used from an exe app?


@norman.yuan wrote:

You CANNOT use AutoCAD .NET API assenblies (accoremgd/acdbmgd/acmgd.dll) in stand-alone EXE app.

... 


What is the point/purpose if it cannot be used in an exe?

 

0 Likes
Message 4 of 5

norman.yuan
Mentor
Mentor

Using external EXE to automate/control super-heavy desktop application AutoCAD is rarely an good solution. Most AutoCAD customizations run in-process (inside AutoCAD). .NET API is more powerful than VBA (COM API), and can also work mixed with COM API. At one point VBA was thought completely dead and would be gone; and only survived when MS finnaly licenced out VBA7 64-bit. But no one should invest seriously into AutoCAD VBA thiese days any more. Thus the point of using .NET API.

 

If you are convinced that your external EXE automating AutoCAD is best solution (again, I doubt in most cases), you have to stick with COM API (or, make your .NET API code COM-able, if you do not mind the pains-taking work).

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 5

ActivistInvestor
Mentor
Mentor

@Anonymous wrote:

What is the point of it if it cannot be used from an exe app?


@norman.yuan wrote:

You CANNOT use AutoCAD .NET API assenblies (accoremgd/acdbmgd/acmgd.dll) in stand-alone EXE app.

... 


What is the point/purpose if it cannot be used in an exe?

 


The point/purpose is that it (the .NET API) can be used to build extensions that AutoCAD loads into its own .exe process.

 

Extensions take the form of a windows dynamic link library (.dll), and AutoCAD loads those extension DLLs into its process, and the extensions are free from the limitations of the ActiveX/COM object model used by VBA and all external (.e.g. "exe") automation clients. Extensions (*.dll) can do many things that aren't possible with the ActiveX/COM API. They can define new commands, for example.