.NET

.NET

Reply
Contributor
12 Posts
0 Kudos
Registered: ‎03-07-2012
Post 1 of 8

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

1609 Views, 7 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.

*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 2 of 8

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
Valued Contributor
82 Posts
0 Kudos
Registered: ‎06-26-2008
Post 3 of 8

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"?

*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 4 of 8

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
Contributor
16 Posts
0 Kudos
Registered: ‎09-25-2013
Post 5 of 8

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

09-26-2013 12:08 AM 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

Valued Contributor
82 Posts
0 Kudos
Registered: ‎06-26-2008
Post 6 of 8

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

09-26-2013 12:15 AM 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...
*Expert Elite*
2,142 Posts
255 Kudos
Registered: ‎04-29-2006
Post 7 of 8

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

09-26-2013 01:08 AM in reply to: Paulio
Contributor
16 Posts
0 Kudos
Registered: ‎09-25-2013
Post 8 of 8

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

09-26-2013 01:16 AM in reply to: Paulio

Got it..Thanks for your reply :smileyhappy:

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!