The definition file provides access to shared parameters.
Use the following steps to gain access to the definition file and its parameters:
The following classes and methods in the Autodesk.Revit.DB namespace provide access to shared parameters using the Revit API.
Because the definition file is a text file, it can be created manually or using code.
Code Region 22-3: Creating a shared parameter file |
private void CreateExternalSharedParamFile(string sharedParameterFile) { System.IO.FileStream fileStream = System.IO.File.Create(sharedParameterFile); fileStream.Close(); } |
Since Revit can have many shared parameter files, it is necessary to specifically identify the file and external parameters you want to access. The following two procedures illustrate how to access an existing shared parameter file.
Set the shared parameter file path as the following code illustrates, then invoke the Application.OpenSharedParameterFile() method.
Code Region 22-4: Getting the definition file from an external parameter file |
private DefinitionFile SetAndOpenExternalSharedParamFile(Autodesk.Revit.ApplicationServices.Application application, string sharedParameterFile) { // set the path of shared parameter file to current Revit application.SharedParametersFilename = sharedParameterFile; // open the file return application.OpenSharedParameterFile(); } |
The following sample illustrates how to traverse the parameter entries and display the results in a message box.
Code Region 22-5: Traversing parameter entries |
private void ShowDefinitionFileInfo(DefinitionFile myDefinitionFile) { StringBuilder fileInformation = new StringBuilder(500); // get the file name fileInformation.AppendLine("File Name: " + myDefinitionFile.Filename); // iterate the Definition groups of this file foreach (DefinitionGroup myGroup in myDefinitionFile.Groups) { // get the group name fileInformation.AppendLine("Group Name: " + myGroup.Name); // iterate the difinitions foreach (Definition definition in myGroup.Definitions) { // get definition name fileInformation.AppendLine("Definition Name: " + definition.Name); } } TaskDialog.Show("Revit",fileInformation.ToString()); } |
The following sample shows how to change the parameter definition group owner.
Code Region 22-6: Changing parameter definition group owner |
private void ReadEditExternalParam(DefinitionFile file) { // get ExternalDefinition from shared parameter file DefinitionGroups myGroups = file.Groups; DefinitionGroup myGroup = myGroups.get_Item("MyGroup"); if (myGroup != null) { ExternalDefinition myExtDef = myGroup.Definitions.get_Item("MyParam") as ExternalDefinition; if (myExtDef != null) { DefinitionGroup newGroup = myGroups.get_Item("AnotherGroup"); if (newGroup != null) { // change the OwnerGroup of the ExternalDefinition myExtDef.OwnerGroup = newGroup; } } } } |