__title__ = "Change CropBox of Viewports."
__author__ = "Erik Frits"
__doc__ = """Version = 1.0
Date = 14.09.2022
_____________________________________________________________________
Description:
Change CropBox of Selected Viewports
_____________________________________________________________________
How-to:
-> Adjust Hardcoded values (TOP,BOTTOM,RIGHT,LEFT)
-> Select Viewports on Sheets
-> Run the script
_____________________________________________________________________
Last update:
- [14.09.2022] - 1.0 RELEASE
_____________________________________________________________________
Author Erik Frits"""
from Autodesk.Revit.DB import *
from pyrevit.forms import alert
uidoc = __revit__.ActiveUIDocument
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
rvt_year = int(app.VersionNumber)
def convert_cm_to_internal(length):
"""Function to convert cm to internal units."""
if rvt_year < 2022:
from Autodesk.Revit.DB import DisplayUnitType
return UnitUtils.Convert(length,
DisplayUnitType.DUT_CENTIMETERS,
DisplayUnitType.DUT_DECIMAL_FEET)
else:
from Autodesk.Revit.DB import UnitTypeId
return UnitUtils.ConvertToInternalUnits(length, UnitTypeId.Centimeters)
def get_selected_elements():
"""Property that retrieves selected views or promt user to select some from the dialog box."""
selected_elements = [doc.GetElement(el_id) for el_id in uidoc.Selection.GetElementIds()]
return selected_elements
__controls__ = """
ADJUST THESE VALUES AS YOU WANT.
You can use Positive, Negative and Zero values"""
TOP = convert_cm_to_internal(0)
BOTTOM = convert_cm_to_internal(-10)
RIGHT = convert_cm_to_internal(0)
LEFT = convert_cm_to_internal(0)
if __name__ == '__main__':
selected = get_selected_elements()
selected_viewports = [i for i in selected if type(i) == Viewport]
if not selected_viewports:
alert("No ViewPorts were selected.\nPlease, try again.", exitscript=True)
with Transaction(doc,__title__) as t:
t.Start()
for vp in selected_viewports:
view.CropBoxActive = True
view_id = vp.ViewId
view = doc.GetElement(view_id)
view_bb = view.CropBox
BB = BoundingBoxXYZ()
BB.Min = XYZ(view_bb.Min.X + LEFT , view_bb.Min.Y + BOTTOM , view_bb.Min.Z)
BB.Max = XYZ(view_bb.Max.X + RIGHT , view_bb.Max.Y + TOP , view_bb.Max.Z)
view.CropBox = BB
t.Commit()