Most of the content of the NavisWorks API is two development, adding the required functionality to the NavisWorks software.
And the job I was given was to open and interact with files in. NWC and other formats through the window. That is, you need a program to invoke the API to open the Navisworks file.
In the. NET API Reference Guide In fact, there is a more complete example, you can implement a lot of features, here I will combine the API documentation and some other information I found on the network, specifically for this aspect to explain, on the one hand, to leave a record of their work, On the other hand, we hope that friends with similar development tasks can communicate together.
The first step is to introduce two dll:Autodesk.Navisworks.Api and Autodesk.Navisworks.Controls in the program. These two DLL files can be found in the "~\autodesk\navisworks Manage 2014" directory.
The Program.cs file is written directly to the sample in the document:
1 usingSystem;2 usingSystem.Windows.Forms;3 usingAutodesk.Navisworks.Api.Controls;4 5 namespaceViewer6 {7 Static class Program8 {9 /// <summary>Ten ///The main entry point for the application. One /// </summary> A [STAThread] - Static voidMain () - { the //Set to Single document mode -Autodesk.Navisworks.Api.Controls.ApplicationControl.ApplicationType =applicationtype.singledocument; - - //initialise the API + Autodesk.Navisworks.Api.Controls.ApplicationControl.Initialize (); - + application.enablevisualstyles (); AApplication.setcompatibletextrenderingdefault (false); atApplication.Run (NewViewer ()); - - //Finish use of the API. - Autodesk.Navisworks.Api.Controls.ApplicationControl.Terminate (); - } - } in}
Add a Viewcontrol control to the window design, which is used to implement the file display, and then add a Documentcontrol control, which is used to control the file.
Set the Documentcontrol property of the Viewcontrol control to the Documentcontrol control you just added to specify that the Viewcontrol show Documentcontrol content.
Add a menu bar or button to open the file function. You can select a file by opening a file Browse window, or you can specify the file you want to open directly in the background. My task this time is to require the direct loading of the specified file, do not allow users to freely select open files. So I omitted the code to open the File browser window. The code that enables Documentcontrol to open the specified file is as follows:
1 // Open dialog box for selected files in Dlg 2 DocumentControl.Document.TryOpenFile (dlg. FileName); 3 4 // OR 5 // Open the specified file directly 6 documentControl.Document.TryOpenFile ("res\\ test file. NWC");
With the above operation, you can open the specified. nwc file in a window.
Next, add a description of how to interact with the open file.
First, don't forget to add a reference to the program in the window designer:
using Autodesk.Navisworks.Api.Controls; using Autodesk.Navisworks.Api;
In the window to add a few keys, to achieve the translation of the image and other functions. If you like to pursue visual effects, you can also use toolbar.
Here I have implemented the most basic functions, in general, through similar statements to invoke the method. A detailed approach can be achieved by referencing the tool enumeration in the API manual.
1 #regionNavigate View2 //Rotation Model3 Private voidOrbit_click (Objectsender, EventArgs e)4 {5DocumentControl.Document.Tool.Value =Autodesk.Navisworks.Api.Tool.NavigateFreeOrbit;6 }7 8 //scale model9 Private voidZoom_click (Objectsender, EventArgs e)Ten { OneDocumentControl.Document.Tool.Value =Autodesk.Navisworks.Api.Tool.NavigateZoom; A } - - //panning a model the Private voidPan_click (Objectsender, EventArgs e) - { -DocumentControl.Document.Tool.Value =Autodesk.Navisworks.Api.Tool.NavigatePan; - } + - //Reset Model + Private voidReset_click (Objectsender, EventArgs e) A { at //There is no reset method in the API, where the load file is called to implement the reset - loaddocument (); - } - - //Component Radio Selection - Private voidSinselect_click (Objectsender, EventArgs e) in { -DocumentControl.Document.Tool.Value =Autodesk.Navisworks.Api.Tool.Select; to } + - //Component Marquee Selection the Private voidMulselect_click (Objectsender, EventArgs e) * { $DocumentControl.Document.Tool.Value =Autodesk.Navisworks.Api.Tool.SelectBox;Panax Notoginseng } - the #endregion
In addition, in this development task, you also need to implement the ability to focus the lens on the specified components. Since I have not used Navisworks before, it is not very clear whether such a function already exists. In the end I can only use a very stupid method to achieve, but this method is very imperfect, first look at the following code:
1 Private voidLocation_click (Objectsender, EventArgs e)2 {3 //Create a new search4Search s =NewSearch ();5 6 //set the selection to everything7 S.selection.selectall ();8 9 //Add a search conditionTen //Modify the component name to enable the ability to select different components One S.searchconditions.add (Searchcondition.haspropertybyname (Propertycategorynames.item, DataPropertyNames.Ite Mname) A. Equalvalue (Variantdata.fromdisplaystring ("Component Name"))); - - //get the resulting collection by applying this search theModelitemcollection searchresults = S.findall (Autodesk.Navisworks.Api.Application.ActiveDocument,false); - - //clears the currently selected - Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.Clear (); + - + //Show the results A foreach(ModelItem miinchsearchresults) at { - AUTODESK.NAVISWORKS.API.APPLICATION.ACTIVEDOCUMENT.CURRENTSELECTION.ADD (MI); - } - - //Automatically Center search results - Autodesk.Navisworks.Api.Application.ActiveDocument.ActiveView.FocusOnCurrentSelection (); in}
As you can see, first a search was made for the component with the specified name. This is not the only way to search for the statement, the other search methods set the conditions of the parameters are not the same, you can use the search criteria to call different search methods.
When the results are searched, the search results are selected and the center method is called to fix the view on the search results. However, the problem with this approach is that the auto-centering method does not automatically adjust the angle of view and scale, that is, when the method is applied to the selected component, the viewing angle may be very ugly, and the size is inappropriate. How this is optimized remains to be studied later.
. NET opens and interacts with the. nwc file in a window via the Navisworks API