1.获取元素的ID
通过UIDocument的Selection属性获取当前视图中选中的元素的ID/类型。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace GetAllElements
{
[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;
ElementSet collection = selection.Elements;
if (0 == collection.Size)
{
//If no elements selected.
TaskDialog.Showing("Revit", "You haven't selected any elements.");
}
else
{
String info = "Ids of selected elements in the document are: ";
foreach (Element elem in collection)
{
info += "\n\t" + elem.Id.IntegerValue;//所选元素的ID
//info += "\n\t" + elem.GetTypr().ToString(); 此为所选元素的类型
}
TaskDialog.Show("Revit", info);
}
}
catch (Exception e)
{
message = e.Message;
return Autodesk.Revit.UI.Result.Failed;
}
return Autodesk.Revit.UI.Result.Succeeded;
}
}
}
2.用过滤器获取元素
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace GetAllElements
{
[Autodesk.Revit.Attributes.Transaction(TransactionMode.ReadOnly)]
public class Document_Selection : IExternalCommand
{
public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Document document = revit.Application.ActiveUIDocument.Document;
//Create a Filter to get all the doors in the document
ElementClassFilter familyInstanceFilter = new ElementClassFilter(typeof(FamilyInstance));
ElementCategoryFilter doorsCategoryfilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);
LogicalAndFilter doorInstancesFilter = new LogicalAndFilter(familyInstanceFilter, doorsCategoryfilter);
FilteredElementCollector collector = new FilteredElementCollector(document);
ICollection<ElementId> doors = collector.WherePasses(doorInstancesFilter).ToElementIds();
String prompt = "The ids of the doors in the current document are: ";
foreach (ElementId id in doors)
{
prompt += "\n\t" + id.IntegerValue;
}
//Give the user some information
TaskDialog.Show("Revit", prompt);
}
}
}
3.获取所选元素的全部值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using System.Windows.Forms;
using Autodesk.Revit.UI.Selection;
//如果出现缺少using指令或程序集引用,去API上查
//点击revit中的墙,或者其他东西,会出现这个东西的全部信息
namespace GetAllParamentsValues
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class GetAllParamentsValues : IExternalCommand
{
public Autodesk.Revit.UI.Result Execute(ExternalCommandData CommandData, ref string messages, ElementSet elements)
{
UIApplication uiApp = CommandData.Application;
Autodesk.Revit.ApplicationServices.Application app = uiApp.Application;
Document doc = uiApp.ActiveUIDocument.Document;
Selection sel = uiApp.ActiveUIDocument.Selection;
//please select an element
// sel.StatusbarTip = "Please select an element";
sel.PickPoint("Please select an element");
// sel.PickOne();
Element elemPick = null;
foreach (Element elem in sel.Elements)
{
elemPick = elem;
break;
}
string strParamInfo = null;
foreach (Parameter param in elemPick.Parameters)
{
if(param.AsValueString()!=null)
strParamInfo += param.Definition.Name + "value is :" + param.AsValueString() + "\n";
else
strParamInfo += param.Definition.Name + "value is :" + param.AsString() + "\n";
}
MessageBox.Show(strParamInfo);
return Autodesk.Revit.UI.Result.Succeeded;
}
}
}
例如,在revit中选取一面墙,就会得到以下信息: