|
在与用户交互式的绘图的时候可能需要批量获取一些图形,而在这一批选择的时候你可能需要过滤一些不需要的类型自动排除在你选择的集合以外,下面这个例子就是只接受选择文本、圆、直线三种类型,其余的图形将不能选中。
using System;- <p>
- </p><p>using Autodesk.AutoCAD.Runtime;</p><p>using Autodesk.AutoCAD.ApplicationServices;</p><p>using Autodesk.AutoCAD.DatabaseServices;</p><p>using Autodesk.AutoCAD.EditorInput;</p><p>
- </p><p>[assembly: CommandClass(typeof(Sample.Selection))]</p><p>namespace Sample</p><p>{</p><p> /// <summary></p><p> /// 类型过滤枚举类</p><p> /// </summary></p><p> public enum FilterType</p><p> {</p><p> Curve, Dimension, Polyline, BlockRef, Circle, Line, Arc, Text, MText</p><p> }</p><p>
- </p><p> class Selection</p><p> {</p><p> [CommandMethod("Selection")]</p><p> public void Select()</p><p> {</p><p> FilterType LineType = FilterType.Line;</p><p> FilterType TextType = FilterType.Text;</p><p> FilterType CircleType = FilterType.Circle;</p><p>
- </p><p> FilterType[] Types = new FilterType[3];</p><p> Types[0] = LineType;</p><p> Types[1] = TextType;</p><p> Types[2] = CircleType;</p><p> DBObjectCollection EntityCollection = GetSelection(Types);</p><p> }</p><p>
- </p><p> public static DBObjectCollection GetSelection(FilterType[] tps)</p><p> {</p><p> Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;</p><p> Database db = doc.Database;</p><p> Editor ed = doc.Editor;</p><p> Entity entity = null;</p><p> DBObjectCollection EntityCollection = new DBObjectCollection();</p><p> PromptSelectionOptions selops = new PromptSelectionOptions();</p><p> // 建立选择的过滤器内容</p><p> TypedValue[] filList = new TypedValue[tps.Length + 2];</p><p> filList[0] = new TypedValue((int)DxfCode.Operator, "<or");</p><p> filList[tps.Length + 1] = new TypedValue((int)DxfCode.Operator, "or>");</p><p> for (int i = 0; i < tps.Length; i++)</p><p> {</p><p> filList[i + 1] = new TypedValue((int)DxfCode.Start, tps[i].ToString());</p><p> }</p><p> // 建立过滤器</p><p> SelectionFilter filter = new SelectionFilter(filList);</p><p> // 按照过滤器进行选择</p><p> PromptSelectionResult ents = ed.GetSelection(selops, filter);</p><p> if (ents.Status == PromptStatus.OK)</p><p> {</p><p> using (Transaction transaction = db.TransactionManager.StartTransaction())</p><p> {</p><p> SelectionSet SS = ents.Value;</p><p> foreach (ObjectId id in SS.GetObjectIds())</p><p> {</p><p> entity = (Entity)transaction.GetObject(id, OpenMode.ForWrite, true);</p><p> if (entity != null)</p><p> EntityCollection.Add(entity);</p><p> }</p><p> transaction.Commit();</p><p> }</p><p> }</p><p> return EntityCollection;</p><p> }</p><p> }</p><p>}</p>
复制代码 加载程序运行命令后框选图形上的对象后得到如下效果:
|
|