Revit API 开发 (2): 显示选中的图元(element)

*如何创建一个Revit AddIn 项目参考:Revit API 开发 (1): Hello World

  1. 重载IExternalCommandExecute方法。
  2. 通过UIApplication.ActiveUIDocument.Selection.GetElementIds() 得到被选中的elementId
  3. 创建一个**.addin**文件,参考Revit API 开发 (1): Hello World

代码如下:
(注意在执行这个命令之前,需要先选中一些elements。)

using System;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using System.Collections.Generic;

namespace Revit_Snippets
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.ReadOnly)]
    public class Document_Selection : IExternalCommand
    {
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
            ref string message, ElementSet elements)
        {
            try
            {
                // 得到当前文档的句柄。注意commandData通常是我们和Revit交互的中介。
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                
                // 得到当前的选择集,然后从选择集中得到已经选中的element id。
                Selection selection = uidoc.Selection;
                ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();

                if (0 == selectedIds.Count)
                {
                    // 没有被选中的element。
                    TaskDialog.Show("Revit","You haven't selected any elements.");
                }
                else
                {
                    // 打印所有选中element的id。
                    String info = "Ids of selected elements in the document are: ";
                    foreach (ElementId id in selectedIds)
                    {
                       info += "\n\t" + id.IntegerValue;
                    }

                    TaskDialog.Show("Revit",info);
                }
            }
            catch (Exception e)
            {
                message = e.Message;
                return Autodesk.Revit.UI.Result.Failed;
            }

            return Autodesk.Revit.UI.Result.Succeeded;
        }
    }
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客BIM工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值