Revit .NET has provided the PreviewControl API since version 2013. In this series of posts, we are going to explore the Revit PreviewControl .NET API step by step.
In this part, we create the simplest custom PreviewControl to get the basic idea first about the PreviewControl API.
• Use the Revit .NET Addin Wizard (RevitNetAddinWizard) to create a C# addin project for Revit 2014. To make it as simple as possible, what we only need to do is change the default project name from whatsoever to RevitModelPreview and accept any other default settings.
• Manully upgrade the project type of the RevitModelPreview.csproj from Class Library to WPF User Control Library through adding the <ProjectTypeGuids> element to the Project/PropertyGroup:
• Add a grid to the Window1.xaml and make it look a bit nice:
Title="Window1" Height="300" Width="300">
<Grid Height="234" HorizontalAlignment="Left" Name="grid1" VerticalAlignment="Top" Width="278" />
• Add the PreviewControl creation code to the Execute callback of the ExtCmd:
FilteredElementCollector collecotr = new FilteredElementCollector(CachedDoc);
Window1 win = new Window1();
win.grid1.Children.Add(new PreviewControl(CachedDoc, collecotr.FirstElementId()));
That is about it. A simple but working custom PreviewControl addin has been successfully done in a couple of minutes and we are ready to go! If we press the F5 key to give it a try in the Revit 2014, as chosen during the project creation time by Revit .NET Addin Wizard (RevitNetAddinWizard), we will notice the first view of the current document appears nicely in the WPF window.
Revit Addin Wizard (RevitAddinWizard) provides various wizards, coders and widgets to help program Revit addins. It can be downloaded from the Download link at the bottom of the blog index page.