Revit API使用PromptForFamilyInstancePlacement放置详细信息组件

时间:2011-08-02 15:26:49

标签: c# api revit

我正在尝试使用PromptForFamilyInstancePlacement放置详细信息组件,但无法正确定义FamilySymbol

我找到的示例显示如何使用FilteredElementCollector,但我尝试按名称定义FamilySymbol

3 个答案:

答案 0 :(得分:0)

您是否已将Family文档加载到您正在使用的项目中?如果没有,则可以使用Document.LoadFamilySymbolDocument.LoadFamily将系列加载到项目中。否则,要找到您要查找的系列符号,您可以使用类似于以下代码的内容:

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);

答案 1 :(得分:0)

尝试此代码(需要System.Linq)和.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>();
    }

答案 2 :(得分:-1)

它们都有效,即使在Revit 2017中也是如此。(在第二个解决方案中,如果行需要==而不是=,则为else。)

在第二个解决方案中,我使用它来确保我有正确的姓氏和家庭符号名称:

foreach (FamilySymbol familySymbol in familyCollector)
{
  if (familySymbol.Name == "Put your Family Name here" && familySymbol.Family.Name == "Put your Family Symbol Name here")
                        familySymbolToFind = familySymbol;
}