Rotate crop box of multiple views

Hi

I’m trying to rotate a list of views based on a list of rotations (in degrees) by modifying the code in this post. @Konrad_K_Sobon @Mark.Ackerley @Dimitar_Venkov

This is what I have but I am getting an error “unexpected token ‘v’”

import clr

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

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

rot = IN[0]
viewports = UnwrapElement(IN[1])

def GetViewCropBoxElement(view):
	doc = DocumentManager.Instance.CurrentDBDocument
	TransactionManager.Instance.ForceCloseTransaction()
	tGroup = TransactionGroup(doc, "Temp to find crop box element")
	tGroup.Start()
	trans1 = Transaction(doc, "Temp to find crop box element")
	trans1.Start()
	view.CropBoxVisible = False
	trans1.Commit()
	
	shownElems = FilteredElementCollector(doc, view.Id).ToElementIds()
	
	trans1.Start()
	view.CropBoxVisible = True
	trans1.Commit()
	
	cropBoxElement = FilteredElementCollector(doc, view.Id).Excluding(shownElems).FirstElement()
	tGroup.RollBack()
	return cropBoxElement
	
def RotateCropBox(view, cropBox, angle):
    doc = DocumentManager.Instance.CurrentDBDocument
    bbox = view.CropBox
    center = 0.5 * (bbox.Max + bbox.Min)
    axis = Line.CreateBound(center, center + XYZ.BasisZ)
    TransactionManager.Instance.EnsureInTransaction(doc)
    ElementTransformUtils.RotateElement(doc, cropBox.Id, axis, angle)
    TransactionManager.Instance.TransactionTaskDone()
    return view

For v in viewports:
   	crpbx = GetViewCropBoxElement(v)
    crpbxRt = RotateCropBox(v, crpbx, rot)

OUT = crpbxRt

Any ideas?

Thanks

503_View rotation.dyn (8.5 KB)

1 Like

Can you try instead ? for v in viewports:
(For in lowercase )

import clr

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

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

rot = IN[0]
viewports = UnwrapElement(IN[1])

def GetViewCropBoxElement(view):
	doc = DocumentManager.Instance.CurrentDBDocument
	TransactionManager.Instance.ForceCloseTransaction()
	tGroup = TransactionGroup(doc, "Temp to find crop box element")
	tGroup.Start()
	trans1 = Transaction(doc, "Temp to find crop box element")
	trans1.Start()
	view.CropBoxVisible = False
	trans1.Commit()
	
	shownElems = FilteredElementCollector(doc, view.Id).ToElementIds()
	
	trans1.Start()
	view.CropBoxVisible = True
	trans1.Commit()
	
	cropBoxElement = FilteredElementCollector(doc, view.Id).Excluding(shownElems).FirstElement()
	tGroup.RollBack()
	return cropBoxElement
	
def RotateCropBox(view, cropBox, angle):
    doc = DocumentManager.Instance.CurrentDBDocument
    bbox = view.CropBox
    center = 0.5 * (bbox.Max + bbox.Min)
    axis = Line.CreateBound(center, center + XYZ.BasisZ)
    TransactionManager.Instance.EnsureInTransaction(doc)
    ElementTransformUtils.RotateElement(doc, cropBox.Id, axis, angle)
    TransactionManager.Instance.TransactionTaskDone()
    return view

for v in viewports:
   	crpbx = GetViewCropBoxElement(v)
    crpbxRt = RotateCropBox(v, crpbx, rot)

OUT = crpbxRt
2 Likes

Thanks. That fixed that error but now I get:

File “”, line 46, in
File “”, line 40, in RotateCropBox
TypeError: expected float, got list

Any ideas how to fix? I believe it is because I am feeding in a list of angles (which should actually be in radians).

Hey, sorry I can’t check it right now, but I would maybe try a zip rather than for…? So you’re iterating through both lists together? Something like…

for v, r in zip(viewports, rot):
   	crpbx = GetViewCropBoxElement(v)
    crpbxRt = RotateCropBox(v, crpbx, r)

Hopefully that is helpful :slight_smile:

Mark

2 Likes

Thanks @Mark.Ackerley. Yep that was pretty much it. Just had to append the output…

import clr

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

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

#rotations to be in radians
rot = UnwrapElement(IN[0])
viewports = UnwrapElement(IN[1])
views = []

def GetViewCropBoxElement(view):
	doc = DocumentManager.Instance.CurrentDBDocument
	TransactionManager.Instance.ForceCloseTransaction()
	tGroup = TransactionGroup(doc, "Temp to find crop box element")
	tGroup.Start()
	trans1 = Transaction(doc, "Temp to find crop box element")
	trans1.Start()
	view.CropBoxVisible = False
	trans1.Commit()
	
	shownElems = FilteredElementCollector(doc, view.Id).ToElementIds()
	
	trans1.Start()
	view.CropBoxVisible = True
	trans1.Commit()
	
	cropBoxElement = FilteredElementCollector(doc, view.Id).Excluding(shownElems).FirstElement()
	tGroup.RollBack()
	return cropBoxElement
	
def RotateCropBox(view, cropBox, angle):
    doc = DocumentManager.Instance.CurrentDBDocument
    bbox = view.CropBox
    center = 0.5 * (bbox.Max + bbox.Min)
    axis = Line.CreateBound(center, center + XYZ.BasisZ)
    TransactionManager.Instance.EnsureInTransaction(doc)
    ElementTransformUtils.RotateElement(doc, cropBox.Id, axis, angle)
    TransactionManager.Instance.TransactionTaskDone()
    return view
    
for v, r in zip(viewports, rot):
	crpbx = GetViewCropBoxElement(v)
	crpbxRt = RotateCropBox(v, crpbx, r)
	views.append(crpbxRt)

OUT = views
3 Likes

Thanks for the Python @Paul_Wintour . But i don’t get the expected rotation.
Can you explain what i am missing?

EDIT
Solved! I totally missed

awesome, it works perfectly.
I am just wondering how to change “for” to make the list writing with same number of angles ?
like i want to rotate all my views for .26 radiant, but i dont have to create a list of the same number repeating itself.

Hey,

I guess you mean this bit?

for v, r in zip(viewports, rot):
    crpbx = GetViewCropBoxElement(v)
    crpbxRt = RotateCropBox(v, crpbx, r)
    views.append(crpbxRt)
    OUT = views

you can replace it with this… (untested!)

for r in (rot):
    crpbx=GetViewCropBoxElement(view)
    crpbxRt = RotateCropBox(view, crpbx, r)
    views.append(crpbxRt)
    OUT = views

Hopefully that’s right :wink:

Mark

I don’t think repeat r would be the case. I think we should repeat append views in the list with the same “rot”

Hey, yes you’re right, my bad, but you get the idea… ‘for’ instead of ‘zip’ :slight_smile:

Has anyone gotten this to work for rotating the section boxes? I came across this post and this one by Jeremy, but I have been unable to get the section box to actually rotate the way that I need it to.

What goes wrong?