Creating a new 3D view with section box aligned to 2D crob box and depth

Hi everyone,

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)```

Here’s a section from a similar script I’ve written.

elevation = view.GenLevel.LookupParameter('Elevation').AsDouble()
vCrop = list(view.GetCropRegionShapeManager().GetCropShape())[0]
vCropPointsX = []
vCropPointsY = []
for x in vCrop:
    vCropPointsX.append(x.GetEndPoint(0).X)
    vCropPointsY.append(x.GetEndPoint(0).Y)
    vCropPointsX.append(x.GetEndPoint(1).X)
    vCropPointsY.append(x.GetEndPoint(1).Y)
minX = min(vCropPointsX)
minY = min(vCropPointsY)
maxX = max(vCropPointsX)
maxY = max(vCropPointsY)           

new_box = BoundingBoxXYZ()
new_box.Min = XYZ(minX, minY, elevation - 10)
new_box.Max = XYZ(maxX, maxY, elevation + 10)
v_name = view.Name
new_iso = DB.View3D.CreateIsometric(doc,threeDTypes[0].Id)
new_iso.SetSectionBox(new_box)
name_param = new_iso.LookupParameter('View Name')
name_param.Set('{} ISOMETRIC'.format(v_name))

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

2 Likes

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.

1 Like

Thank you very much! I will give it a shot today!

Hi, i have the same idea for a while in my head, did you manage to solve your problem? Got the same problem with the BoundingBox.

1 Like

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)

def create_xzybox(boundmin,boundmax, top, bottom):

min_pbox = DB.XYZ(boundmin[0], boundmin[1], bottom)
max_pbox = DB.XYZ(boundmax[0], boundmax[1], (top + 2.3))   

box = DB.BoundingBoxXYZ()
box.Min = min_pbox
box.Max = max_pbox
return box