欢迎加我的QQ群:193522571,一起来讨论、交流!

过滤选择集

 /// <summary>
        /// 过滤选择集合
        /// 调用方法如: ObjectIdCollection EntityCollection = GetSelection();  
        /// </summary>
        /// <returns>对象集合</returns>
        public static ObjectIdCollection GetSelection()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            Entity entity = null;
            DBObjectCollection entityCollection = new DBObjectCollection();

            // 建立选择的过滤器内容
            TypedValue[] values = new TypedValue[]
            {
                new TypedValue((int)DxfCode.Start,"Text")
            };
            SelectionFilter filter = new SelectionFilter(values);
            PromptSelectionResult optSel = ed.GetSelection(filter);

            if (optSel.Status == PromptStatus.OK)
            {
                using (Transaction transaction = db.TransactionManager.StartTransaction())
                {
                    SelectionSet SS = optSel.Value;
                    foreach (ObjectId id in SS.GetObjectIds())
                    {
                        entity = (Entity)transaction.GetObject(id, OpenMode.ForWrite, true);
                        if (entity != null)
                            entityCollection.Add(entity);
                    }
                    transaction.Commit();
                }
            }

            ObjectIdCollection ids = new ObjectIdCollection();
            foreach (Entity ent in entityCollection)
            {
                ObjectId id = ent.ObjectId;
                ids.Add(id);
            }
            return ids;
        }

posted @ 2014-07-07 07:54  swtool  阅读(634)  评论(0编辑  收藏  举报
欢迎加我的QQ群:193522571,一起来讨论、交流!