Using the WriteTransmissionData Method

I presented a sample to using the TransmissionData class to list linked files, and Mike Caruso submitted a comment asking how to use it to set a new path on a RVT file. Furthermore, Guming recently asked how to make use of the ExternalFileReference class. Here is a sample answering both of these questions:

Question: Can you demonstrate how I would load, unload and make changes to the path of linked Revit files?

Answer: Loading, unloading and editing path to referenced RVT files is possible using methods on the TransmissionData class. Please note that the host RVT file must be closed in order to be editable by these methods.

The Revit API help file RevitAPI.chm description of the TransmissionData class includes a sample code snippet defining the aptly named method UnloadRevitLinks to unload all Revit links.

Here is a sample external command based on that to change the Revit linked model path:

/// <summary>
///  This command will change the path of all linked
///  Revit files the next time the document at the 
///  given location is opened. 
///  Please refer to the TransmissionData reference
///  for more details.
/// </summary>
[Transaction( TransactionMode.ReadOnly )]
public class CmdChangeLinkedFilePath : IExternalCommand
{
  public Result Execute(
    ExternalCommandData commandData,
    ref string message,
    ElementSet elements )
  {
    UIApplication uiapp = commandData.Application;
    UIDocument uidoc = uiapp.ActiveUIDocument;
    Application app = uiapp.Application;
    Document doc = uidoc.Document;
 
    FilePath location = new FilePath( "C:/file.rvt" );
 
    TransmissionData transData
      = TransmissionData.ReadTransmissionData(
        location );
 
    if( null != transData )
    {
      // Collect all (immediate) external 
      // references in the model
 
      ICollection<ElementId> externalReferences
        = transData.GetAllExternalFileReferenceIds();
 
      // Find every reference that is a link
 
      foreach( ElementId refId in externalReferences )
      {
        ExternalFileReference extRef
          = transData.GetLastSavedReferenceData(
            refId );
 
        if( extRef.ExternalFileReferenceType
          == ExternalFileReferenceType.RevitLink )
        {
          // Change the path of the linked file,
          // leaving everything else unchanged:
 
          transData.SetDesiredReferenceData( refId,
            new FilePath( "C:/MyNewPath/cut.rvt" ),
            extRef.PathType, true );
        }
      }
 
      // Make sure the IsTransmitted property is set
 
      transData.IsTransmitted = true;
 
      // Modified transmission data must be saved 
      // back to the model
 
      TransmissionData.WriteTransmissionData(
        location, transData );
    }
    else
    {
      TaskDialog.Show( "Unload Links",
        "The document does not have"
        + " any transmission data" );
    }
    return Result.Succeeded;
  }
}

By setting the four parameters of SetDesiredReferenceData method, you can also cause a loaded linked file to be unloaded, and vice versa.

Here is version 2012.0.93.0 of The Building Coder samples including the new CmdChangeLinkedFilePath command.

Response: Thank you! That worked like a charm.