Reputation: 15
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.
Upvotes: 1
Views: 12389
Reputation: 169
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
Upvotes: 2
Reputation: 70314
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)));
Upvotes: 2