|
![]() |
There are three controls providing wrappers around aspects of Navisworks:
-
ApplicationControl – singleton, required for initializing and terminating use of the API.
-
DocumentControl – a System.ComponentModel Component that hosts a Navisworks Document.
-
ViewControl – A Windows Forms Control that hosts a View and is used to display the Document within a DocumentControl.
All the controls are presented in the Autodesk.Navisworks.Api.Controls namespace and are usable from the Visual Studio forms designer.
![]() |
---|
Redistribution of Navisworks assemblies (including Controls) are not supported. A valid Installation of Navisworks Manage or Navisworks Simulate must be present on the machine. |
Using the API Controls we will create an application that is capable of opening Navisworks files and viewing them outside of the main application.
This example can be found in: '<drive>:\Program Files\Autodesk\Navisworks <Product> 2015\api\net\examples\Controls\Viewer'.
![]() |
---|
This sample assumes that you've already added the Controls to the toolbox in Visual Studio. For instructions on how to add Controls to the toolbox see here |
Overview
Step | Key Information |
---|---|
Create blank form project | |
Add API Reference | Autodesk.Navisworks.Controls.dll Autodesk.Navisworks.Api.dll |
Add any other references | |
Add controls onto the form | |
Link the DocumentControl to the ViewControl | |
Add code to open a file |
|
Build | |
Run Program |
Creating a program to use the Controls API
-
Create a new Windows Forms Application called 'Viewer'.
-
Add API references.
-
Add ApplicationControl methods Initialize and Terminate and set the ApplicationType in the Program.cs file:
CopySimple Viewer changes
using System; using System.Windows.Forms; using Autodesk.Navisworks.Api.Controls; using Autodesk.Navisworks.Api.Resolver; namespace Viewer { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { // The "Navisworks.Autodesk.Resolver" assembly should be included in your installation. // Other Navisworks.Autodesk.*" assemblies will be resolved at runtime, // and thus should not be included. // Autodesk.Navisworks.Controls controls usage inside the VS Designer is deprecated, and instead they should be created dynamically. String runtimeName = Resolver.TryBindToRuntime(RuntimeNames.Any); if (String.IsNullOrEmpty(runtimeName)) { throw new Exception("Failed to bind to Navisworks runtime"); } XMain(); } static void XMain() { //Set to single document mode Autodesk.Navisworks.Api.Controls.ApplicationControl.ApplicationType = ApplicationType.SingleDocument; //Initialise the api Autodesk.Navisworks.Api.Controls.ApplicationControl.Initialize(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Viewer()); //Finish use of the API. Autodesk.Navisworks.Api.Controls.ApplicationControl.Terminate(); } } }
-
Open the form in the designer.
-
Insert the ViewControl onto the form and set the 'Dock' property to 'Fill' the main window.
-
Insert a DocumentControl into the window and set the view to use this as its document. (ViewControl's DocumentControl property).
-
Add a menu and option to 'Open a file'.
-
Implement the 'File -> Open' menu item with the code below.
CopyLoading a Navisworks Document
//Dialog for selecting the Location of the file toolStripMenuItem1 open OpenFileDialog dlg = new OpenFileDialog(); //Ask user for file location if (dlg.ShowDialog() == 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); }
-
Edit the app.config file.
Copyapp.config file
<?xml version="1.0"?> <configuration> <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
-
Complete!
![]() |
---|
The Application's MainDocument property is null until SetAsMainDocument is called. However SetAsMainDocument must only be called once during the runtime of applications as there can only be one main document running at a time. The DocumentControl which has the Main Document assigned must not be disposed until the end of the application. |
ApplicationControl class
This class is primarily used to control when the API is 'Initialized' and 'Terminated'. The Initialize method initializes the API and keeps it in memory until the Terminate method is called or the program closes, whichever happens first.
![]() |
---|
Initialize MUST be called before using any aspects of the Controls API specifically. Failure to do so will result in an exception being thrown. |
The ApplicationControl class also lets the developer configure various global properties. Some of these must be set before initializing the API with the Initialize method.
ApplicationControl.RequestedRuntime
The ApplicationControl's property called RequestedRuntime allows the developer to control which Navisworks runtime is used when running their applications. For example, if a developer wishes to do something with the API that requires the Navisworks Manage product, then they would set this property to 'NavisworksManage'.
If however the developer wanted to use something in the API that was available in both Navisworks Manage and Navisworks Simulate then they would set the value to 'NavisworksManage; NavisworksSimulate'. This indicates that the preferred runtime is Navisworks Manage but that Navisworks Simulate would be used if Navisworks Manage does not exist on the system.
There is another value for use on this property, 'Any', which serves two purposes. The first is to identify that any Navisworks runtime can be used. The second purpose is that any runtime listed in the RequestedRuntime property after the 'Any' value is negated from being used. For example if a user wishes any Navisworks runtime except Simulate to be usable by a program then the property would be set to 'Any;Navisworks Simulate'.
Complicated combinations can be formed, for example the value 'Navisworks Manage;Any;Navisworks Simulate' would indicate that the Navisworks Manage runtime should be used in preference to any other, but that Navisworks Simulate is excluded.
![]() |
---|
Some of the standard values for use with the property 'RequestedRuntime' are included in the enumeration 'RuntimeNames'. Product OEM's and verticals may have additional values that can be used with the 'RequestedRuntime' property. |
ApplicationControl.ApplicationType
ApplicationControl has a ApplicationType property that allows developers to specify how many DocumentControls will be used in a given application. If this property is set to ApplicationType.SingleDocument, non-native file loaders will also be available. However using an additional DocumentControls will throw 'InvalidOperationException' exceptions when accessing the Documents of the additional DocumentControls. The default value is ApplicationType.MultipleDocument, but developers must ensure that, whichever value is chosen, it is assigned before calling the Initialize method.
The DocumentControl and ViewControl classes
The combination of these two classes enables developers to add the ability to view and interact with Navisworks Models.
The DocumentControl class provides the 'Document' property which provides interaction with the Navisworks Model. The ViewControl class then links to this DocumentControl, by way of the property 'DocumentControl', providing a visual display of the underlying Document.
Whilst a ViewControl provides no functionality without a DocumentControl, the DocumentControl can be used without the ViewControl. Primarily to open, save and explore the Model structures within a Navisworks Document, without having to load the full Navisworks product into memory.
IApplicationGui interface
This interface has been created to provide access to the GUI of the application which is hosting the API. The Gui property provides this interface, primarily for plug-ins contained within Navisworks. However, applications using the controls API will find that Gui is null as Navisworks is not loaded. Instead, applications which wish to access this (possibly using plug-ins which support controls) will need to implement this interface and call SetApplicationGui.