Simple Code to Use Revit API & C# to Create Floor Plan and Apply a View Template
Using Revit API is very efficient to facilitate repetitive tasks if you use Revit in your company and you need to setup projects. You can use different programming languages to work with Revit API such as:
- C#
- Visual Basic
- Python
- Dynamo
C# is a Microsoft programming language which is an object oriented language and the most efficient way to work with Revit API. It can create faster, robust and more reliable Add-Ins.
Let's start with a simple code and expand it to the complicated Revit commands. For Revit 2017/2018, .Net Framework 4.6 and for Revit 2019 .Net Framework 4.7 is required. Revit API.dll, Revit API UI.dll should be referenced using Visual Studio Solution Explorer. Here is a useful link for creating an addin file.
The files should be copied to the following address:
C:\Users\Username\AppData\Roaming\Autodesk\Revit\Addins\2017
The following code is a simple example to duplicate an existing Floor Plan and apply "Architectural - FloorPlan" view template on that. This command will be added to the Add-Ins> External Tools on your Revit 2017:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
namespace MyRevitCommands
{
[TransactionAttribute(TransactionMode.Manual)]
public class DuplicateView : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
//Obtain UIDocument
UIDocument uidoc = commandData.Application.ActiveUIDocument;
//Obtain Document
Document doc = uidoc.Document;
//Obtain the Current Floor Plan Properties
View actView = doc.ActiveView;
Level level = actView.GenLevel;
String actViewName = actView.Name.ToString();
//Obtain Family Symbol
ViewFamilyType viewfamily = new FilteredElementCollector(doc)
.OfClass(typeof(ViewFamilyType))
.Cast<ViewFamilyType>()
.First(x => x.ViewFamily == ViewFamily.FloorPlan);
//Obtain the View Template Architectural - FloorPlan
View VTArchFP = new FilteredElementCollector(doc)
.OfClass(typeof(View))
.Cast<View>()
.First(x => x.Name == "Architectural - FloorPlan");
try
{
using (Transaction trans = new Transaction(doc, "DuplicateView"))
{
trans.Start();
//Create a New View
ViewPlan vplan = ViewPlan.Create(doc, viewfamily.Id , level.Id);
vplan.Name = actViewName + " - Architectural";
vplan.ViewTemplateId = VTArchFP.Id;
trans.Commit();
}
return Result.Succeeded;
}
catch (Exception e)
{
message = e.Message;
return Result.Failed;
}
}
}
}
If you need to Create New Views Using Duplicate with Detailing, the following lines should be used for Creating New Views:
//Create New Views
ElementId ArchFPId = actView.Duplicate(ViewDuplicateOption.WithDetailing);
ViewPlan ArchFP = doc.GetElement(ArchFPId) as ViewPlan;
ArchFP.ViewTemplateId = VTArchFP.Id;
ArchFP.Name = actViewName + " - Architectural";
I will try to share more information in this regard.
Best Regards
Mike