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...

8 responses to “Loading .NET modules programmatically into AutoCAD”

  1. I've been using a simple lisp function to demand-load my .NET dlls.
    I create a lisp function with the same name as the command I want to call in the dll.
    So if the command is "ReBlock" I create a lisp command function named "ReBlock".

    (defun c:ReBlock()
    (command "netload" "C:/Program Files/CompanyCAD/DotNetCAD.dll" "ReBlock")
    (princ)
    )

    I place this in my acad.lsp (or any lisp file that autoloads.

    The first time the command is called the lisp version runs. It netloads the dll and runs the command.
    On subsequent calls to the command the lisp function is ignored and the command in the dll runs.

    For me this proves easier than the alternatives. Hopefully it's also a safe and stable way to do this. I haven't had any problems so far.

  2. Sure - using LISP is certainly an option. In fact you could very easily write your own .NET module-loading equivalent of the (autoload) function.

    I personally prefer to live with the overhead of managing Registry keys rather than relying on an .lsp file. Once again, it's a choice.

    Kean

  3. Quite useful post.Thank you.
    Geospatial Utilities

  4. bonjour,

    est ce que vous pouvez me conseiller des titres de livres concernant la programmation d'autocad avec arx et c#.net

  5. bonjour,

    est ce que vous pouvez me conseiller des titres de livres concernant la programmation d'autocad avec arx et c#.net

  6. Asmaa -

    Jerry Winters has written a good VB.NET book, I believe. Back in the day Charles McAuley had written a good introductory ObjectARX book, but I don't know whether it's still in print.

    I would definitely take a look at the resources on the AutoCAD Developer Center, especially the tutorials linked to/posted there.

    Kean

  7. Hi Kean - wondering if I'm missing something here - how will users netload this particular command. it's a chicken-egg problem right?

    1. Hi Ben,

      Well, yes. 🙂

      You do indeed need the "loader" module to be loaded - whether manually, via demand-loading or the Autoloader - so this post is more about loading dependent modules.

      Kean

Leave a Reply to Kean Walmsley Cancel reply

Your email address will not be published. Required fields are marked *