Author Topic: Need help with prepending a string to all the layer names in a list  (Read 158 times)

0 Members and 1 Guest are viewing this topic.

Nomad

  • Mosquito
  • Posts: 3
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");
                        }
                       }
                } 

jtoverka

  • Newt
  • Posts: 118
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).

Nomad

  • Mosquito
  • Posts: 3
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?

jtoverka

  • Newt
  • Posts: 118
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.

kdub

  • Mesozoic keyThumper
  • SuperMod
  • Swamp Rat
  • Posts: 1438
  • class keyThumper<T>:ILazy<T>
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.  

called Kerry in my other life

Sometimes the question is more important than the answer.

I don't really work crazy hours . . I just live at UTC + 12.00
#ridesober

kdub

  • Mesozoic keyThumper
  • SuperMod
  • Swamp Rat
  • Posts: 1438
  • class keyThumper<T>:ILazy<T>
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.  

« Last Edit: May 09, 2022, 12:52:50 PM by kdub »
called Kerry in my other life

Sometimes the question is more important than the answer.

I don't really work crazy hours . . I just live at UTC + 12.00
#ridesober

kdub

  • Mesozoic keyThumper
  • SuperMod
  • Swamp Rat
  • Posts: 1438
  • class keyThumper<T>:ILazy<T>
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.
called Kerry in my other life

Sometimes the question is more important than the answer.

I don't really work crazy hours . . I just live at UTC + 12.00
#ridesober

Nomad

  • Mosquito
  • Posts: 3
Thank you guys so much for the help! The code ran successfully and updated the layer list.