.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

7 REPLIES 7
Reply
Message 1 of 8
dreamwtx
9113 Views, 7 Replies

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

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

7 REPLIES 7
Message 2 of 8
Hallex
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
Message 3 of 8
Paulio
in reply to: Hallex

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

Message 4 of 8
Hallex
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

 

Smiley Indifferent

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 5 of 8
Radnus_87
in reply to: Hallex

Thanks for Posting this routine. But i need to get the list of layer names which are turned on in current drawing not all the layers in the drawing. How to get it

Message 6 of 8
Paulio
in reply to: Radnus_87

You could test the IsOff property of the LayerTableRecord before adding it to the list:

 

                foreach (ObjectId layerId in lt)
                {
                    layer = tr.GetObject (layerId, OpenMode.ForWrite) as LayerTableRecord;
                    if (!layer.IsOff)
                    {
                         lstlay.Add(layer.Name);
                    }
                }

 


You could also test for IsFrozen if required...
Message 7 of 8
_gile
in reply to: Paulio

Hi,

 

About StartTransaction() vs StartOpenCloseTransaction(), you can read this:

http://adndevblog.typepad.com/autocad/2012/08/the-right-tools-for-the-job-autocad-part-5.html

and this:

http://docs.autodesk.com/ACD/2014/DEU/index.html?url=files/GUID-50FD6118-B2D1-4313-A7D6-830794DFDEFA...



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 8
Radnus_87
in reply to: Paulio

Got it..Thanks for your reply 🙂

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost