1. 程式人生 > >C# Revit二次開發基礎/核心程式設計---元素過濾器

C# Revit二次開發基礎/核心程式設計---元素過濾器

//============程式碼片段3-37:元素過濾器============

/// <summary>

/// 使用ElementCategoryFilter過濾元素

/// </summary>

void TestElementCategoryFilter(Document doc)

{

   // 找到所有屬於牆類別的元素:牆例項和牆型別都將會被過濾出來

   FilteredElementCollector collector = new FilteredElementCollector(doc);

   ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_Walls);

   ICollection<Element> founds = collector.WherePasses(filter).ToElements();

   foreach (Element elem in founds)

   {

     Trace.WriteLine(String.Format("  Element id: {0}, type: {1}", elem.Id.IntegerValue, elem.GetType().Name));

   }

}

//============程式碼片段3-38:元素過濾器============

/// <summary>

/// 使用ElementClassFilter過濾元素

/// </summary>

void TestElementClassFilter(Document doc)

{

  // 找到所有屬於FamilySymbol的元素:元素的子類也將被過濾出來

  FilteredElementCollector collector = new FilteredElementCollector(doc);

  ElementClassFilter filter = new ElementClassFilter(typeof(FamilySymbol));

  ICollection<ElementId> founds = collector.WherePasses(filter).ToElementIds();

  Trace.WriteLine(String.Format("  Found {0} FamilySymbols.", founds.Count));

}

//============程式碼片段3-39:元素過濾器============

/// <summary>

/// 使用ElementIsElementTypeFilter過濾元素

/// </summary>

void TestElementIsElementTypeFilter(Document doc)

{

  // 找到所有屬於ElementType的元素

  FilteredElementCollector collector = new FilteredElementCollector(doc);

  ElementIsElementTypeFilter filter = new ElementIsElementTypeFilter();

  ICollection<ElementId> founds = collector.WherePasses(filter).ToElementIds();

    Trace.WriteLine(String.Format("  Found {0} ElementTypes.", founds.Count));

}

//============程式碼片段3-40:元素過濾器============

/// <summary>

/// 使用FamilySymbolFilter過濾元素

/// </summary>

void TestFamilySymbolFilter(Document doc)

{

  // 找到當前文件中族例項所對應的族型別

  FilteredElementCollector collector = new FilteredElementCollector(doc);

  ICollection<ElementId> famIds = collector.OfClass(typeof(Family)).ToElementIds();

  foreach (ElementId famId in famIds)

  {

    collector = new FilteredElementCollector(doc);

    FamilySymbolFilter filter = new FamilySymbolFilter(famId);

    int count = collector.WherePasses(filter).ToElementIds().Count;

    Trace.WriteLine(String.Format("  {0} FamilySybmols belong to Family {1}.", count, famId.IntegerValue));

  }

}

//============程式碼片段3-41:元素過濾器============

/// <summary>

/// 使用ExclusionFilter過濾元素

/// </summary>

void TestExclusionFilter(Document doc)

{

  // 找到所有除族型別FamilySymbol外的元素型別ElementType

  FilteredElementCollector collector = new FilteredElementCollector(doc);

  ICollection<ElementId> excludes = collector.OfClass(typeof(FamilySymbol)).ToElementIds();

  // 建立一個排除族型別FamilySymbol的過濾器

  ExclusionFilter filter = new ExclusionFilter(excludes);

  ICollection<ElementId> founds = collector.WhereElementIsElementType().WherePasses(filter).ToElementIds();

  Trace.WriteLine(String.Format("  Found {0} ElementTypes which are not FamilySybmols", founds.Count));

}

//============程式碼片段3-42:元素過濾器============

/// <summary>

/// 使用ElementLevelFilter過濾元素

/// </summary>

void TestElementLevelFilter(Document doc)

{

  // 找到當前所有標高對應的所有元素

  FilteredElementCollector collector = new FilteredElementCollector(doc);

  ICollection<ElementId> levelIds = collector.OfClass(typeof(Level)).ToElementIds();

  foreach (ElementId levelId in levelIds)

  {

    collector = new FilteredElementCollector(doc);

    ElementLevelFilter filter = new ElementLevelFilter(levelId);

    ICollection<ElementId> founds = collector.WherePasses(filter).ToElementIds();

    Trace.WriteLine(String.Format("  {0} Elements are associated to Level {1}.", founds.Count, levelId.IntegerValue));

  }

}

//============程式碼片段3-43:元素過濾器============

/// <summary>

/// 使用ElementParameterFilter過濾元素

/// </summary>

void TestElementParameterFilter(Document doc)

{

  // 找到所有id大於99的元素

  BuiltInParameter testParam = BuiltInParameter.ID_PARAM;

  // 提供者

  ParameterValueProvider pvp = new ParameterValueProvider(new ElementId((int)testParam));

  // 評估者

  FilterNumericRuleEvaluator fnrv = new FilterNumericGreater();

  // 規則值   

  ElementId ruleValId = new ElementId(99); // Id 大於 99

  // 建立規則過濾器及對應的元素引數過濾器

  FilterRule fRule = new FilterElementIdRule(pvp, fnrv, ruleValId);

  ElementParameterFilter filter = new ElementParameterFilter(fRule);

  FilteredElementCollector collector = new FilteredElementCollector(doc);

  ICollection<Element> founds = collector.WherePasses(filter).ToElements();

  foreach (Element elem in founds)

  {

    Trace.WriteLine(String.Format("  Element id: {0}", elem.Id.IntegerValue));

  }

}

//============程式碼片段3-44:元素過濾器============

/// <summary>

/// 使用FamilyInstanceFilter過濾元素

/// </summary>

void TestFamilyInstanceFilter(Document doc)

{

  // 找到名字"W10X49"的族型別

  FilteredElementCollector collector = new FilteredElementCollector(Document);

  collector = collector.OfClass(typeof(FamilySymbol));

  var query = from element in collector

      where element.Name == "W10X49"

      select element; // Linq 查詢

  List<Autodesk.Revit.DB.Element> famSyms = query.ToList<Autodesk.Revit.DB.Element>();

  ElementId symbolId = famSyms[0].Id;

  // 建立過濾器並找到該族型別對應的所有族例項

  collector = new FilteredElementCollector(doc);

  FamilyInstanceFilter filter = new FamilyInstanceFilter(doc, symbolId);

  ICollection<Element> founds = collector.WherePasses(filter).ToElements();

  foreach (FamilyInstance inst in founds)

  {

    Trace.WriteLine(String.Format("  FamilyInstance {0}, FamilySybmol Id {1}, Name: {2}",inst.Id.IntegerValue, inst.Symbol.Id.IntegerValue, inst.Symbol.Name));

  }

}

//============程式碼片段3-45:元素過濾器============

      ///  /// <summary>

/// 使用CurveElementFilter 過濾元素

/// </summary>

void TestCurveElementFilter(Document doc)

{

    // 找到所有線元素型別對應的線型元素

    Array stTypes = Enum.GetValues(typeof(CurveElementType));

    foreach (CurveElementType tstType in stTypes)

    {

        if (tstType == CurveElementType.Invalid) continue;

        FilteredElementCollector collector = new FilteredElementCollector(Document);

        CurveElementFilter filter = new CurveElementFilter(tstType);

        int foundNum = collector.WherePasses(filter).ToElementIds().Count;

        Trace.WriteLine(String.Format(" {0}: elements amount {1}", tstType.GetType().Name, foundNum));

    }

}

//============程式碼片段3-46:元素過濾器============

/// <summary>

/// 使用LogicalOrFilter過濾元素

/// </summary>

void TestLogicalOrFilter(Document doc)

