My First Plug-in Training

Lesson 2: Getting to Know your Development Environment

In the previous lesson, you saw how you can increase productivity in AutoCAD by implementing a plug-in built from a small amount of Visual Basic .NET code.

.NET (or the .NET Framework) is a technology created by Microsoft that enables programmers to create and extend software applications. A programmer may use the .NET Framework to create a new software application from scratch, to implement communication or interoperability between two software applications, or to extend a software application by implementing a plug-in for it (as you are doing here for AutoCAD). If you are interested in learning more, you will find information in the Additional Topics section.

You will now look more closely at what happened when you built and executed the code in the previous lesson.

Provide Feedback: Please provide feedback about this AutoCADTraining or this lesson via email:

What does it mean to “build” code?

The code you typed into Visual Studio Community in Lesson 1 was a set of human-readable instructions (source code) that needed to be converted into code that could be understood and executed by the computer. The “build” you performed did just that: it packaged up the resulting executable code into a DLL (Dynamic-Link Library) that can be loaded into AutoCAD.

The following screenshot shows the output in DLL form along with the associated program debug database (which provides additional information when troubleshooting the DLL) that you built using Visual Studio Community in Lesson 1. The path to where the DLL gets compiled is specified in the Visual Studio Community project settings and is set, by default, to the bin\Release or bin\Debug sub-folder of the Visual Studio Community project folder (depending if you’re building a Release or Debug version of your DLL – we’ll talk about that later).

Choosing a Programming Language and Development Tool

