Creating a New ViewPlan in a Family Document using the Revit API

By Saikat Bhattacharya

In Revit family documents, unlike with Revit project documents, we cannot use the Document.FamilyCreate.NewViewPlan() to create a new ViewPlan. It throws an exception of InvalidOperationException type. This is not totally a surprise. If we open up a specific family document in Revit using the Revit user interface (UI), we can see that there is no way to create a new plan view using the UI. And since API mostly enables workflows that are supported by UI in most cases, it is not surprising that we cannot create new floor plans using the API in those family documents.

Alternatively, the Revit UI does allow users to create a duplicate of views and if this is acceptable by the API users, we can consider using this workflow for creating new floor plans in family documents. The approach to do this is to filter out the desired view (view plan) that needs to be duplicated and use the View.Duplicate() method to create a duplicate of the view. While using this method, you can see that it provides three options for the duplicate method via the ViewDuplicateOption enum and this imitates the functionality available from the UI too – which is to create a duplicate, duplicate with detailing and duplicate as dependent. Once the view has been duplicated, the parameters of the new view can be altered – the snippet below shows how we can change the name, for example.

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
 
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
 
namespace Revit.SDK.Samples.HelloRevit.CS
{
 
  [Transaction(TransactionMode.Manual)]
  public class Command : IExternalCommand
  {
    public Result Execute(ExternalCommandData commandData,
      ref string message,
      ElementSet elements)
    {
 
      Document doc = commandData.Application.ActiveUIDocument.Document;
 
      // Filter views that match the required criteria
      FilteredElementCollector collector
        = new FilteredElementCollector(doc)
          .OfClass(typeof(View));
 
      // Get the specific view(s)
      IEnumerable views
        = from Autodesk.Revit.DB.View f
          in collector
          where (f.ViewType == ViewType.FloorPlan
            && !f.IsTemplate && f.ViewName.Equals("Ref. Level"))
          select f;
 
      using (Transaction trans = new Transaction(doc, "ViewDuplicate"))
      {
        trans.Start();
        foreach (View vw in views)
        {          
          // Duplicate the existing ViewPlan view
          View newView = doc.GetElement(vw.Duplicate(ViewDuplicateOption.Duplicate)) as View;
          // Assign the desired name
          if (null != newView)
            newView.ViewName = "MyViewName";
        }
      trans.Commit();
      }
 
      return Result.Succeeded;
    }
  }         
}
 
And the new viewplan can be confirmed via the Revit UI, as shown below:

Comments

3 responses to “Creating a New ViewPlan in a Family Document using the Revit API”

  1. Hi Saikat,
    I wanted the code to apply View template after duplicating view. here is the approach I use, it works any suggestion? Or any better technique?
    public void CreatingNewViewPlan(Document doc)
    {
    // Filter views that match the required criteria
    FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(View));
    // Get the specific view(s)
    IEnumerable views = from Autodesk.Revit.DB.View f in collector
    where (f.ViewType == ViewType.EngineeringPlan
    && !f.IsTemplate && f.ViewName.Equals(“W_Level 2”))
    select f;
    View viewTemplate = (from viewTem in new FilteredElementCollector(doc).OfClass(typeof(View)).Cast()
    where viewTem.IsTemplate == true && viewTem.Name == “HYD_STR_PLAN_Profile”
    select viewTem).First();
    using (Transaction trans = new Transaction(doc, “ViewDuplicate”))
    {
    trans.Start();
    foreach (View vw in views)
    {
    // Duplicate the existing ViewPlan view
    View newView = doc.GetElement(vw.Duplicate(ViewDuplicateOption.Duplicate)) as View;
    newView.ViewTemplateId = viewTemplate.Id;
    // Assign the desired name
    if (null != newView) newView.ViewName = “MyViewName”;
    }
    trans.Commit();
    }
    }

  2. Hi Babu, It will be very nice if you show me how to use this code. I don’t know where i can put this code and how i can put it. Thanks

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading