Unpin Views from Project Browser

Thought I share this script I just made. I was trying to delete views from my project browser and kept getting “Cannot delete pinned elements”. Quite annoying even though the views were not on sheets. I found this on the forums. Not sure if this is a result from also using the pyRevit “Pin All Viewports” tool.

https://www.autodesk.com/support/technical/article/caas/sfdcarticles/sfdcarticles/Revit-view-cannot-be-deleted-because-it-is-pinned.html

simply select one or more views from the Project Browser

# -*- coding: utf-8 -*-
import clr
clr.AddReference("RevitAPI")
clr.AddReference("RevitServices")

from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
from pyrevit import revit, script

doc = revit.doc
uidoc = revit.uidoc
selection = uidoc.Selection.GetElementIds()

if not selection:
    script.show.warning("No views selected. Please select views in the Project Browser.")
    script.exit()

# Start transaction
t = Transaction(doc, "Unpin Selected Views")
t.Start()

unpinned = []
skipped = []

for eid in selection:
    el = doc.GetElement(eid)
    if isinstance(el, View):
        try:
            if el.Pinned:
                el.Pinned = False
                unpinned.append(el.Name)
            else:
                skipped.append(el.Name)
        except Exception as e:
            skipped.append(el.Name + " (Error)")
    else:
        skipped.append(str(el))

t.Commit()

# Report
output = script.get_output()
output.print_md("### 🔓 Unpin Results")

if unpinned:
    output.print_md("**✅ Unpinned Views:**")
    for name in unpinned:
        output.print_md("- `{}`".format(name))
else:
    output.print_md("*No views were unpinned.*")

if skipped:
    output.print_md("\n**⚠️ Skipped or Already Unpinned:**")
    for name in skipped:
        output.print_md("- `{}`".format(name))

```

Took the liberty to make it a bit more pyrevit and less dynamo.
(untested)

from pyrevit import revit, DB, script

output = script.get_output()
output.close_others(all_open_outputs=True)

selection = revit.get_selection()

def unpin_views():
    if not selection:    
        script.show.warning("No views selected. Please select views in the Project Browser.")
        return

    # Start transaction
    with revit.Transaction("Unpin Selected Views"):
        unpinned = []
        skipped = []

        for eid in selection:
            el = revit.doc.GetElement(eid)
            if not isinstance(el, DB.View):
                skipped.append(str(el))
                continue
            try:
                if not el.Pinned:
                    skipped.append(el.Name)
                    continue
                el.Pinned = False
                unpinned.append(el.Name)                    
            except Exception as e:
                # you define e but not using it?
                skipped.append(el.Name + " (Error)")

    # Report    
    output.print_md("### 🔓 Unpin Results")

    if unpinned:
        output.print_md("**✅ Unpinned Views:**")
        for name in unpinned:
            output.print_md("- `{}`".format(name))
    else:
        output.print_md("*No views were unpinned.*")

    if skipped:
        output.print_md("\n**⚠️ Skipped or Already Unpinned:**")
        for name in skipped:
            output.print_md("- `{}`".format(name))

if __name__ == "__main__":
	unpin_views()
1 Like

What is

script.show.warning(). ?

I have not seen that before.

correction, should be forms.alert