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 Viewpoint
, 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!
Apologies – I commented this on Part 1:
Do you know why the getCamera() method might always throw a System.AccessViolation on a Viewpoint. The repo code has this method call commented out – your screenshots here show you were successful making this call.
LikeLike
Hi Jonathon, maybe some license or access permission issues. You can remove the comments in the repo.
LikeLike
Thanks, I was only curious if the commenting was because you’d run into difficulties.
What I found was that I was accessing this method within an Event and in some circumstances even though the ActiveDocument.CurrentViewpoint and its properties are available, the GetCamera() method throws a corrupted memory error.
Needed wrapping with a special case try…catch using [HandleProcessCorruptedStateExceptions] decorator.
LikeLiked by 1 person