Navisworks API : Viewpoint (Part-2)

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.

Set Camera via JSON AutodeskCommonCamera
Zoom Selection

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)

figure 1.1 – Get Viewpoint

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.

GetCamera Method

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.

JSON AutodeskCommonCamera object

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).

figure 1.2 – Set Viewpoint

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).

figure 1.3 – ZoomBox method

That’s all for this tutorial, you can get the complete source code from here, Cheers!

Author: min.naung

Founder and Developer @twentytwo.space

3 thoughts on “Navisworks API : Viewpoint (Part-2)”

  1. 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.

    Like

      1. 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.

        Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.