Settings up the Viewers
In this chapter, we will walk through setting up two web viewers – one to display the full assembly of our configured model and one to give a preview view of a selected component.
- Instantiating and starting the WebViewers
- Setting callbacks
- Configuring additional WebViewer options
Create the main entry and structure for the application
To get started, we will create a new JS file titled app.js and place it in the src/js folder. The app.js file will contain our main application logic, as well as manage the setup and handling of our WebViewer object.
To better organize our code, we will be breaking up our code and using the external module approach for this development. We will be using the ES6 class syntax to encapsulate functionality. Let us start by making the main class in our app.js file. This is where we will instantiate our viewer and build our application through composition from other objects we will create later. Right now, we will just leave the class and constructor blank and return to it.
Setup, instantiate, and start the WebViewer objects
As discussed in the Building A Basic Application tutorial, HOOPS Communicator requires that the DOM content and structure be loaded before trying to instantiate any WebViewer objects, since they will look for a <div> ID to populate. Because of this, we will instantiate our main class within the onload browser event callback, once all the DOM content has been loaded.
Lastly, since we have provided the CSS file for you, you can import the CSS at the top of the app.js file. Your app.js file should now look something like this:
Because we have provided the HTML and styling, we should see the <div> element present that we can instantiate our WebViewers into. We will use these <div> IDs when setting up our viewer.
We can see that the <div> IDs we are interested in are called viewer and comp-viewer. We will do the same procedure documented in the Building A Basic Application tutorial to instantiate an empty viewer. We will instantiate with empty viewers since we do not know which model the user will want to load. Go ahead and write the following code in the constructor of your main class, using the viewer and comp-viewer <div> IDs.
If you look back at your web application, you will see nothing has changed. This is because we have not yet started the viewer - we only instantiated it. We could store the viewers in an array and use the map() function, like in the Additive Manufacturing tutorial, but since we do not have too much to set on the component viewer, we will simply explicitly start them both.
Again, you may be wondering why nothing has changed – don't worry, we haven't loaded a model yet (you can specify a model when instantiating the viewer, but we elected not to).
Create the WebViewer callback skeletons
The next step is to setup the callbacks on the WebViewer. If you are unfamiliar with basic callback usage in HOOPS Communicator, or need a refresher, please review the callbacks section of the Building A Basic Application tutorial. For our application, we are interested in two main callbacks – modelStructureReady and selectionArray.
Because we want to load a model to the scene after we have instantiated and started the viewers, we need to wait for the modelStructureReady callback to fire, or else we will get an error to trying to access the Communicator.Model class too quickly. Since models are going to be loaded when the user clicks on a component, we want to ensure those UI components cannot be triggered before the modelStructureReady callback is fired, or else we will get a model load failure. By default, these anchor elements have no reference, so clicking them should not initiate any unwanted behavior. We will set up the behavior of these anchors later in this tutorial.
From the comments, you can see we will be performing several steps in the modelStructureReady callback.
When the modelStructureReady callback is fired, we should be able to add some additional elements to our scene, like a navigation cube and axis triad. By default, you can access the navigation cube and axis triad via the Communicator.View object in the WebViewer. We will retrieve these objects, enable them, and move the navigation cube from its default position to the lower right corner of the scene.
In addition, we will spice up our viewers by adding a colored background consistent with our UI theme. This is as simple as calling setBackgroundColor on the Communicator.View object. You can specify a single background color, or make a gradient from top to bottom by providing two colors. We will choose to make the gradient.
Lastly, we will be working with many independent models from multiple sources. It is impossible to say whether our model data vertices were constructed appropriately (hopefully they were), but to ensure the rendering is complete, we will enable rendering of the backfaces. This ensures that any culling based on the orientation of vertices will not result in missing facets in our model. For the model data we have provided, for example, one model is displayed with missing triangles if we do not enable this option (rotating the camera 180 degrees around the model would display these faces, but then the previously displayed faces would disappear as they are now being viewed from behind). This is because the model author did not consistently construct the model faces. When enabling the backfaces, we can see both sides of the face from any camera angle.
With this, our model structure ready callback for the main viewer should be complete.
We can perform the same desired operations on the component viewer, though we do not need the nav cube and axes in this viewer.
We will use the selectionArray callback later to add additional functionality to our scene.