由于Revit2015后改变了 selection.Elements 的方法获取元素,因此只能通过Id来获取元素
Revit2018给出的代码如下:
[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 { // Select some elements in Revit before invoking this command // Get the handle of current document. UIDocument uidoc = commandData.Application.ActiveUIDocument; // Get the element selection of current document. Selection selection = uidoc.Selection; ICollectionselectedIds = uidoc.Selection.GetElementIds(); if (0 == selectedIds.Count) { // If no elements selected. TaskDialog.Show("Revit","You haven't selected any elements."); } else { 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; } /// }