Monday, April 3, 2017

Asking User To Select Entity In Other Drawing - 1 of 2

A question recently posted in AutoCAD discussion forum reminded me the very same situation I had dealt with when my office upgraded to AutoCAD 2015: a couple of existing AutoCAD add-in apps developed prior to AutoCAD 2015 were broken: during the execution of a custom command (with CommandFlags.Session being set), user has to switch another drawing (if it is not currently open in the same AutoCAD session, it then would be opened first, of course) and pick something (either merely picking a point, or picking one or more entities); after picking, AutoCAD should switch back to the drawing where the command starts from and continue.

There is no problem of doing this operation with AutoCAD prior to AutoCAD 2015. But since AutoCAD2015, a quite impacting change was introduced: switching active drawing from one drawing to another drawing in AutoCAD session will cancel currently executing command. It is this change broke our existing applications, and I had to fix the breaks before our office can move up to AutoCAD2015. It was done quite a while ago. At that time, I thought to post my solution in my blog here and wrote something down as draft. However I had never found time to complete it. Now seeing the question is raised, a few days ago, I dug the old stuff out and updated it, so that it can be shared by people who may be interested in.

I tried 2 different ways to deal with this issue. Here is the discussion on the first approach.

The solution is rather simple actually with these key points:
1. Do not have the source drawing (the drawing you want user to go into and select something there) been already open in AutoCAD, so that you DO NOT switch active drawing (in order to pick);
2. In a session command, you can use code to open the source drawing; upon the drawing being opened, it becomes MdiActiveDocument automatically (because of CommandFlags.Session, of course); then you can use Editor.Getxxxx() to let user select; once selecting is done, close the source drawing, so that the original working drawing becomes MdiActiveDocument again, the command continues.

My following code demonstrates 2 commands being used in the situation:

  • Command "GetDataCmd": once the command starts, AutoCAD open the source drawing for user to select a circle; after selecting, the source drawing is closed; then a circle of the same center point and radius is drawn;
  • Command "GetDataUI": the command brings up a modal dialog; user clicks "Get Circle" button to hide to dialog and opens the source drawing for selecting. If selecting is done, the source drawing is closed and the dialog box comes back to show the data obtained from source drawing; clicking "OK" button draws a circle in current drawing with obtained circle data (center point and radius).
  • The source drawing has a few circles in it for user to select. For the simplicity, I omitted the code to show an "Open File" dialog box to allow user to choose a source drawing. Instead, I hard-coded a file name.
See following code.

The CommandClass code:
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
 
[assemblyCommandClass(typeof(SelectFromOtherDwg.MyCommands))]
 
namespace SelectFromOtherDwg
{
    public class MyCommands
    {
        private static string _sourceDwgFile = @"C:\Temp\MyCircles.dwg";
 
        [CommandMethod("GetDataCmd"CommandFlags.Session)]
        public static void GetDataFromOtherDwgViaCmd()
        {
            var doc = CadApp.DocumentManager.MdiActiveDocument;
            if (!doc.IsNamedDrawing)
            {
                CadApp.ShowAlertDialog(
                    "Current drawing is untitled. Please save it first!");
                return;
            }
 
            string curDwgName = doc.Name;
 
            var sourceDwg = OpenDataSourceDrawing(_sourceDwgFile);
            if (sourceDwg != null)
            {
                Point3d point;
                double radius;
                bool picked = false;
 
                picked = GetSourceCircleCenter(
                    sourceDwg.Editor, out point, out radius);
                sourceDwg.CloseAndDiscard();
 
                if (picked)
                {
                    foreach (Document d in CadApp.DocumentManager)
                    {
                        if (d.Name.ToUpper()==curDwgName.ToUpper())
                        {
                            DrawCircle(d, point, radius);
                            CadApp.DocumentManager.MdiActiveDocument = d;
                            break;
                        }
                    }           
                }
            }
            else
            {
                CadApp.ShowAlertDialog("Cannot open source drawing!");
            }
        }
 
