Typically, to work with the application of view control of .NET, we need to add the control to our application (say an Windows Form) firstly. In the designer of Visual Studio, we will add the reference Autodesk.Navisworks.Controls.dll. It will generate two controls ( View control and Document control ) in the designer:
After adding the instances of the document control and view control, we can start to use them in the application. The help reference [Developer Guide] >> [Using the Controls] tells in details.
Unfortunately, this workflow is only applicable on 32bits. On 64bits, if you try to add the reference Autodesk.Navisworks.Controls.dll in the [Designer ] >> [Choose Toolbox Items], you will receive an error:
"Autodesk.Navisworks.Controls.dll is not a valid .NET module".
Actually, this is not an issue of Navisworks API, but a known issue of Visual Studio. In short, the behavior is by design. Visual Studio is a 32-bit process, and therefore can only execute 32-bit modules. While Visual Studio allows you to add a reference to a 64-bit assembly, it cannot actually JIT compile it to 64-bit and execute it in process. This article of MSDN knowledge base tell more:
http://support.microsoft.com/kb/963017
The solutions are:
1) either create the application on 32bits with the option [Any CPU], and copy it to 64bits. You are able to run the application directly because it will load the 64bits control at runtime. Even you can see it in the designer, so you can still re-design the view control with its properties.
2) or, you can add the controls at runtime. If you dig into the normal workflow of a Windows Form application, you can see each form class has one partial class in the MyForm.Designer.cs, where all the controls are initialized and added to the form. So we can mimic the workflow and add the controls dynamically. Of course, you need to be careful to set location/width/height of the view control to make it nice in visual.
static class Program
{
private Autodesk.Navisworks.Api.Controls.DocumentControl documentControl1;
private Autodesk.Navisworks.Api.Controls.ViewControl viewControl1;
public Form1()
{
// Initialize other controls
InitializeComponent();
//Initialize document control
if(this.components == null)
this.components = new System.ComponentModel.Container();
this.documentControl1 = new Autodesk.Navisworks.Api.Controls.DocumentControl(this.components);
// Initialize view control
this.viewControl1 = new Autodesk.Navisworks.Api.Controls.ViewControl();
// attach the document control to the view control
this.viewControl1.DocumentControl = this.documentControl1;
//set the location, width and height of the control
this.viewControl1.Location = new System.Drawing.Point(Left + 10 , Top -10 );
this.viewControl1.Name = "viewControl1";
this.viewControl1.Size = new System.Drawing.Size(Width - 20 , Height -20);
this.viewControl1.TabIndex = 0;
this.viewControl1.Text = "viewControl1";
// add it to the layout
this.Controls.Add(viewControl1);
}
}
Finally, do not forget to add the lines of initializing application of control:
static void Main()
{
//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 Form1());
//Finish use of the API.
Autodesk.Navisworks.Api.Controls.ApplicationControl.Terminate();
}