Question:
I have an application in Excel VBA which did some jobs related with Navisworks model by COM API. Now I wanted to save the model to lower version, but I do not find an API in COM.
Solution:
.NET API allows Document.SaveFile allows you to save the file to specific version. While VBA does not support .NET. however you could produce a .NET Automation application, and a .NET plugin. In plugin, use Document.SaveFile to save to specific version. The Automation executes the plugin. In Excel, run the Automation (exe) with the parameter of the source file.
This sounds a little awkward. The better solution is to create a DocumentControl application (without viewcontrol), do the similar job. A DocumentControl application is a light process which does not require to start Navisworks standard process. In addition, you do not need to write a plugin.
The following is the core code of a DocumentControl application. It iterates each file from the argument, saves them to version 2014. Assume the source files are available in the path.
classProgram { [STAThread] staticvoid Main(string[] args) { if (args.Count<string>() > 0) { //single document mode Autodesk.Navisworks.Api.Controls.ApplicationControl.ApplicationType = ApplicationType.SingleDocument; //initialize application control ApplicationControl.Initialize(); //create a DocumentControl DocumentControl documentCtrl = new DocumentControl(); //open a document
foreach (string fullfilename in args) { if (documentCtrl.Document.TryOpenFile(fullfilename)) { string oldName = System.IO.Path.GetFullPath(fullfilename);
string newfullfilename = oldName + "-lowerversion" +".nwd"; //save to a lower version documentCtrl.Document.SaveFile(newfullfilename, DocumentFileVersion.Navisworks2014); } }
//destroy DocumentControl documentCtrl.Dispose(); //terminate application control ApplicationControl.Terminate(); } } } }
|
The VBA code executes the application with the parameters.
Public Sub StartNWApp_WithArgument() strProgramName = "C:\temp\Nw_Without_View_control\bin\Debug\Nw_Without_View_control.exe" Call Shell(strToExecute, vbNormalFocus) |