How much impact does a document.regenerate have?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Background information
I have created an Addin which will generate windows of a user defined width and height.
- The user can choose between two windows, window A and window B.
- He then enters the width and height and places the window
Codewise, I first check if the required window exists in the project.
oFamily = oUtil.FindElementByName(oDoc, GetType(Family), sFamilyName)
If the family does not exist in the project, I will load that family into the project.
Then, I check if there's already an instance of that family, with the code by Matthew Taylor to search for a specific familysymbol (findelementbyname-for-a-specific-familysymbol)
Public Function GetFamilySymbol(ByVal famName As String, ByVal famSymbolName As String) As DB.FamilySymbol
Dim famSymbol As DB.FamilySymbol = _
New DB.FilteredElementCollector(m_doc).OfClass(GetType(DB.FamilySymbol)).Cast(Of DB.FamilySymbol).Where(Function(fs) IsMatching(fs, famName, famSymbolName)).FirstOrDefault
Return famSymbol
End Function
Private Shared Function IsMatching(ByVal fs As DB.FamilySymbol, ByVal famName As String, ByVal famSymbolName As String) As Boolean
Return fs.Family.Name.Equals(famName, StringComparison.InvariantCultureIgnoreCase) AndAlso fs.Name.Equals(famSymbolName, StringComparison.InvariantCultureIgnoreCase)
End Function
If it doesn't exists, I will use the Duplicate function to create a new instance.
The user can then place the window(s)
uiDoc.PromptForFamilyInstancePlacement(oFamilyType)
The problem
I am using a For Each Loop to replace all selected elements, checking each time if the target family symbol is in the project. But when replacing multiple elements for one and the same family symbol (which does not previously exists in the project), I am getting an error on the second loop. The code halts at the "oFamilySymbol.Duplicate(sNewFamilyTypeName)". The error makes sense, because that new family symbol has been created in the first loop.
But the function "GetFamilySymbol" should have noticed that the required family symbol is present in the project...or the document has not been properly updated with the information about the newly placed family symbol from the first loop.
The Fix
I have fixed this by adding this piece of code:
If oDoc.IsModified And bRegenerateDoc Then
Dim trans4 As Transaction = New Transaction(oDoc)
trans4.Start("JAZO - Updating document")
oDoc.Regenerate()
trans4.Commit()
bRegenerateDoc = False
End If
The boolean "bRegenerateDoc" is to regenerate the document just once.
According to this post (calling-document-regenerate-inside-transaction), a transaction.commit should call document.regenerate...but my code proves the opposite (my code will crash if I do not call document.regenerate explicitly).
So somehow, the "document" does not see the newly generated family symbol (which is already inserted into the model) unless a specific document.regeneration is being called.
The Question(s)
1. Have I encountered a bug in the Revit API?
2. While I have fixed the error...how much impact does the document.regenerate have on large models?