Hi Jeremy, I'm opening the document in the background using the OpenDocumentFile method of the application. I checked that that was the case this morning and your post here seems to confirm that.
I'm then using code from the AutoParameter sample in the SDK to add the shared parameters then setting a single parameter in each type using this code:
''' <summary>
''' Sets shared parameter values in each type in the family
''' </summary>
''' <param name="Params">A dictionary of the parameters to set and their values</param>
''' <param name="doc">The current document</param>
''' <returns>true if all ok</returns>
''' <remarks></remarks>
Private Function SetSharedParamValues(Params As Dictionary(Of String, Object), doc As Document) As Boolean
'Start a transaction
Dim trans As Transaction = New Transaction(doc, Guid.NewGuid.GetHashCode.ToString)
trans.Start()
Try
prog.prog2.Maximum = m_manager.Types.Size
prog.prog2.Value = 0
'Get an enumerator for the dictionary so we can loop through all the entries
Dim keys As IEnumerator = Params.Keys.GetEnumerator
'Loop through all of the types in the family setting the value of the parameter in each type.
For Each t As FamilyType In m_manager.Types
keys.Reset()
prog.prog2.Value += 1
prog.lblMinorInfo.Text = String.Format("Setting Parameters in type {0}", t.Name)
System.Windows.Forms.Application.DoEvents()
'Set the current type in the family
m_manager.CurrentType = t
'Loop
While keys.MoveNext
'Get the parameter
Dim Param As FamilyParameter = m_manager.Parameter(keys.Current)
If Not Param Is Nothing Then
'Call the function to set it
SetParamaterValue(Param, Params(keys.Current))
End If
End While
Next
trans.Commit()
Return True
Catch ex As Exception
'Something went wrong so roll back the transaction
trans.RollBack()
log.WriteLine(String.Format("Error setting parameter value. {0}", ex.Message))
Return False
End Try
End Function
''' <summary>
''' Sets the parameter value checking the storage type and converting the value to suit
''' </summary>
''' <param name="p">the parameter to set</param>
''' <param name="Val">the value to set the parameter to</param>
''' <remarks></remarks>
Private Sub SetParamaterValue(p As FamilyParameter, Val As Object)
Try
Select Case p.StorageType
Case StorageType.None
Exit Sub
Case StorageType.Double
If TypeOf (Val) Is String Then
m_manager.Set(p, Double.Parse(TryCast(Val, String)))
Else
m_manager.Set(p, Convert.ToDouble(Val))
End If
Case StorageType.Integer
If TypeOf (Val) Is String Then
m_manager.Set(p, Integer.Parse(TryCast(Val, String)))
Else
m_manager.Set(p, Convert.ToInt32(Val))
End If
Case StorageType.String
m_manager.Set(p, Val.ToString)
Case StorageType.ElementId
If TypeOf (Val) Is ElementId Then
m_manager.Set(p, TryCast(Val, ElementId))
ElseIf TypeOf (Val) Is String Then
m_manager.Set(p, New ElementId(Integer.Parse(TryCast(Val, String))))
Else
m_manager.Set(p, New ElementId(Convert.ToInt32(Val)))
End If
End Select
Catch ex As Exception
log.WriteLine(String.Format("Failed to set parameter {0} to {1}{2}{3}", p.Definition.Name, Val.ToString, vbCrLf, ex.Message))
End Try
End Sub
The second function is based on code from Spiderinnet's blog site (I use the term 'based on' loosely of course!)
I've added these attributes to the top of my classes:
<Autodesk.Revit.Attributes.Regeneration(Attributes.RegenerationOption.Manual)>
<Autodesk.Revit.Attributes.Transaction(Attributes.TransactionMode.Manual)>
It takes almost two seconds for each type to set a single parameter with half the time being taken up just setting the current type in the family manager.
From what I've read, the RegenerationOption should do what I want i.e. not regen the document until I tell it to, it just doesn't seem to work.
Thanks for your time
Paul