TheSwamp

Code Red => .NET => Topic started by: Nomad on May 09, 2022, 09:11:04 AM

Title: Need help with prepending a string to all the layer names in a list
Post by: Nomad on May 09, 2022, 09:11:04 AM
I wrote a C# plugin that cycles through the layer list and inserts a string in front of the name for every layer.  However when I run the dll I get an unhandled exception error message and I do not know how to proceed. Any help would be greatly appreciated.
 
using (Transaction trans = acCurDb.TransactionManager.StartTransaction())
            {
                LayerTable lyTab = trans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
                lyTab.UpgradeOpen();
                {
                    foreach (ObjectId lyID in lyTab)
                    {
                       
                        LayerTableRecord ltr = trans.GetObject(lyID, OpenMode.ForRead) as LayerTableRecord;
                        if (ltr.Name != "0" || ltr.Name != "Defpoints")
                        {
                            ltr.UpgradeOpen();
                            ltr.Name.Insert(0, "Engineering|");
                            trans.Commit();
                           
                        }
                        else
                        {
                            ed.WriteMessage("Skipping Layer");
                        }
                       }
                } 
Title: Re: Need help with prepending a string to all the layer names in a list
Post by: jtoverka on May 09, 2022, 09:24:02 AM
You are starting a transaction once, and then committing a transaction for each layer.
You want to start a transaction once, commit that transaction at the end (once).
Title: Re: Need help with prepending a string to all the layer names in a list
Post by: Nomad on May 09, 2022, 10:04:43 AM
I moved the transaction commit outside of the foreach statement and the command executes without error, however, it doesn't actually update the layer list.  Is there a flaw in my logic?
Title: Re: Need help with prepending a string to all the layer names in a list
Post by: jtoverka on May 09, 2022, 10:08:46 AM
Below is what you have:
Code: [Select]
ltr.Name.Insert(0, "Engineering|");

This doesn't actually update the property, it does insert the string, but it returns as a new string

Code: [Select]
ltr.Name = ltr.Name.Insert(0, "Engineering|");

Is what you are going for.
Title: Re: Need help with prepending a string to all the layer names in a list
Post by: kdub_nz on May 09, 2022, 12:07:09 PM
I assume you want to rename the layers ?

Perhaps something like

Code - C#: [Select]
  1. [CommandMethod("RenameMyLayers")]
  2.         public static void RenameMyLayers()
  3.         {
  4.             const string layerNamePrefix = "FiddleSticks|";
  5.  
  6.             Active.WriteMessage("\nRenaming Layers.\n ");
  7.  
  8.             using (Transaction transaction = Active.Database.TransactionManager.StartTransaction())
  9.             {
  10.                 // Returns the layer table for the current database
  11.                 LayerTable layerTable = (LayerTable)transaction.GetObject(
  12.                     Active.Database.LayerTableId, OpenMode.ForWrite);
  13.  
  14.  
  15.                 foreach (ObjectId layerId in layerTable)
  16.                 {
  17.                     LayerTableRecord layerTableRecord = (LayerTableRecord)transaction.GetObject(
  18.                         layerId, OpenMode.ForRead);
  19.  
  20.                     if (layerTableRecord.Name == "0" || layerTableRecord.Name == "Defpoints")
  21.                     {
  22.                         Active.WriteMessage($"layer {layerTableRecord.Name} skipped.\n ");
  23.                     }
  24.                     else
  25.                     {
  26.                         layerTableRecord.UpgradeOpen();
  27.                         layerTableRecord.Name = $"{layerNamePrefix}{layerTableRecord.Name}";
  28.                         Active.WriteMessage($"layer {layerTableRecord.Name} renamed.\n ");
  29.                     }
  30.                 }
  31.                 transaction.Commit();
  32.             }
  33.        }
  34.  

Title: Re: Need help with prepending a string to all the layer names in a list
Post by: kdub_nz on May 09, 2022, 12:32:16 PM
If you wanted just a list. that will be another thing !

just declare a
Code - C#: [Select]
  1. List<string> layerNames = new List<string>();  

outside the loop, and then use this in the loop
Code - C#: [Select]
  1. layerNames.Add(layerTableRecord.Name);


Perhaps something like

Code - C#: [Select]
  1.         [CommandMethod("ListMyLayers")]
  2.         public static void ListMyLayers()
  3.         {
  4.             const string layerNamePrefix = "FiddleSticks|";
  5.  
  6.             Active.WriteMessage("\nGenerating Layers List.\n ");
  7.  
  8.             List<string> layerNames = new List<string>();
  9.  
  10.             using (Transaction transaction = Active.Database.TransactionManager.StartTransaction())
  11.             {                
  12.                 LayerTable layerTable = (LayerTable)transaction.GetObject(
  13.                     Active.Database.LayerTableId, OpenMode.ForWrite);
  14.  
  15.                 foreach (ObjectId layerId in layerTable)
  16.                 {
  17.                     LayerTableRecord layerTableRecord = (LayerTableRecord)transaction.GetObject(
  18.                         layerId, OpenMode.ForRead);
  19.  
  20.                     if (layerTableRecord.Name == "0" || layerTableRecord.Name == "Defpoints")
  21.                     {
  22.                         Active.WriteMessage($"layer {layerTableRecord.Name} skipped.\n");
  23.                     }
  24.                     else
  25.                     {
  26.                         layerNames.Add($"{layerNamePrefix}{layerTableRecord.Name}");
  27.                     }
  28.                 }
  29.             }
  30.  
  31.             foreach (string layerName in layerNames)
  32.             {
  33.                 Active.WriteMessage($"{layerName}.\n");
  34.             }
  35.         }
  36.  

Title: Re: Need help with prepending a string to all the layer names in a list
Post by: kdub_nz on May 09, 2022, 01:00:42 PM
Below is what you have:
Code: [Select]
ltr.Name.Insert(0, "Engineering|");

This doesn't actually update the property, it does insert the string, but it returns as a new string

Code: [Select]
ltr.Name = ltr.Name.Insert(0, "Engineering|");


Is what you are going for.

Nomad,
My bad. I just realised that  jtoverka had you covered with this post.
Title: Re: Need help with prepending a string to all the layer names in a list
Post by: Nomad on May 09, 2022, 01:48:50 PM
Thank you guys so much for the help! The code ran successfully and updated the layer list.