Build a Plugin Command for AutoCAD Architecture10 Dec, 2015 By: Andrew G. Roe
Use Visual Studio.NET to customize vertical applications.
In previous articles, we’ve explored the process of building plugins for standard AutoCAD and AutoCAD Civil 3D. This time, we’ll look at a programming example that you can use to customize AutoCAD Architecture. While Architecture is primarily geared toward those in architecture, engineering, and construction (AEC) professions, this example demonstrates some key concepts for customizing other Autodesk products, so it is relevant to users in other fields as well. This example will build on the concepts covered in two of my earlier articles: “Create a Plugin for AutoCAD” and “Build a Plugin Command for AutoCAD Civil 3D.”
To review, a plugin is a custom command that can be run directly from the AutoCAD environment. In this example, the command will prompt the user to select an entity in a drawing and return some information about it. If it is an intelligent AEC object, the command will identify what type of object it is. If it is not an AEC object, the user will be prompted to select a different entity.
As in the articles mentioned above, this example uses the Visual Basic.NET programming environment, although you can use Visual C#.NET instead. To complete this exercise, you'll need either Visual Studio.NET or the free Visual Studio Express. If you are using AutoCAD 2016, make sure you are using Visual Studio 2012 or newer, which includes the .NET 4.5 framework.
Create a Plugin
1. Start Visual Studio and create a new project by clicking File > New > Project.
2. In the New Project dialog box, select Class Library, then type a name and file location in the first two fields at the bottom of the box, as shown here.
3. Click OK to close the New Project dialog box.
4. Click View > Solution Explorer.
5. Double-click on My Project in the Solution Explorer; the Project Properties screen will appear.
6. Click the References tab on the left.
7. Click the Add button to display the Add Reference dialog box.
8. In the Add Reference dialog box, click the Browse tab, and navigate to where AutoCAD Architecture is installed. Select the following files by clicking on one and Ctrl-clicking on the others, in any order: AcCoreMgd.dll, AcDbMgd.dll, AcMgd.dll, AecArchMgd.dll, AecBaseMgd.dll, AecPropDataMgd.dll, and AecStructureMgd.dll. The first three are for accessing standard AutoCAD information, and the last four are specific to Architecture. (This example is for Architecture 2016; references and file names may vary slightly for other versions.)
9. Click OK to close the Add Reference dialog box.
10. In the Properties window, change the Copy Local property to False for all seven of the referenced DLL files.
11. If you are using 64-bit AutoCAD, click the Compile tab on the left and click Advanced Compile Options. Change the Target CPU to x64 and click OK. This indicates to Visual Studio that the other application (Architecture) is a 64-bit application.
12. In Solution Explorer, rename the class from Class1 to EntityInfo.
13. Double-click on the EntityInfo.vb file, and modify the code to read as shown below. (Hint: You can copy and paste the code instead of retyping it!)
Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.EditorInput Imports Autodesk.AutoCAD.Runtime Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.Geometry Imports Autodesk.Aec.ApplicationServices Imports Autodesk.Aec.DatabaseServices Imports Autodesk.Aec.Arch.DatabaseServices Imports Autodesk.Aec.Arch.ApplicationServices Imports AcObject = Autodesk.AutoCAD.DatabaseServices.DBObject Public Class EntityInfo <CommandMethod("SelectEntity")> Public Sub mySelect()
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor Dim myEnt As New PromptEntityOptions(vbLf & "Select an AEC entity") myEnt.SetRejectMessage(vbLf & "Selected entity is not an AEC entity. Try again...") myEnt.AddAllowedClass(GetType(Geo), False) Dim resEnt As PromptEntityResult = ed.GetEntity(myEnt) If resEnt.Status <> PromptStatus.OK Then ed.WriteMessage("Selection error - aborting") Exit Sub End If 'Create a transaction to open the object. Dim tr As Transaction = _ Application.DocumentManager.MdiActiveDocument.TransactionManager. StartTransaction Try Dim obj As AcObject = tr.GetObject(resEnt.ObjectId, OpenMode.ForRead) MsgBox("You have selected an " & obj.GetRXClass.Name) tr.Commit() Catch tr.Abort() Finally tr.Dispose() End Try End Sub End Class
15. Build the application by clicking Build, then clicking on your project name. If you receive any error messages, Visual Studio provides guidance in the Output window about where the errors occurred and what the fix might be. Note that if you perform multiple builds after running the command, you may have to exit AutoCAD to avoid error messages when building the application.
About the Author: Andrew G. Roe
In her easy-to-follow, friendly style, long-time Cadalyst contributing editor and Autodesk Technical Evangelist Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD video tips. Subscribe to the free Cadalyst Video Picks newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!
Poll not yet available.