November 2014

Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            










« Highlighting named blocks using AutoCAD 2010’s overrule API from .NET | Main | Happy birthday to me… happy birthday to me… »

June 17, 2009

A new project wizard for AutoCAD .NET development

Some of you may remember a philosophical question I raised in a post some time ago. The overall consensus of the feedback we received was to prioritise delivering code samples (given a choice between code samples and wizard-like code-generation tools). Which we are doing, both via the ADN site and our developer blogs… but that doesn’t mean some investment in code-generation tools isn’t also appropriate, from time-to-time. :-)

Cyrille Fauvel, who manages the Media & Entertainment arm of DevTech but also occasionally works on technical activities that relate to our other products, has put together a new (currently draft) version of the AutoCAD .NET Wizard. This tool integrates with the Visual Studio 2008 IDE (as well as the 2008 editions of Visual C# Express and Visual Basic Express), adding C#/VB.NET project templates for AutoCAD development. (This version of the tool only works with the 2008 versions – we’re currently evaluating whether (and how) to support the 2005 versions of Visual Studio [Express] or simply to recommend using our previously posted templates.)

When you select one of these “Autodesk” project templates you will get a skeleton project that defines the application initialization protocol plus a few placeholder commands while setting up the ability to define localized commands.

Here are a few images stepping you through the installation of this Wizard (nothing very surprising, which is why I’ve kept the thumbnails so small :-):

AutoCAD 2010 NET Wizards install AutoCAD 2010 NET Wizards install location AutoCAD 2010 NET Wizards install confirmation AutoCAD 2010 NET Wizards install progress AutoCAD 2010 NET Wizards install complete

[Note: the current installer requests an install location, which is unnecessary. The installer only uses this location for temporary files, removing them afterwards. This step will be removed from a future build of the tool.]

Once installed (and Visual Studio 2008 has been re-started), you will get some new options when you start a new project…

AutoCAD 2010 NET Wizards new project menu item

… that include the “AutoCAD 2010 plug-in” project template:

AutoCAD 2010 NET Wizards new project template

We’ve numbered these templates “2010”, as they provide support for API features delivered with AutoCAD 2010, but if you don’t select these features the created project should work with older versions of AutoCAD. At least that’s the theory. :-)

When you first select one of these project types, you will be asked to select the location of your ObjectARX SDK:

AutoCAD 2010 NET Wizards configurator

You should specify this along with the assemblies you would like to reference for this particular project:

AutoCAD 2010 NET Wizards configurated

On completion of this dialog your skeleton project will be created.

[Something else to note: the tool currently creates projects which allow addition of Windows Presentation Foundation content – to do this we had to add a little piece of XML to the project file that specifies a target .NET Framework version of 3.0 or above as well as a GUID to identify the project as WPF-compatible. This tells Visual Studio to list WPF item-types when you add new items to the project, but I expect this to become another configuration option in a future version as some developers will want to target framework versions where WPF is unavailable.]

Let’s take a look at the skeleton code created for a C# project, reformatted to fit the width of the blog…

First the myPlugin.cs file:

using System;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.EditorInput;

 

// This line is not mandatory, but improves loading performance

[assembly: ExtensionApplication(

  typeof(Autodesk.AutoCAD.AutoCAD_2010_plug_in1.MyPlugin)

)]

 

namespace Autodesk.AutoCAD.AutoCAD_2010_plug_in1

{

 

  // This class is instantiated by AutoCAD once and kept alive for

  // the duration of the session. If you don't do any one time

  // initialization then you should remove this class.

  public class MyPlugin : IExtensionApplication

  {

 

    void IExtensionApplication.Initialize()

    {

      // Add one time initialization here

      // One common scenario is to setup a callback function here

      // that unmanaged code can call.

      // To do this:

      // 1. Export a function from unmanaged code that takes a

      //    function pointer and stores the passed in value in a

      //    global variable.

      // 2. Call this exported function in this function passing

      //    delegate.

      // 3. When unmanaged code needs the services of this managed

      //    module you simply call acrxLoadApp() and by the time

      //    acrxLoadApp returns  global function pointer is

      //    initialized to point to the C# delegate.

      // For more info see:

      //http://msdn2.microsoft.com/en-US/library/5zwkzwf4(VS.80).aspx

      //http://msdn2.microsoft.com/en-us/library/44ey4b32(VS.80).aspx

      //http://msdn2.microsoft.com/en-US/library/7esfatk4.aspx

      // as well as some of the existing AutoCAD managed apps.

 

      // Initialize your plug-in application here

    }

 

