Pin Views on Sheets Using Python and Revit API

Hi there,
I’m trying to Pin all views on my sheets using only python. I’ve managed to come so far until I get this error in the following image. Anyone would know how to fix this problem? (I know how to do it through regular dynamo process. I just want to do it completely through python and Revit API)

And also when I get the views, how can I tell Revit to pin them on sheets? actually I have no idea how to use “Pin Property” in the API

Hello, here with a single viewport and the Pinned property
Class Element

import sys
import clr

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 
from RevitServices.Transactions import TransactionManager 

clr.AddReference("RevitAPI")

import Autodesk 
from Autodesk.Revit.DB import *


doc = DocumentManager.Instance.CurrentDBDocument


elt=UnwrapElement(IN[0])
bool=(IN[1])
TransactionManager.Instance.EnsureInTransaction(doc)
elt.Pinned=bool
TransactionManager.Instance.TransactionTaskDone()
a=elt.Pinned

OUT = a

Sincerely
christian.stan

1 Like

Hey Christian, thanks a lot for the reply. This would be helpful however since I’m practicing to work with the Revit API, I don’t want to create any inputs for the python and I want to perform the action completely based on python code. That is where I cannot solve the problem.

So far I’ve been able to get all elements Ids of all viewports placed on sheets but cannot figure it out the rest.

1 Like

Hi,
Here, the Pinned property is based on the Element Class and not on the ElementId class

Your question was taken from the Revit API forum.
With Dynamo it’s still simpler :wink:.

doc = DocumentManager.Instance.CurrentDBDocument

coll_sheets=FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Sheets).WhereElementIsNotElementType().ToElements()


viewport_id=[ViewSheet.GetAllViewports(s) for s in coll_sheets]
viewport=[doc.GetElement(i) for j in range(len(viewport_id)) for i in viewport_id[j]]

TransactionManager.Instance.EnsureInTransaction(doc)
for v in viewport:
    v.Pinned=True
TransactionManager.Instance.TransactionTaskDone()


OUT = coll_sheets,viewport_id,viewport

Sincerely
christian.stan

Thanks a lot. That solved my problem with Python method.

1 Like