Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace layerlist
{
    public class Class1
    {
        // This method can have any name
        [CommandMethod("DL")]
        public void TestDisplayLayers()
        {
            {
                Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                Editor ed = doc.Editor;
                Transaction tr = doc.Database.TransactionManager.StartTransaction();
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                List<string> info = LayersToList(db);

                PromptPointResult ppr;
                PromptPointOptions ppo = new PromptPointOptions("");
                ppo.Message = "\n Select the place for print output:";
                //get the coordinates from user
                ppr = ed.GetPoint(ppo);
                if (ppr.Status != PromptStatus.OK)
                    return;
                Point3d startPoint = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);
                //Point3d startPoint1 = startPoint.Subtract();
                Vector3d disp = new Vector3d(0.0, -2.0 * db.Textsize, 0.0);
                Vector3d disp2 = new Vector3d(0.0, -2.0 * db.Textsize, 0.0);

                TextStyleTable ts = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead);
                ObjectId mtStyleid = db.Textstyle;
                if (ts.Has("NAL-TEXT"))
                {
                    mtStyleid = ts["NAL-FORMAT"];
                }
                foreach (string lname in info)
                {
                    MText mtext = new MText();
                    mtext.Location = startPoint;
                    mtext.Width = 75.207;
                    mtext.Height = 1.488;
                    mtext.TextStyleId = mtStyleid;
                    mtext.Contents = "\n" + lname;

                    curSpace.AppendEntity(mtext);
                    tr.AddNewlyCreatedDBObject(mtext, true);
                    db.TransactionManager.QueueForGraphicsFlush();
                    startPoint += disp;
                }
                tr.Commit();
            }
        }

        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;
        }
    }
}


What I have tried:

This is my code which retrieve me the layer name of dwg file but instead of this I want to print only layers which started from NL like NL-log etc.
Posted
Comments
Richard Deeming 14-Sep-16 9:29am    
So test whether the layer name matches before adding it to the list.

Or test whether the layer name matches before processing it within the foreach loop.

lname.StartsWith("NL")

What's the problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900