        [CommandMethod("GetDataUI"CommandFlags.Session)]
        public static void GetDataFromOtherDwgViaUI()
        {
            var doc = CadApp.DocumentManager.MdiActiveDocument;
            if (!doc.IsNamedDrawing)
            {
                CadApp.ShowAlertDialog(
                    "Current drawing is untitled. Please save it first!");
                return;
            }
 
            string curDwgName = doc.Name;
 
            var dlg = new dlgGetData();
 
            double radius = 0.0;
            Point3d point = Point3d.Origin;
 
            try
            {
                var res = CadApp.ShowModalDialog(dlg);
                if (res== System.Windows.Forms.DialogResult.OK)
                {
                    if (dlg.CloseForPick)
                    {
                        var sourceDwg = OpenDataSourceDrawing(_sourceDwgFile);
                        if (sourceDwg != null)
                        {
                            Point3d pt;
                            double r;
                            bool picked = false;
 
                            picked = GetSourceCircleCenter(
                                sourceDwg.Editor, out pt, out r);
                            sourceDwg.CloseAndDiscard();
 
                            if (picked)
                            {
                                dlg.SetPickedData(pt.X, pt.Y, pt.Z, r);
                                res = CadApp.ShowModalDialog(dlg);
                                if (res == System.Windows.Forms.DialogResult.OK)
                                {
                                    if (!dlg.CloseForPick)
                                    {
                                        radius = dlg.Radius;
                                        point = new Point3d(
                                            dlg.CenterX, dlg.CenterY, dlg.CenterZ);
                                    }
                                }
                            }
                        }
                        else
                        {
                            CadApp.ShowAlertDialog("Cannot open source drawing!");
                        }
                    }
                }
 
                if (radius > 0.0)
                {
                    foreach (Document d in CadApp.DocumentManager)
                    {
                        if (d.Name.ToUpper() == curDwgName.ToUpper())
                        {
                            DrawCircle(d, point, radius);
                            CadApp.DocumentManager.MdiActiveDocument = d;
                            break;
                        }
                    }
                }
            }
            finally
            {
                dlg.Dispose();
            }
        }
 
        #region private methods
 
        private static Document OpenDataSourceDrawing(string dwgFileName)
        {
            if (!File.Exists(dwgFileName)) return null;
 
            var dwg = CadApp.DocumentManager.Open(dwgFileName, true);
            return dwg;
        }
 
        private static bool GetSourceCircleCenter(
            Editor ed, out Point3d centerPoint, out double radius)
        {
            centerPoint = Point3d.Origin;
            radius = 0.0;
 
            var opt = new PromptEntityOptions("\nSelect a circle:");
            opt.SetRejectMessage("\nInvalid: not a circle.");
            opt.AddAllowedClass(typeof(Circle), true);
 
            var res = ed.GetEntity(opt);
            if (res.Status==PromptStatus.OK)
            {
                using (var tran = 
                    ed.Document.TransactionManager.StartTransaction())
                {
                    var c = (Circle)tran.GetObject(
                        res.ObjectId, OpenMode.ForRead);
                    centerPoint = c.Center;
                    radius = c.Radius;
                    tran.Commit();
                }
 
                return true;
            }
            else
            {
                return false;
            }
        }
 
        private static void DrawCircle(Document dwg, Point3d pt, double r)
        {
            using (var lck = dwg.LockDocument())
            {
                using (var tran = 
                    dwg.TransactionManager.StartTransaction())
                {
                    var model = (BlockTableRecord)tran.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(
                            dwg.Database), OpenMode.ForWrite);
 
                    var c = new Circle();
                    c.Center = pt;
                    c.Radius = r;
                    c.SetDatabaseDefaults(dwg.Database);
 
                    model.AppendEntity(c);
                    tran.AddNewlyCreatedDBObject(c, true);
 
                    tran.Commit();
                }
            }
        }
 
        #endregion
    }
}

This is the modal dialog form:


The form's code is here:
using System;
using System.Windows.Forms;
 
namespace SelectFromOtherDwg
{
    public partial class dlgGetData : Form
    {
        private bool _closeForPick = false;
 
        public dlgGetData()
        {
            InitializeComponent();
 
            btnOK.Enabled = false;
        }
 
        public bool CloseForPick
        {
            get { return _closeForPick; }
        }
 
        public double Radius
        {
            get { return double.Parse(txtR.Text); }
        }
 
        public double CenterX
        {
            get { return double.Parse(txtX.Text); }
        }
 
        public double CenterY
        {
            get { return double.Parse(txtY.Text); }
        }
 
        public double CenterZ
        {
            get { return double.Parse(txtZ.Text); }
        }
 
        public void SetPickedData(double x, double y, double z, double r)
        {
            txtX.Text = x.ToString();
            txtY.Text = y.ToString();
            txtZ.Text = z.ToString();
            txtR.Text = r.ToString();
 
            btnOK.Enabled = true;
        }
 
        private void btnGet_Click(object sender, EventArgs e)
        {
            _closeForPick = true;
            this.DialogResult = DialogResult.OK;
        }
 
        private void btnOK_Click(object sender, EventArgs e)
        {
            _closeForPick = false;
            this.DialogResult = DialogResult.OK;
        }
 
