Autodesk Navisworks has been providing API for advanced users to extend its functionality since 2011. The newest version, 2013, has even more power build in. However, it seems there is a lack of complete getting started tutorial, either in the API doc package or on the Web. So here I am writing one myself.
Before getting your hands dirty, make sure you have Navisworks Manage (Simulate should work also) installed. Freedom does not support API. Also, make sure you have Microsoft Visual Studio (VS) installed. This tutorial is finished in VS 2012, but older version should also work. Besides the software requirements, you do not need any knowledge about programming language to finish this tutorial, but obviously, if you want to do something serious with the API you need to learn programming. I recommend C#.
You may have been trying to search the Web for an API package download and in vain. In fact the API is shipped with the standard Navisworks installation. Go to the Navisworks installation folder (I am gonna use <NavisworksFolder> below to denote this folder), you should be able to find an “api” folder. All we need are in there.
Among the three folders (COM, net, nwcreate) in the api folder, we are going to use the “net” folder. In the “net\documentation” folder, there is a windows help file named “NET API.chm”. This file has two main parts: a “Developer Guide” as a tutorial and a “Reference Guide” as the API documentation. This is a good resource and you may want to read through the first part and finish the sample code later and keep the second part handy for reference. Most of the content in this tutorial is corresponding to the “Developer Guide – Plug-ins – Writing Plug-in” article in this help file. But: first, some very critical information is missing from that article (although the information is scattered through the help file); second, this article is hidden in the middle of the help file and there is no clue it is the getting started tutorial that you should read first (seriously, the article should be placed at the very beginning of the help file).
Now let’s code our first plug-in in Navisworks that does nothing but show us a “Hello World” message.
1. Start a new project.
Click File – New – Project to start a new project in VS. Choose “Visual C#” from the left panel of Templates, and then the “Class Library” template. Give the project a name “LeZhangPlugin” (or whatever you like). VS will automatically create a solution with the same name to put this project in. Make sure the “Create directory for solution” is checked. Don’t worry about the solution stuff but do remember the location of the project, since you will need to go there later to dig some file out. Click OK and it may take a while for VS to create and open the project. (BTW, click the pictures in this blog to see actual size pics.)
2. Add project reference.
In the right panel Solution Explorer, right-click the “References” and click “Add Reference”.
First reference we need to add is System.Windows.Forms. Find it in “Assemblies-Framework”.
The second reference we need is Autodesk.Navisworks.Api.dll. You need to click “Browse” and find it in the folder <NavisworksFolder>\api\net\bin\x86.
Click OK. Note: After the references are added, you need to select Autodesk.Navisworks.Api in the Solution Explorer, and change the “Copy Local” property to false. Otherwise it will not build.
3. Add code.
VS has already added some code skeleton. We need to add something more:
Add these two lines below the last “using xxx”:
using System.Windows.Forms;
using Autodesk.Navisworks.Api.Plugins;
Add the following code below “namespace LeZhangPlugin {“ and above “public class Class1″. Let’s ignore the meaning of the strings for now, just remember it is a configuration code for our plugin.
[PluginAttribute("LeZhangPlugin.MyAddIn", "LZNT", ToolTip = "Hello World by LeZhang.net", DisplayName = "LeZhang.net")]
After “public class”, change “Class1″ to “MyAddIn : AddInPlugin”.
Add the following code in the inner curly brackets:
public override int Execute(params string[] parameters)
{
MessageBox.Show(Autodesk.Navisworks.Api.Application.Gui.MainWindow, “Hello World”);
return 0;
}
At last, although not really necessary, you may want to change the name of the cs file from Class1.cs to MyAddIn.cs (same name as the class name). This is a good programming habit that will benefit you in the long run.
4. Build the project.
Click Build – Build Solution, or simply press F6 on your keyboard. If Warnings are shown at the bottom, ignore it for now as long as they are not Errors.
5. Create a folder for the plugin.
Go to <NavisworksFolder>\Plugin, create a folder named “LeZhangPlugin” (the exact name used for the project).
6. Copy the dll file.
Go to <VisualStudioProjectFolder>\LeZhangPlugin\LeZhangPlugin\bin\Debug, copy the LeZhangPlugin.dll file to the <NavisworksFolder>\Plugin\LeZhangPlugin folder we have just created. Make sure the dll file name and the plugin folder name match each other.
7. Start Navisworks. You should see a new tab named “Add-ins”, with a new button named “LeZhang.net”. Move your mouse on the button and it shows “Hello World by LeZhang.net”. Now you know the meaning of the configuration code. Click it and a message box is shown saying “Hello World”.
Hooray~ This is the end of this tutorial. If you find this is getting interesting, you may want to checkout my next tutorial: Navisworks 2013 API DockPane Tutorial