Tuesday, April 24, 2012

Autodesk Autocad Autoloader

Introduction

With the launch of Autocad 2012 there is a new way to install custom made plug-ins build in MSVS. They refer to them as bundles or packages who are read by Autocad on startup. The Autoloader doesn't change anything to previous solutions who install plug-ins, but is an additional and new way of Autodesk to easily install all your plug-ins without too much hassle.


1.New C# Example Autoloader Project
Start a new C#.net project and name it to your heart's desire. (This example could easily be translated to VB.net). Make sure you create a class library project.

2.Add Autocad references
Add references to the acdbmgd and the acmgd dll. (These files are located inside your Autocad installation folder) Make sure you change the properties of the dll's inside MSVS and set it to Copy Local = False.

3.Set Autocad as startup while debugging

MSVS Full version
In the properties of your project make sure you debug using Autocad as an external program. This option is only available in the full version of MSVS. In the express version you'll need a work-around to accomplish the same behavior.

MSVS Express Edition
Add the following to your project xml file to launch Autocad from within MSVS C# Express.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
    <StartAction>Program</StartAction>
    <StartProgram>C:\Program Files\Autodesk\AutoCAD 2012\acad.exe</StartProgram>
  </PropertyGroup>
</Project>


4.Class Code
 Rename Class1.cs and name it Commands and place following code inside the class. Make sure you reference the acdbmgd assembly found in the autocad installation folder!

using Autodesk.AutoCAD.Runtime;
using System.Windows;

namespace AutoLoaderExample
{
    public class Commands
    {
        [CommandMethod("AutoLoaderExample")]
        public void AutoLoaderExample()
        {
            MessageBox.Show("AutoLoader Example");
        }
    }
}


Now that we have set up visual studio build your solution and we head over to the Autoloader bundle and its contents.

5.Availability Autoloader bundle

The first thing to point out is that it's possible to install the plug-in for all users and to install it for only a particular user.
·         For all user access to your plug-in we will place the plug-in in our installation folder under the map ApplicationPlugins. (Most likely this will be the Program Files/Autodesk/ ApplicationPlugins/ folder)
·         To install the plugin for a particular user place the plug-in inside the [AppDataFolder]/Autodesk/ ApplicationPlugins.


6.Autoloader map and file structure

It is important to understand that the autoloader looks for folders of a particular format. The folder has to look like this => AutoLoader.bundle, where the .bundle behind any given name is very important to the Autoloader to identify your bundle. The AutoLoader folder can be renamed to anything you like as long as it's followed by the .bundle.
 Because of the .bundle format we won't talk about plug-ins anymore if we talk about any .bundle under the ApplicationPlugins folder. I'll refer to them as bundles or packages.
Now we know that your bundle can be placed on 2 different locations on your hard drive let's look at the content of a one.

7.Autoloader contents

There are 2 files a bundle should always have inside, and it's the PackageContents.xml file which contains all necessary information for the autoloader on how to load your plug-in inside Autocad. And the second file is the assembly that contains all your Autocad commands. Or in case of multiple assembly files you could place them inside a separate folder. In my projects I try to separate my assemblies and resources within their own respective folder to make things more manageable.
In this example I'll place my bundle inside the "available to all Autocad users".


Inside the AutoLoader.bunde I place my example assembly , produced by Visual Studio on Build, and an PackageContents.xml file.


8.Assembly Location MS Visual Studio Example

For those who are new to how MSVS works when you have build the project you created earlier you can find the .dll inside the bin folder of your project. The default projects folder of MSVS can be found under Documents/ Visual Studio 2010/ and the picture should give you an idea how to find your assembly file.


9.PackageContent.xml definition

Place the following code inside the xml file you created earlier. If you placed yours inside a folder, make sure you adjust the attribute ModuleName to match the correct path.


<?xml version="1.0" encoding="utf-8" ?>
<ApplicationPackage SchemaVersion="1.0" Name=" AutoLoaderExample " AppVersion="1.0">
  <Components>
    <ComponentEntry AppName="AutoLoader"
             ModuleName="AutoLoaderExample.dll"
          AppDescription="AutoLoader Example" />
  </Components>
</ApplicationPackage>

Let's take a look at the contents of the xml file first. I've kept it very simple and only the bare minimum got placed inside the file to ensure the plug-in is loaded when Autocad is launched.
If you wish to define the file in depth, I advise you to take a look at the section provided inside AutoDesk Help which explains in detail how to apply certain element and attributes inside the xml file. As far as I know there isn't a schema definition that can help you with your xml definition so you'll have to do it with the information inside the Help file.
If I am not mistaking it also explains how to attach CUI files who then get loaded in the Plug-in ribbon section of Autocad 2012 products. If I find myself some time I'll tackle this one and post how it's done.

10.Finally launch Autocad!!
The assembly file should be loaded on startup and you should be able to execute the "ALExample" command. Hope it works out well!!

autoloader bundle example download

autoloader c# project download

More information can be found in the Help of Autocad (keyword:PackageContents.xml)

4 comments:

  1. Your article cites acbmgd but I think you mean acdbmgd. That is what is referenced in the csproj.

    ReplyDelete
    Replies
    1. You're right Jason. Looks like a 'typo'. I"ll change the reference name. Thank you for pointing that one out. People that use Autoloader on Autocad 2013 won't be able to use this post, as the XML definition that goes with the bundle has to be defined differently then what's presented in this post.

      Delete
    2. I actually have the same XML working in AutoCAD 2012 and 2013 right now, which is nearly a dead ringer for what you have.

      Oh, and I forgot before but thanks for the great post. It was one of several from various sources that really helped me come up to speed on the AutoLoader plugin feature.

      Delete
  2. Your article really helped me.
    Thanks for sharing knowledge!

    ReplyDelete