Revit二次开发之“遍历过滤对象”

遍历过滤对象,得到想要的对象,是Revit二次开发一个重要的操作。
/*
 *Revit对象的标识
 * Revit对象的类名称
 * 直接可以用类名唯一标识出来:如:Wall,WallType,Floor,FloorType..
 * 不同对象,但是共用一个类:FamilyInstance,FamilySymbol,Family,Element
 * 
 * Rervit对象的Category名称
 * 通过Revit的Category可以唯一标识对象的类别
 * 门实例和门类型具有相同的Category
 * 
 * 其他属性
 * 如所在的楼层
 * 如某一个参数的值
 * 
 * 如何识别Revit中不同的对象
 * 根据标识来识别区分不同的对象
 * 只使用类名来判断
 * 它们的特点:
 * 片关能容纳其它对象或特定用途的类
 * 如:Wall,Floor,contFooting,CelingAndFloor等系统族的实例
 * 
 * 如果通过类名无法分别出来,需要联合对象的类别(Category)来判断
 * 门窗柱等对象没有专用的类来表示,都是FamilyInstance的实例
 * 用Category来判断其类别
 * 用枚举型的BuiltInCategory,来创建ElementCategoryFilter对象,支持多国语言
 
*/
[Transaction(TransactionMode.Automatic)]
[Regeneration(RegenerationOption.Automatic)]
public class GetElement : IExternalCommand
{
    
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
    {
        
try
        {
            
if (null == commandData)
            {
                
throw new ArgumentNullException("commandData");
            }

            UIApplication uiApp 
= commandData.Application;
            Document doc 
= uiApp.ActiveUIDocument.Document;
            Selection sel 
= uiApp.ActiveUIDocument.Selection;

            FilteredElementCollector collector 
= new FilteredElementCollector(doc);
            collector.OfClass(
typeof(Duct)).OfCategory(BuiltInCategory.OST_DuctCurves);//风管,直接用类名
            
//collector.OfClass(typeof(Wall)).OfCategory(BuiltInCategory.OST_Walls);//墙,直接用类名
            
//collector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Windows);//窗,类型是FamilyInstance
            IList<Element> lists = collector.ToElements();
            
string strMsg = string.Format("有{0}个", lists.Count);
            MessageBox.Show(strMsg);

            
//使用过滤器
            FilteredElementCollector collector2 = new FilteredElementCollector(doc);
            ElementClassFilter classFilter 
= new ElementClassFilter(typeof(FamilyInstance));
            ElementCategoryFilter catFilter 
= new ElementCategoryFilter(BuiltInCategory.OST_Doors);
            
//把过滤器联合起来
            LogicalAndFilter logicalFilter = new LogicalAndFilter(classFilter, catFilter);
            collector2.WherePasses(logicalFilter);
            IList
<Element> list2 = collector2.ToElements();
            strMsg 
= string.Format("有门{0}个", list2.Count);
            MessageBox.Show(strMsg);
        }
        
catch (Exception e)
        {
            messages 
= e.Message;
            
return Result.Failed;
        }

        
return Result.Succeeded;
    }
}
posted @ 2011-03-20 09:35  大气象  阅读(4084)  评论(8)    收藏  举报
http://www.tianqiweiqi.com