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<View> 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: