Revit二次开发实战02(选择对象Selection)

发布时间 2023-05-20 17:41:56作者: 张德长

Revit二次开发实战

 

Selection主要用于和用户交互,通过用户的选择,设置操作对象,以便进行处理;

Selection属于界面操作的范畴,因此位于UIDocument类下面,而不是Document类下面;

可以选择一个对象、多个对象、选择点、选择矩形框、框选多个对象等;

通过过滤器可以提供一个强大的功能,可以通过各种条件筛选出想要选择的对象;

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 RevitHello
{
    //选择一个对象
    [Transaction(TransactionMode.Manual)]
    class CSelectOne : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //获取UIDocument对象
            UIDocument uiDoc = commandData.Application.ActiveUIDocument;
            //选择一个元素,并返回该元素的引用
            //这是一个阻塞方法,当没有选择任何对象时,后面的代码不会执行
            Reference reference = uiDoc.Selection.PickObject(ObjectType.Face);
            TaskDialog.Show("GetType().FullName", reference.GetType().FullName);
            //获取Document对象
            Document doc = uiDoc.Document;
            //从引用获取元素对象
            Element element = doc.GetElement(reference);
            //显示元素相关参数
            TaskDialog.Show("element", $"Name={element.Name}\nId={element.Id}\nCategory.Name={element.Category.Name}");
            return Result.Succeeded;
        }
    }
    //选择多个对象
    [Transaction(TransactionMode.Manual)]
    class CSelectMany : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uiDoc = commandData.Application.ActiveUIDocument;
            //获取多个元素引用的列表
            IList<Reference> refList = uiDoc.Selection.PickObjects(ObjectType.Element);
            Document doc = uiDoc.Document;
            StringBuilder sb = new StringBuilder();
            //遍历所有元素
            foreach (var r in refList)
            {
                Element e = doc.GetElement(r);
                string s = $"Name={e.Name},Id={e.Id},Category={e.Category.Name}\n";
                sb.Append(s);

            }
            //显示信息
            TaskDialog.Show("PickObjects", sb.ToString());

            return Result.Cancelled;
        }
    }
    //使用过滤器进行选择
    [Transaction(TransactionMode.Manual)]
    class CSelectFilter : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //获取文档对象
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            //使用过滤器进行选择
            IList<Reference> refList = uidoc.Selection.PickObjects(ObjectType.Element,new DoorFilter());
            //获取选择对象的信息
            StringBuilder sb = new StringBuilder();
            foreach (var r in refList)
            {
                Element e = doc.GetElement(r);
                sb.AppendLine($"Name={e.Name},Id={e.Id}");
            }
            //显示信息
            TaskDialog.Show(typeof(CSelectFilter).Name,sb.ToString() );

            return Result.Cancelled;
        }
    }

    class DoorFilter : ISelectionFilter
    {
        public bool AllowElement(Element elem)
        {
            if (elem is FamilyInstance) return true;
            return false;
        }

        public bool AllowReference(Reference reference, XYZ position)
        {
            return true;
        }
    }
}