    void IExtensionApplication.Terminate()

    {

      // Do plug-in application clean up here

    }

  }

}

And now the myCommands.cs file:

using System;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.EditorInput;

 

// This line is not mandatory, but improves loading performances

[assembly: CommandClass(

  typeof(Autodesk.AutoCAD.AutoCAD_2010_plug_in1.MyCommands)

)]

 

namespace Autodesk.AutoCAD.AutoCAD_2010_plug_in1

{

 

  // This class is instantiated by AutoCAD for each document when

  // a command is called by the user the first time in the context

  // of a given document. In other words, non static data in this

  // class is implicitly per-document!

  public class MyCommands

  {

    // The CommandMethod attribute can be applied to any public

    // member function of any public class.

    // The function should take no arguments and return nothing.

    // If the method is an intance member then the enclosing class is

    // intantiated for each document. If the member is a static

    // member then the enclosing class is NOT intantiated.

    //

    // NOTE: CommandMethod has overloads where you can provide helpid

    // and context menu.

 

    // Modal Command with localized name

    [CommandMethod(

      "MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal

    )]

    public void MyCommand() // This method can have any name

    {

      // Put your command code here

    }

 

    // Modal Command with pickfirst selection

    [CommandMethod(

      "MyGroup", "MyPickFirst", "MyPickFirstLocal",

      CommandFlags.Modal | CommandFlags.UsePickSet

    )]

    public void MyPickFirst() // This method can have any name

    {

      PromptSelectionResult result =

        Application.DocumentManager.MdiActiveDocument.Editor.

          SelectImplied();

      if (result.Status == PromptStatus.OK)

      {

        // There are selected entities

        // Put your command using pickfirst set code here

      }

      else

      {

        // There are no selected entities

        // Put your command code here

      }

    }

 

    // Application Session Command with localized name

    [CommandMethod(

      "MyGroup", "MySessionCmd", "MySessionCmdLocal",

      CommandFlags.Modal | CommandFlags.Session

    )]

    public void MySessionCmd() // This method can have any name

    {

      // Put your command code here

    }

 

    // LispFunction is similar to CommandMethod but it creates a

    // lisp callable function. Many return types are supported not

    // just string or integer.

    [LispFunction("MyLispFunction", "MyLispFunctionLocal")]

    public int MyLispFunction(ResultBuffer args)

    // This method can have any name

    {

      // Put your command code here

 

      // Return a value to the AutoCAD Lisp Interpreter

      return 1;

    }

  }

}

The myCommands.resx file needed to define the local command names (for the MySessionCmd command) has also been added to the project automatically to help with command localization.

All well and good… but it may be that these project templates don’t quite fit your internal needs, whether due to the copyright notices in the source and assembly properties [something I do expect to be removed from a future build – it doesn’t make sense for Autodesk copyright notices to be placed in your project skeleton] or because you have a standard approach you’ve adopted for command registration (or whatever). The good news is that it’s pretty easy for you to modify these baseline templates for your own needs:

  1. Locate the template you wish to update, e.g. “C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\CSharp\Autodesk\AutoCAD 2010 plug-in.zip” (this path will vary if you’re running on a 64-bit or localized OS or if you’re using an Express version of Visual Studio, but the technique should still work).
  2. Unzip the files somewhere.
  3. Modify the source code and project setup to meet your needs.
  4. Modify the template file (MyTemplate.vstemplate – an XML file providing Visual Studio with additional information not stored in the project), as needed.
  5. ZIP the files back up, choosing a new name for the .ZIP archive.
  6. Post the file within the ProjectTemplates folder structure.
  7. Run Visual Studio with the /InstallVSTemplates command parameter (the easiest way is to open a “Visual Studio 2008 Command Prompt” and use it to execute “devenv /InstallVSTemplates”).

As mentioned a few times, I do expect us to provide a new version of this tool with some minor wrinkles ironed out, but it’s pretty much ready-to-go (and certainly ready for people to try out and respond with their feedback :-). Please let us know how you get on, whether by posting a comment or by emailing our wizard developers.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83452464869e20115711cf35c970b

Listed below are links to weblogs that reference A new project wizard for AutoCAD .NET development:

blog comments powered by Disqus

Feed/Share

10 Random Posts