How much impact does a document.regenerate have?

How much impact does a document.regenerate have?

Anonymous
Not applicable
934 Views
1 Reply
Message 1 of 2

How much impact does a document.regenerate have?

Anonymous
Not applicable

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?

935 Views
1 Reply
Reply (1)
Message 2 of 2

matthew_taylor
Advisor
Advisor

Hi @Anonymous,

I'm not 100 clear on what your issue is, as you've left out the section of code that actually has the issue.

 

I'd say you probably just need to 'activate' the familysymbol once you have duplicated it. Not sure why you'd need to duplicate it often...the whole point of familysymbols is to use the same one often. That may be explained by your code. 🙂

 

To answer your second question, Regenerate doesn't take long if within your existing transaction, but if you're adding a transaction especially for it it may take time.

 

If my points don't solve your issue, please post the 'for' loop code.

 

Cheers,

 

-Matt


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?