15
Demand-Loading AutoCAD® .NET Plug-ins James E. Johnson – Synergis Software CP5239 This class will discuss and illustrate required registry settings for demand-loading a .NET or ObjectARX® plug-in. We will examine code samples that illustrate how to make an AutoCAD plug-in add the demand-load registry settings when it is first loaded and how to create a command that removes the registry settings. We will also discuss the new AutoCAD AutoLoader for automatic loading of ObjectARX and .NET plug-ins. Learning Objectives At the end of this class, you will be able to: Setting demand-load registry settings. Manage demand-load registry settings from your code. Prepare code and setups required for installers. Use the AutoCAD AutoLoader to automatically load plug-ins. About the Speaker James has worked with CAD products for more than 25 years, involved in many positions from being a CAD drafter to writing automation applications. In his current position, he is doing CAD integration for Adept document management system. In previous positions, he has used Autodesk® RealDWG® to write custom automation to create AutoCAD® drawings of industrial kitchen equipment, and has worked at Autodesk resellers in software development groups doing custom applications for Inventor® and AutoCAD®. He has taught AutoCAD® and VBA classes while working for resellers, and was a CAD instructor at two different community colleges. Email: [email protected]

Demand-Loading AutoCAD® .NET Plug-ins - Autodeskaucache.autodesk.com/au2011/sessions/5239/class_handouts/v1_CP5239...Demand-Loading AutoCAD® .NET Plug-ins James E. Johnson – Synergis

Embed Size (px)

Citation preview

Demand-Loading AutoCAD® .NET Plug-insJames E. Johnson – Synergis Software

CP5239 This class will discuss and illustrate required registry settings for demand-loading a .NET or ObjectARX® plug-in. We will examine code samples that illustrate how to make an AutoCAD plug-in add the demand-load registry settings when it is first loaded and how to create a command that removes the registry settings. We will also discuss the new AutoCAD AutoLoader for automatic loading of ObjectARX and .NET plug-ins.

Learning  ObjectivesAt the end of this class, you will be able to:• Setting demand-load registry settings.• Manage demand-load registry settings from your code.• Prepare code and setups required for installers.• Use the AutoCAD AutoLoader to automatically load plug-ins.

About  the  SpeakerJames has worked with CAD products for more than 25 years, involved in many positions from being a CAD drafter to writing automation applications. In his current position, he is doing CAD integration for Adept document management system. In previous positions, he has used Autodesk® RealDWG® to write custom automation to create AutoCAD® drawings of industrial kitchen equipment, and has worked at Autodesk resellers in software development groups doing custom applications for Inventor® and AutoCAD®. He has taught AutoCAD® and VBA classes while working for resellers, and was a CAD instructor at two different community colleges.

Email: [email protected]

To run you application during development you use the Netload command to run and debug your application. When deploying a .NET plugin for AutoCAD you would not want your users to need to run the Netload command to load the plugin every time they need to use it. There have been several ways to load applications in AutoCAD from acad.lsp to the AutoCAD Appload contents.

Demand loading the application is the most professional way and lends itself to installers better than editing files or handling the Appload registry settings.

Setting demand-load registry settings

Demand load settings are stored in one of two places in the registry: a. HKEY_LOCAL_MACHINE (for all users requires privileges for installers)b. HKEY_CURRENT_USER (for current user only)

The decision on which will depend on if the application is to be shared across all users but also if using an installer that the user has privileges to write to HKEY_LOCAL_MACHINE.

Under the Software\Autodesk\AutoCAD key is Release version key (e.g. R18.2)

A001 is the product ID for AutoCAD 2012.

409 is English version

HKEY_LOCAL_MACHINE\Software\Autodesk\AutoCAD\R18.2\ACAD-A001:409

Demand-Loading AutoCAD® .NET Plug-ins

2

Loading applications on AutoCAD Startup

Under HKEY_LOCAL_MACHINE\Software\Autodesk\AutoCAD\R18.2\ACAD-A001:409\Applications we add a registry key for our application.

This shows an application named CSharpPlugin added.

With settings to demand load the application on startup.

A registry reg file could be created to create this file with the following contents, in the next section we will look at some code examples to create these settings with code and later with an installer:

Windows Registry Editor Version 5.00[HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R18.2\ACAD-A001:409\Applications\CSharpPlugin]"DESCRIPTION"="C# sample plugin""LOADCTRLS"=dword:00000002"LOADER"="C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\CSharpPlugin\\CSharpPlugin\\bin\\Release\\CSharpPlugin.dll""MANAGED"=dword:00000001

"DESCRIPTION" is any text string to describe the application"LOADCTRLS" defines the action demand load action 0x01 Load the application upon detection of proxy object. 0x02 Load the application upon startup. 0x04 Load the application upon invocation of a command. 0x08 Load the application upon request by the user or another application. 0x10 Do not load the application. 0x20 Load the application transparently.A value of 2 like the sample ‘reg’ file contents will demand load the application on startup and is used to load the application when AutoCAD starts, a setting of 4 with some additional values will load on command invocation. These are the two settings most commonly used for .NET plugin applications.

"LOADER" must contain the location of the application"MANAGED" must be set to 1 for .NET applications

Demand-Loading AutoCAD® .NET Plug-ins

3

Loading applications on Command Invocation

Creating a sub key under the Application key named commands allows setting up the demand loading to load the application on command invocation.

To load the command on invocation of the command you would set LOADCTRLS to 4

Add string settings to the key for each command in the application that you want to be able to load the application...

In addition to the “Commands” key you can also create a key named “Groups” then add string settings for any command groups that your application uses.

The only group used in the example is LOADGROUP.

Note:In AutoCAD the DEMANDLOAD system variable controls the demand loading options of ObjectARX applications. The legitimate for the system variable may be used in combination. They are defined as follows:0 Disables demand loading of all ObjectARX applications.1 Enables demand loading of ObjectARX applications upon detection of proxy objects.2 Enables demand loading of ObjectARX applications upon command invocation.3 Enables demand loading for both proxy objects and command invocation

Demand-Loading AutoCAD® .NET Plug-ins

4

Manage demand-load registry settings from your codeIn simple applications it is often easy to add code in the application to add or remove the demand load registry settings. An example of this would be:

Commands:

[CommandMethod("LOADGROUP","myPlugLoad",CommandFlags.Session)]public  void  myPlug(){        setDemandLoad(true);}

[CommandMethod("LOADGROUP","myPlugUnLoad",CommandFlags.Session)]public  void  myUnPlug(){        setDemandLoad(false);}

