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