Welcome to the ninth tutorial of
Navisworks® API series, this post is to cover how to get and set Viewpoint’s camera via JSON AutodeskCommonCamera object and, to zoom the Viewpoint’s camera to the selected ModelItem(s). As usual, we’re going to write an add-ins for this.
Setting up Navisworks® Add-Ins project in the visual studio, you can follow
Creating Navisworks Add-Ins tutorial to set up the project. When you are ready, in the
Execute method creates two variables, doc and vpoint to store Document and Viewpoint respectively. To collect
doc.CurrentViewpoint property returns
DocumentCurrentViewpoint object and it includes a method to get a copy of the current Viewpoint object,
CreateCopy() method. (figure 1.1)
Viewpoint includes a method to collect its properties in terms of an JSON AutodeskCommonCamera object (it is actually a json file), we can get/write and export this return json data of viewpoint to a text file and save it on desktop or anywhere you like.
Copy these json data and open it in one of the JSON Formatter/Validator website, here is one. And you will see data structure something like below image, and save it as a JSON format file.
You can modify and play with these numbers from the exported json file. After modified these values, we will uses this JSON AutodeskCommonCamera object to set Viewpoint value. First have to read these json data as a string and provide the string value to SetCamera method as a parameter (figure 1.2).
The last part is to zoom selected Modelitems to the center of the Viewpoint. Viewpoint contains a method called “ZoomBox” which requires a BoundingBox parameter. To collect the BoundingBox of the selected Modelitems, just simply call BoundingBox method with boolean parameter value (true value is not to include hidden items).
That’s all for this tutorial, you can get the complete source code from here, Cheers!