Delete Views, Remove Linked Files from Revit File

Delete Views, Remove Linked Files from Revit File

dwightduronidavy
Enthusiast Enthusiast
226 Views
2 Replies
Message 1 of 3

Delete Views, Remove Linked Files from Revit File

dwightduronidavy
Enthusiast
Enthusiast

Dear all,

I have created the below application module macro to undertake the following tasks:
delete all views; legends; schedules (except with suffix "_!")
remove all linked files (including instances)

However, the revision history gets deleted from Landing Sheet (which should be retained)
and CAD Formats unable to be removed.

Any help would be very much appreciated.
Thanks




using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using System.Collections.Generic;
using System.Linq;

namespace DDA_CSharpModule1
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("EB0917F2-8040-4F52-9F49-945C816A26E4")]
    public partial class ThisApplication
    {
        private void Module_Startup(object sender, EventArgs e)
        {
        }

        private void Module_Shutdown(object sender, EventArgs e)
        {
        }

        #region Revit Macros generated code

        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Module_Startup);
            this.Shutdown += new System.EventHandler(Module_Shutdown);
        }

        #endregion

        public void DeleteViewsSheetsRemoveLinks()
        {
            UIApplication uiApp = new UIApplication(this.Application);
            UIDocument uiDoc = uiApp.ActiveUIDocument;
            Document doc = uiDoc.Document;

            using (Transaction transaction = new Transaction(doc, "Delete Views, Schedules, and Links"))
            {
                try
                {
                    transaction.Start();

                    // Filter views to delete
                    List<View> viewsToDelete = new FilteredElementCollector(doc)
                        .OfClass(typeof(View))
                        .Cast<View>()
                        .Where(view => view.IsValidObject && !view.IsTemplate && !view.Name.EndsWith("_!") &&
                                       (view.ViewType == ViewType.FloorPlan ||
                                        view.ViewType == ViewType.Section ||
                                        view.ViewType == ViewType.ThreeD ||
                                        view.ViewType == ViewType.DraftingView ||
                                        view.ViewType == ViewType.Legend ||
                                        view.ViewType == ViewType.Elevation ||
                                        view.ViewType == ViewType.Schedule))
                        .ToList();

                    foreach (View view in viewsToDelete)
                    {
                        if (view.IsValidObject)
                        {
                            try
                            {
                                doc.Delete(view.Id);
                            }
                            catch (Exception ex)
                            {
                                TaskDialog.Show("Error""Failed to delete view: " + view.Name + " due to: " + ex.Message);
                            }
                        }
                    }

                    // Filter schedules to retain Revision Codes and Issue Dates
                    List<ViewSchedule> schedulesToDelete = new FilteredElementCollector(doc)
                        .OfClass(typeof(ViewSchedule))
                        .Cast<ViewSchedule>()
                        .Where(schedule => schedule.IsValidObject &&
                                           !schedule.Name.Contains("Revision") &&
                                           !schedule.Name.Contains("Issue Date"))
                        .ToList();

                    foreach (ViewSchedule schedule in schedulesToDelete)
                    {
                        if (schedule.IsValidObject)
                        {
                            try
                            {
                                doc.Delete(schedule.Id);
                            }
                            catch (Exception ex)
                            {
                                TaskDialog.Show("Error""Failed to delete schedule: " + schedule.Name + " due to: " + ex.Message);
                            }
                        }
                    }

                    // Remove linked files (instances and types)
                    List<Element> linkedInstances = new FilteredElementCollector(doc)
                        .OfClass(typeof(RevitLinkInstance))
                        .Where(instance => instance.IsValidObject)
                        .ToList();

                    foreach (Element instance in linkedInstances)
                    {
                        if (instance.IsValidObject)
                        {
                            try
                            {
                                doc.Delete(instance.Id);
                            }
                            catch (Exception ex)
                            {
                                TaskDialog.Show("Error""Failed to delete linked instance ID: " + instance.Id + " due to: " + ex.Message);
                            }
                        }
                    }

                    List<Element> linkedTypes = new FilteredElementCollector(doc)
                        .OfClass(typeof(RevitLinkType))
                        .Where(linkType => linkType.IsValidObject)
                        .ToList();

                    foreach (Element linkType in linkedTypes)
                    {
                        if (linkType.IsValidObject)
                        {
                            try
                            {
                                doc.Delete(linkType.Id);
                            }
                            catch (Exception ex)
                            {
                                TaskDialog.Show("Error""Failed to delete linked type ID: " + linkType.Id + " due to: " + ex.Message);
                            }
                        }
                    }

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.RollBack();
                    TaskDialog.Show("Critical Error""An error occurred during the transaction: " + ex.Message);
                }
            }

            TaskDialog.Show("Operation Complete""Views, schedules, and links have been processed as requested.");
        }
    }
}

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

dwightduronidavy
Enthusiast
Enthusiast
Accepted solution

SOLVED!
FYI, Herewith resolved new macro:

using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using System.Collections.Generic;
using System.Linq;

