Use Navisworks API with WPF – Create a .NET control application of WPF

By Xiaodong Liang

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.

image

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.

image

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" />

<

p style=”text-align: left” class=”MsoNormal” align=”left”></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&quot;

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;       

        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!

image

 

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.


Comments

8 responses to “Use Navisworks API with WPF – Create a .NET control application of WPF”

  1. Hi Xiaodong,
    Do we need Navisworks Manage installed on the machine where this dev is being done?

  2. Xiaodong Liang Avatar
    Xiaodong Liang

    yes, Simulate/Manage are required to install & license in order to use your application: .NET API (plugin, control, automation). COM (plugin, Integrated control, automation).
    The only exception is: if your app works with Redistributable control of COM only, it just needs to install the installer of Redistributable.

  3. Thanks Xiaodong, another question about the Redistributable COM control, is that being supported in Navisworks 2014?
    Or is it End of Life? If so what is the alternative?

  4. Xiaodong Liang Avatar
    Xiaodong Liang

    It is still supported. download SDK from
    http://adndevblog.typepad.com/aec/2013/04/where-is-navisworks-2014-sdk.html
    The installer are still there.

  5. Thanks Xiaodong, I have installed the 2014 SDK from your link, and also installed the Redistributable from the “Navisworks 2014\api\COM\bin\Navisworks ActiveX Redistributable x86 Setup.exe” but i still am unable to build/run the sample Viewer project provided in the examples “api\NET\examples\Basic Examples\CSharp\Viewer\”
    As you mentioned earlier i do not need the Manage/Simulate installed on my PC if i have the redistributable installed – does this need any additional configuration?

  6. Xiaodong Liang Avatar
    Xiaodong Liang

    that sample uses Integrated control, instead of Redis control. And on 64bits, to work with the Integrated control, you need to be aware:
    http://adndevblog.typepad.com/aec/2012/06/navisworks-2013-dummy-activex-integrated-control-for-64bits.html
    To work with Redis control:
    – Standalone Winform program: insert the control to toolbox from COM tab of [Choose Items]
    – Web application: use the correct GUID of Redis control. see this blog:
    http://adndevblog.typepad.com/aec/2012/05/navisworks-activex-controls.html

  7. Also i’ve just noticed that i don’t have the Autodesk.Navisworks.Api.dll file in the bin folder at the following location
    ‘Autodesk\Navisworks 2014\api\NET\bin\x86’
    There is only one file ‘Autodesk.Navisworks.Controls.dll’ does this look correct?

  8. Hi Xiaodong Liang,
    I have installed Navisworks Simulate now and when i run the viewer sample included in the examples folder i get the following error
    “A procedure imported by ‘Autodesk.Navisworks.Api.dll’ could not be loaded.”
    Any idea what error this is ?

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading