I am building my first script for work and decided to build one that takes you straight to a 3D view of any 2D view that you are in at that moment.
Everything is working perfectly, except for the cropping of the section box on to the 2 view crop region.
I am using the SetSectionBox(my3Dview, boundingBoxXYZ) method, my problem is that I can’t seem to find anything that returns a boundingBoxXYZ that is usable in my script.
Please see my code below for reference:
"""Create 3D view from current view"""
from pyrevit import revit, DB, UI
from pyrevit import forms
from pyrevit import script
#Create new 3D view with name of current user, cropped to the active view
def new_view(view, user):
if type(view) == revit.DB.View3D:
forms.alert("Please use a 2D view")
else:
with script.revit.Transaction("Create View"):
newThreeDee = revit.create.create_3d_view(user + " temp")
revit.UI.UIDocument.RequestViewChange(revit.HOST_APP.uidoc, newThreeDee)
revit.DB.View3D.SetSectionBox(newThreeDee, revit.DB.View.getBoundingBox(view, view))
# print(type(revit.DB.View.GetCropRegionShapeManagerForReferenceCallout(revit.HOST_APP.doc, revit.DB.View.CropBox(newThreeDee))))
originalView = revit.active_view
#Check if active view is a 3D view
#Return false if view is 2D
get_view_type = type(originalView) == revit.DB.View3D
#Gets the name of the user currently active
get_current_user = revit.HOST_APP.username
new_view(originalView, get_current_user)```
This creates a 3D view cropped to the maximum extents of the view. The section box is always orthogonal to project north in the above code, if you want to look into rotated section boxes, you can dive into BoundingBoxXYZ.Transform. ApiDocs.co · Revit · Transform Property
Yes, you have to create a new section box based on the view range of the view. In the example it has a fixed height, but it is better to use the actual view range.
I use UI.Selection.PickBox to have users draw a box around where they want the 3D view and create a bounding box with the 2 points returned using the bounding box constructor. Please note that if the viewplan has a tranform applied to it you will need to apply the views transform to the points to rotate and/or translate the coordinates to the correct location. Otherwise the 3D section box will not closely match what you drew. Here’s my code to get the the box and then my fuction to create the DB.BoundingBox:
from Autodesk.Revit.UI.Selection import PickBoxStyle
opt = PickBoxStyle.Crossing
try:
picked_box = HOST_APP.uidoc.Selection.PickBox(
opt, “Draw Rectangle around area to create 3D view”)
except Exception as e:
forms.alert(str(e), exitscript=True)