To start API programming for AutoCAD, some softwares needs to be installed.
1. Prerequisites:
Visual Studio:
Install the latest version of Visual Studio. The Community Edition is free and sufficient for this purpose.
AutoCAD .NET API:
Ensure you have the AutoCAD .NET API installed. This can be found in the AutoCAD ObjectARX SDK. ObjectARX for 2024 version can be downloaded from Autodesk Platform Services Website.
2. Create a New Project:
- Open Visual Studio.
- Go to File > New > Project.
- Choose Class Library (.NET Framework) as the project type but check if it is VB based.
- Name your project and choose a location to save it.
3. Add References:
- Right-click on the References node in the Solution Explorer.
- Choose Add Reference.
- Browse to the location where the AutoCAD .NET assemblies are located (typically in the AutoCAD installation directory under ./inc).
- Add references to following files. The files are usually present in folder (C:\Program Files\Autodesk\AutoCAD 20XX\)
- AcMgd.dll (This is the main AutoCAD Managed DLL that provides access to the core AutoCAD functionality.)
- AcDbMgd.dll (This DLL is responsible for database operations in AutoCAD, including working with drawing objects.)
- AcCoreMgd.dll (This DLL provides access to core AutoCAD services and application initialization.)
In some cases, additional references may be required for some special functions. The additional references are not necessarily to be added to our project. These are following:
- AcCui.dll (If you're working with custom user interfaces (e.g., ribbon tabs), you'll need it.)
- AcWindows.dll (This DLL is needed for Windows-specific functionality within AutoCAD)
For AutoCAD Civil 3D, additional references are required.
- AeccDbMgd.dll (This DLL provides access to the AutoCAD Civil 3D database and objects. It's specific to Civil 3D)
- AecBaseMgd.dll (It facilitates database operations and interactions with AEC-specific objects and data within the drawing)
- AeccPressurePipesMgd.dll (It provides access to networks and pressure pipe networks)
4. Write Your Code:
In the default class file, add the following imports:
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Create a new command by adding a method with the <CommandMethod> attribute. For example:
<CommandMethod("HelloWorld")>
Public Sub HelloWorldCmd()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
ed.WriteMessage(vbLf & "Hello, AutoCAD from VB.NET!")
End Sub
- Save your project.
- Click Build > Build Solution to compile your code into a DLL.
6. Load the DLL into AutoCAD:
- Open AutoCAD.
- Type NETLOAD in the command line.
- Browse to the location of your DLL and select it.
- Once loaded, you can run your command (HelloWorld in the example) by typing it into the command line.
7. Debugging (Optional):
- If you want to debug your DLL:
- In Visual Studio, go to Debug > Attach to Process.
- Find and select the acad.exe process.
- Set breakpoints in your code.
- Run commands in AutoCAD to hit the breakpoints.
8. Distributing Your DLL:
Once you're satisfied with your DLL, you can distribute it to other AutoCAD users. They will need to use the NETLOAD command to load it into their AutoCAD session.
This guide provides a basic introduction to creating a DLL for AutoCAD using VB.NET. Depending on your needs, you may want to explore more advanced features of the AutoCAD .NET API.

No comments:
Post a Comment