简体   繁体   中英

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

I would like to add a plug in that reads in a file of data that has a string of RevitIds and paints them.

I can't figure out how to find a given element in Revit based on a string elementId using C#.

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

I know that this gives me a document but I don;t know how to get all of the ids. I was thinking of having a foreach loop that checks the string of element id with that of all in the document until it finds a match. Then, I can manipulate it.

One way is to use a FilteredElementCollector to iterate through specific element types to get their elementId's.

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

followed by(as you suggested):

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

Modified version:

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

followed by(as you suggested):

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

If you are thinking about iterating through ALL elements I suggest looking at this blog post from The Building Coder: Do Not Filter For All Elements

You can use the Document.GetElement method to get an element by it's ElementId . The answer to your question depends a bit on if you have a UniqueId or an ElementId in string representation. Check here for some clarification: https://boostyourbim.wordpress.com/2013/11/18/getting-an-element-from-a-string-id/

Assuming you have an ElementId (not a GUID, just a number), you can do this:

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

Or even shorter:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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