Delete all views not working

Delete all views not working

floretti
Advocate Advocate
1,019 Views
5 Replies
Message 1 of 6

Delete all views not working

floretti
Advocate
Advocate

Hi all,

 

I'm trying to delete all of my views (not including sheets at this point) but despite everything running as expected, the views continue to be in the model.

 

I've done quite a bit of research and also tried refreshing my project browser as I know some actions need that (such as renaming views) but no success either.

 

I'm basically trying to isolate the callout view parents and the dependent view parents so they are deleted last otherwise I run into an error saying the element doesn't exist (if you delete the parent first, the callout also gets deleted).

 

This is a sample of the code.

var viewsToDelete = new List<ElementId>();
var parentViews = new List<ElementId>();

var allViews = new FilteredElementCollector(doc).
					  OfClass(typeof(View)).
					  WhereElementIsNotElementType().
					  ToElements();

foreach (View view in allViews)
{
	ElementId parentId = view.GetPrimaryViewId();

	if (parentId.IntegerValue == -1 && !view.IsTemplate && !view.IsCallout)
	{
		// View is NOT a dependent
		viewsToDelete.Add(view.Id);
		continue;
	}
	else if (parentId.IntegerValue != -1 && !view.IsTemplate && !view.IsCallout)
	{
		// View is dependent
		parentViews.Add(view.GetPrimaryViewId());
		continue;
	}
	else if (view.IsTemplate)
	{
		// View is template
		continue;
	}
	else if (view.IsCallout && !parentViews.Contains(view.GetCalloutParentId()))
	{
		// View is callout
		parentViews.Add(view.GetCalloutParentId());
	}
	else
	{
		console.ShowBoldMessage($"Could not find classification for view {view.Id}");
	}
}

foreach (var viewId in parentViews)
{
	viewsToDelete.Add(viewId);
}

foreach (var elemId in viewsToDelete)
{
	try
	{
		doc.Delete(elemId);
	}
	catch
	{
		console.ShowMessage($"Could not delete element {elemId}");
		continue;
	}
}

 

0 Likes
Accepted solutions (2)
1,020 Views
5 Replies
Replies (5)
Message 2 of 6

Moustafa_K
Advisor
Advisor
Accepted solution

I think there are more than just kind of view that will not allow you to delete it. For example SystemBrowser or internal.

I see the best way is to use switch ViewType (See the example below). this will shed some light to what you are really after. beside you can not delete all views.. there must be at least one view available.

 

var viewsToDelete = new List<ElementId>();
                var parentViews = new List<ElementId>();

                var allViews = new FilteredElementCollector(Doc)
                    .OfClass(typeof(View))
                    .WhereElementIsNotElementType()
                    .ToElements();

                foreach (View view in allViews)
                {
                    // one view must be available lets keep the current one
                    if (view.Id == UiDoc.ActiveGraphicalView.Id)
                        continue;

                    switch (view.ViewType)
                    {
                        case ViewType.FloorPlan:
                        case ViewType.EngineeringPlan:
                        case ViewType.AreaPlan:
                        case ViewType.CeilingPlan:
                        case ViewType.Elevation:
                        case ViewType.Section:
                        case ViewType.Detail:
                        case ViewType.ThreeD:
                        case ViewType.Schedule:
                        case ViewType.DraftingView:
                        case ViewType.DrawingSheet:
                        case ViewType.Legend:
                        case ViewType.Undefined:
                        case ViewType.PanelSchedule:
                        case ViewType.ColumnSchedule:
                        case ViewType.Walkthrough:
                        case ViewType.Rendering:
                            viewsToDelete.Add(view.Id);
                            break;
                        case ViewType.Report:
                        case ViewType.ProjectBrowser:
                        case ViewType.SystemBrowser:
                        case ViewType.CostReport:
                        case ViewType.LoadsReport:
                        case ViewType.PresureLossReport:
                        case ViewType.SystemsAnalysisReport:
                        case ViewType.Internal:
                            break;
                    }
                }

                Doc.Delete(viewsToDelete);

  

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 3 of 6

TripleM-Dev.net
Advisor
Advisor
Accepted solution

Hi @floretti,

 

You are also including sheets, when filtering for "View" type in the collector, this will also include Sheets.

Make sure to exlude ViewSheets or exclude it by View.Viewtype = DrawingSheet.

 

Also it's better to replace "parentId.IntegerValue == -1" with "parentId == ElementId.InvalidElementId", see: ElementId Class 

 

- Michel

0 Likes
Message 4 of 6

floretti
Advocate
Advocate

Hey guys, thanks a lot. I'll give those two ideas a go and report back. Much appreciated, have a great weekend.

0 Likes
Message 5 of 6

floretti
Advocate
Advocate

Oh my, I found the issue.

 

I had commented out lots of blocks of code to troubleshoot some parts of it and ended up leaving a Commit() commented out before an Assimilate(). My deletions were never getting committed. I just uncommented the line below and it all worked. Weirdly I would've expected that to throw an exception but no.

 

floretti_1-1715336262905.jpeg

 

floretti_0-1715336158989.png

 

0 Likes
Message 6 of 6

Moustafa_K
Advisor
Advisor

hahaha... experience is not cheap 🙂 

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1