.NET

Reply
Contributor
dreamwtx
Posts: 12
Registered: ‎03-07-2012
Message 1 of 8 (1,330 Views)

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

1330 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*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 8 (1,324 Views)

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
Paulio
Posts: 82
Registered: ‎06-26-2008
Message 3 of 8 (1,300 Views)

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*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 8 (1,297 Views)

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
Member
Radnus_87
Posts: 3
Registered: ‎09-25-2013
Message 5 of 8 (756 Views)

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
Paulio
Posts: 82
Registered: ‎06-26-2008
Message 6 of 8 (751 Views)

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*
_gile
Posts: 2,091
Registered: ‎04-29-2006
Message 7 of 8 (743 Views)

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
Member
Radnus_87
Posts: 3
Registered: ‎09-25-2013
Message 8 of 8 (738 Views)

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:

Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.