.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Select layers retrieve by below code.

1 REPLY 1
Reply
Message 1 of 2
Anonymous
526 Views, 1 Reply

Select layers retrieve by below code.

This is my code which retrieve some layer names and I want to select that all layers when I running the command. How this is achieve give me some suggestions on that. Thank you in advance.

 

 

Snippet

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
                    {
                      
                        symbol.Color = Color.FromColorIndex(ColorMethod.ByAci, 15);
                        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();
            }
        }
    }
}



 

 

 

 

Tags (2)
1 REPLY 1
Message 2 of 2
arcticad
in reply to: Anonymous

What are you trying to do? 

 

This line needs to change to forWrite if your going to change to the color.

 

LayerTableRecord symbol = (LayerTableRecord)tr.GetObject(id, OpenMode.ForRead);

 

 

---------------------------



(defun botsbuildbots() (botsbuildbots))

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


AutoCAD Beta