*如何创建一个Revit AddIn 项目参考:Revit API 开发 (1): Hello World
- 重载IExternalCommand的Execute方法。
- 通过UIApplication.ActiveUIDocument.Selection.GetElementIds() 得到被选中的element的Id。
- 创建一个**.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;
}
}
}