GetTopLevelLink Method

GetTopLevelLink Method

api_architecture
Explorer Explorer
243 Views
2 Replies
Message 1 of 3

GetTopLevelLink Method

api_architecture
Explorer
Explorer

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;
}

 

 

0 Likes
Accepted solutions (1)
244 Views
2 Replies
Replies (2)
Message 2 of 3

TripleM-Dev.net
Advisor
Advisor
Accepted solution

Hi,

 

Retrieving a "GetTopLevelLink" doesn't mean it's the most-top link, it could be another nested link.

You need to retrieve the links that return NO toplink.

 

So check the Return ElementId, if it is ElementId.Invalid, then the passed ExternalResourceReference IS a toplevel link (has no parent)

 

See Remarks in this link: GetTopLevelLink Method (Document, ExternalResourceReference)  

 

Ps. So in the delete section you also pass invalid id's!

0 Likes
Message 3 of 3

api_architecture
Explorer
Explorer

Hi @TripleM-Dev.net.

 

that solved the problem! 

 

Thank you, much appreciated! 

0 Likes