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.
Hello,
First of all i'm sorry for my poor english (French don't like to learn other languages...). I'm new in making some plugin for revit and I use your web site all days to improve myself. But i have a problem with external file management. I'm able to ready links but when i want to change the path it makes me an exception.
Autodesl.Revit.Exceptions.InvalidOperationException: Failed to transmit TransmissionData.
It happens when revit performs this command : transData.SetDesiredReferenceData(...
Do you have an idee of what i have to do?
Thank you,
V.
Posted by: Valentin | February 14, 2012 at 05:35
Dear Valentin,
Don't worry, your English looks absolutely fine to me!
I am glad to hear that the blog is useful, and thank you for your appreciation.
Sorry, I have no idea what might be going wrong at your end.
The code above should work and make a successful call to the SetDesiredReferenceData method. Are you using it differently somehow?
Cheers, Jeremy.
Posted by: Jeremy Tammik | February 15, 2012 at 07:15
Dear Jeremy,
I finaly found the problem. The code (or something else i did) only work on a closed file.
Thank you for all you make for beginners.
thebuildingcoder > autodesk.
V.
Posted by: Valentin | February 16, 2012 at 08:27
Dear Valentin,
Thank you for the update and appreciation!
Congratulations on solving your issue!
Cheers, Jeremy.
Posted by: Jeremy Tammik | February 23, 2012 at 06:05
Hello,
I know it's quite an old post but I'm trying to do a little plugin that basically copy a template project to another location and renames all the files to match the new project and so on.
I was able (thanks to your code) to manage the new paths if I don't rename the files but I cannot find how to change also the name of the external reference.
The result till now is that if I leave the ShouldLoad setted to True Revit cannot load the project (it gives me a Data Error).
Thank you
Stefano
Posted by: Stefano | May 15, 2015 at 05:19
Dear Stefano,
Thank you for your appreciation.
I am glad that the code presented here helped with part of your task.
Are you saying that TransmissionData.SetDesiredReferenceData method does not work in your case?
Can you provide a reproducible case for that, so we can explore it further?
http://thebuildingcoder.typepad.com/blog/about-the-author.html#1b
Thank you!
Cheers, Jeremy.
Posted by: Jeremy Tammik | May 19, 2015 at 02:50