繁体   English   中英

如何使用C#获取Revit中所有元素的列表

[英]How to get a list of all elements in a revit with c#

我想在其中添加一个插件,该插件读取具有RevitIds字符串的数据文件并绘制它们。

我无法弄清楚如何使用C#基于字符串elementId在Revit中找到给定元素。

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

我知道这给了我一个文档,但我不知道如何获取所有ID。 我正在考虑有一个foreach循环,该循环检查元素id的字符串与文档中所有元素的字符串,直到找到匹配为止。 然后,我可以操纵它。

一种方法是使用FilteredElementCollector遍历特定元素类型以获得其elementId。

FilteredElementCollector docCollector = new FilteredElementCollector(document).OfCategory(BuiltInCategory.OST_Walls);

其次(根据您的建议):

foreach(Element el in docCollector)
{
ElementId elID = el.Id;
//....
}

修改版本:

List<ElementId> ids = new FilteredElementCollector(document).OfCategory(BuiltInCategory.OST_Walls).ToElementIds().ToList();

其次(根据您的建议):

foreach(ElementId elId in ids)
{
//....
}

如果您想遍历所有元素,建议您阅读The Building Coder中的此博客文章:不要过滤所有元素

您可以使用Document.GetElement方法通过ElementId获取元素。 问题的答案在ElementId取决于您是否具有字符串表示形式的UniqueIdElementId 请在此处查看一些说明: https//boostyourbim.wordpress.com/2013/11/18/getting-an-element-from-a-string-id/

假设您有一个ElementId (不是GUID,而是一个数字),则可以执行以下操作:

int idInt = Convert.ToInt32(idAsString);
ElementId id = new ElementId(idInt);
Element eFromId = doc.GetElement(id);

甚至更短:

Element element = doc.GetElement(new ElementId(Convert.ToInt32(idAsString)));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM