Results 1 to 3 of 3

Thread: Add Layers Descriptions

  1. #1
    Member
    Join Date
    2013-04
    Posts
    16
    Login to Give a bone
    0

    Default Add Layers Descriptions

    Hi All,

    I am trying to add a layer to a drawing using a function and it is not updating the Layers Description.
    It works fine adding layers but dosen't apply the description, any ideas ?

    Many Thanks,

    Code:
    publicvoid AddLayer(string LayerName, string Description)
    {
    Document doc = MgdAcApplication.DocumentManager.MdiActiveDocument;
    Database db = HostApplicationServices.WorkingDatabase;
    Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
    doc.LockDocument();
    Transaction tr = db.TransactionManager.StartTransaction();
    using (tr)
    {
    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForWrite) asLayerTable;
     
    try
    {
    //checks provided layer has a valid name
    SymbolUtilityServices.ValidateSymbolName(LayerName, false);
    //check to see if exists
    if (lt.Has(LayerName))
    {
    //ed.WriteMessage("\nLayer :" + LayerName + "Already Exists");
    }
    else
    {
    LayerTableRecord ltr = newLayerTableRecord()
    {
    Name = LayerName,
    Description = Description, 
    };
    //ltr.Name = LayerName; // this was used for testing
    //ltr.Description = "Hello";
    ObjectId ltID = lt.Add(ltr);
    tr.AddNewlyCreatedDBObject(ltr, true);
    //db.Clayer = ltID;
    db.Clayer = lt[LayerName];
    tr.Commit();
    //ed.WriteMessage("\nCreated Layer :" + LayerName);
    }
    }
    catch
    {
    //ed.WriteMessage("\nInvalid Layer Name");
    }
    }
    }
    

  2. #2
    I could stop if I wanted to kpblc2000's Avatar
    Join Date
    2006-09
    Posts
    212
    Login to Give a bone
    0

    Default Re: Add Layers Descriptions

    I think you have to add a layer record to database and after that change description:
    Code:
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.LayerManager;
    using Autodesk.AutoCAD.Runtime;
    
    namespace AddLayer
    {
      public class CmdClass
      {
        [CommandMethod("cmdlayer")]
        public static void cmdAddLayer()
        {
          Document doc = Application.DocumentManager.MdiActiveDocument;
          Database db = doc.Database;
          doc.LockDocument();
          kpblc_AddLayer(ref db, "test layer 1", "description1");
        }
    
        private static void kpblc_AddLayer(ref Database db, string sLayerName, string Description)
        {
          Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
          using (Transaction tr = db.TransactionManager.StartTransaction())
          {
            LayerTable tblLayer = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
            try
            {
              // SymbolUtilityServices.ValidateSymbolName(sLayerName, false);
              if (!(tblLayer.Has(sLayerName)))
              {
                // tblLayer.UpgradeOpen();
                LayerTableRecord recLayer = new LayerTableRecord();
                recLayer.Name = sLayerName;
                // recLayer.Description = Description;
                ObjectId recLayerID = tblLayer.Add(recLayer);
                tr.AddNewlyCreatedDBObject(recLayer, true);
                recLayer.Description = Description;
                tr.Commit();
              }
              else 
              {
                ed.WriteMessage("\nLayer {0} already exist", sLayerName);
                
              }
            }
            catch
            {
              ed.WriteMessage("\nLayer {0} can't be create: Invalid symbols", sLayerName);
            }
          }
        }
      }
    }

  3. #3
    Member
    Join Date
    2013-04
    Posts
    16
    Login to Give a bone
    0

    Default Re: Add Layers Descriptions

    That worked a treat thank you,

Similar Threads

  1. 2014: legal descriptions
    By rmk in forum AutoCAD Civil 3D - Survey
    Replies: 1
    Last Post: 2015-01-21, 04:45 PM
  2. 3d site descriptions
    By mr_mom40 in forum AutoCAD 3D (2007 and above)
    Replies: 1
    Last Post: 2008-09-02, 06:47 PM
  3. Revision descriptions
    By Chad Smith in forum Revit - In Practice
    Replies: 3
    Last Post: 2006-10-03, 05:07 PM
  4. Layer descriptions
    By autocad.wishlist1734 in forum AutoCAD Wish List
    Replies: 1
    Last Post: 2006-05-14, 01:36 PM
  5. Assembly Descriptions
    By ita in forum Revit Architecture - Families
    Replies: 15
    Last Post: 2004-06-22, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •