Here is a handy little app we wrote for batch writing all shared parameters located in a shared parameter file to the currently open family. This app will allow for quick consistant adding of the same params to families without having to add them manually which can be very painful.
I have created a very simple form called "frmAddParameter" which looks like this:
This program consist of two .vb files. "frmAddParameter.vb" contains all the code for reading and writing the parameters and "Commands.vb" which implements the iexternalcommand.execute method below:
Public Function Execute(ByVal commandData As ExternalCommandData, _
ByRef message As String, _
ByVal elements As ElementSet) As Result Implements IExternalCommand.Execute
Me.RevitApp = commandData.Application.Application
Me.RevitDoc = commandData.Application.ActiveUIDocument.Document
Using myTrans As New Transaction(Me.RevitDoc, "Shared Parameter Write")
myTrans.Start()
Try
If (Me.RevitDoc.IsFamilyDocument) Then
Dim newform As New frmAddParameter(commandData)
newform.Show()
Else
End If
myTrans.Commit()
Catch ex As Exception
TaskDialog.Show("Error", "Writing the Shared Parameters Failed")
Return Result.Failed
End Try
End Using
Return Result.Succeeded
End Function
The only purpose of this method is to declare the form and open it for the user. Now onto the form code... where the fun stuff happens!
To the right side of the text box there is a button "btnOpenFile" which will fire a open file dialogue box which the user then selects the shared parameter to use. I like to set some preset options on this function so that txt files are already filtered for the user:
Private Sub btnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile.Click
'--------------------File Dialogue Options---------------------
Dim objFileDialog As New OpenFileDialog()
objFileDialog.InitialDirectory = "c:\"
objFileDialog.Filter = "Shared Parameter File (*.txt)|*.txt"
objFileDialog.RestoreDirectory = True
objFileDialog.Multiselect = False
objFileDialog.Title = "Select a Shared Parameter File"
objFileDialog.ShowDialog()
txtSharedParameterFile.Text = objFileDialog.FileName
End Sub
Before you write the parameters you must read them, we accomplish this with the following method. This method is then called in the final subroutine.
Private Function IBDSharedParameterFile(ByVal application As Autodesk.Revit.ApplicationServices.Application, ByVal sharedParameterFilePath As String) As DefinitionFile
application.SharedParametersFilename = sharedParameterFilePath
Return application.OpenSharedParameterFile
End Function
This next method will loop through all parameters located in the passed in shared parameter file from the previous method and write each one to the family:
Public Sub WriteSharedParams(ByVal doc As Document)
If Not (doc.IsFamilyDocument) Then
TaskDialog.Show("Error", "This is not a Family File")
Return
End If
If (doc.FamilyManager.CurrentType Is Nothing) Then
TaskDialog.Show("Error", "No Valid Types in this Family")
Return
End If
Dim myDefinitionFile As DefinitionFile = Me.IBDSharedParameterFile(Me.RevitApp, txtSharedParameterFile.Text)
Dim groupIter As DefinitionGroupsBaseIterator = myDefinitionFile.Groups.ForwardIterator()
Try
While (groupIter.MoveNext())
Dim group As DefinitionGroup = groupIter.Current()
Dim iter = group.Definitions.GetEnumerator()
While (iter.MoveNext())
Dim def As Definition = iter.Current()
Dim idParameter As FamilyParameter = doc.FamilyManager.AddParameter(def, BuiltInParameterGroup.PG_GRAPHICS, False)
End While
End While
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
You will notice that we use two loops to iterate through all the parameters in the predefined file. We must first iterate through the groups, then for each parameter in each group we write that parameter to the currently opened family file.
The source code can be downloaded here. Feel free to leave a comment below.
SG
This is awesome! Except it doesn't put the parameters under the correct group. When I run it, all parameters are added under "Graphics". Help?
Posted by: philbert943 | 06/28/2011 at 04:09 PM
Oh I see now, it's "BuiltInParameterGroup.PG_Graphics" putting them all there. So is the group the parameter belongs to not part of the shared parameter file?
Posted by: philbert943 | 06/28/2011 at 05:08 PM
Hi Phil thanks for the comment.
I believe your correct, in this example we have the group hard coded. You would want to use a variable for the group name and have that variable reset and used to write each shared param from the file.
Let me know if you get stuck and I'll try to help you out.
Posted by: Steve Germano | 06/28/2011 at 08:34 PM
Still hung up on the group arguement ("BuiltInParameterGroup.PG_Graphics"). It's seems you have to pick from the list. Isn't that information stored in the shared parameter file (5th item)?
Posted by: philbert943 | 07/01/2011 at 02:40 PM
Hi Phil - Thats correct, we designed the app that way for the use of our content creation team. Feel free to change that and use the app in any way you would like. We will have a much more expanded version of this app available for download in the future. For now this code was intended to show how you can read from a shared parameter file and batch write to a family. Hope that helps.
Posted by: Steve Germano | 07/16/2011 at 12:30 PM
Sounds really cool...How is this intalled?
Posted by: Yaoho | 07/27/2011 at 06:27 PM
Hi Yaoho - Once you download and unzip go to the following folder: IBD Write Shared Parameters\IBD Write Shared Parameters\bin\Debug
In this folder you will find the IBD Write Shared Parameters.dll file. You may use the app by loading this dll into the Revit Add-In Manager.
If you do not already have the add-in manager installed you can install it from this post here:http://ibdrg.typepad.com/ibd-resource-group/2011/05/how-to-start-programming-the-revit-api-part-2.html
The add-in manager is part of the SDK supplied from the Autodesk API team. If enough people find the app useful I will try to add it to a ribbon button and supply a installer for it.
Hope that helps, let me know if you have any questions!
Posted by: Steve Germano | 07/27/2011 at 07:26 PM
Thanks alot for the quick response and all of the info. Unfornately I still can't get it to work.
Here are the steps that I have followed:
1. I installed the Add In Manager
2. I then went to the Add ins tab>External Tools>Addin Manager(Manual Mode)>Load>i selected the .dll file you specified above>close the Add in manager>Close then Open Revit again and the tool doesn't appear on the ribbon. Your help is truly appreciated. Thanks.
Posted by: Yaoho | 07/28/2011 at 09:21 AM
Sorry i forgot a step. After i loaded the .dll file through the add in manager i save it then i closed it. Thanks again.
Posted by: Yaoho | 07/28/2011 at 09:27 AM
Hi Yaoho, your almost there! You missed he step to actually run the app. Once you load the DLL into the add-in manager you will see two subcategories, you will want to select the "ibd.Revit.Commands" subcategory and then click "RUN" in the add-in manager.
This will instantly run the app, there is not ribbon button for this command, you have to run it from the add-in manager each time you want to use it. This is a typical workflow for developers to test their apps without having to program a ribbon button also.
I hope that gets you up and running and batching some shared params! :)
Cheers,
Steve
Posted by: Steve Germano | 07/28/2011 at 08:10 PM
Hi, great add-in but i'm having some trouble. Did a little test and got it to work ok, but now coming up against "No valid types in this family" error message, and can't figure out what is causing it. Doesn't seem to be the family category (in revit), datacategory or groupname (in excel file). Any suggestions?
Posted by: Mark | 11/18/2011 at 02:44 AM
Hi Mark,
Glad you found it useful. You need to have at least 1 family type present or the API cannot write parameters to the family. Try to just add a type then rerun the program and it should work!
Be sure to check the blog coming up here soon, we are releasing a ton of apps early next year with lots of freebies!
Posted by: Steve Germano | 11/18/2011 at 01:14 PM