大家都知道UIDcument下有一个实时预览拖拽函数PromptForFamilyInstancePlacement,当然针对这个函数本身来说,没啥特别的,就是传递一个FamilySymbol类型而已,那么有时我们希望在使用这个函数退出后我们能够立即获得被放置的构件,那怎么办呢?其实我们可以使用DocumentChanged事件去做监控,在拖拽函数之前注册DocumentChanged事件,在拖拽函数之后注销DocumentChanged事件,然后通过DocumentChangedEventArgs参数下的GetAddedElementIds()方法获得新添加的构件,传入全局集合以供后续使用——代码很简洁,只做演示。
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Mechanical;
using System.Xml;
using Autodesk.Revit.UI.Events;
using System.Windows.Forms;
using Autodesk.Revit.DB.Events;
namespace HelloWorld
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class Test : IExternalCommand
{
ICollection collection = new List();
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
Document Doc=uiDoc.Document;
FamilySymbol symbol = new FilteredElementCollector(Doc).OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_Doors).FirstOrDefault() as FamilySymbol;
if (symbol.IsActive == false)
{
using (Transaction ts = new Transaction(Doc, "Active"))
{
ts.Start();
symbol.Activate();
ts.Commit();
}
}
uiApp.Application.DocumentChanged += new EventHandler(DocumentChangedForSomething);
uiDoc.PromptForFamilyInstancePlacement(symbol);
uiApp.Application.DocumentChanged -= new EventHandler(DocumentChangedForSomething);
MessageBox.Show(collection.Count.ToString());
return Result.Succeeded;
}
private void DocumentChangedForSomething(object sender,DocumentChangedEventArgs e)
{
foreach (ElementId id in e.GetAddedElementIds())
{
collection.Add(id);
}
}
}
}