【发布时间】:2014-01-13 19:09:16
【问题描述】:
我正在尝试创建我的第一个 Revit 插件。
我正在使用 Revit 2014,我想要放置一个从文件加载的族的 SINGLE 实例。我实际上正在使用此代码:
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
public class InsertFamily : IExternalCommand
{
readonly List<ElementId> _addedElementIds = new List<ElementId>();
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Document document = uiApp.ActiveUIDocument.Document;
FamilySymbol family = null;
bool good = false;
using (var trans = new Transaction(document, "inserting family"))
{
trans.Start();
good = document.LoadFamilySymbol(@"my file path.rfa", "my type", new FamilyLoadingOverwriteOption(), out family);
trans.Commit();
}
if (good && family != null)
{
_addedElementIds.Clear();
uiApp.Application.DocumentChanged += applicationOnDocumentChanged;
uiApp.ActiveUIDocument.PromptForFamilyInstancePlacement(family);
uiApp.Application.DocumentChanged -= applicationOnDocumentChanged;
}
return Result.Succeeded;
}
private void applicationOnDocumentChanged(object sender, DocumentChangedEventArgs documentChangedEventArgs)
{
_addedElementIds.AddRange(documentChangedEventArgs.GetAddedElementIds());
}
}
class FamilyLoadingOverwriteOption : IFamilyLoadOptions
{
public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues)
{
overwriteParameterValues = true;
return true;
}
public bool OnSharedFamilyFound(Family sharedFamily, bool familyInUse, out FamilySource source, out bool overwriteParameterValues)
{
source = FamilySource.Family;
overwriteParameterValues = true;
return true;
}
}
问题在于PromptForFamilyInstancePlacement 方法允许用户插入该族的多个实例。我希望用户只能将一个实例插入到项目中。我还编写了返回插入实例的代码(使用DocumentChanged 事件,如您所见),因此该处理程序可能在某些方面有用..
【问题讨论】: