![](https://img-blog.csdnimg.cn/img_convert/6537de05b6a3477495e10b832904b2b2.png)
这是Revit的选择器Api,位于Autodesk.Revit.UI.Selection.Selection中,从上面可以看到,不仅是选点,还可以选物选框选盒子。
另外中国程序员通常不太注意的一件事:该选择器是非托管资源,也就是你不释放的话,是会使得内存不断提升的。即所谓的内存泄露。使用Selection选择器后,要及时释放。
下面仅以点选多选为例供自己复习:
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RetrieveInformation
{
[Transaction(TransactionMode.Manual)]
public class SelectionDemo : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var uiDoc = commandData.Application.ActiveUIDocument;
var doc = uiDoc.Document;
try
{
var wallFilter = new WallSelectionFilter();
var reference = uiDoc.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, wallFilter);
string name = ":"+ "\r\n";
int i = 1;
foreach (var wallItem in reference)
{
var element = doc.GetElement(wallItem.ElementId);
var wall = "名称:"+(element as Wall).Name +" "+"ID:"+ (element as Wall).Id;
name += i+"、"+wall+","+ "\r\n";
i++;
}
TaskDialog.Show("操作提示", "已选择"+ name);
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException e)
{
TaskDialog.Show("操作提示", "您已取消了选择!!!");
}
return Result.Succeeded;
}
}
//限制鼠标选择时只能选择墙
public class WallSelectionFilter : ISelectionFilter
{
public bool AllowElement(Element elem)
{
if (elem.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Walls)
return true;
else return false;
}
public bool AllowReference(Reference reference, XYZ position)
{
return false;
}
}
}
![](https://img-blog.csdnimg.cn/img_convert/cc96e63693cc40e38008ecaa2e09a555.png)
![](https://img-blog.csdnimg.cn/img_convert/be142deed4af452a9557951bf09b322e.png)