RevitAPI: How to resize cropbox of view?

中文链接By Aaron Lu

Question:
Why does nothing happen when setting the cropbox of view?

What he did is, first create a view and then set its CropBox property.

ViewFamilyType vTypeElev = Class1.getviewfamilytypes(
ViewFamily.FloorPlan, RevitDoc).First<ViewFamilyType>();
var view = ViewPlan.Create(RevitDoc, vTypeElev.Id,
RevitDoc.ActiveView.GenLevel.Id);
view.CropBoxActive = true;
BoundingBoxXYZ box = new BoundingBoxXYZ();
box.Min = new XYZ(100, 100, 0);
box.Max = new XYZ(200, 200, 100);
view.CropBox = box;

and he said it works (note: above code should be included in transaction).

However, when he change the CropBox seperately, there is nothing happen:

var view = RevitDoc.ActiveView;
BoundingBoxXYZ box = new BoundingBoxXYZ();
box.Min = new XYZ(0, 0, 0);
box.Max = new XYZ(100, 100, 100);
view.CropBox = box;

I tried his code, which works on my machine, I’m not sure why.

Even thouogh, I suggested him another way: using method SetCropRegionShape of class ViewCropRegionShapeManager:

double length = 100;
var view = RevitDoc.ActiveView;
List<Curve> nl = new List<Curve>();
XYZ p2 = new XYZ(0, 0, 0);
XYZ p3 = new XYZ(length, 0, 0);
XYZ p4 = new XYZ(length, length, 0);
XYZ p5 = new XYZ(0, length, 0);
nl.Add(Line.CreateBound(p2, p3));
nl.Add(Line.CreateBound(p3, p4));
nl.Add(Line.CreateBound(p4, p5));
nl.Add(Line.CreateBound(p5, p2));
CurveLoop cl = CurveLoop.Create(nl);
ViewCropRegionShapeManager vpcr = view.GetCropRegionShapeManager();
bool cropValid = vpcr.IsCropRegionShapeValid(cl);
if (cropValid)
{
vpcr.SetCropRegionShape(cl);
}

which is much powerful, because it not only support rectangle but other shapes.


Comments

One response to “RevitAPI: How to resize cropbox of view?”

  1. Vlad Paly Avatar
    Vlad Paly

    Works fine
    using (Transaction transaction = new Transaction(document, “ImTemporaltrans”))
    {
    if (transaction.Start() == TransactionStatus.Started)
    {
    var vieww = uidoc.ActiveView;
    BoundingBoxXYZ box = new BoundingBoxXYZ();
    box.Min = new XYZ(-0.046855190, -0.046855190, -578.982246251);
    box.Max = new XYZ(0.046855190, 0.046855190, -0.578982246);
    vieww.CropBox = box;
    if (transaction.Commit() == TransactionStatus.Committed)
    {
    uidoc.ActiveView = vieww;
    }
    }
    }

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading