Click twice to create a cropped dependent view

Here is how  to prompt the user to click twice and use those points to create a new dependent view cropped to the rectangle formed by those points.

public void MakeViewAndSheetWith2Clicks()
    Document doc = this.ActiveUIDocument.Document;
    UIDocument uidoc = new UIDocument(doc);

    // prompt user to pick two points
    XYZ lowLeftPickPoint = uidoc.Selection.PickPoint("Pick lower left corner");
    XYZ upRightPickPoint = uidoc.Selection.PickPoint("Pick upper right corner");

    // create a new BoundingBoxXYZ & set its Min and Max to the XYZ points selected by the user
    BoundingBoxXYZ bboxFromPicks = new BoundingBoxXYZ();
    bboxFromPicks.Min = lowLeftPickPoint;
    bboxFromPicks.Max = upRightPickPoint;

    // Find a titleblock in the project, or use InvalidElementId to create a sheet with no titleblock
    ElementId titleblockId = ElementId.InvalidElementId;
    FamilySymbol titleBlockSymbol = doc.TitleBlocks.Cast<FamilySymbol>().FirstOrDefault();
    if (titleBlockSymbol != null)
        titleblockId = titleBlockSymbol.Id;

    ViewSheet sheet = null;
    using (Transaction t = new Transaction(doc,"crop"))

        // duplicate the active view
        ElementId newViewId = doc.ActiveView.Duplicate(ViewDuplicateOption.AsDependent);
        View newView = doc.GetElement(newViewId) as View;

        // set the crop box of the new view to the bounding box created from the two picked points
        newView.CropBox = bboxFromPicks;

        // Create the new sheet
        sheet = ViewSheet.Create(doc, titleblockId);

        newView.Name = sheet.Name + "-" + sheet.SheetNumber;
        newView.Scale = 10;

        // Create the viewport to put the new view on the new sheet at (0,0,0)
        Viewport.Create(doc, sheet.Id, newViewId, XYZ.Zero);
    // make the new sheet the active view
    uidoc.ActiveView = sheet;

8 thoughts on “Click twice to create a cropped dependent view

  1. This is really useful. I was wondering if it could be possible to have a macro that runs when a view template is applied to a view. We have working views that we would like to automatically be added to a dummy working view sheet. This way our project browser (organized by sheet number) will automatically organize working views upon a view template being applied.

  2. Hi Harry,
    I have just tried to use this code and all seems to work fine, however, when the new dependent view is created, it changes the scale of the parent and the dependent view from 1:50 to 1:10.

  3. Hi Harry,
    Very useful indeed!!
    Is there a way to specify a particular titleblock family rather than using the “firstordefault” method?

  4. Hi ,
    Ive tried this, but got an error on: TitleBlocks

    FamilySymbol titleBlockSymbol = doc.TitleBlocks.Cast().FirstOrDefault();

    It says Document does not contain definition for TitleBlocks.
    What should I do?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s