        private void dlgGetData_Load(object sender, EventArgs e)
        {
 
        }
    }
}

Now, as usual, go to this video clip to see the code in action.

One may notice that at beginning of each command, I check if current drawing is new, untitled drawing or not. That is because if the command is executed with an untitled drawing (usually it is automatically created by AutoCAD, when the system variable "StartUp" is set 0), when AutoCAD opens an existing drawing (in this case, the source drawing), AutoCAD would close the untitled drawing silently (if not change is made to it). Then after user selects in the source drawing and AutoCAD closes the source drawing, there would be no drawing gin AutoCAD left open to continue with the command.

As the code and the video shows, this approach is rather simple. However, if the source drawing is large in size, opening the drawing takes time. Especially, if the process required user to do the selection in the source drawing multiple time, the repeated slow opening/closing would be quite annoying. In next post, I'll discuss the second approach I used. Stay tuned.

Friday, March 3, 2017

Handling Document.UnknownCommand Event, 2 of 2

In previous article I showed how to write a simple on demand application loader by handling Document.UnknownCommand event. We have seen that AutoCAD acts differently from other Document/Document event when it is handling: after running the code in the event handler, AutoCAD try to execute the previously unknown command again. That is, the first time AutoCAD executes an unknown command, Document.UnknownCommand event fires, which allows a chance for custom application to do something - here is how a custom on demand loading process could be plugged in; then AutoCAD tries to execute the same command again; if the command is still not defined, then AutoCAD spills "Unknown command" message at command line; only this time, no UnknownCommand event is fired.

During my exploration of handling UnknownCommand event, I came across another interesting behaviour of this event, which is quite similar to the notorious behaviour of IExtensionApplication.Initialize(): the code in the event handler must make sure any possible exception being handled. If an exception occurs inside the event handler, instead of breaking AutoCAD process, AutoCAD simply silently swallow the exception and keeps going, but the event handler will stop working, as if it is removed.

Here is the code to show this behaviour:
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
 
[assemblyCommandClass(typeof(UnknownCommandException.MyCommands))]
 
namespace UnknownCommandException
{
    public class MyCommands 
    {
        private bool _handled = false;
 
        [CommandMethod("HandleUkCm")]
        public void HandleUnknownCommandEvent()
        {
            var doc = CadApp.DocumentManager.MdiActiveDocument;
            var ed = doc.Editor;
 
            if (!_handled)
            {
                doc.UnknownCommand += Doc_UnknownCommand;
                _handled = true;
                ed.WriteMessage("\nUnknownCommand event handler has been added.");
            }
            else
            {
                doc.UnknownCommand -= Doc_UnknownCommand;
                _handled = false;
                ed.WriteMessage("\nUnknownCommand event handler has been removed.");
            }
        }
 
        private void Doc_UnknownCommand(object sender, UnknownCommandEventArgs e)
        {
            var msg = "This is custom UnknowCommand event handler." +
                "\n\nClick \"Yes\" to suppress ths event handler" +
                "\nClick \"No\" to keep it" +
                "\n\n Do you want to suppress this event handler?";
            var res = System.Windows.Forms.MessageBox.Show(
                msg, "Custom UnknownCommand Handler",
                System.Windows.Forms.MessageBoxButtons.YesNo,
                System.Windows.Forms.MessageBoxIcon.Question,
                System.Windows.Forms.MessageBoxDefaultButton.Button2);
            if (res== System.Windows.Forms.DialogResult.Yes)
            {
                throw new ApplicationException(
                    "Stop custom UnknownCommand event handler.");
            }
        }
    }
}

As the code logic in the event handler shows, as long as the user does not click "Yes" button in the message box, the event handler gets execution each time an unknown command is entered. Once user clicks "Yes", an exception is raised and left not handled, AutoCAD continues in spite of the exception not being handled. However, from that point on, the UnknownCommand event handler is no longer executed when unknown command is entered. This video clip shows this behaviour.

As we see, unless the code in custom UnknownCommand event handler is very simple and no chance to raise any exception, we'd better always wrap up the code in UnknownCommand event handler in try...catch... block, just as we do in IExtensionApplication.Initialize().



Monday, February 27, 2017

Handling Document.UnknownCommand Event, 1 of 2

In one of my recent article (Custom On Demand Loading), I mentioned of handling Document.UnknownCommand could be used for on demand loading (last paragraph of that article). In my further dealing with UnknownCommand event, I also ran into an interesting issue. So, I decided to write down my experience on dealing with UnknownCommand to share with the community. I split the writing into 2 parts. This is the part 1: yet another on demand loading approach.

