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<DllModule>
    {
        public DllModule GetDemandLoadModule(string region)
        {
            foreach (var module in this)
            {
                if (module.Region.ToUpper() == region.ToUpper())
                {
                    return module;
                }
            }
 
            return null;
        }
 
        public IEnumerable<DllModule> FindDemandLoadMuduleByCommand(
            string commandName)
        {
            var lst = new List<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.

Tuesday, December 27, 2016

Splitting Polyline/Curve With AutoCAD Civil 3D Labels Associated

In the era of using plain AutoCAD, I had written routines in AutoLISP and VBA to split a polyline into individual segment (e.g. breaking polyline at each vertex). After AutoCAD .NET API came, it is even easier to do this splitting: simply calling Curve.GetSplitCurves() will gives you back all the individual segment of a polyline/curve, based in the input points on the polyline/curve; in the case of polyline, most likely these points are the polyline's vertices.

However, once we moved from plain AutoCAD to some of the AutoCAD vertical products, in my case, it was AutoCAD Map, then AutoCAD Civil, thing can get complicated. 

With AutoCAD Map, the polylines, to which we used to do splitting, may have Object Data (a kind of attribute data that can be attached to AutoCAD entity, a bit similar to XData) attached. Once the usual splitting is done the attached data is gone, because the splitting is actually creating the individual segment of the polyline as new entities and the original polyline is erased.

With AutoCAD Civil, the splitting target polyline, besides possible Object Data being attached, may also be annotated with AutoCAD Civil label. In the case of Civil label, if the entity (polyline) being labelled is erased, the label will also be erased automatically. So, when my office moved from AutoCAD Map to AutoCAD Civil a while a go, I was asked to modify our polyline splitting tool (which have already handled the attached Object Data issue as aforementioned) to retain AutoCAD Civil label, if the polyline has been labelled.

Labelling is huge part of AutoCAD Civil 3D in terms of its customization. I am still pretty new on this. It took quite some study for me to figure out a working solution, be it is optimized or not. Since there is not as much programming resources available on AutoCAD Map/Civil3D, as on plain AutoCAD, I thought sharing my solution with fellow programmers would be good thing.

Here are the requirements:

1. Split a polyline (assume it is LWPolyline for the simplicity) into individual segment at each vertices;
2. If the polyline has been annotated with labels (assume the label is general segment line/curve label), the labels in the same label style remain.

Here are the logics of the solution:

1. Determine if the polyline is annotated with labels. If yes, what the 2 label styles (for line and curve segment) are used;
2. Get all individual segments and add them into database; 
3. Erase the polyline (then the labels annotating the polyline are gone);
4. Recreate labels on each individual segment with label style determined in Step 1.

Translating the logics into code:
using System.Collections.Generic;
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.PolylineSplitter))]
 
namespace Civil3DLabels
{
    public class PolylineSplitter
    {
        private const string DXF_LABEL = "AECC_GENERAL_SEGMENT_LABEL";
 
        [CommandMethod("SplitPoly")]
        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;
 
            // If the polyline is annotated with general segmen
            // label, find the label styles (line label and curve label),
            //If not labels, both label styles are ObjectId.Null.
            CadDb.ObjectId lineLabelStyleId;
            CadDb.ObjectId curveLabelStyleId;
            GetCurveGeneralSegmentLabelStyles(
                polyId, out lineLabelStyleId, out curveLabelStyleId);
            bool hasLabel = !lineLabelStyleId.IsNull && !curveLabelStyleId.IsNull;
 
            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 (label would also be gone, if exist
                        poly.UpgradeOpen();
                        poly.Erase(true);
                    }
                    else
                    {
                        ents[0].Dispose();
                    }
                }
 
                //Add label to each newly added entities
                if (newIds.Count>0 && hasLabel)
                {      
                    AddGeneralSegmentLabels(
                        newIds, lineLabelStyleId, curveLabelStyleId);
                }
 
                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 void AddGeneralSegmentLabels(
            IEnumerable<CadDb.ObjectId> featureIds, 
            CadDb.ObjectId lineLabelStyleId, 
            CadDb.ObjectId curveLabelStyleId)
        {
            foreach (var featureId in featureIds)
            {
                var labelId = CivilDb.GeneralSegmentLabel.Create(
                    featureId, 0.5, lineLabelStyleId, curveLabelStyleId);
            }
        }
 
        private void GetCurveGeneralSegmentLabelStyles(
            CadDb.ObjectId curveId,
            out CadDb.ObjectId lineStyleId, 
            out CadDb.ObjectId curveStyleId)
        {
            bool hasLabel = false;
            lineStyleId = CadDb.ObjectId.Null;
            curveStyleId = CadDb.ObjectId.Null;
 
            var id1 = CadDb.ObjectId.Null;
            var id2 = CadDb.ObjectId.Null;
 
            using (var tran = curveId.Database.TransactionManager.StartTransaction())
            {
                var ent = (CadDb.Entity)tran.GetObject(curveId, 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)
                        {
                            if (label.FeatureId == curveId)
                            {
                                if (!hasLabel) hasLabel = true;
 
                                if (id1.IsNull)
                                {
                                    id1 = label.StyleId;
                                }
                                else if (label.StyleId != id1)
                                {
                                    id2 = label.StyleId;
                                }
                            }
                        }
                    }
 
                    if (!id1.IsNull && !id2.IsNull) break;
                }
 