{

  // 情形 1: 合併兩個過濾器 ->

    // 找到所有屬於牆類別或者屬於標高類別的元素

  ElementCategoryFilter filterWall = new ElementCategoryFilter(BuiltInCategory.OST_Walls);

  ElementCategoryFilter filterLevel = new ElementCategoryFilter(BuiltInCategory.OST_Levels);

  LogicalOrFilter orFilter = new LogicalOrFilter(filterWall, filterLevel);

  FilteredElementCollector collector = new FilteredElementCollector(doc);

  ICollection<Element> founds = collector.WherePasses(orFilter).ToElements();

  foreach(Element elem in founds)

  {

    Trace.WriteLine(String.Format("  Element Id {0}, type {1}", elem.Id.IntegerValue, elem.GetType()));

  }

  // 情形 2: 合併兩個過濾器集合 -> 找到所有屬於傳入型別的元素

  Type[] elemTypes = { typeof(Wall), typeof(Level), typeof(Floor), typeof(Rebar), typeof(MEPSystem)};

  List<ElementFilter> filterSet = new List<ElementFilter>();

  foreach (Type elemType in elemTypes)

  {

    ElementClassFilter filter = new ElementClassFilter(elemType);

    filterSet.Add(filter);

  }

  orFilter = new LogicalOrFilter(filterSet);

  collector = new FilteredElementCollector(doc);

  founds = collector.WherePasses(orFilter).ToElements();

  foreach (Element elem in founds)

  {

    Trace.WriteLine(String.Format("  Element Id {0}, type {1}", elem.Id.IntegerValue, elem.GetType().Name));

  }

}

//============程式碼片段3-47:元素過濾器============

/// <summary>

/// 使用LogicalAndFilter過濾器

/// </summary>

void TestLogicalAndFilter(Document doc)

{

  // 情形 1: 合併兩個過濾器 -> 找到所有符合特定設計選項的牆

  ElementClassFilter wallFilter = new ElementClassFilter(typeof(Wall));

  FilteredElementCollector collector = new FilteredElementCollector(doc);

  ICollection<ElementId> designOptIds = collector.OfClass(typeof(DesignOption)).ToElementIds();

  foreach(ElementId curId in designOptIds)

  {

    ElementDesignOptionFilter designFilter = new ElementDesignOptionFilter(curId);

    LogicalAndFilter andFilter = new LogicalAndFilter(wallFilter, designFilter);

    collector = new FilteredElementCollector(doc);

    int wallCount = collector.WherePasses(andFilter).ToElementIds().Count;

    Trace.WriteLine(String.Format("  {0} Walls belong to DesignOption {1}.", wallCount, curId.IntegerValue));

  }

  // 情形 2: 找到所有符合特定設計選項並且其StructuralWallUsage 屬於承重的牆

  foreach (ElementId curId in designOptIds)

  {

    // 構造邏輯與過濾器

    List<ElementFilter> filters = new List<ElementFilter>();

    filters.Add(wallFilter);

    filters.Add(new ElementDesignOptionFilter(curId));

    filters.Add(new StructuralWallUsageFilter(StructuralWallUsage.Bearing));

    LogicalAndFilter andFilter = new LogicalAndFilter(filters);

    // 應用該過濾器並遍歷獲取到的元素

    collector = new FilteredElementCollector(doc);

    int wallCount = collector.WherePasses(andFilter).ToElementIds().Count;

    Trace.WriteLine(String.Format("  {0} Bearing Walls belong to DesignOption {1}.", wallCount, curId.IntegerValue));

  }

}

//============程式碼片段3-48:元素過濾器============

  FilteredElementCollector collector = new FilteredElementCollector(document);

  // 找到所有符合某種特定設計選項的牆

  optionICollection<ElementId> walls =    collector.OfClass(typeof(Wall)).ContainedInDesignOption(myDesignOptionId).ToElementIds();