Message 1 of 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello everyone,
I have been trying to write a code that would delete all Revit links from a document. I have managed to get it work, however when there are nested links(attached) into the linked files, my code fails as it is collecting their IDs too.
I have been trying to filter out the top-level links only by using GetTopLevelLink Method (Document, ExternalResourceReference), but not being able to get it working for a few hours and decided to ask for help.
Any assistance would be highly appreciated!
Here is the code so far:
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;
FilteredElementCollector typeCollector = new FilteredElementCollector(doc);
typeCollector.OfClass(typeof(RevitLinkType));
List<ElementId> linksTopLevel = new List<ElementId>();
ICollection<Element> linksElement = typeCollector.OfClass(typeof(RevitLinkType)).ToElements();
foreach(Element element in linksElement)
{
RevitLinkType linkType = element as RevitLinkType;
ExternalFileReference er2 = linkType.GetExternalFileReference();
IDictionary<ExternalResourceType, ExternalResourceReference> resourceReferences = linkType.GetExternalResourceReferences();
foreach(var var in resourceReferences)
{
ExternalResourceReference externalReference = var.Value;
ElementId linkTypeId1 = RevitLinkType.GetTopLevelLink(doc, externalReference);
linksTopLevel.Add(linkTypeId1);
}
}
using (Transaction t = new Transaction(doc))
{
t.Start("del");
doc.Delete(linksTopLevel);
t.Commit();
}
return Result.Succeeded;
}
Solved! Go to Solution.