这个问题看似比较复杂,首先要考虑数据库中的对象时具有几何属性,另外还要判断是否在当前视图可见。 所幸的是Revit API提供了一个非常好的FilteredElementCollector 的重载构造函数,可以方便简单高效获得所有的可见对象 - [csharp] view plaincopy
- public FilteredElementCollector(
- Document document,
- ElementId viewId
- )
复制代码
第一个参数指明从哪个文档或模型文件,第二个参数设置希望获取哪个视图中可见的对象。
下面是实现这个功能的代码。关键代码只有一行。 酷吧! [TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)] - <p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; "></p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; "></p><div style="line-height: 26px; font-family: 'Courier New'; background-color: white; color: black; font-size: 8pt; "><p><span style="color: blue; line-height: 16px; ">public</span><span style="line-height: 16px; "><span style="color: blue; ">class</span><span style="color: rgb(43, 145, 175); ">RevitCommand</span> :<span style="color: rgb(43, 145, 175); ">IExternalCommand</span></span></p><p><span style="line-height: 16px; ">{</span></p><p><span style="line-height: 16px; "> </span><span style="color: blue; line-height: 16px; ">public</span><span style="line-height: 16px; "><span style="color: rgb(43, 145, 175); ">Result</span> Execute(<span style="color: rgb(43, 145, 175); ">ExternalCommandData</span> commandData,<span style="color: blue; ">ref</span><span style="color: blue; ">string</span> messages,<span style="color: rgb(43, 145, 175); ">ElementSet</span> elements)</span></p><p><span style="line-height: 16px; "> {</span></p><p> </p><p><span style="line-height: 16px; "> </span><span style="color: rgb(43, 145, 175); line-height: 16px; ">UIApplication</span><span style="line-height: 16px; "> app = commandData.Application;</span></p><p><span style="line-height: 16px; "> </span><span style="color: rgb(43, 145, 175); line-height: 16px; ">Document</span><span style="line-height: 16px; "> doc = app.ActiveUIDocument.Document;</span></p><p> </p><p><span style="line-height: 16px; "> </span><span style="color: rgb(43, 145, 175); line-height: 16px; ">FilteredElementCollector</span><span style="line-height: 16px; "> collector =</span><span style="color: blue; line-height: 16px; ">new</span><span style="line-height: 16px; "><span style="color: rgb(43, 145, 175); ">FilteredElementCollector</span>(doc, doc.ActiveView.Id);</span></p><p><span style="line-height: 16px; "> </span><span style="color: rgb(43, 145, 175); line-height: 16px; ">TaskDialog</span><span style="line-height: 16px; ">.Show(</span><span style="color: rgb(163, 21, 21); line-height: 16px; ">"visible element"</span><span style="line-height: 16px; ">,</span><span style="color: rgb(163, 21, 21); line-height: 16px; ">"number is "</span><span style="line-height: 16px; "> + collector.ToElementIds().Count.ToString());</span></p><p> </p><p><span style="line-height: 16px; "> </span><span style="color: blue; line-height: 16px; ">return</span><span style="line-height: 16px; "><span style="color: rgb(43, 145, 175); ">Result</span>.Succeeded ;</span></p><p><span style="line-height: 16px; "> }</span></p><p><span style="line-height: 16px; ">}</span></p></div>
复制代码返回值就存储在collector变量中。运行结果会在对话框显示可见对象的数量 转载请复制以下信息:原文链接: http://blog.csdn.net/joexiongjin/article/details/7641140作者: 叶雄进 , Autodesk ADN
|