- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.");
}
}
}
Solved! Go to Solution.