My First AutoCAD Plug-in Overview
Are you an AutoCAD® power user with an interest in becoming even more productive? Would you like to automate or extend the capabilities of AutoCAD, but are new to computer programming? If so, then this guide is designed for you.
“My First Plug-in” is a self-paced tutorial guide for a smooth introduction to the programming world. This is a “one-stop shop” learning path for users who know Autodesk products but are absolutely new to programming and are thinking about taking the plunge. In this guide, you will be working with the AutoCAD .NET Application Programming Interface (API) and the Visual Basic .NET programming language to create a ‘plug-in’ – a module that loads into AutoCAD to extend its functionality. Once you have finished this tutorial, you will understand the basics of .NET programming and how they are applied to AutoCAD.
Product: AutoCAD 2018*
Programming Language: Visual Basic .NET
Application Programming Interface (API): AutoCAD .NET API
*This guide was created for ‘vanilla’ AutoCAD 2018. However, all steps in this guide are applicable to any AutoCAD ‘vertical’ product (AutoCAD Architecture, AutoCAD Civil 3D, etc.). The AutoCAD .NET API is common to all these products; each vertical adds its own specialized API on top of the AutoCAD .NET API. The archived code that is attached will work in AutoCAD 2013, 2014, 2015 and 2016 and 2017 (just substitute 2015 or 2016 whenever you read 2017)).
There are many resources available on the web for you to learn about the AutoCAD .NET API. However, these resources tend to be designed for people who already know programming. This guide is different: it assumes no previous programming knowledge and yet helps you build your first plug-in quickly, without swamping you with details. You’ll have a working application within an hour of starting this material, irrespective of your current level of programming expertise.
The guide will start by reviewing the benefits of customizing Autodesk software before moving on to lessons covering the use of the AutoCAD .NET API. The lessons will start by building a working plug-in before covering more detailed explanations of the underlying principles and further developing the plug-in’s functionality.
The Benefits of Customizing AutoCAD
Time is money! The more efficiently you can perform routine operations, the more competitive you are in today’s economy. This is a key benefit of customization: tailoring the software you use on a daily basis to streamline or automate your workflows. Autodesk provides powerful APIs and SDKs (Software Development Kits) that allow you to gain greater value from your investment in Autodesk software by tailoring it to the needs of your business - customizing the product’s existing features or adding entirely new ones.
AutoCAD has four different APIs – .NET, LISP, ActiveX and ObjectARX (C++). This tutorial uses .NET because it offers the most powerful and modern programming environment - combining the very comprehensive AutoCAD .NET API with the most modern programming tools (the Microsoft .NET Framework and Microsoft Visual Studio). .NET gives you access to any of the .NET compliant programming languages (Visual Basic .NET, C#, F#, etc.) to develop plug-ins. While each language has its own relative benefits, Visual Basic .NET is the natural choice for beginners: it is the easiest to learn and use; it is the most human readable; and (like any other .NET language) it gives you complete access to the power of the underlying Microsoft .NET Framework. As you become comfortable with the content of this guide - and more proficient with Visual Basic .NET - you’ll be able to move on to solving more complex problems with the language.
Programming can be very rewarding. We hope you feel that way too after completing these lessons. Have fun!
Summary of the Plug-in
“My First Plug-in” implements a command that allows you to select block inserts in your drawing with attributes that you wish to keep parallel to the WCS x-axis regardless of the block insert’s rotation. I.e. the block’s attributes always remain horizontal in the WCS.
Keeping annotation text horizontal for the reader is a common requirement, and this simple plug-in makes it so easy. The finished plug-in can easily be extended to cover other annotation objects, such as Text and Mtext; and to display text always horizontal to the view, not just the WCS.
The Complete Plug-in
This video demonstrates the final plug-in you will be creating in Lesson 6 of this “My First Plug-in” guide.
Software to Install Prior to Starting
Video: How to install Visual Basic 2010 Express
- The ObjectARX SDK
The ObjectARX SDK includes documentation for the AutoCAD .NET API (also referred to as the Managed API), and some important files we must reference from our Visual Studio project. It also includes a number of sample .NET projects. (The SDK contains documentation and files for both the ObjectARX (C++) API and the .NET API. Don’t let all the C++ documentation worry you - .NET is a lot easier).
- The AutoCAD .NET Wizards
The Wizards are simple templates you can use to quickly create new AutoCAD .NET (Visual Basic or C#) projects. Importantly for Visual Basic Express users, they setup the project to allow you to launch AutoCAD from your debugger – something that isn’t possible through the Visual Studio Express user interface.
Summary of the Lessons
- Lesson 1: The Basic Plug-in
Create your very first basic AutoCAD plug-in for keeping block attributes horizontal. In this first version of the plug-in, the behavior of every block in the drawing is modified in the same way. You’ll add code later to allow you to select which blocks exhibit this changed behavior and which keep the standard AutoCAD behavior.
- Lesson 2: Getting to Know your Development Environment
Look more closely at what happens when you execute the plug-in code; explore the Visual Basic Express development environment; and learn how to step through code in the debugger.
- Lesson 3: A First Look at Code
Take your first in-depth look at the AutoCAD .NET API. You’ll spend time looking at the Visual Basic .NET code you typed into the plug-in’s project in Lesson 1, examining each of the lines of code in turn. Additionally, you’ll learn about some basic programming concepts, such as Object-Oriented Programming, classes, methods, variable declarations, creating class objects and properties.
- Lesson 4: Debugging your code
Learn how to use the Visual Studio debugger to step through your code line-by-line to follow the program execution; and to watch the values of variables as they are changed by your code.
- Lesson 5: Being Selective
Extend the plug-in to ask the user for input – selecting which blocks your changed behavior will be applied to. You will also learn about uniquely identify an object in a drawing using Handles and ObjectIds.
- Lesson 6: Looping and Persistence
Complete and improve the plug-in’s functionality by extending it to allow AutoCAD to store data in the DWG file - so your plug-in remembers which blocks you selected even when a drawing is reopened after being saved. You will also learn how to use loops and conditional statements.
- Lesson 7: Learning More
Information on additional resources to continue learning the AutoCAD .NET API, including help-files, documentation, samples and other tutorials.
- Lesson 8: Bonus Plug-in
Finish off the tutorial by studying a (slightly more complicated) bonus plug-in that extends the functionality of the plug-in you developed to provide full ‘billboarding’ behavior for block attributes.