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.

Saturday, December 31, 2016

Splitting Polyline/Curve With AutoCAD Civil 3D Labels Associated - Updated

The code shown in my previous post, as I mentioned there, only re-creates single Civil3D segment label when a polyline/curve is split. However, each segment of a split-able curve/polyline could be annotated by multiple labels (in different label styles, of course). Also, the code shown in that post does not re-store the labels in their original locations. I was able to find a few hours sitting down to update the code to cover these issues.

The solution is to search drawing to find all labels that associate to the polyline/curve to be split, and save these labels' information for later need (when re-creating labels). So, I defined this class SegmentLabelInfo:

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
 
namespace Civil3DLabels
{
    public enum SegmentLabelType
    {
        LineLabel=0,
        CurveLabel=1,
    }
 
    public class SegmentLabelInfo
    {
        public ObjectId LabelStyleId { setget; }
        public string LabelLayer { setget; }
        public Point3d LabelPosition { setget; }
        public SegmentLabelType LabelType { setget; }
 
        public SegmentLabelInfo()
        {
            LabelStyleId = ObjectId.Null;
            LabelLayer = "";
            LabelPosition = Point3d.Origin;
            LabelType = SegmentLabelType.LineLabel;
        }
 
        public bool IsOnCurve(ObjectId curveId, out double ratio)
        {
            ratio = 0.5;
            bool onCurve = false;
 
            Point3d pt;
            using (var tran = 
                curveId.Database.TransactionManager.StartTransaction())
            {
                var curve = (Curve)tran.GetObject(
                    curveId, OpenMode.ForRead);
                pt = curve.GetClosestPointTo(LabelPosition, false);
 
                //If the label anchored on this curve
                var dist = LabelPosition.DistanceTo(pt);
                onCurve = dist <= Tolerance.Global.EqualPoint;
 
                //the distance from curve's start point to anchor point
                dist = curve.GetDistAtPoint(pt);
                ratio = dist / curve.GetDistAtPoint(curve.EndPoint);
 
                tran.Commit();
            }
 
            return onCurve;
        }
    }
}

As the code shows, besides the 4 public properties used to store a label's information, a method IsOnCurve() is also defined to determine if the label sits on one of the segment split from the original polyline/curve; if yes, what location (distance to the start point) the label is at (output parameter ratio).

Now, here is the updated working code, where I changed the class name from PolylineSplitter to MyPolylineSplitter:

using System.Collections.Generic;
using System.Linq;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using CadDb = Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using CivilApp = Autodesk.Civil.ApplicationServices.CivilApplication;
using Civil = Autodesk.Civil;
using CivilDb = Autodesk.Civil.DatabaseServices;
using Autodesk.Civil.DatabaseServices.Styles;
 
[assemblyCommandClass(typeof(Civil3DLabels.MyPolylineSplitter))]
 
namespace Civil3DLabels
{
    public class MyPolylineSplitter
    {
        private const string DXF_LABEL = "AECC_GENERAL_SEGMENT_LABEL";
 
        [CommandMethod("ExplodePoly")]
        public void DoSplit()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;
            try
            {
                Split(dwg);
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: {0}", ex.Message);
                ed.WriteMessage("\n*Cancel*");
            }
 
            Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
        }
        public void Split(Document dwg)
        {
            CadDb.ObjectId polyId = SelectPolyline(dwg.Editor);
            if (polyId.IsNull) return;
 
            //Collect all labels' information that associate with the polyline
            IEnumerable<SegmentLabelInfo> labels = FindAssociatedLabels(polyId);
            
            //Get default line/curve label style IDs
            var defaultLineLabelStyleId = CadDb.ObjectId.Null;
            var defaultCurveLabelStyleId = CadDb.ObjectId.Null;
            if (labels.Count() > 0)
            {
                GetSettingsCmdAddSegmentLabelDefaultStyles(
                    out defaultLineLabelStyleId, out defaultCurveLabelStyleId);
            }
 
            using (var tran = dwg.TransactionManager.StartTransaction())
            {
                var poly = (CadDb.Polyline)tran.GetObject(
                    polyId, CadDb.OpenMode.ForRead);
 
                //Find current space
                var curSpace = (CadDb.BlockTableRecord)tran.GetObject(
                    poly.OwnerId, CadDb.OpenMode.ForRead);
 
                //Newly created entities because of the splitting
                var newIds = new List<CadDb.ObjectId>();
 
                //Get split segments and add them into current space
                using (var ents = GetSplitCurvesFromPolyline(poly))
                {
                    if (ents.Count > 1)
                    {   
                        curSpace.UpgradeOpen();
                        foreach (CadDb.DBObject ent in ents)
                        {
                            var id = curSpace.AppendEntity(ent as CadDb.Entity);
                            tran.AddNewlyCreatedDBObject(ent, true);
 
                            newIds.Add(id);
                        }
 
                        // Erase the polyline,
                        // Associated labels would also be gone
                        poly.UpgradeOpen();
                        poly.Erase(true);
                    }
                    else
                    {
                        ents[0].Dispose();
                    }
                }
 
                //Add label to each newly added entities
                if (newIds.Count>0 && labels.Count()>0)
                {      
                    foreach (var entId in newIds)
                    {
                        AddSegmentLabel(
                            entId, 
                            labels, 
                            defaultLineLabelStyleId, 
                            defaultCurveLabelStyleId,
                            tran);
                    }
                }
 
                tran.Commit();
            }         
        }
 
        #region private methods
 
        private CadDb.ObjectId SelectPolyline(Editor ed)
        {
            var opt = new PromptEntityOptions(
                "\nSelect a polyline:");
            opt.SetRejectMessage("\nInvalid: not a polyline.");
            opt.AddAllowedClass(typeof(CadDb.Polyline), true);
 
            var res = ed.GetEntity(opt);
            if (res.Status==PromptStatus.OK)
            {
                return res.ObjectId;
            }
            else
            {
                return CadDb.ObjectId.Null;
            }
        }
 
        private CadDb.DBObjectCollection GetSplitCurvesFromPolyline(
            CadDb.Polyline poly)
        {
            var points = new Point3dCollection();
            for (int i = 0; i < poly.NumberOfVertices; i++)
            {
                points.Add(poly.GetPoint3dAt(i));
            }
 
            var dbObjects = poly.GetSplitCurves(points);
 
            return dbObjects;
        }
 
        private IEnumerable<SegmentLabelInfo> FindAssociatedLabels(
            CadDb.ObjectId polyId)
        {
            var labels = new List<SegmentLabelInfo>();
 
            CadDb.Database db = polyId.Database;
 
            using (var tran = db.TransactionManager.StartTransaction())
            {
                var ent = (CadDb.Entity)tran.GetObject(
                    polyId, CadDb.OpenMode.ForRead);
                var space = (CadDb.BlockTableRecord)tran.GetObject(
                    ent.OwnerId, CadDb.OpenMode.ForRead);
 
                foreach (CadDb.ObjectId id in space)
                {
                    if (id.ObjectClass.DxfName.ToUpper() == DXF_LABEL)
                    {
                        var label = tran.GetObject(
                            id, CadDb.OpenMode.ForRead) as CivilDb.Label;
                        if (label != null && label.FeatureId==polyId)
                        {
                            var info = new SegmentLabelInfo()
                            {
                                LabelStyleId = label.StyleId,
                                LabelLayer = label.Layer,
                                LabelPosition = label.AnchorInfo.Location
                            };
 
                            // Since each label uses either line label style or curve label style
                            // We test if the label style is line label style.
                            // if not, the label style must be curve label style
                            var lineStyles =
                                CivilApp.ActiveDocument.Styles.LabelStyles.GeneralLineLabelStyles;
                            info.LabelType = IsSegmentLabelStyle(label.StyleId, lineStyles, tran) ?
                                SegmentLabelType.LineLabel : SegmentLabelType.CurveLabel;
 
                            labels.Add(info);
                        }
                    }
                }
 
                tran.Commit();
            }
 
            return labels;
        }
 
        private void AddSegmentLabel(
            CadDb.ObjectId entId, 
            IEnumerable<SegmentLabelInfo> labels,
            CadDb.ObjectId defaultLineLabelStyleId, 
            CadDb.ObjectId defaultCurveLabelStyleId, 
            CadDb.Transaction tran )
        {
            foreach (var labelInfo in labels)
            {
                double ratio;
                if (labelInfo.IsOnCurve(entId, out ratio))
                {
                    var lineLabel = labelInfo.LabelType == SegmentLabelType.LineLabel ?
                        labelInfo.LabelStyleId : defaultLineLabelStyleId;
                    var curveLabel = labelInfo.LabelType == SegmentLabelType.CurveLabel ?
                        labelInfo.LabelStyleId : defaultCurveLabelStyleId;
 
                    //Add label to the segment
                    var id = CivilDb.GeneralSegmentLabel.Create(
                        entId, ratio, lineLabel, curveLabel);
                    var label = (CivilDb.Label)tran.GetObject(
                        id, CadDb.OpenMode.ForRead);
                    if (label.Layer.ToUpper()!=labelInfo.LabelLayer.ToUpper())
                    {
                        label.UpgradeOpen();
                        label.Layer = labelInfo.LabelLayer;
                    }
                }
            }
        }
 
        private bool IsSegmentLabelStyle(
            CadDb.ObjectId styleId, 
            IEnumerable<CadDb.ObjectId> styleCollection, 
            CadDb.Transaction tran)
        {
            bool found = false;
 
            foreach (CadDb.ObjectId id in styleCollection)
            {
                if (styleId == id)
                {
                    found = true;
                }
                else
                {
                    var style = (LabelStyle)tran.GetObject(
                        id, CadDb.OpenMode.ForRead);
                    if (style.ChildrenCount > 0)
                    {
                        var styleIds = new List<CadDb.ObjectId>();
                        for (int i = 0; i < style.ChildrenCount; i++)
                        {
                            styleIds.Add(style[i]);
                        }
 
                        //Recursive call
                        found =
                            IsSegmentLabelStyle(styleId, styleIds, tran);
                    }
                }
 
                if (found) break;
            }
 
            return found;
        }
 
        private void GetSettingsCmdAddSegmentLabelDefaultStyles(
            out CadDb.ObjectId lineLabelStyleId,
            out CadDb.ObjectId curveLabelStyleId)
        {
            var settings = CivilApp.ActiveDocument.Settings;
            var cmdSettings = 
                settings.GetSettings<Civil.Settings.SettingsCmdAddSegmentLabel>();
 
            lineLabelStyleId = cmdSettings.Styles.LineLabelStyleId.Value;
            curveLabelStyleId = cmdSettings.Styles.CurveLabelStyleId.Value;
        }
        
        #endregion
    }
}

The polyline to be split in this video clip is annotated with 2 labels for each of its segment; some of the labels are on different layers (in different colors); Also, the labels' anchor point (location) on the segment have been dragged randomly along the segment. After execute command "ExplodePoly", all labels look like unchanged, even though they are all re-created.

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.