This post was prompted by Sagar’s inquiry about accessing properties of Revit model using View and Data API. He left a comment saying that he followed my tutorial and was able to view the uploaded model successfully. So let’s take View and Data API Intro Lab4 as a starting point and build on top of it. In this post, we are going to add two functionalities that allow you to:
- Select an object and obtain its properties
- Search the model for a given string and isolate them in the view
Below is a sample image showing how a final web page may look like. Notice right above the viewer area; there are “Get Properties” and “Search” buttons along with text fields. We are going to add those.
Modification to Viewer-Embed.js
This allows the calling function to get hold of the viewer object and access various methods of the viewer. Save viewer-embed.js.
Now, let’s move on and add a new functionality to get properties of a selected object in the viewer. To do this, we use two methods of _viewer or an instance of Viewer3D class:
getSelection() takes no argument, and returns the array of the currently selected object id’s.
getProperties() takes three arguments: objectId, onSuccessCallback and onErrorCallback; and upon success, it calls onSuccessCallback. The below shows the minimum code to realize this functionality:
You can set this function as a callback for a “Get Properties” button click, e.g.,
This is it in terms of getting properties of an selected object. You might be finding it easier than you thought.
You should be able to test your code now. Try uploading a model, select one element, and press “Get Properties” button. If everything is working as intended, you will see a list of properties displayed in the text box above the viewing area.
While I have your attention on this topic, let’s add one more functionality. This time we are going to add “search”. Search is probably one of attractive features of View and Data API. You can search through a model for any attributes or properties attached to individual components of the model. For this, we use two methods of _viewer or Viewer3D class:
search() takes four arguments: textToSearch, onSuccessCallback, onErrorCallback, and arrayOfAttributeNames. The fourth one is optional; if omitted, search will be performed for any attributes. And upon success, it calls onSuccessCallback function.
isolate() takes an array of object ids and isolate them or dim objects which is not a part of an array.
We call this to show what objects are returned as a result of search.
In addition, we used clearSelection() to clears current selection.
But this is to clear the functionality we added earlier to get properties of selected object.
The below shows the minimum code to realize this functionality:
You can set this function as a callback for a Search button click, e.g.,
Now time for another test. This time, type in a word, for example, “door” in the small text box next to “Search >>” button, then click “Search >>”. If everything is working as intended, you will see a collection of object isolated in the viewing area. To finish isolation, right click anywhere in the view and click “Show all objects”.
The updated Labs project source code is attached here: Download View Data API Intro Labs_2015 0710
For more information, please refer to View and Data API developer portal. In particular, the following sample and documentation are my recommendation to look at about the viewer functionalities:
- Sample application LmvDbg – a sample web page intended as a leaning tool. *very* useful. Written by Jim Awe.
- Viewer3D Class description on Reference Guide.
For those who haven’t seen my original post about “Hello World” style introduction to View and Data API, I also put the link here: Autodesk View and Data API Intro Overview
Thank you, Sagar, for prompting this topic.