Search code examples
c#revit-api

Revit 2022 API: Deleted Links Still Visible in UI Despite Successful Removal from Model


Context: I'm using Revit 2022 and developing a C# add-in using the Revit API. My goal is to delete specific link instances from a Revit project. The links I want to delete are identified by having "COM" in their name.

Issue: The script successfully deletes the links from the model, as confirmed by re-running the script and not finding these links anymore. However, the UI still shows these links as if they are present in the project. This discrepancy between the model and the UI is confusing. I expect the UI to reflect the changes made by the API, i.e., the deleted links should not be visible in the UI.

Code: Here is the function I'm using to delete the links:

public static void ProcessAndRemoveLinks(Document doc)
        {
            List<string> deletedLinkNames = new List<string>();

            // Collect all Revit link instances in the document
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            var revitLinks = collector.OfClass(typeof(RevitLinkInstance)).Cast<RevitLinkInstance>();

            foreach (RevitLinkInstance linkInstance in revitLinks)
            {
                // Check if the name contains 'COM'
                if (linkInstance.Name.Contains("_COM"))
                {
                    RevitLinkType linkType = doc.GetElement(linkInstance.GetTypeId()) as RevitLinkType;
                    if (linkType != null)
                    {
                        // Wrap unload and delete operations in transactions with error handling
                        try
                        {
                            using (Transaction trans = new Transaction(doc, "Unload and Delete Link"))
                            {
                                // Unload the link
                                linkType.Unload(null);


                                trans.Start();
                                // Delete the link instance
                                deletedLinkNames.Add(linkInstance.Name);
                                doc.Delete(linkInstance.Id);
                                // Regenerate the document to reflect changes
                                doc.Regenerate();
                                trans.Commit();
                            }
                        }
                        catch (Exception ex)
                        {
                            TaskDialog.Show("Error", $"Error processing link '{linkInstance.Name}': {ex.Message}");
                        }
                    }
                }
            }

Attempts to Resolve: I've tried regenerating the document (doc.Regenerate()) after deletion. I also attempted refreshing the active view (uiDoc.RefreshActiveView()). Ensured that each deletion is wrapped in a separate transaction and committed properly.

Question: Why are the deleted links still visible in the Revit UI, and how can I ensure that the UI accurately reflects the changes made by my add-in? Is there a specific method or procedure in the Revit API to update the UI after such operations, especially for link deletions?

I've tried regenerating the document (doc.Regenerate()) after deletion. I also attempted refreshing the active view (uiDoc.RefreshActiveView()). Ensured that each deletion is wrapped in a separate transaction and committed properly.


Solution

  • When you link a file to a Revit project, it creates both a link instance and a corresponding type for that link instance. I suggest that you remove both the link instance and the corresponding type.