Just as humans use different languages to communicate, you have various language options available to you when creating an AutoCAD plug-in:  for the purposes of this guide we have chosen Visual Basic .NET, a strong general-purpose programming language. Visual Basic .NET is particularly popular with people learning to program, because the language syntax is more easily readable than many other languages (such as C# or C++).

There are a number of tools available for developing Visual Basic .NET code. They range from open source tools such as SharpDevelop and MonoDevelop to Microsoft’s flagship, professional development environment, Visual Studio. This tutorial assumes you’re using Visual Studio Community, a free version of Visual Studio for building Visual Basic .NET applications.

Visual Studio Community is an Integrated Development Environment (IDE) because it is composed of various tools, menus and toolbars which ease the creation and management of your code.

The project system in Visual Studio Community comprises Solution and Project files as well as Project Items, the individual files belonging to projects. A solution is a container for one or more projects. Each project can in turn be considered a container for project items – such as source files, icons, etc. – most of which get compiled into the resultant executable file (EXE or DLL). Visual Studio Community provides a Solution Explorer that organizes and displays the contents of the loaded solution in a tree-view format:

The Visual Studio Community interface also contains a text editor and interface designer. These are displayed in the main window depending on the type of file being edited. The text editor is where you will enter the Visual Basic .NET code for your AutoCAD plug-in. This editor provides advanced features such as IntelliSense and collapsible code sections along with the more classic text-editing features such as bookmarks and the display of line numbers.

IntelliSense is an extremely valuable feature of the Visual Studio family that greatly improves programmer productivity: it automatically provides suggestions for the code being written based on the objects available and the letters that are being typed. You will have already seen IntelliSense at work if you typed in the code in Lesson 1. That is one reason we suggested you don't copy and paste.

Clearly one of the key features of Visual Studio Community is its ability to build Visual Basic .NET code into an executable file. During the build process, the language compiler performs various checks and analyses on the code. One such check is to ensure the code conforms to the syntactical rules of the Visual Basic .NET language. The compiler also performs various other checks, such as whether a variable has been appropriately defined or not. Detected errors are reported via the Error List window, typically found at the bottom of the main window. If you made a mistake typing in the code in lesson 1, you may already have seen this when you tried to build your plug-in.

Reviewing your use of Visual Studio Community

In this section, you will review the steps performed using Visual Studio Community from the previous lesson. However, we will put them in the context of what you have just learned about programming in general and building your code.

  1. In the first step, you simply launched Visual Studio Community.

  2. You then created a new Visual Basic .NET project of type AutoCAD plug-in. This project template was added when you installed the AutoCAD .NET Wizards.

    Since the development language used for this guide is Visual Basic .NET, you are working with Visual Basic in Visual Studio Community, and therefore you see Visual Basic under the Installed Templates portion of the New Project dialog. The AutoCAD plug-in template is essentially a Class Library template, but with some additional settings.

    In the middle section of this dialog, you saw various types of applications that can be created; you select the template according to the type of application you wish to create.

    The name you entered at the bottom of the dialog is used to identify the project within the solution.

  3. Your blank project was created, containing a few standard project references to core .NET components along with references to the three files that define the AutoCAD API (AcCoreMgd.dll, AcMgd.dll and AcDbMgd.dll). The project also includes two Visual Basic .NET class files (MyCommands.vb and MyPlugin.vb - you may have optionally deleted the MyPlugin.vb file from the project because it’s not needed for this tutorial). These files contained some simple boilerplate code. Clicking on one of those files in Solution Explorer displays the code it contains in the text editor window.

  4. You looked at the References section of the ‘My Project’ project settings and checked that AcCoreMgd.dll, AcDbMgd.dll and AcMgd.dll were correctly referenced (Note: AcCoreMdg.dll is from for AutoCAD 2013 and higher).

  5. Saving the solution created physical files representing the contents of your project on the computer’s hard drive, allowing you to open and edit it at another time in the future. You closed and reopened the project to ensure Visual Studio Community had correctly parsed the project files.

  6. AcCoreMgd.dll, AcMgd.dll and AcDbMgd.dll contain definitions of the AutoCAD APIs you will most commonly use in your plug-ins. You will always reference these files in your AutoCAD plug-in projects. You will sometimes reference others too.

    • AcMgd.dll contains the APIs for controlling the AutoCAD application itself – defining custom commands
      opening and closing documents, plotting, etc.
    • AcDbMgd.dll contains the APIs for creating, editing or querying the contents of a DWG file.
    • From AutoCAD 2013, the APIs in AcMdg.dll were split between AcMgd.dll and AcCoreMgd.dll. AcCoreMgd.dll contains APIs related to the AutoCAD application login (such as selections set, commands and keywords), and AcMgd.dll contains ‘User Interface’ related APIs (such as dialogs).
    When you created your AutoCAD plug-in project, the AutoCAD .NET Wizard Configurator dialog had these selected by default (you can’t unselect them). There were options to include other API definition files that we ignored.

  7. Next you added Visual Basic .NET code using the AutoCAD API into your project. In other words, providing AutoCAD with instructions on how to modify how a block attribute behaves when it is rotated.

While developing code, it’s a good idea to build the solution from time to time, to check whether errors have been introduced in the code. The code does not necessarily have to be complete or functional when building the solution. This approach can help avoid potentially lengthy troubleshooting once the code is complete, and has the side benefit of automatically saving any edited source files before the build starts.

To build a solution inside Visual Studio Community, select Build Solution from the Build pull-down menu.

If the build process was successful, you would see a Build Succeeded status in the bottom left corner of the Visual Studio Community application frame. If there was an error in your code, Visual Studio Community will display an error list explaining the errors it has found. It will also underline the error in your code in blue. Here’s an example where we deliberately added a mistake to the code you typed in lesson one:

In this lesson you took a brief look at what happens when you build a project, as well as some background information on Visual Basic .NET and Visual Studio Community. You reviewed the steps you had taken in the previous lesson to build your basic AutoCAD plug-in, putting it in the context of what you have learned about programming.

Additional Topics

Visual Studio Community vs. Visual Studio Professional

In this guide, you are using Visual Studio Community. This is a free version of Visual Studio and so it’s a great tool to start learning with. Microsoft has targeted the Community editions of Visual Studio at students, hobbyists and other part-time programmers. While it provides most of the features of Visual Studio Professional, such as IntelliSense, it does have certain limitations. For instance, it contains fewer project templates and has limited options for debugging and troubleshooting your code. If you are serious about plug-in development beyond this introductory guide – and particularly if you want to start developing commercial plug-ins – we recommend investing in one of the more fully-featured members of the Visual Studio product family.

*There are several ‘professional’ versions of Visual Studio. Visit the Microsoft Visual Studio website for more information.

What is .NET?

The remainder of this lesson includes quite a bit of technical jargon. Don’t worry if you don’t completely understand it all when you first read it. It will make more sense once you’ve become more familiar with .NET after writing a few of your own plug-ins.

The .NET Framework is a software framework that sits on top of the Microsoft® Windows® operating system* and provides the underlying platform, libraries and services for all .NET applications. The services include memory management, garbage collection, common type system, class libraries, etc.

* Subsets of .NET are also available on other operating systems, whether via the open source Mono project or via Microsoft® Silverlight®, but these are not topics for this guide. You will focus solely on using.NET on Microsoft Windows.

What does the .NET Framework Contain?

The framework contains two main components:

  1. Common Language Runtime (CLR)This is the agent (or execution engine) in the .NET Framework responsible for managing the execution of code. Which is why code written to target this runtime is also known as managed code. All managed code runs under the supervision of the CLR, but what does this mean? The CLR manages code by providing core services such as memory management (which includes automatically releasing the computer’s memory for reuse on other tasks when it is no longer needed), error (or exception) handling, managing the use of multiple threads of execution and ensuring rules around the use of different types of object are adhered to. The CLR is really the foundation of the .NET Framework.

  2. .NET Framework Class LibraryAs the name suggests, this is a library or collection of object types that can be used from your own code when developing .NET applications. These .NET applications are targeted for Windows (whether command-prompt based or with a graphical user interface), the web or mobile devices. This library is available to all languages using the .NET Framework.

As mentioned above, the CLR improves code robustness by making sure the executing code conforms to a common type system (CTS). The CTS ensures that all .NET (or managed) code – irrespective of the language – uses a similar set of object types and can work together in the same environment. It is this feature that makes it possible for you to write applications in the development language of your choice and yet make use of components/code written by programmers using other .NET languages.

Building Executables

When you built your code into an EXE, it was compiled into Common Intermediate Language (CIL – also known as MSIL) code using the language-specific compiler. CIL is a CPU-independent set of instruction that can be executed by the CLR on Windows operating systems. CIL is typically portable across 32- and 64-bit systems and even – to some extent – to non-Windows operating systems. The CIL code generated from your VB source code was then packaged into a .NET assembly. Such an assembly is a library of CIL code stored in Portable Executable (PE) format (which contains both the CIL and its associated metadata). Assemblies can either be process assemblies (EXEs) or library assemblies (DLLs).

During the course of this guide, you will focus on developing a particular type of AutoCAD plug-in: a process assembly (EXE) which communicates with AutoCAD. Because of the overhead associated with developing them, you will not spend time looking at AutoCAD AddIns, which are usually library assemblies (DLLs) that get loaded into and executed within the memory space of AutoCAD. One reason that implementing an EXE to work with AutoCAD is simpler than developing an AddIn is related to its user interface: Executables do not need to integrate seamlessly with the AutoCAD user interface by adding ribbon buttons (for instance).

Running Executables

During execution of a .NET assembly, CIL (residing in the assembly) is passed through the CLR’s just-in-time (JIT) compiler to generate native (or machine) code. JIT compilation of the CIL to native code occurs when the application is executed. As not all of the code is required during execution, the JIT compiler only converts the CIL when it is needed, thus saving time and memory. It also stores any generated code in memory, making it available for subsequent use without the need to recompile.

In the last step of this process, the native code gets executed by the computer’s processor.

If you would like more details on the process of building .NET applications, please refer to the MSDN Library.