namespace DDA_CSharpModule4
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("CC2ECAA5-7631-4A47-96C5-BDD4DFF8073C")]
    public partial class ThisApplication
    {
        private void Module_Startup(object sender, EventArgs e)
        {

        }

        private void Module_Shutdown(object sender, EventArgs e)
        {

        }

        #region Revit Macros generated code
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Module_Startup);
            this.Shutdown += new System.EventHandler(Module_Shutdown);
        }
        #endregion

        public void DeleteSheetsAndViewsRemoveLinkedFiles()
        {
            UIApplication uiApp = new UIApplication(this.Application);
            UIDocument uiDoc = uiApp.ActiveUIDocument;
            Document doc = uiDoc.Document;

            using (Transaction transaction = new Transaction(doc, "Delete Sheets, Views, and Linked Files"))
            {
                try
                {
                    transaction.Start();

                    // Delete sheets except those with specified prefixes
                    List<ViewSheet> sheetsToDelete = new FilteredElementCollector(doc)
                        .OfClass(typeof(ViewSheet))
                        .Cast<ViewSheet>()
                        .Where(sheet => !sheet.Name.StartsWith("Architecture") &&
                                        !sheet.Name.StartsWith("Architectural") &&
                                        !sheet.Name.StartsWith("Landscape") &&
                                        !sheet.Name.StartsWith("Site"))
                        .ToList();

                    foreach (ViewSheet sheet in sheetsToDelete)
                    {
                        if (sheet.IsValidObject)
                        {
                            try
                            {
                                doc.Delete(sheet.Id);
                            }
                            catch (Exception ex)
                            {
                                TaskDialog.Show("Error"string.Format("Failed to delete sheet: {0}. Error: {1}", sheet.Name, ex.Message));
                            }
                        }
                    }

                    // Delete views except those with suffix "_!", schedules, System Browser, and Project Views
                    List<View> viewsToDelete = new FilteredElementCollector(doc)
                        .OfClass(typeof(View))
                        .Cast<View>()
                        .Where(view => !view.IsTemplate && !view.Name.EndsWith("_!") &&
                                       view.ViewType != ViewType.Schedule &&
                                       view.ViewType != ViewType.SystemBrowser &&
                                       view.ViewType != ViewType.ProjectBrowser)
                        .ToList();

                    foreach (View view in viewsToDelete)
                    {
                        if (view.IsValidObject)
                        {
                            try
                            {
                                doc.Delete(view.Id);
                            }
                            catch (Exception ex)
                            {
                                TaskDialog.Show("Error"string.Format("Failed to delete view: {0}. Error: {1}", view.Name, ex.Message));
                            }
                        }
                    }

                    // Delete schedules except those related to revision numbers and issue dates
                    List<ViewSchedule> schedulesToDelete = new FilteredElementCollector(doc)
                        .OfClass(typeof(ViewSchedule))
                        .Cast<ViewSchedule>()
                        .Where(schedule => !schedule.Name.Contains("Revision") && !schedule.Name.Contains("Issue Date"))
                        .ToList();

                    foreach (ViewSchedule schedule in schedulesToDelete)
                    {
                        if (schedule.IsValidObject)
                        {
                            try
                            {
                                doc.Delete(schedule.Id);
                            }
                            catch (Exception ex)
                            {
                                TaskDialog.Show("Error"string.Format("Failed to delete schedule: {0}. Error: {1}", schedule.Name, ex.Message));
                            }
                        }
                    }

                    // Remove all linked Revit files (instances and types)
                    List<Element> linkedInstances = new FilteredElementCollector(doc)
                        .OfClass(typeof(RevitLinkInstance))
                        .Where(instance => instance.IsValidObject)
                        .ToList();

                    foreach (Element instance in linkedInstances)
                    {
                        if (instance.IsValidObject)
                        {
                            try
                            {
                                doc.Delete(instance.Id);
                            }
                            catch (Exception ex)
                            {
                                TaskDialog.Show("Error"string.Format("Failed to delete linked instance ID: {0}. Error: {1}", instance.Id, ex.Message));
                            }
                        }
                    }

                    List<Element> linkedTypes = new FilteredElementCollector(doc)
                        .OfClass(typeof(RevitLinkType))
                        .Where(linkType => linkType.IsValidObject)
                        .ToList();

                    foreach (Element linkType in linkedTypes)
                    {
                        if (linkType.IsValidObject)
                        {
                            try
                            {
                                doc.Delete(linkType.Id);
                            }
                            catch (Exception ex)
                            {
                                TaskDialog.Show("Error"string.Format("Failed to delete linked type ID: {0}. Error: {1}", linkType.Id, ex.Message));
                            }
                        }
                    }

                    // Remove all linked CAD formats (ImportInstance elements)
                    List<Element> cadFormats = new FilteredElementCollector(doc)
                        .OfClass(typeof(ImportInstance))
                        .Where(cad => cad.IsValidObject)
                        .ToList();

                    foreach (Element cad in cadFormats)
                    {
                        if (cad.IsValidObject)
                        {
                            try
                            {
                                doc.Delete(cad.Id);
                            }
                            catch (Exception ex)
                            {
                                TaskDialog.Show("Error"string.Format("Failed to delete CAD format ID: {0}. Error: {1}", cad.Id, ex.Message));
                            }
                        }
                    }

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.RollBack();
                    TaskDialog.Show("Critical Error"string.Format("An error occurred during the transaction: {0}", ex.Message));
                }
            }

            TaskDialog.Show("Operation Complete""Sheets, views, schedules, and linked files have been processed as requested.");
        }
    }
}

0 Likes
Message 3 of 3

dwightduronidavy
Enthusiast
Enthusiast

Albeit with a minor error - but still meets my objective

0 Likes