#AU2013 Wish 2 Granted!

Nancy asked for “auto duplicating and placing views through Design Option set creation”

Here is a macro that creates new views and sheets, with a new view for each design option. Each duplicated view is set to display a different design option and is placed on a sheet in the same location as on the sheet hosting the main model viewport.

public void ViewDuplicateOptions()
{
    Document doc = this.ActiveUIDocument.Document;
    UIDocument uidoc = this.ActiveUIDocument;
    IEnumerable<DesignOption> designOptions = new FilteredElementCollector(doc).OfClass(typeof(DesignOption)).Cast<DesignOption>().Where(q => !q.IsPrimary);
    if (designOptions.Count() == 1)
    {
        TaskDialog.Show("Error","Only one design option exists. No views duplicated.");
        return;
    }

    IList<ViewSheet> newSheets = new List<ViewSheet>();

    IList<ViewSheet> viewSheets = new FilteredElementCollector(doc).OfClass(typeof(ViewSheet)).Cast<ViewSheet>().ToList();

    using (Transaction t = new Transaction(doc,"Duplicate Views by Option"))
    {
        t.Start();
        foreach (ViewSheet vs in viewSheets)
        {
            IEnumerable<Viewport> viewportsOnSheet = new FilteredElementCollector(doc).OfClass(typeof(Viewport)).Cast<Viewport>().Where(q => q.SheetId == vs.Id);
            if (viewportsOnSheet.Count() == 0)
                continue;

            FamilyInstance titleblock = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_TitleBlocks).Cast<FamilyInstance>().First(q => q.OwnerViewId == vs.Id);

            foreach (DesignOption dOpt in designOptions)
            {
                ViewSheet newSheet = ViewSheet.Create(doc, titleblock.GetTypeId());
                newSheet.Name = vs.Name + " - " + dOpt.Name;
                newSheets.Add(newSheet);
                foreach (Viewport vp in viewportsOnSheet)
                {
                    View view = doc.GetElement(vp.ViewId) as View;
                    XYZ vpCenter = vp.GetBoxCenter();
                    View newView = doc.GetElement(view.Duplicate(ViewDuplicateOption.WithDetailing)) as View;
                    newView.Name = view.Name.Replace("{","").Replace("}","") + " - " + dOpt.Name;
                    newView.get_Parameter("Visible In Option").Set(dOpt.Id);
                    Viewport newVp = Viewport.Create(doc, newSheet.Id, newView.Id, vpCenter);
                }
            }
        }
        t.Commit();
    }

    foreach(ViewSheet v in newSheets)
    {
        uidoc.ActiveView = v;
    }
}

2 thoughts on “#AU2013 Wish 2 Granted!

  1. Harry,

    I have two questions for this post.
    1) if you have multiple design option sets it duplicates the views on the sheet for all sets. which gives me alot of extra duplicate views. how do i provide a menu to select a specific option set?
    2) I am getting an error in Revit 2016 when trying to build this line of code, which is to assign the option to each view. If i build the code with this line commented out it builds successful but doesnt assign options to views.
    newView.get_Parameter(“Visible In Option”).Set(dOpt.Id);

    thanks!

    • also i tried this tool in a full project and it tried duplicating every view in the project browser not isolating just the views on the active sheet.

Leave a comment