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            










« More quiet command-calling: adding an inspection dimension inside AutoCAD using .NET | Main | An interview with John Walker - Part 2 »

September 17, 2008

Loading .NET modules programmatically into AutoCAD

The question of how to perform a "NETLOAD" programmatically has come in a few times. Well, it turns out the answer - provided by someone in our Engineering team - is refreshingly simple.

The NETLOAD command actually has a bare-bones implementation: the hard work of parsing the metadata defining commands etc. is done from an AppDomain.AssemblyLoad event-handler. To recreate the NETLOAD command, all you need to do is call Assembly.LoadFrom(), passing in the path to your assembly.

Here's some C# code to demonstrate this:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using System.Reflection;


namespace LoadModule

{

  public class Commands

  {

    [CommandMethod("MNL")]

    static public void MyNetLoad()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;


      PromptStringOptions pso =

        new PromptStringOptions(

          "\nEnter the path of the module to load: "

        );

      pso.AllowSpaces = true;


      PromptResult pr = ed.GetString(pso);

      if (pr.Status != PromptStatus.OK)

        return;


      try

      {

        Assembly.LoadFrom(pr.StringResult);

      }

      catch(System.Exception ex)

      {

        ed.WriteMessage(

          "\nCannot load {0}: {1}",

          pr.StringResult,

          ex.Message

        );

      }

    }

  }

}

Incidentally, my preferred way to manage this is to enable demand loading for your assemblies, as per this previous post. But there are certainly situations where the above technique will prove useful.

In the next post: on Friday you should see the second installment of the interview with John Walker...

TrackBack

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

Listed below are links to weblogs that reference Loading .NET modules programmatically into AutoCAD:

blog comments powered by Disqus

Feed/Share

10 Random Posts