In the past, Navisworks will load all valid plugins in the folder <Naivsworks>\Plugins. Of course, as indicated in Navisworks 2014 API new feature – One more path to load plugin, Navisworks will also load the plugins in the new folder automatically as well.
In some situations, we may need to demand loading the plugin when necessary. 2014 provides such mechanism.
ApplicationParts.ApplicationPlugins.AddPluginAssembly(absolute path name of the plugin)
Following is a small experiment. It self explains the usage which is very straightforward.
1. create a simple AddinPlugin and put the binary to any folder such as c:\temp
[PluginAttribute("BasicPlugIn_LoadTest",
"ADSK",
DisplayName = "BasicPlugIn_LoadTest")]
public class BasicPlugIn_LoadTest :
AddInPlugin
{
public override int Execute(params string[] parameters)
{
MessageBox.Show("BasicPlugIn_LoadTest is executed!");
return 0;
}
}
2. create a plugin which is put in the two folders where Navisworks will load the plugin automatically. e.g. <Naivsworks>\Plugins
[PluginAttribute("BasicPlugIn",
"ADSK",
ToolTip = "BasicPlugIn tool tip",
DisplayName = "Hello World Plugin")]
public class ABasicPlugin : AddInPlugin
{
public override int Execute(params string[] parameters)
{
//the name of the plugin which is to be loaded dynamically
string plugin_name_to_load = "BasicPlugIn_LoadTest.ADSK";
// plugin record
PluginRecord dotest = null;
try
{
dotest = Autodesk.Navisworks.Api.Application.Plugins.
FindPlugin(plugin_name_to_load);
if (dotest != null)
{
// the plugin has been loaded. exit.
MessageBox.Show("the plugin " +
plugin_name_to_load +
" has been loaded!");
return 0;
}
else
{
// load the plugin assembly
Autodesk.Navisworks.Api.Application.Plugins.AddPluginAssembly(
@"C:\temp\BasicPlugIn_LoadTest.ADSK.dll");
// get the plugin record.
PluginRecord otherpluginrecord =
Autodesk.Navisworks.Api.Application.Plugins.
FindPlugin(plugin_name_to_load);
if (otherpluginrecord != null)
{
//loaded
if (!otherpluginrecord.IsLoaded)
{
//load the plugin
otherpluginrecord.LoadPlugin();
MessageBox.Show("the plugin " +
plugin_name_to_load +
" has been loaded by AddPluginAssembly!");
//get the plugin of the record
Plugin otherplugin =
otherpluginrecord.LoadedPlugin;
//call one method of the plugin
//since we do not know the type of the other plugin,
//use InvokeMember
string[] pa = { "dummy"};
otherplugin.GetType().InvokeMember("Execute",
System.Reflection.BindingFlags.InvokeMethod,
null, otherplugin,pa);
}
}
else
{
// any other problems
MessageBox.Show(
"Navisworks has tried to load the plugin " +
plugin_name_to_load +
" but it is still null. Please check the path name!");
}
}
}
catch (Exception ex)
{
// any exception
System.Diagnostics.Debug.Write(ex.ToString());
}
return 0;
}
}
When you click the button of BasicPlugIn, it will try to load the other plugin BasicPlugIn_LoadTest.ADSK and call its method Execute. From this experiment, you could also find the binary dll of other plugin could be within any folder. It would be a convenience for you to deploy your application.