Speed problems updating Parameter values

Speed problems updating Parameter values

Paulio
Advocate Advocate
742 Views
4 Replies
Message 1 of 5

Speed problems updating Parameter values

Paulio
Advocate
Advocate

My app is opening family files and adding parameters and setting the values. My problem is that it's taking over 20 seconds in total to set a single parameter value in a family with 12 types.

 

The code is nothing special and not doing anything complicated:

 

            Dim Param As FamilyParameter = m_manager.Parameter(ParamName)
            '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
                m_manager.CurrentType = t
                m_manager.Set(Param, Value)
            Next

Is it normal to take that long to set parameters? Is there anything else I should be doing to speed it up? Is there another way to set them?

I tried adding a SuspendUpdating block but as I'm using 2012 it's not vaild when RegenerationOption is Manual (which is the only option in 2012).

 

Thanks in advance.

0 Likes
743 Views
4 Replies
Replies (4)
Message 2 of 5

jeremytammik
Autodesk
Autodesk

Dear Paulio,

 

Are you opening the document as a main document or in the background?

 

Background might be faster.

 

Show the entire code.

 

Manual regen is correct, and SuspendUpdating is no use, correct.

 

Manual transaction mode is also essential.

 

Best regards,

Jeremy
--
Jeremy Tammik
Autodesk Developer Network -- http://www.autodesk.com/joinadn
The Building Coder -- http://thebuildingcoder.typepad.com




Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 5

Paulio
Advocate
Advocate

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

0 Likes
Message 4 of 5

jeremytammik
Autodesk
Autodesk

everything sounds fine to mee. sorry you are seeing such bad performance, then, but i'm afraid i have nothing more to suggest from my side...



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 5 of 5

Paulio
Advocate
Advocate

OK no problems.

 

As long as I'm not doing anything wrong.

 

Thanks again for your assitance.

0 Likes