Method to Create registry keys and settings (C# example):

public  void  setDemandLoad(bool  createTree){        Microsoft.Win32.RegistryKey  acadAppKey  =  GetAcadAppKey(true);        string  curAssemblyPath  =  System.Reflection.Assembly.GetExecutingAssembly().Location;        Microsoft.Win32.RegistryKey  acadAppInspectorKey  =         acadAppKey.CreateSubKey("CSharpPlugin");        try        {                if  (createTree)                {                        //  Add  demand  load  settings                        acadAppInspectorKey.SetValue("DESCRIPTION",  "C#  sample  plugin",               Microsoft.Win32.RegistryValueKind.String);                        //  for  load  on  startup                      //acadAppInspectorKey.SetValue("LOADCTRLS",  2,               Microsoft.Win32.RegistryValueKind.DWord);            //  for  load  by  command  invoke                                    acadAppInspectorKey.SetValue("LOADCTRLS",  4,               Microsoft.Win32.RegistryValueKind.DWord);                          acadAppInspectorKey.SetValue("LOADER",  curAssemblyPath,               Microsoft.Win32.RegistryValueKind.String);                        acadAppInspectorKey.SetValue("MANAGED",  1,               Microsoft.Win32.RegistryValueKind.DWord);                                                                //  Add  Commands                        Microsoft.Win32.RegistryKey  acadAppCommandKey  =             acadAppInspectorKey.CreateSubKey("Commands");                        acadAppCommandKey.SetValue("FirstCommand",  "FirstCommand",             Microsoft.Win32.RegistryValueKind.String);                        acadAppCommandKey.SetValue("myPlugUnLoad",  "myPlugUnLoad",             Microsoft.Win32.RegistryValueKind.String);                        acadAppCommandKey.SetValue("myPlugLoad",  "myPlugLoad",             Microsoft.Win32.RegistryValueKind.String);

                                       

Demand-Loading AutoCAD® .NET Plug-ins

5

Use Reflection to get location of Application.

Set Required Settings for Demand Loading.

                       //  Add  Groups                        Microsoft.Win32.RegistryKey  acadAppGroupsKey  =             acadAppInspectorKey.CreateSubKey("Groups");                        acadAppGroupsKey.SetValue("LOADGROUP",  "LOADGROUP",             Microsoft.Win32.RegistryValueKind.String);                }                else                {                        acadAppKey.DeleteSubKeyTree("CSharpPlugin");                }        }        catch        {        }

       acadAppKey.Close();}

private  Microsoft.Win32.RegistryKey  GetAcadAppKey(bool  forWrite){        Microsoft.Win32.RegistryKey  acadKey  =  Microsoft.Win32                                                                                                      .Registry                                                                                                      .CurrentUser                                                                                                      .OpenSubKey(Autodesk.AutoCAD                                                                                                             .DatabaseServices                                                                                           .HostApplicationServices                                                                                                             .Current                           .RegistryProductRootKey);

       return  acadKey.OpenSubKey("Applications",  forWrite);}

The above code example creates two commands one that will create the registry keys and settings to load the application on command invocation and the other to remove the registry keys.

Demand-Loading AutoCAD® .NET Plug-ins

6

Gets Registry AutoCAD registry key of version application is loaded into...

Prepare code and setups required for installersTo create demand loading in a setup project requires doing similar setup in your Setup project as we did above in the example demand loading code.

In Visual Studio Create a new setup project and open the registry editor...

Add the ‘keys’ the same as required in the Registry to the setup project registry editor.

Create registry ‘keys’ for each version of AutoCAD the application is targeted to run in...

There are properties for not creating that may need to be set...

Add string values for demand loading...

Under Commands ‘key’ and ‘Groups ‘key’ add string values for commands and groups used in application.

Demand-Loading AutoCAD® .NET Plug-ins

7

Under Applications key add a key for the name of the application this does not need to be the name of the DLL.

Use the AutoCAD® AutoLoader to automatically load plug-insA new feature with AutoCAD® 2012 is the plug-in auto loader mechanism which uses a package format to make deployment of custom applications easy.

The package format is a folder with an extension of ‘.bundle’ in the folder is an XML file that defines the plug-in components and folders containing the application(s). This makes it easy to target multiple operating systems and product releases since the parameters of your plug-in are defined in the XML file.

The plug-in defined by each package is loaded into AutoCAD by placing it in one of the ApplicationPlugins folders on your local drive.

There are two different ApplicationPlugins folders that you can use:

1. %ProgramFiles%\Autodesk\ApplicationPlugins (Any User)2. %AppData%\Roaming\Autodesk\ApplicationPlugins (Only Current User)

When AutoCAD starts, both ApplicationPlugins folders are checked for plug-in applications.

Packages found are automatically registered and loaded based on the metadata in the XML file of each package.

In AutoCAD a command and a system variable are associated with the plug-in autoloader:

1. APPAUTOLOADER - Command LIST - displays a list of plugins Reload - reloads plugins

2. APPAUTOLOAD - System Variable (initial value = 14) The load behavior for plug-ins is controlled with this system variable

0 Do not load plug-in applications at any time

1 Display all messages when loading plug-in applications

2 Load plug-in applications at startup (initial)

4 Load plug-in applications when a new drawing is opened (initial)

8 Load plug-in applications when they appear in the plug-ins folder (initial)

With a default setting of 14 plug-ins are automatically registered with AutoCAD and when a new plug-in is installed during the current session.

When APPAUTOLOAD is set to 0, plug-ins are not loaded unless the APPAUTOLOADER command is used.

Uninstall Plug-in Packages

A package can be uninstalled by removing the appropriate folder with a .bundle extension from the ApplicationPlugins folder. This can be accomplished by offering an uninstall option with the original installer or to manually delete the .bundle folder.

Demand-Loading AutoCAD® .NET Plug-ins

8

The Package folder contains an XML file named “PackageContents.xml” with metadata that defines the plug-in.

Sample Of a PackageContents.xml

There are three primary elements in this file ApplicationPackage,CompanyDetails, and Components. The Components element has two elements RuntimeRequirements and ComponentEntry.

The settings contained in the PackageContents.xml file are used to determine what plug-ins are loaded when and how they are loaded, setup for specific operating systems and display versioning and localization names and files.

The following Images and text about the Attributes and Elements in the PackageContents.xml were pulled from the Autodesk Exchange Online help...

Demand-Loading AutoCAD® .NET Plug-ins

9

Command(s) are listed under the Commands element.

Location of application uses a relative path to point to the Contents folder in the Package

Using the ComponentEntry Attribute LoadOnCommandInvocation

Allows the application to be loaded when needed.

This sample loads a C# DLL and a simple LSP file on startup...

ApplicationPackage Attributes

Demand-Loading AutoCAD® .NET Plug-ins

10

SchemaVersion and AppVersion are REQUIRED

OPTIONAL local

REQUIRED for Autodesk Exchange

REQUIRED

OPTIONALlocal

REQUIRED for Autodesk Exchange

CompanyDetails Attributes

Components Element - RuntimeRequirements Attributes

Demand-Loading AutoCAD® .NET Plug-ins

11All RUNTIMEREQUIREMENTS are OPTIONAL

All COMPANYDETAILS are OPTIONAL

Components Element

The ComponentEntry element is required and is used to specify details about each individual component in the Components element.

You can specify as many ComponentEntry elements as needed. Component types can be one of the following file formats:

• AutoLISP (LSP)• AutoLISP Fastload (FAS)• Visual LISP (VLX)• Managed or Mixed Mode .NET Assembly (.dll)• ObjectARX (ARX)• ObjectDBX (DBX)• Partial Customization (CUIx)

A ComponentEntry element may contain a Commands element if the LoadReasons attribute is set to LoadOnCommandInvocation.

ComponentEntry Attributes

Demand-Loading AutoCAD® .NET Plug-ins

12

APPNAME is REQUIRED

APPDESCRIPTION and APPTYPE are OPTIONAL

Demand-Loading AutoCAD® .NET Plug-ins

13

MODULNAME is REQUIRED

OPTIONAL

Commands and Command Element

The Commands element is optional unless the LoadOnCommandInvocation parameter is enabled for the LoadReasons attribute. Used to specify which commands to register for LoadOnCommandInvocation.

You can specify more than one Command element as needed.

A Commands element can have the following attribute attached to it:

Command ElementSpecifies the global and local names for each command.

A Command element can have any of the following attributes attached to it:

The following is a full list of all supported locale codes:

• Chs - Chinese (PRC)• Cht - Chinese (Taiwan)• Csy - Czech• Deu - GermanEnu - English• Esp - Spanish• Fra - French• Hun - Hungarian• Ita - Italian• Jpn - Japanese• Kor - Korean• Plk - Polish• Rus - Russian

Demand-Loading AutoCAD® .NET Plug-ins

14

REQUIRED if COMMANDS are present.

OPTIONALlocal

REQUIRED for Autodesk Exchange

LocalEsp would be the local Spanish command...

Using an InstallerUsing the Autoloader with an installer is as simple as creating a folder with an extension name of .bundle and copying files to that folder or sub-folders in either of the ApplicationPlugins folders with a ProjectContents.xml file to control loading actions. To uninstall the installer needs to delete the folder with the “.bundle” extension and files/sub-folders contained in the folder.

Sunmmary

Demand loading is well documented in the AutoCAD ObjectArx help documents, in the help document search on “Creating AutoCAD Subkeys” and “demand loading applications”.

AutoLoader is well documented in the AutoCAD online help search on ‘APPAUTOLOADER’

Links:Kean Walmsley :

http://through-the-interface.typepad.com/through_the_interface/2006/09/automatic_loadi.html

Stephen and Fenton talk about the Autoloader:http://through-the-interface.typepad.com/through_the_interface/2011/05/adn-devcast-episode-6-autoloader.html

AutoCAD Exchange:

http://exchange.autodesk.com/autocad/enu/online-help/search#WS73099cc142f4875533992bfb12ce8a5f915-7e45.htm

Demand-Loading AutoCAD® .NET Plug-ins

15