Create Sheet with Custom Viewport location

This post is about how you can create sheet with custom viewports location by using Ironpython in Revit Dynamo Application.

First to import the modules: clr to load .Net Assemblies as IronPython modules which are RevitAPI.dll for DBRevitServices.dll for DocumentManager and TransactionManagerCollect the current document and store in doc variable.

# dynamo version - 1.3.4

import clr
clr.AddReference('RevitAPI')
clr.AddReference("RevitServices")

from Autodesk.Revit.DB import *

from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

The first input is a titleblock , and second input is a list of views both inputs keep unwrap.

# titleblock family
t = UnwrapElement(IN[0])

# input a list of view
# in this sample with 4 views 
views = UnwrapElement(IN[1])

And we have to open a transaction before creating the sheet. ViewSheet class has, Create, a static method to create a sheet by providing document and titleblock’s id as arguments ViewSheet.Create(doc, t.Id) .

Create Sheet
# transaction start
TransactionManager.Instance.EnsureInTransaction(doc)

# create sheet
sheet = ViewSheet.Create(doc, t.Id)

Next step is to place the viewports on the sheet with our custom/desire locations. To do so, first, we need to get rectangle outline paper space (so called BoundingBoxUV ) of the sheet by using Outline property from the created sheet.

Outline Property & Return Value
# get outline of sheet
o = sheet.Outline

BoundingBoxUV uses UV coordinate system which we can collect U, V coordinates’ maximum and minimum point values. In the sample code line 27 and 28 assigned x and y variables to U and V net values. (Net value = maximum – minimum)

Understanding UV Coordinates’ values
# net values
x = o.Max.U - o.Min.U
y = o.Max.V - o.Min.V

After we collect the values, we need to compute four location points to properly place the four views from IN[1] input. Construct XYZ point with x, y, z values. Divisible values for x, y are most suitable values that i find for this sample, you can play around with these numbers. And z value must be 0.

XYZ Point Constructor
# first role
uv1 = XYZ( x/5, y/1.4, 0)
uv2 = XYZ( x/1.7, y/1.4, 0)

# second role
uv3 = XYZ( x/5, y/3.5,0)
uv4 = XYZ( x/1.7, y/3.5,0)

Finally, create a viewport with a location point, for each of the view by looping (views, [points]) together on the created sheet. Viewport class has, Create, a static method which need to provide four arguments as follow and close the transaction.

# create viewport for each view
for v,loc in zip(views,[uv1,uv2,uv3,uv4]):
	Viewport.Create(doc, sheet.Id , v.Id,loc)

# transaction close
TransactionManager.Instance.TransactionTaskDone()	

# output sheet
OUT = sheet

Well, that’s all for this sample and see the full sample code below.

Sample Usage
# dynamo version - 1.3.4

import clr
clr.AddReference('RevitAPI')
clr.AddReference("RevitServices")

from Autodesk.Revit.DB import *

from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

# titleblock family
t = UnwrapElement(IN[0])

# input a list of view
# in this sample with 4 views 
views = UnwrapElement(IN[1])

# transaction start
TransactionManager.Instance.EnsureInTransaction(doc)

# create sheet
sheet = ViewSheet.Create(doc, t.Id)

# get outline of sheet
o = sheet.Outline
# net values
x = o.Max.U - o.Min.U
y = o.Max.V - o.Min.V

# first role
uv1 = XYZ( x/5, y/1.4, 0)
uv2 = XYZ( x/1.7, y/1.4, 0)

# second role
uv3 = XYZ( x/5, y/3.5,0)
uv4 = XYZ( x/1.7, y/3.5,0)

# create viewport for each view
for v,loc in zip(views,[uv1,uv2,uv3,uv4]):
	Viewport.Create(doc, sheet.Id , v.Id,loc)

# transaction close
TransactionManager.Instance.TransactionTaskDone()	

# output sheet
OUT = sheet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.