Dear Jeremy,
What I need to do is to:
1) load a revit link file to the current document;
2) unload the link file;
3) reload the link file.
Now the codes can work.
The codes are:
1) load a revit link file to the current document;
-------------------------------------------------------------------------
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
class OpenLinkDoc : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
// get document objects
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
// Get the handle of current document.
Document doc = uiDoc.Document;
using (Transaction transaction = new Transaction(doc))
{
if (transaction.Start("create a link file") == TransactionStatus.Started)
{
string linkFile = FileManager.GetPath("link_file");
CreateRevitLink(doc, linkFile);
if (TransactionStatus.Committed != transaction.Commit())
{
TaskDialog.Show("Failure", "Transaction could not be comitted");
}
}
else
{
transaction.RollBack();
return Result.Failed;
}
}
return Result.Succeeded;
}
// ---------------------------------------
// Code Region: Create new Revit Link
// create a new Revit link type and load the linked document
// ---------------------------------------
public bool CreateRevitLink(Document doc, string pathName)
{
FilePath path = new FilePath(pathName);
RevitLinkOptions options = new RevitLinkOptions(false);
RevitLinkLoadResult result = RevitLinkType.Create(doc, path, options);
RevitLinkInstance.Create(doc, result.ElementId);
return true;
}
}
------------------------------------------------------------------------- end of 1)
2) unload the link file;
-------------------------------------------------------------------------
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
class UnloadLinkedDoc : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
// get document objects
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
// Get the handle of current document.
Document doc = uiDoc.Document;
try
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(RevitLinkInstance));
string tmpstr = "";
foreach (Element elem in collector)
{
RevitLinkInstance instance = elem as RevitLinkInstance;
Document linkDoc = instance.GetLinkDocument();
RevitLinkType type = doc.GetElement(instance.GetTypeId()) as RevitLinkType;
type.Unload(null);
}
}
catch (Exception e)
{
TaskDialog.Show("in doc:", "err" + e.Message);
message = e.Message;
return Result.Failed;
}
return Result.Succeeded;
}
}
------------------------------------------------------------------------- end of 2)
3) reload the link file.
-------------------------------------------------------------------------
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
class ReloadLinkedDoc : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
// get document objects
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
// Get the handle of current document.
Document doc = uiDoc.Document;
try
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(RevitLinkInstance));
foreach (Element elem in collector)
{
RevitLinkInstance instance = elem as RevitLinkInstance;
Document linkDoc = instance.GetLinkDocument();
RevitLinkType type = doc.GetElement(instance.GetTypeId()) as RevitLinkType;
type.Load();
}
}
catch (Exception e)
{
TaskDialog.Show("in doc:", "err" + e.Message);
message = e.Message;
return Result.Failed;
}
return Result.Succeeded;
}
}
------------------------------------------------------------------------- end of 3)
Regards,
Davix