Loading, unloading and editing path to Revit referenced files is only possible to Revit Rvt file that is closed.
TransmissionData class provides the methods to do the job. If the rvt file is opened, this file cannot be edited by methods in TransmissionData class.
Here is a sample to change the Revit linked model path.
<code_begin>
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class Foo : IExternalCommand
{
static AddInId m_appId = new AddInId(new Guid("CF184371-E65A-4a35-BA4F-2959CA87A42D"));
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
Document doc = uidoc.Document;
FilePath location = new FilePath("C:\\file.rvt");
TransmissionData transData = TransmissionData.ReadTransmissionData(location);
if (transData != null)
{
// 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)
{
// we do not want to change neither the path nor the path-type
// we only want the links to be unloaded (shouldLoad = false)
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
{
Autodesk.Revit.UI.TaskDialog.Show("Unload Links", "The document does not have any transmission data");
}
return Result.Succeeded;
}
}
<code_end>
Via setting the four parameter of SetDesiredReferenceData() method, we can also set a loaded linked file to be unload, and vice versa