Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;

namespace NAL_Layer11
{
    public class class1
    {
        [CommandMethod("NLCL")]
        public static void PurgeLayers()
        {
            Document doc = 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);
            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);

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

            // using (Transaction tr =
            //  db.TransactionManager.StartTransaction())
            {
                LayerTable table = tr.GetObject(db.LayerTableId,
                    OpenMode.ForRead) as LayerTable;

                ObjectIdCollection layIds = new ObjectIdCollection();
                foreach (ObjectId id in table)
                {
                    layIds.Add(id);
                }

                //this function will remove all
                //layers which are used in the drawing file
                db.Purge(layIds);
                MText mtext1 = new MText();
                mtext1.Location = startPoint;
                mtext1.TextStyleId = mtStyleid;
                // mtext1.Contents = "The unauthorized layers are:";
                mtext1.Width = 85.000;
                mtext1.Height = 2.000;
                mtext1.Contents = "{\\C1;The unauthorized layers are:}\\P";
                curSpace.AppendEntity(mtext1);
                tr.AddNewlyCreatedDBObject(mtext1, true);
                db.TransactionManager.QueueForGraphicsFlush();

                foreach (ObjectId id in layIds)
                {
                    DBObject obj = tr.GetObject(id, OpenMode.ForWrite);
                    obj.Erase();
                }

                SymbolTable symTable = (SymbolTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);

                // mtext.Contents = "The unauthorized layers are:";
                foreach (ObjectId id in symTable)
                {
                    ColorDialog cd = new ColorDialog();
                    LayerTableRecord symbol = (LayerTableRecord)tr.GetObject(id, OpenMode.ForRead);

                    if (symbol.Name.StartsWith("NL"))
                    {
                        // ed.WriteMessage("The authorized layres are:" + symbol.Name);
                    }
                    else
                    {
                       
                        MText mtext = new MText();
                        mtext.Contents = "\n" + symbol.Name;
                        mtext.Location = startPoint;
                        mtext.Width = 75.207;
                        mtext.Height = 1.488;
                        mtext.TextStyleId = mtStyleid;
                        curSpace.AppendEntity(mtext);
                        tr.AddNewlyCreatedDBObject(mtext, true);
                        db.TransactionManager.QueueForGraphicsFlush();
                        startPoint += disp;
                    }
                }
                tr.Commit();
            }
        }
    }
}


What I have tried:

This is my code which retrieve me the some layers and I want to display this layers on my dwg file so I can view the layers properly on my drawing.
Posted

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