When thinking of loading application (DLLs, in the case of AutoCAD .NET add-in), the key is to find a suitable trigger moment when AutoCAD runs.When this trigger is pull off, a set of conditions/configurations should be examined to determine if particular applications/DLLs are already loaded in the AutoCAD process, and loaded them if necessary.

In my previous article, I wanted particular applications being loaded when the currently activated drawing is from specific project (project type, location, client... could be used as the criteria), then the trigger point is DocumentCollection.DocumentBecameCurrent event: the code would examine the drawing's project information to decide what applications should be available. Then if the applications have not been loaded, load them.

Now with handling Document.UnknownCommand we can have more generic on demand loading approach: whenever user enters a command from an application which has not been loaded, AutoCAD fires the UnknownCommand event. This allows our code in the event handler to use the unknown command name against a set of on demand loading configuration data to determine which applications are needed to be loaded. Here, we do not need to check if the application has already been loaded (otherwise, the UnknownCommand would not fire).

Here is a DLL project that is compiled as MyToolA.dll, in which 2 commands are defined:
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
 
[assemblyCommandClass(typeof(MyToolA.Commands))]
[assemblyExtensionApplication(typeof(MyToolA.Commands))]
 
namespace MyToolA
{
    public class Commands : IExtensionApplication
    {
        public void Initialize()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;
            ed.WriteMessage(
                "\nLoading MyToolA.dll...done.\n");
        }
 
        public void Terminate()
        {
            
        }
 
        [CommandMethod("Command_A1")]
        public static void RunCommandA1()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;
 
            ed.GetString(
                "\nThis \"Command_A1\" in DLL \"MyToolA.dll\"......" +
                "Press anykey to continue...");
            ed.WriteMessage("\n");
        }
 
        [CommandMethod("Command_A2")]
        public static void RunCommandA2()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;
 
            ed.GetString(
                "\nThis \"Command_A2\" in DLL \"MyToolA.dll\"......" +
                "Press anykey to continue...");
            ed.WriteMessage("\n");
        }
    }
}

Then here is another DLL project. compiled as MyToolB.ll, in which another 2 commands are defined:
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
 
[assemblyCommandClass(typeof(MyToolB.Commands))]
[assemblyExtensionApplication(typeof(MyToolB.Commands))]
 
namespace MyToolB
{
    public class Commands : IExtensionApplication
    {
        public void Initialize()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;
            ed.WriteMessage(
                "\nLoading MyToolB.dll is...done.\n");
        }
 
        public void Terminate()
        {
 
        }
 
        [CommandMethod("Command_B1")]
        public static void RunCommandA1()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;
 
            ed.GetString(
                "\nThis \"Command_B1\" in DLL \"MyToolB.dll\"......" +
                "Press anykey to continue...");
            ed.WriteMessage("\n");
        }
 
        [CommandMethod("Command_B2")]
        public static void RunCommandA2()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;
 
            ed.GetString(
                "\nThis \"Command_B2\" in DLL \"MyToolB.dll\"......" +
                "Press anykey to continue...");
            ed.WriteMessage("\n");
        }
    }
}

Now, I want these 2 DLLs to be loaded on demand, that is, when one of the commands defined in these 2 DLLs is called in AutoCAD, AutoCAD will load the DLL. So, I first came up with a simple set of auto-loading configuration, which is a Dictionary. That is, each Dictionary entry represents a DLL file name (entry's key) and an array of command names defined in the DLL (entry's value):
using System.Collections.Generic;
 
namespace HandlingUnknownCommand
{
    public class AutoLoaderSettings : Dictionary<stringstring[]>
    {
        public string GetDllByCommandName(string cmdName)
        {
            foreach (var item in this)
            {
                var cmds = item.Value;
                foreach(var cmd in cmds)
                {
                    if (cmd.ToUpper() == cmdName.ToUpper()) return item.Key;
                }
            }
 
            return null;
        }
 
        // A static factory method to generate a set of test AutoLoaderSetting
        public static AutoLoaderSettings CreateTestSettings()
        {
            var settings = new AutoLoaderSettings();
 
            settings.Add(
                "MyToolA.dll",
                new string[] { "Command_A1""Command_A2" });
 
            settings.Add(
                "MyToolB.dll",
                new string[] { "Command_B1""Command_B2" });
 
            return settings;
        }
    }
}

Here comes the auto-loader:
using System.Reflection;
 
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
 
[assemblyCommandClass(typeof(HandlingUnknownCommand.MyAutoLoader))]
[assemblyExtensionApplication(typeof(HandlingUnknownCommand.MyAutoLoader))]
 
