Monday, 23 February 2009

Looping through selected elements

Fred emailed and asked about how to iterate through a selection set and make a modification to each element in that set, in particular regarding adding a leader to selected text notes.

The API provides the ElementSetIterator class. I used its method MoveNext in a While loop, and this takes us through each selected element, on which you can then test for type and act accordingly.

Public Sub LoopSelectedElements()

Dim activeDoc As Document = revitApp.ActiveDocument

Dim selectionIterator As Autodesk.Revit.ElementSetIterator
selectionIterator = revitApp.ActiveDocument.Selection.Elements.ForwardIterator

'loop through iterator
While selectionIterator.MoveNext

Dim currElement As Autodesk.Revit.Element = selectionIterator.Current

'see what elements we've found

'check for type and make mods
If TypeOf currElement Is Autodesk.Revit.Elements.TextNote Then
Dim textNote As Autodesk.Revit.Elements.TextNote = currElement
End If

End While

End Sub

I didn't really test the textNote.AddLeader part. Fred reports that it works but the new leader only becomes visible when you move the textnote. Something to look into when I've got a spare five mins :)

1 comment:

  1. Probably because you didn't wrap it in a transaction.To trigger an update on the screen you need to use a transaction. There's little performance penalty but if there are a lot of textnotes you might want to wrap the loop in one transaction




Comments are moderated, so you'll have to wait a little bit before they appear!