Monday, February 23, 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
Debug.Print(currElement.Name.ToString)

'check for type and make mods
If TypeOf currElement Is Autodesk.Revit.Elements.TextNote Then
Dim textNote As Autodesk.Revit.Elements.TextNote = currElement
textNote.AddLeader(Enums.TextNoteLeaderTypes.TNLT_STRAIGHT_R)
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 comments:

Guy said...

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

doc.BeginTransaction()
textNote.AddLeader(Enums....)
doc.EndTransaction();

Guy

Post a Comment

Comments are moderated so you may have to wait a while before your comment appears :)

Thanks for your contribution!