namespace HandlingUnknownCommand
{
    public class MyAutoLoader : IExtensionApplication 
    {
        private static bool _autoLoad = false;
        private static AutoLoaderSettings _settings = 
            AutoLoaderSettings.CreateTestSettings();
 
        public void Initialize()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;
 
            try
            {
                ed.WriteMessage("\nInitializing MyAutoLoader...");
                CadApp.DocumentManager.DocumentCreated += 
                    DocumentManager_DocumentCreated;
                foreach (Document doc in CadApp.DocumentManager)
                {
                    doc.UnknownCommand += Document_UnknownCommand;
                }
                _autoLoad = true;
                ed.WriteMessage("\nMyAutoLoader initialized successfully.");
            }
            catch(System.Exception ex)
            {
                _autoLoad = false;
                ed.WriteMessage(
                    "\nInitialzing MyAutoLoader failed:\n{0}\n", ex.Message);
            }
        }
 
        public void Terminate()
        {
 
        }
 
        private static void DocumentManager_DocumentCreated(
            object sender, DocumentCollectionEventArgs e)
        {
            e.Document.UnknownCommand += Document_UnknownCommand;
        }
 
        private static void Document_UnknownCommand(
            object sender, UnknownCommandEventArgs e)
        {
            if (!_autoLoad) return;
 
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;
 
            var cmdName = e.GlobalCommandName;
            var dllFileName = _settings.GetDllByCommandName(cmdName);
            if (!string.IsNullOrEmpty(dllFileName))
            {
                try
                {
                    LoadAssemblyDll(dllFileName);
                    ed.WriteMessage(
                        "\nDll {0} loaded, command {1} resuming...",
                        dllFileName.ToUpper(), cmdName.ToUpper());
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage(
                        "\nAutoloading DLL failed: {0}\n", ex.Message);
                }    
            }
        } 
        
        private static void LoadAssemblyDll(string dllFileName)
        {
            string path = System.IO.Path.GetDirectoryName(
                Assembly.GetExecutingAssembly().Location);
            string dll = path + "\\" + dllFileName;
 
            if (!ExtensionLoader.IsLoaded(dll))
            {
                if (System.IO.File.Exists(dll))
                {
                    ExtensionLoader.Load(dll);
                }
                else
                {
                    throw new System.IO.FileNotFoundException(
                        "Cannot find DLL file: " + dllFileName + "!");
                }
            }
        } 
    }
}


Now, see this video clip to see how the code runs.

As we can see, the 2 Dlls that actually define the commands are not preloaded, yet we can enter the commands and let the custom AutoLoader load the Dlls. Another interesting thing is that once the custom AutoLoader loads the Dlls according to configuration, AutoCAD automatically resumes the command (previously unknown whent eh command is issued, but becoming known after the Dll is loaded) execution. An extr message at command line is generated by AutoCAD. See the highlight in this picture:



AutoCAD's this behaviour indicates that if there is any UnknowCommand event handler other than .NET API's built-in one, AutoCAD will try the command again after the custom event handler execution. This allows us to make a command purely be loaded on demand and provide a good user experience: a not loaded command is loaded and executed without interruption, except for the extra line of message at command line. Well, there is a little catch: AutoCAD's auto-command-completion might be a bit annoying, if you have a custom command that similar to an existing AutoCAD command and you have to enter the custom command at command line.

Anyway. simply handling UnknownCommand event would allow us to build a custom Dll/command auto-loading process quite easily.

I'll be talking another interesting issue on handling UnknownCommand event in next post.

Thursday, January 12, 2017

About Layer Description (LayerTableRecord.Description Property)

I recently worked on a project, where I need to get access to layer's description and based on the formatted description value to do something.

In AutoCAD's Layer Manager's view, the last column displays layer description. Of course the description is stored as LayerTableRecord.Description property. So, pretty simple, eh? Wrong. it is not as simple as it looks.

Whenever a new layer is created, a default description, which is the same as the layer name, is set, the Layer Manager shows. Also, if one tries to clear the description, the Layer Manager simply restores it back with the layer's name.

However, when trying to use code to access LayerTableRecord.Description property, it returns empty (String.Empty).

That means, the default layer description appearing in Layer Manager looks just displayed by Layer Manager if the description is not set explicitly. If we edit the layer description in the Layer Manager, then, yes, the description will be assigned to the layer (LayerTableRecord) with one exception: you cannot edit the description as the same as the layer name (i.e. you clear the appeared "fake" layer description, and then enter the layer name as the description), the description in LayerTableRecord remains empty.

