Here you are
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Linq;
using Autodesk.Revit.DB.Structure;
namespace RevitAddinCS2
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
public class ExtCmd : IExternalCommand
{
#region Class Memeber Variables
private Autodesk.Revit.ApplicationServices.Application m_revit;
private Autodesk.Revit.DB.Document m_Document;
#endregion
#region Class Interface Implementation
public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
try
{
m_revit = commandData.Application.Application;
m_Document = commandData.Application.ActiveUIDocument.Document;
FilteredElementCollector SectionCollector = new FilteredElementCollector(m_Document);
SectionCollector.OfClass(typeof(ViewSection)).WhereElementIsNotElementType();
List<ViewSection> sections = SectionCollector.Cast<ViewSection>().Where(sh => sh.ViewType == ViewType.Section).ToList();
FilteredElementCollector SheetCollector = new FilteredElementCollector(m_Document);
SheetCollector.OfClass(typeof(ViewSheet));
List<ViewSheet> sheets = SheetCollector.Cast<ViewSheet>().ToList();
List<ElementId> sectionsIds = sections.Select( s => s.Id).ToList();
List<ElementId> viewsInSheetsIds = new List<ElementId>();
foreach (ViewSheet sh in sheets)
{
List<ElementId> viewsIds = sh.GetAllPlacedViews().ToList();
if (viewsIds.Count > 0)
{
viewsInSheetsIds.AddRange(viewsIds);
}
}
List<ElementId> sectionsToDeleteIds = sectionsIds.Except(viewsInSheetsIds).ToList();
ElementId Remain = ElementId.InvalidElementId;
if(sectionsToDeleteIds.Count > 0)
{
using (Transaction t = new Transaction(m_Document,"delete"))
{
t.Start();
foreach (ElementId id in sectionsToDeleteIds)
{
ViewSection v = m_Document.GetElement(id) as ViewSection;
if (v != null)
{
//if (!commandData.Application.ActiveUIDocument.GetOpenUIViews().Select(v => v.ViewId).Contains(id))
if (commandData.Application.ActiveUIDocument.ActiveGraphicalView.Name != v.Name)
{
using (SubTransaction st = new SubTransaction(m_Document))
{
st.Start();
m_Document.Delete(id);
st.Commit();
}
}
else
{
Remain = id;
}
}
}
t.Commit();
}
}
if(Remain != ElementId.InvalidElementId)
{
TaskDialog.Show("Revit", string.Format("Section: {0} couldn't be deleted because it is currently opened.", m_Document.GetElement(Remain).Name));
}
return Autodesk.Revit.UI.Result.Succeeded;
}
catch (Exception e)
{
message = e.ToString();
return Autodesk.Revit.UI.Result.Failed;
}
}
#endregion
}
}
Don't forget to mark this reply as an answer. Kudo will be appreciated. 
¯\_(ツ)_/¯
Let it work like a charm.
Mustafa Salaheldin


Digital Integration Manager, DuPod
Facebook |
Twitter |
LinkedIn