Revit API placing detail component using PromptForFamilyInstancePlacement
I'm trying to place a detail component using PromptFor开发者_如何学JAVAFamilyInstancePlacement
, but am having trouble properly defining the FamilySymbol
.
Examples that I have found show how to to this using a FilteredElementCollector
, but I am trying to define the FamilySymbol
by name.
Try this code (requires System.Linq) and .NET4
FamilySymbol symbol = GetElements<FamilySymbol>(commandData.Application.ActiveUIDocument.Document)
.Where(item => item.Name == "NameYouWant")
.First();
commandData.Application.ActiveUIDocument.PromptForFamilyInstancePlacement(symbol);
/// <summary>
/// Get the collection of elements of the specified type.
/// <para>The specified type must derive from Element, or you can use Element but you get everything :)</para>
/// </summary>
/// <typeparam name="T">The type of element to get</typeparam>
/// <returns>The list of elements of the specified type</returns>
public IEnumerable<T> GetElements<T>(Document document) where T : Element
{
FilteredElementCollector collector = new FilteredElementCollector(document);
collector.OfClass(typeof(T));
return collector.Cast<T>();
}
Have you already loaded the Family document into the project that you are working in? If not then you can load the family into the project using Document.LoadFamilySymbol
or Document.LoadFamily
. Otherwise, to find the family symbol that you are looking for, you can use something similar to the code below:
UIApplication application = commandData.Application;
UIDocument uiDocument = application.ActiveUIDocument;
Document document = application.ActiveUIDocument.Document;
FilteredElementCollector familyCollector = new FilteredElementCollector(document);
familyCollector.OfClass(typeof(FamilySymbol));
FamilySymbol familySymbolToFind = null;
foreach (FamilySymbol familySymbol in familyCollector)
{
//To search by FamilySymbol name
if (familySymbol.Name == "[Name of FamilySymbol to find]")
familySymbolToFind = familySymbol;
//To search by Family name
else if (familySymbol.Family.Name = "[Name of Family to find]")
familySymbolToFind = familySymbol;
}
uiDocument.PromptForFamilyInstancePlacement(familySymbolToFind);
They both work, even in Revit 2017. (On the second solution, the else if line needs a ==
instead of an =
.)
In the second solution, I used this to make sure I had the right family name and family symbol name:
foreach (FamilySymbol familySymbol in familyCollector)
{
if (familySymbol.Name == "Put your Family Name here" && familySymbol.Family.Name == "Put your Family Symbol Name here")
familySymbolToFind = familySymbol;
}
精彩评论