However, you can use code to set LayerTableRecord.Description with the same text value as layer name (i.e. LayerTableRecord.Description property can be set via code to whatever value you want); or, in Layer Manager, you can first set the description to nay value other than empty text or layer name, then again you can set it back to the layer name. This time, the displayed description (the same as layer name) is really assigned to the LayerTableRecord.Descriotion property, not a "fake" value made up by Layer Manager.

So, I'd consider this is a bug of Layer Manager: if AutoCAD wants to give a layer a default description when a new layer is created, it should do so, not left it empty; and if the Description property is empty, the Layer Manager should leave it alone, not display a fake, misleading value.

However, by Layer Manager, I mean the "newer" paletteset style, floating window version (since AutoCAD 2009, I think). If open the old "Layer Properties Manager" modal dialog box (with "LayerDlgMode" system variable set to 0), the description in the dialog box is displayed correctly (emtpy) when a new layer is created. This convinces me that the issue I described here is a bug of Layer Manager (since AutoCAD 2009!).

Friday, January 6, 2017

Custom On Demand Loading

Most people, including myself, who start learning AutoCAD .NET API, would be wondering, after getting passed the first "Hello World" command tutorial, how to make their custom commands contained in the .NET DLL files available/loaded in AutoCAD, rather than entering command "NETLOAD" to manually load the DLL file. Then when they study/learn more, they would know a few ways to load the .NET DLLs into AutoCAD automatically: using acad.lsp to load at AutoCAD start-up; registering the DLL/commands in Windows Registry, so that the DLL will be loaded automatically when a command defined in a DLL is called and the DLL has not been loaded; or using AutoCAD's built-in Autoloader mechnism, where an XML file defines how a DLL file is loaded.

The company I work for is a huge one with offices allover the world, and the division I belong to has many offices scattered every corner of the country. So, even the entire division provides the same or similar services to the same industries, meaning the business operation processes are the same or very similar, our CAD applications still have to be grouped according to business factors, such as regions, industries...This results in scores of .NET add-in DLLs being developed, and many more are coming.

Originally, we had simply loaded all available .NET DLLs at AutoCAD start-up. But later on, with more and more .NET applications being developed for different offices/regions/industries, it does not make much sense any more to load all DLLs at AutoCAD start-up because many of them only get used in one AutoCAD session when a drawing is from particular project. However the reality is that our CAD users are trained/expected to work all project country-wide and they want a corresponding set of CAD applications to be loaded automatically based on the project (i.e. when a drawing in a project is opened, that set of tools should be available).

Obviously, the existing .NET DLL auto-loading mechanism (especially the Windows Registry demand loading and AutoCAD's AutoLoader) is not enough to deal with this situation.

Acad.lsp/AcadDoc.lsp can be used for this, of course: we can use LISP code to determine the DLL loading conditions (drawing's project, thus region, industry,...), then load the corresponding .NET DLLs.

Here I tried a custom .NET DLL demand loading solution, which is very easy to configure when a .NET DLL needs to be loaded automatically according to some conditions. 

The Scenario

1. There are 2 sets of CAD tools contained in 2 DLL files: 

ToolSetA.dll: it needs to be loaded when a drawing from project A, located in region "AAA" is opened in AutoCAD. This DLL file defines 2 commands: "ADoWorkOne" and "ADoWorkTwo".

ToolSetB.dll: it needs to be loaded when a drawing from project B, located in region "BBB" is opened in AutoCAD. This DLL file defines 2 commands: "BDoWorkOne" and "BDoWorkTwo".

The commands in these 2 DLL files do nothing other than showing AutoCAD alert message box.

2. The 2 DLL files are not loaded on AutoCAD start-up, nor other AutoCAD built-in on demand loading mechanism is applied to.

3. If a drawing from project A or project B is opened in AutoCAD, ToolSetA.dll or ToolSetB.dll should be loaded automatically if the DLL file has not been loaded into this AutoCAD session.

The Solution

My solution includes 2 parts: a set of configurable settings, which defines the on demand loading condition (which DLL files to be loaded for particular project location), and an IExtensionApplication class that dynamically loads DLL file into AutoCAD according to the settings. Of course the IExtensionApplication class itself has to be loaded in AutoCAD, for example, using Acad.lsp.

So, I created a class DllModule, and DllModuleCollection to hold and load the setting data:
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
 
namespace DemandLoading
{
    public class DllModule
    {
        public string Region { setget; }
        public string[] DllFileNames { setget; }
        public string[] CommandNames { setget; }
        public string ModuleName { setget; }
    }
 
    public class DllModuleCollection : List&lt;DllModule>
    {
        public DllModule GetDemandLoadModule(string region)
        {
            foreach (var module in this)
            {
                if (module.Region.ToUpper() == region.ToUpper())
                {
                    return module;
                }
            }
 
            return null;
        }
 
        public IEnumerable&lt;DllModule> FindDemandLoadMuduleByCommand(
            string commandName)
        {
            var lst = new List&lt;DllModule>();
 
            foreach (var module in this)
            {
                bool match = false;
                foreach (var cmd in module.CommandNames)
                {
                    if (cmd.ToUpper() == commandName.ToUpper())
                    {
                        match = true;
                        break;
                    }
                }
 
                if (match) lst.Add(module);
            }
 
            return lst;
        }
 
        public static DllModuleCollection LoadModuleListFromAppSettings()
        {
            var modules = new DllModuleCollection();
 
            Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                    System.Reflection.Assembly.GetExecutingAssembly().Location);
            if (config.HasFile)
            {
                var moduleNames = 
                    config.AppSettings.Settings["Dll Modules"].Value.Split('|');
                foreach (var moduleName in moduleNames)
                {
                    var setting = config.AppSettings.Settings[moduleName.Trim()];
                    var data = setting.Value.ToUpper().Split('|');
 
                    try
                    {
                        string region = null;
                        string[] dlls = null;
                        string[] cmds = null;
 
                        foreach (var item in data)
                        {
                            string val = item.Trim();
                            if (val.StartsWith("REGION:"))
                            {
                                region = item.Substring(7);
                            }
                            else if (val.StartsWith("DLLNAME:"))
                            {
                                dlls = val.Substring(8).Split(',');
                            }
                            else if (val.StartsWith("COMMANDS:"))
                            {
                                cmds = val.Substring(9).Split(',');
                            }
                        }
 
                        if (region!=null && dlls!=null && cmds!=null)
                        {
                            var module = new DllModule()
                            {
                                ModuleName = moduleName,
                                Region = region.Trim(),
                                DllFileNames = (from d in dlls select d.Trim()).ToArray(),
                                CommandNames = (from c in cmds select c.Trim()).ToArray()
                            };
 
                            modules.Add(module);
                        }
                    }
                    catch { }
                }
            }
 
            return modules;
        }
    }
}

I use app.config added into the DLL project to store the settings:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Dll Modules" value="Tool Set A | Tool Set B"/>
    <add key="Tool Set A" value="Region: AAA | DllName: ToolSetA.dll | Commands: ADoWorkOne, ADoWorkTwo"/>
    <add key="Tool Set B" value="Region: BBB | DllName: ToolSetB.dll | Commands: BDoWorkOne, BDoWorkTwo"/>
  </appSettings>
</configuration>

The app.config file is compiled to xxxxx.dll.config and goes with the DLL file.

Now this is the IExtensionApplication class OnDemandLoader, which does the work of loading corresponding DLL files on demand:
using System;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
 
[assemblyCommandClass(typeof(DemandLoading.OnDemandLoader))]
[assemblyExtensionApplication(typeof(DemandLoading.OnDemandLoader))]
 
namespace DemandLoading
{
    public class OnDemandLoader : IExtensionApplication
    {
        private static DllModuleCollection _dllModules = null;
 
        private bool _newDocAdded = false;
 
        public void Initialize()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;
 
            try
            {
                ed.WriteMessage(
                    "\nInitializing \"ON DEMAND LOADER\"...");
 
                _dllModules = DllModuleCollection.LoadModuleListFromAppSettings();
                if (_dllModules.Count==0)
                {
                    ed.WriteMessage(
                        "\nNo Dll module is configured for demand loading.\n");
                }
                else
                {
                    CadApp.DocumentManager.DocumentCreated += 
                        DocumentManager_DocumentCreated;
                    CadApp.DocumentManager.DocumentBecameCurrent += 
                        DocumentManager_DocumentBecameCurrent;
 
                    foreach (Document doc in CadApp.DocumentManager)
                    {
                        doc.UnknownCommand += Document_UnknownCommand;
                    }
 
                    EnsureRegionModuleLoaded();

                    ed.WriteMessage(
                        "\nON DEMAND LOADER loaded successfully.\n");
                }             }             catch(System.Exception ex)             {                 ed.WriteMessage(                     "\nInitializing \"DEMAND LOADER\" failed:\n{0}\n", ex.Message);              }         }         public void Terminate()         {         }         #region private methods         private void Document_UnknownCommand(             object sender, UnknownCommandEventArgs e)         {             string cmd = e.GlobalCommandName;             var modules = _dllModules.FindDemandLoadMuduleByCommand(cmd);             if (modules.Count() > 0)             {                 //Prompt user to manually load the module that contains the command                 var msg = new StringBuilder();                 msg.Append("The *.dll module containing command \"" + cmd + "\"\n" +                     "is not automatically loaded according to the project." +                     "\n\nFollowing DLL file(s) must be loaded:\n\n");                 foreach (var m in modules)                 {                     foreach (var dll in m.DllFileNames)                     {                         msg.Append(string.Format("\"{0}\"", dll));                     }                 }                 msg.Append(                     "\n\nYou can manually load the DLL file(s) with command \"NETLOAD\".");                 CadApp.ShowAlertDialog(msg.ToString());             }         }         private void DocumentManager_DocumentBecameCurrent(             object sender, DocumentCollectionEventArgs e)         {             if (_newDocAdded)             {                 EnsureRegionModuleLoaded();                 _newDocAdded = false;             }         }         private void DocumentManager_DocumentCreated(             object sender, DocumentCollectionEventArgs e)         {             _newDocAdded = true;             e.Document.UnknownCommand += Document_UnknownCommand;         }         private void EnsureRegionModuleLoaded()         {             //Get project region from drawing (USERI1)             string region;             var regionCode = Convert.ToInt32(CadApp.GetSystemVariable("USERI1"));             switch (regionCode)             {                 case 10:                     region = "AAA";                     break;                 case 20:                     region = "BBB";                     break;                 default:                     region = "";                     break;             }             //Get demand laoding DllModule info             var module = _dllModules.GetDemandLoadModule(region);             if (module != null)             {                 EnsureModuleLoaded(module);             }         }         private void EnsureModuleLoaded(DllModule module)         {             string path = System.IO.Path.GetDirectoryName(                 System.Reflection.Assembly.GetExecutingAssembly().Location);             foreach (var dllFile in module.DllFileNames)             {                 string file = path + "\\" + dllFile;                 if (!ExtensionLoader.IsLoaded(file))                 {                     ExtensionLoader.Load(file);                 }             }         }         #endregion     } }

In the OnDemandLoader class, the key logic is to identify the DLL file loading condition when a drawing is created in AutoCAD. In this case the condition is the project's region of current drawing. Normally, the project a drawing file belongs to can be decided by the file name, file storage folder, and so on. In this article, I use system variable USERI1 stored in drawing to stimulate the project's region, just to make the code run-able.

The Result

With the code completed, I tested the in following steps:

1. Start AutoCAD, and manually load the OnDemandLoader into AutoCAD (DemandLoading.dll). In real use, it would be automatically loaded on AutoCAD start-up, of course.

2. Since the currently opened drawing in AutoCAD does not belong to Project A (in region "AAA"), nor Project B (in region "BBB"), thus the 2 DLL files (ToolSetA.dll and ToolSetB.dll) are not loaded.

3. Enter one of the commands defined in the 2 not loaded DLL files, user is prompted for it.

4. Open 2 drawings prepared for Project A and Project B (in the 2 drawings, system variable "USERI1" has been set to indicate the project's region). Upon the 2 drawings' opening, message shown at command line indicates that the corresponding DLL file (ToolSetA.dll or ToolSetB.dll) is loaded, because each of these DLL files implemented IExtensionApplication.Intialize() method, where loading message is printed at command line.

5. With the project drawing open, enter commands defined in the on demand loading DLL files. It proves that the commands work as expected, thus on demand loading is done successfully.

6. Close one of the project drawing and re-open it. This time, no DLL loading message shows at command line, because the corresponding DLL file has been loaded previously.

See this video clip showing the test steps.

As the code shows, the key logic in this solution is simply carefully designing the settings to define DLL loading conditions and make sure the code can identify if the conditions are meet at run-time under different AutoCAD states (in my case, it is when document is created and becomes current the first time). The on demand loading process then does its work completely according to the settings, which can be updated whenever it is necessary without breaking the on demand loading process.

In the OnDemandLoader class I also handles Document.UnknownCommand event so that when a CAD application/custom command is tried in a drawing of not intended project, user is warned/prompted. Obviously, we could develop our own on demand loading process in this event handler: whenever a unknown command is entered, let the code to somewhere to look up where the command is defined and what the containing DLL file is; then load the DLL and reinvoke the command. I guess this is the logic AutoCAD's built-in on demand loading (using either Windows Registry or Autoloader) uses.

Followers

About Me

My photo
After graduating from university, I worked as civil engineer for more than 10 years. It was AutoCAD use that led me to the path of computer programming. Although I now do more generic business software development, such as enterprise system, timesheet, billing, web services..., AutoCAD related programming is always interesting me and I still get AutoCAD programming tasks assigned to me from time to time. So, AutoCAD goes, I go.