I want to get all names of layers in a drawing ,and then put them in a array. how to carry out it.
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'~
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
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
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...
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:
Can't find what you're looking for? Ask the community or share your knowledge.