                //Only go further when at least one style is found
                if (hasLabel)
                {
                    if (!id1.IsNull)
                    {
                        //if it is Line Segment Style or Curve Segment Style
                        var civilDoc = CivilApp.ActiveDocument;
                        var lineStyles = 
                            civilDoc.Styles.LabelStyles.GeneralLineLabelStyles;
                        var curveStyles = 
                            civilDoc.Styles.LabelStyles.GeneralCurveLabelStyles;
 
                        if (IsTheLineSegmentStyle(id1, lineStyles, tran))
                        {
                            lineStyleId = id1;
                            if (!id2.IsNull)
                            {
                                if (IsTheLineSegmentStyle(id2, curveStyles, tran))
                                {
                                    curveStyleId = id2;
                                }
                            }
                        }
                        else
                        {
                            if (IsTheLineSegmentStyle(id1, curveStyles, tran))
                            {
                                curveStyleId = id1;
                                if (!id2.IsNull)
                                {
                                    if (IsTheLineSegmentStyle(id2, lineStyles, tran))
                                    {
                                        lineStyleId = id2;
                                    }
                                }
                            }
                        }
                    }
 
                    // The annotated polyline may only have line label or curve label
                    // So, here is to get default label style (either line label style
                    // or curve label style
                    CadDb.ObjectId defaultLineStyleId;
                    CadDb.ObjectId defaultCurveStyleId;
                    GetSettingsCmdAddSegmentLabelDefaultStyles(
                        out defaultCurveStyleId, out defaultLineStyleId);
 
                    if (lineStyleId.IsNull) lineStyleId = defaultLineStyleId;
                    if (curveStyleId.IsNull) curveStyleId = defaultCurveStyleId;
                }
 
                tran.Commit();
            }
        }
 
        private bool IsTheLineSegmentStyle(
            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 = 
                            IsTheLineSegmentStyle(styleId, styleIds, tran);
                    }
                }
 
                if (found) break;
            }
 
            return found;
        }
 
        private void GetSettingsCmdAddSegmentLabelDefaultStyles(
            out CadDb.ObjectId curveLabelStyleId, 
            out CadDb.ObjectId lineLabelStyleId)
        {
            var settings = CivilApp.ActiveDocument.Settings;
            var cmdSettings = 
                settings.GetSettings<Civil.Settings.SettingsCmdAddSegmentLabel>();
 
            lineLabelStyleId = cmdSettings.Styles.LineLabelStyleId.Value;
            curveLabelStyleId = cmdSettings.Styles.CurveLabelStyleId.Value;
        }

        #endregion
    }
}

In the code, the critical portion of the code is the private method GetCurveGeneralSegmentLabelStyle(), which does these things:

1. Check if the curve to be split has general segment labels associated or not.
2. If there is general segment labels associated, what the 2 label styles (line label and curve label) are.
3. Since the curve's split-able segments could be only lines, or only curves, we may only find one label style (line label style or curve label style) that is associated to the split-able curve.
4. The reason that I need to find both line and curve label styles, even the split-able curve may only use one label style, is that I need to re-create general segment labels on each split segment with Autodesk.Civil.DatabaseServices.GeneralSegmentLabel.Create(ObjectId, double, ObjectId, ObjectId), instead of Autodesk.Civil.DatabaseServices.GeneralSegmentLabel.Create(ObjectId, double). That is, I use the Create() method with 2 label styles' ObjectId passed in, instead of the Create() method that uses current default general segment label styles, set in Autodesk.Civil.Settings.SettingsCmdAddSegmentLable.Styles, which may be different from the original label styles found with the split-able curve.
5. Each of the 2 possible label styles (line or curve label styles) could have one or more child label styles, and each of the child styles could also have child styles...and so on. So the recursive method in the code to identify label style to be used.

This video clip shows the result of running the code.

The code is just a simplified way to re-label the individual segments after splitting a curve, it may not  re-create the label exactly as the original ones. For example, an original label could have been dragged along the segment from default location (centre of the segment), while the code shown here always create the label at the segment centre (the double argument of the Create() method is passed with value 0.5). So, to make the re-created label locate at exact location as the original one, when searching associated labels to the split-able curve, I would need to save the property value of GeneralSegmentLabel class of each found label of each segment, and use the value later when re-creating the label. It would need quite some extra code. I choose to leave it outside of this post.

Adittion Note

I also realize that a segment of line/curve could be annotated with multiple labels (in different label styles, of course). So, the code I posted here only works with segment being labelled with one label style. Obviously, if I want to retain all the labels in the case of multiple labels used on segments, I would need to identify all the labels that is associated to each segment before the splitting target polyline is erased. I'll see if I can find time to work out that part code later.

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.