SetBoxCenter Method


Viewport Set Box Center Method

Moves this viewport so that the center of the box outline (excluding the viewport label) is at a given point.

Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 25.0.0.0 (25.0.0.0)
Syntax
public void SetBoxCenter(
	XYZ newCenterPoint
)

Parameters

newCenterPoint XYZ
The desired center for the box outline.
Exceptions
Exception Condition
ArgumentNullException A non-optional argument was null
InvalidOperationException The viewport is not on a sheet.
Example
public static void PlaceAlignedViewsAtLeftCorner(Document doc)
{
    FilteredElementCollector fec = new FilteredElementCollector(doc);
    fec.OfClass(typeof(ViewPlan));
    var viewPlans = fec.Cast<ViewPlan>().Where<ViewPlan>(vp => !vp.IsTemplate && vp.ViewType == ViewType.CeilingPlan);

    ViewPlan vp1 = viewPlans.ElementAt(0);
    ViewPlan vp2 = viewPlans.ElementAt(1);

    using (Transaction t = new Transaction(doc, "Place on sheet"))
    {
        t.Start();

        // Add two viewports distinct from one another
        ViewSheet vs = ViewSheet.Create(doc, ElementId.InvalidElementId);
        Viewport viewport1 = Viewport.Create(doc, vs.Id, vp1.Id, new XYZ(0, 0, 0));
        Viewport viewport2 = Viewport.Create(doc, vs.Id, vp2.Id, new XYZ(0, 5, 0));

        doc.Regenerate();

        // Calculate the necessary move vector to align the lower left corner
        Outline outline1 = viewport1.GetBoxOutline();
        Outline outline2 = viewport2.GetBoxOutline();
        XYZ boxCenter = viewport2.GetBoxCenter();
        XYZ vectorToCenter = boxCenter - outline2.MinimumPoint;
        XYZ newCenter = outline1.MinimumPoint + vectorToCenter;

        // Move the viewport to the new location
        viewport2.SetBoxCenter(newCenter);

        t.Commit();
    }
}
See Also
ArchiLabs Desktop

Put AI to work inside Revit.

Give Desktop your instructions and project files to build Revit families, update model elements, and create views and schedules. Works directly in Revit and AutoCAD.

Explore Desktop →