Move the selected objects to the current layer



Build a FilterType[] to select

public enum FilterType
    {
        Curve, Dimension, Polyline, BlockRef, Circle, Line, Arc, Text, MText
    }


public static DBObjectCollection GetSelection(FilterType[] tps)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            Entity entity = null;
            DBObjectCollection EntityCollection = new DBObjectCollection();
            PromptSelectionOptions selops = new PromptSelectionOptions();

            // Name the filter contents
            TypedValue[] filList = new TypedValue[tps.Length + 2];
            filList[0] = new TypedValue((int)DxfCode.Operator, "");
            for (int i = 0; i < tps.Length; i++)
            {
                filList[i + 1] = new TypedValue((int)DxfCode.Start, tps[i].ToString());
            }


             // Build the filter
            SelectionFilter filter = new SelectionFilter(filList);
            // 按照过滤器进行选择
            PromptSelectionResult ents = ed.GetSelection(selops, filter);
            if  (ents.Status == PromptStatus.OK)  
            {
                using  (Transaction transaction = db.TransactionManager.StartTransaction())
                 {
                    SelectionSet SS = ents.Value;
                    foreach (ObjectId id in   SS.GetObjectIds())
                    {
                        entity = (Entity) transaction.GetObject(id, OpenMode.ForWrite,  true );  
                        if(entity!=null)
                        {
                            LayerTableRecord ltr = LayerTools.GetCurrentLayer(db);
                            entity.Layer = ltr.Name;
                            EntityCollection.Add(entity);
                            ed = Autodesk.AutoCAD.ApplicationServices. Application .DocumentManager.MdiActiveDocument.Editor;
                            ed.SelectPrevious();
                        }
                    }
                    transaction.Commit( );  
                }
            }
            return   EntityCollection;
        }

Select Lines and Polylines

public void SelectLines()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            LayerTableRecord ltr = new LayerTableRecord();
            ltr = LayerTools.GetCurrentLayer(db);


            FilterType LineType = FilterType.Line;
            FilterType PolyLineType = FilterType.Polyline;
            FilterType CircleType = FilterType.Circle;

            FilterType[] Types = new FilterType[3];
            Types[0] = LineType;
            Types[1] = PolyLineType;
            Types[2] = CircleType;
            DBObjectCollection EntityCollection = Selection.GetSelection(Types);
        }


Author: Ford Yang
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Ford Yang !
  TOC