Extract the location of center box of the viewport in a sheet

Hi everyone,
is there a way to extract the location of center box of viewports in a sheet in Revit?
I’m trying to find the syntax of the python code if anyone could help.
Appreciate it in advance

Here is a very simple case where I got the sheet, then the viewport, then the outline, then the Min and Max point and subtracted them to find the center.

#Sean Page, 2020
import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

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

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

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

#Preparing input from dynamo to revit
element = UnwrapElement(IN[0])

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

sheets = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Sheets).ToElements()
vps = sheets[0].GetAllViewports()
vp = doc.GetElement(vps[0])
outline = vp.GetBoxOutline()
min = outline.MinimumPoint
max = outline.MaximumPoint
#Subtract the Min point from the Max point, divide that by 2, then add back the Min point to get Center
center = ((max-min)/2) + min
TransactionManager.Instance.TransactionTaskDone()

OUT = sheets, vp,min,max,center

Many thanks Sean,
I tried to run it but I get an error:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 36, in
ValueError: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Could you please help?

The code I gave is a pretty simple example of how to get the information needed. Most likely it is getting a sheet that does not have a ViewPort on it and so it is breaking. If you could provide an image of your graph with previews on I could modify it to work for multiple sheets / viewports.

Also, I modified the code above some to actually get the center point.

See if this works better:

#Sean Page, 2020
import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

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

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

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

#Sheets
if isinstance(IN[0],list):
	sheets = UnwrapElement(IN[0])
else:
   	sheets = [UnwrapElement(IN[0])]

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)
centers = []
for sheet in sheets:
	SheetCenters = []
	SheetViewportIds = sheet.GetAllViewports()	
	for viewportId in SheetViewportIds:
		viewport = doc.GetElement(viewportId)
		outline = viewport.GetBoxOutline()
		SheetCenters.Add((outline.MinimumPoint+((outline.MaximumPoint - outline.MinimumPoint)/2)).ToPoint())
	centers.Add(SheetCenters)
TransactionManager.Instance.TransactionTaskDone()

OUT = sheets, centers
1 Like

Thank you so much.
This works perfect!

Sean,
Could you do me a favor and let me know how we can extract sheet’s height and width too?
I sincerely appreciate your help in advance!

I think there is an API call which you can use to access that information but I don’t know how to use it.
In the meanwhile you can try this.

#Sean Page, 2020
import clr

clr.AddReference(‘RevitAPI’)
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference(‘RevitAPIUI’)
from Autodesk.Revit.UI import *

clr.AddReference(‘System’)
from System.Collections.Generic import List

clr.AddReference(‘RevitNodes’)
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

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

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

#Sheets
if isinstance(IN[0],list):
sheets = UnwrapElement(IN[0])
else:
sheets = [UnwrapElement(IN[0])]

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)
centers =
sheetLength =
sheetWidth =
for sheet in sheets:
oLine = sheet.Outline
minU = oLine.Min.U
minV = oLine.Min.V
maxU = oLine.Max.U
maxV = oLine.Max.V

#get width and length of Rectangle
w = round(maxU-minU,2)
l = round(maxV-minV,2)
sheetLength.Add(l)
sheetWidth.Add(w)

SheetCenters =
SheetViewportIds = sheet.GetAllViewports()
for viewportId in SheetViewportIds:
viewport = doc.GetElement(viewportId)
outline = viewport.GetBoxOutline()
SheetCenters.Add((outline.MinimumPoint+((outline.MaximumPoint - outline.MinimumPoint)/2)).ToPoint())
centers.Add(SheetCenters)
TransactionManager.Instance.TransactionTaskDone()

OUT = sheets, centers, sheetLength, sheetWidth

1 Like

Thanks Amol!

The way to do this is to get the Titlblock Instance from the sheet, and then use the Built-in-Parameter SHEET_WIDTH and SHEET_HEIGHT to retrieve the Values. I’ll put something on here when I get back to my computer.

2 Likes

Thanks for the suggestion @SeanP
Something like this?

#Sean Page, 2020
import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

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

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

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

#Sheets
if isinstance(IN[0],list):
	sheets = UnwrapElement(IN[0])
else:
   	sheets = [UnwrapElement(IN[0])]

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)
centers = []
sLength = []
sWidth = []
for sheet in sheets:
	
	titleBlock = FilteredElementCollector(doc, sheet.Id).OfClass(FamilyInstance).OfCategory(BuiltInCategory.OST_TitleBlocks).FirstElement()
	sheetLength = titleBlock.get_Parameter(BuiltInParameter.SHEET_WIDTH).AsDouble()
	sheetWidth = titleBlock.get_Parameter(BuiltInParameter.SHEET_HEIGHT).AsDouble()
			
	SheetCenters = []
	
	SheetViewportIds = sheet.GetAllViewports()	
	for viewportId in SheetViewportIds:
		viewport = doc.GetElement(viewportId)
		outline = viewport.GetBoxOutline()
		SheetCenters.Add((outline.MinimumPoint+((outline.MaximumPoint - outline.MinimumPoint)/2)).ToPoint())
	sLength.Add(sheetLength)
	sWidth.Add(sheetWidth)
	centers.Add(SheetCenters)
TransactionManager.Instance.TransactionTaskDone()

OUT = sheets, centers, sLength, sWidth
2 Likes

Many thanks Amol!

2 Likes

Working in IronPython2 in Revit 2022.1:

#Inspired by Sean Page, 2020
import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

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

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

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

#Sheets
if isinstance(IN[0],list):
	sheets = UnwrapElement(IN[0])
else:
   	sheets = [UnwrapElement(IN[0])]
centers = []
for sheet in sheets:
	SheetCenters = []
	SheetViewportIds = sheet.GetAllViewports()	
	for viewportId in SheetViewportIds:
		viewport = doc.GetElement(viewportId)
		outline = viewport.GetBoxOutline()
		SheetCenters.Add((outline.MinimumPoint+((outline.MaximumPoint - outline.MinimumPoint)/2)).ToPoint())
	centers.Add(SheetCenters)

OUT = centers


How can we know which references are unneeded?