(This is a continuation from the previous post: Lab4 “Forge API Web Intro JS”)
This post was prompted by an inquiry about accessing properties of Revit model using Forge API. He left a comment saying that he followed my tutorial and was able to view the uploaded model successfully. So let’s take Forge 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 viewer
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 a text field. We are going to add those.
Modification to viewerBasic.js
This allows the calling function to get hold of the viewer object and access various methods of the viewer. Save viewerBasic.js.
Now, let’s 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.
You can call this function from a callback function for “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.
Let’s add one more functionality. This time we are going to add “search”. Search is probably one of attractive features of Forge 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.
This is to clear the result we added earlier to get properties of a selected object.
The below shows the minimum code to realize our search functionality. viewerSearch() takes a viewer object and a search string as input arguments, and isolate elements identified as result of search. The output text area is to display the message in case something did not go as expected in this case:
You can set this function in the 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 within the viewer area and click “Show all objects”.
You can get the source code for Lab 5 from here: Lab5
For those who haven’t seen my original post about “Hello World” style introduction to Forge API, I also put the link here: Forge API Intro Labs Overview