I'm trying to create multiple layers in C#. Each layer should have a unique description, but currently, the description is always the same as the layer name. Could you help me figure out ?

I'm trying to create multiple layers in C#. Each layer should have a unique description, but currently, the description is always the same as the layer name. Could you help me figure out ?

Aravinth.Pichamani
Enthusiast Enthusiast
260 Views
2 Replies
Message 1 of 3

I'm trying to create multiple layers in C#. Each layer should have a unique description, but currently, the description is always the same as the layer name. Could you help me figure out ?

Aravinth.Pichamani
Enthusiast
Enthusiast
 [CommandMethod("Multilayer")]
 public void CreateCustomLayersFixed()
 {
     Document doc = Application.DocumentManager.MdiActiveDocument;
     Editor editor = doc.Editor;
     Database database = doc.Database;
     ObjectId layerId = database.LayerTableId;

     // Sample list: (Layer Name, Description, Color Index)
     List<(string name, string description, short colorIndex)> layersList = new List<(string name, string description, short colorIndex)>
     {
                   ("C-NTWK-ELE3-CABL-AERL-E", "Existing Aerial Cable", 55),
                  ("C-NTWK-ELE3-CABL-AERL-P", "Proposed Aerial Cable", 4),
                 ("C-NTWK-ELE3-CABL-AERL-T", "Temporary Aerial Cable", 160),
     };

     using (Transaction tr = database.TransactionManager.StartTransaction())
     {
         //  Register "LayerDescriptions" for XData
         RegAppTable regAppTable = tr.GetObject(database.RegAppTableId, OpenMode.ForRead) as RegAppTable;
         if (!regAppTable.Has("LayerDescriptions"))
         {
             regAppTable.UpgradeOpen();
             RegAppTableRecord regAppRecord = new RegAppTableRecord
             {
                 Name = "LayerDescriptions"
             };
             regAppTable.Add(regAppRecord);
             tr.AddNewlyCreatedDBObject(regAppRecord, true);
         }

         LayerTable layerTable = tr.GetObject(layerId, OpenMode.ForRead) as LayerTable;

         foreach (var lay in layersList)
         {
             string name = lay.name;
             string description = lay.description;
             short colorIndex = lay.colorIndex;

             editor.WriteMessage($"\nProcessing: Name='{name}', Description='{description}'"); // DEBUG

             if (!layerTable.Has(name))
             {
                 layerTable.UpgradeOpen();

                 LayerTableRecord newLayer = new LayerTableRecord
                 {
                     Name = name,
                     Color = Color.FromColorIndex(ColorMethod.ByAci, colorIndex),
                     Description = description
                 };

                 // Attach XData description
                 ResultBuffer xdata = new ResultBuffer(
                     new TypedValue((int)DxfCode.ExtendedDataRegAppName, "LayerDescriptions"),
                     new TypedValue((int)DxfCode.ExtendedDataAsciiString, description)
                 );
                 newLayer.XData = xdata;

                 layerTable.Add(newLayer);
                 tr.AddNewlyCreatedDBObject(newLayer, true);

                 editor.WriteMessage($"\n Created layer: {name} (Color: {colorIndex}, Desc: {description})");
             }
             else
             {
                 LayerTableRecord existingLayer = (LayerTableRecord)tr.GetObject(layerTable[name], OpenMode.ForWrite);
                 existingLayer.Description = description;
                 editor.WriteMessage($"\nUpdated existing layer: {name} (Desc: {description})");

             }
         }
         tr.Commit();
     }
 }
0 Likes
Accepted solutions (1)
261 Views
2 Replies
Replies (2)
Message 2 of 3

Aravinth.Pichamani
Enthusiast
Enthusiast

AravinthPichamani_0-1750657628064.png

 

0 Likes
Message 3 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

Try setting the description after having added the new layer to the Database.

if (!layerTable.Has(name))
{
    layerTable.UpgradeOpen();

    LayerTableRecord newLayer = new LayerTableRecord
    {
        Name = name,
        Color = Color.FromColorIndex(ColorMethod.ByAci, colorIndex)
    };

    layerTable.Add(newLayer);
    tr.AddNewlyCreatedDBObject(newLayer, true);
 
    newLayer.Description = description;

    editor.WriteMessage($"\n Created layer: {name} (Color: {colorIndex}, Desc: {description})");
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes