Skip to content
Le Duc Anh Tuan

How To Crop View With RevitAPI

The small tool will help create quickly the crop the view in any view

revit
revit-api
2 min read
Published on

Welcome to my blog 🥰

What is Crop View ?

Crop View help to display the elements inside the box that wrapped by the region.

How to active Crop View in Revit ?

Let active two properties or the icon like image.

Cropbox Property

So how to do multiple selection crop a view by RevitAPI ?

In RevitAPI, Crop View is definition by CropBox Property which it can be set by BoundingBoxXYZ

So now let's started 🚀🚀🚀

Step 1:

The region which Crop View is limited to is enclosed by two points. In the current view, two points are located top-right and bottom-left.

Use the method PickBox which will return two points:

C#

// Selection Box by pick rectangular
 PickedBox pickedBox = uidocument.Selection.PickBox(PickBoxStyle.Directional, "Draw Rectangular Box, Press ESC to exit");
 XYZ pointBottomLeft = pickedBox.Min;
 XYZ pointTopRight = pickedBox.Max;

Step 2:

The active view has a Transform. So we need modify the point to be the correct point with the transform of the view.

C#

//Get the invert transform of active view
Transform transform = view.get_BoundingBox(null).Transform.Inverse;
//Transform of the point
pointBottomLeft = transform.OfPoint(pointBottomLeft);
pointTopRight = transform.OfPoint(pointTopRight);

// Get exactly value min and max
double minX = Math.Min(pBox.Min.X, pBox.Max.X);
double maxX = Math.Max(pBox.Min.X, pBox.Max.X);

double minY = Math.Min(pBox.Min.Y, pBox.Max.Y);
double maxY = Math.Max(pBox.Min.Y, pBox.Max.Y);

Step 3:

Declared new BoundingBoxXYZ and apply for CropBox.

C#
//Create new instance of boundingBox
BoundingBoxXYZ boundingBoxXYZ = new BoundingBoxXYZ();
boundingBoxXYZ.Min = new XYZ(minX, minX, 0);
boundingBoxXYZ.Max = new XYZ(maxX, maxY, 0);
boundingBoxXYZ.Transform = view.CropBox.Transform;

//Active Crop View and aplly the new boundingBox for Cropbox
view.CropBoxActive = true;
view.CropBoxVisible = true;
view.CropBox = boundingBoxXYZ;
doc.Regenerate();

Step 4:

Add the while-loop for the code, so you can multiple set Crop View. Remember using Transaction.

C#
PickedBox pickedBox = null;
while (true)
{
    try
    {
        pickedBox = uiDocument.Selection.PickBox(PickBoxStyle.Directional, "Draw Rectangular, Press ESC to exit");
    }
    catch
    {
        break;
    }

    if (pickedBox != null)
    {
        using (Transaction transaction = new Transaction(document))
        {
            transaction.Start("Crop view by selection");
            // code block
            transaction.Commit();
        }
    }
}

Conclusion

Yeah 🎯, So we finished the small tool can help create quickly Crop View.

Reference

You can get full source code and another tool at RevitAPI-Learning