|
我们经常会添加自己的参数到对象中,一个参数可以添加到多个类别的对象。2011提供的过滤功能可以用参数的值来进行过滤。
下面代码可以对自定义参数按照参数值进行过滤。
- <p><font color="#333333" face="Arial"><span style="line-height: 26px;"></span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">
- </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">[c-sharp] view plaincopy</span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using System; </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using System.Collections.Generic; </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using System.Text; </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">//using System.Windows.Forms; </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using Autodesk.Revit .DB; </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using Autodesk.Revit.UI; </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using Autodesk.Revit .ApplicationServices; </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using Autodesk.Revit.Attributes ; </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> [TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)] </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> [RegenerationAttribute(Autodesk.Revit.Attributes.RegenerationOption.Manual)] </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> public class ParamFilterTest : IExternalCommand </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> { </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> public Result Execute( </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> ExternalCommandData commandData, </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> ref string message, </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> ElementSet elements) </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> { </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> UIApplication uiapp = commandData.Application; </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> UIDocument uidoc = uiapp.ActiveUIDocument; </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> Application app = uiapp.Application; </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> Document doc = uidoc.Document; </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> Wall wall = uidoc.Selection.PickObject( </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> Autodesk.Revit.UI.Selection.ObjectType.Element) </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> .Element as Wall; </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> Parameter parameter = wall.get_Parameter( </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> "MyShared"); </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> ParameterValueProvider pvp </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> = new ParameterValueProvider(parameter.Id); </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> FilterStringEquals strEquals = new FilterStringEquals(); </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> FilterStringRule sRule = new FilterStringRule(pvp, strEquals, "MyValue", true); </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> ElementParameterFilter filter </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> = new ElementParameterFilter(sRule); </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> FilteredElementCollector collector </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> = new FilteredElementCollector(doc); </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> collector.WherePasses(filter); </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> TaskDialog.Show("count", collector.ToElementIds().Count.ToString()); </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> return Result.Succeeded; </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> } </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;"> } </span></font></p>
复制代码 作者:叶雄进文章来源:http://blog.csdn.net/joexiongjin/article/category/782739
|
|