WPF is widely used nowadays. Navisworks .NET API supports it. I plan to write the posts to dig some useful cases with WPF. I’d also like to hear any of your suggestion or I’d appreciate if you could share your use case.
The first post is to demo how to create a .NET control application of WPF. Actually there are already 3 samples in SDK folder: <Navisworks Installtation Path>\api\net\examples\Basic Examples\CSharp\WPF. They are well written. In this post, I will analyzed the skeleton and introduce the basic steps to create such application.
Firstly, choose the template of WPF Application.
Next, add the two assemblies
Autodesk.Navisworks.Api
Autodesk.Navisworks.Controls
Because .NET control of Navisworks is still a Windows Form control. It needs to be hosted in WindowsFormsHost. So we need to add one more assembly WindowsFormsIntegration.
WPF allows you to design the interface flexibly. They are configured by the XAML file. If you open MainWindow.xaml file, you can see some default lines which are added by the VS template. On the basis of the lines, we will need to import the Navisworks assemblies and WindowsFormsIntegration. e.g.
xmlns:NwControls="clr-namespace:Autodesk.Navisworks.Api.Controls;assembly=Autodesk.Navisworks.Controls"
Next, add one button which will open the Navisworks file. And the .NET view control of Navisworks.
<WindowsFormsHost>
<NwControls:ViewControl x:Uid="viewControl"
x:Name="viewControl"
Dock="Fill" />
</WindowsFormsHost>
Where x:Name is the name we could use behind the interface. The final XAML is as below. The default lines are marked in grey.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:NwApi="clr-namespace:Autodesk.Navisworks.Api;assembly=Autodesk.Navisworks.Api"
xmlns:NwControls="clr-namespace:Autodesk.Navisworks.Api.Controls;assembly=Autodesk.Navisworks.Controls"
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" Closed="Window_Closed">
<DockPanel>
<Button Content="Open File" Height="23" Name="button1" Width="75" Click="button1_Click" />
<WindowsFormsHost>
<NwControls:ViewControl x:Uid="viewControl"
x:Name="viewControl"
Dock="Fill" />
</WindowsFormsHost>
</DockPanel>
</Window>
The last is to implement the code behind. As always, to work with the .NET control, we need to initialize the application control before the application runs, and terminate it after it is closed. So in the construction method of MainWindow , we initialize the application, in the mean time, create a document control to bind with the view control. In Window_Closed, terminate application control.
As to open file, it is very simple. Please refer to the code.
using Autodesk.Navisworks.Api.Controls;
namespace NwWPFControlApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DocumentControl documentControl = new DocumentControl();
public MainWindow()
{
// initialize the control application
ApplicationControl.Initialize();
InitializeComponent();
viewControl.DocumentControl = documentControl;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//Dialog for selecting the Location of the file
OpenFileDialog dlg = new OpenFileDialog();
//Ask user for file location
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//If the user has selected a valid location,
// then tell DocumentControl to open the file
//As DocumentCtrl is linked to ViewControl
documentControl.Document.TryOpenFile(dlg.FileName);
}
}
private void Window_Closed(object sender, EventArgs e)
{
// terminate the control application
ApplicationControl.Terminate();
}
}
}
Build the program and run it. Our first WPF application was born!
It looks OK, but we seem not find any difference with a Windows Form application. I am far from being an expert of WPF. As I understand, WPF could be looked as the successor of Windows Form, but the WPF control
is extremely flexible. It allows for full visual customization.
In the next post, we will explore how to bind the model hierarchy to the treeview, which has full support for data binding.