• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Contributor
    Posts: 12
    Registered: ‎03-07-2012

    How to get all names of layers in a drawing by traversal layers using c#?

    434 Views, 3 Replies
    03-14-2012 09:32 PM

    I want to get all names of layers in a drawing ,and then  put them in a array.  how to carry out it.

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,338
    Registered: ‎10-08-2008

    Re: How to get all names of layers in a drawing by traversal layers using c#?

    03-14-2012 10:35 PM in reply to: dreamwtx

    I think easier to gather layer names to List of string

    then you can easy to convert to array using

    List.ToArray() method

    Here is a quick sample:

    // This method can have any name
            [CommandMethod("DisplayLayers","displa", CommandFlags.Modal)]
            public void TestDisplayLayers()
            {
                Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                Editor ed = doc.Editor;
                List<string> info = LayersToList(db);
                foreach (string lname in info)
                    ed.WriteMessage("\nLayer Name: \t{0}", lname);
    
            }
    
            public List<string> LayersToList(Database db)
            {
                List<string> lstlay = new List<string>();
    
                LayerTableRecord layer;
                using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
                {
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    foreach (ObjectId layerId in lt)
                    {
                        layer = tr.GetObject (layerId, OpenMode.ForWrite) as LayerTableRecord;
                        lstlay.Add(layer.Name);
                    }
    
                }
                return lstlay;
            }

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Valued Contributor
    Posts: 80
    Registered: ‎06-26-2008

    Re: How to get all names of layers in a drawing by traversal layers using c#?

    03-15-2012 10:02 AM in reply to: Hallex

    Just curious Hallex, what's different about "StartOpenCloseTransaction" as opposed to "StartTransaction"?

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,338
    Registered: ‎10-08-2008

    Re: How to get all names of layers in a drawing by traversal layers using c#?

    03-15-2012 10:11 AM in reply to: Paulio

    To be honestly I did not read Arx docs about this

    I use it when I just do nothing in the drawing,

    as in this case to read something from document only

     

    :smileyindifferent:

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.