Interacting with the Model Tree
Every model loaded into the Web Viewer component has a product structure that contains a tree-like hierarchy of nodes as well as node properties and drawing attributes.
In this section, we'll explore how to parse the model tree and how to support bidirectional highlighting. We also learn how to switch between CAD Views.
- Model Structure Concepts
- How to Parse the Model Tree
- Selection Callbacks
- Bidirectional Selection
- Parsing CAD Views
Creating the Model Tree Class
Once again, we start with an empty JS file which will contain our model tree class and is responsible for displaying the model tree. In the constructor, we will pass the id of the <div> element that we want to use for displaying the model tree. We also reserve a variable for the actual HTML content which we will be generating when parsing the tree.
Add the new JS file to your application. In the code below the new JS file was named ModelTree.js
Function to Generate Model Tree in the DOM
Let's add a function that generates the model tree called generate. Simply add it to the new ModelTree class.
This function will then kick off the recursive process of going through all the model tree nodes and writing them to the DOM:
Every node in the Web Viewer component is identified by a "NodeId" which is a unique identifier that is created during conversion/authoring and will not change between subsequent loads.
Aside from messages, no part of the HC model tree is ever instanced - each node in the tree only has one parent.
In this example, we first determine the type of a node to see if it is relevant for the model tree, then get its name to add it to the node list in the DOM. From there, we query its children and process each one until the leaf nodes of the tree are reached and there are no further children. Please see the Programming Guide for more information on the different node types.
Generating the Model Tree
To generate the model tree, we first initialize the variable during startup in the main file of the application right before the hwv gets started:
Then, in the modelStructureReady callback, we can generate the model tree:
Add the following code to the body of your HTML page. Ensure the id matches the one used in the ModelTree constructor.
If you run the application the result should now look like this:
Adding Node IDs to DOM Elements
Let's add some code to allow for a bidirectional selection so that if we click on a node in the tree the corresponding element(s) in the view will be highlighted and vice versa. Let's start with selection from the model tree. To facilitate that we need to modify the code that generates the model tree a bit:
We are giving every <div> tag an ID corresponding to the NodeId of the node it is associated with and setting up an onclick callback that gets triggered when the node text is selected.
Selection from DOM
The next step is to add the function that gets executed when the node has been clicked on to the ModelTree class (add as a member function to ModelTree):
This function simply uses the NodeId of the selected node in the model tree and highlights the associated part. It also changes the color of the DOM element. There is some additional logic (using the lastid variable) to ensure that a previously selected item is correctly deselected. For that we need to add an additional variable to the constructor:
Run the application again. You should be able to click on a node in the model tree and see the associated element highlighted in the view:
Handle Selection from the Web Viewer component
Let's add selection from the view as well so that the DOM element associated with a piece of geometry in the view is highlighted when that element is selected by the user.
First, let's add a function to the ModelTree class that gets called when a selection has occurred in the Web Viewer.
This function takes a selection event and retrieves its NodeId and then highlights the appropriate element in the DOM. It also ensures that a previous highlighted element is deselected.
Wiring up Selection Callback
Now we need to wire up the selection callback so that whenever a selection occurs the onSelection function is triggered. To do this we need to add another callback handler in our main document:
If you start the application again, you should see that if you click on an element in the Web Viewer window, the associated model tree node is highlighted and vice versa.
Displaying CAD Views
Another important part of the model tree are the CAD Views of a model. Let's add those to the DOM as well. For that we will be using a different model that has CAD Views embedded which can be found here:
Copy this file into the data folder, and make sure to load this file instead of microengine on startup:
To gather the CAD Views we create another very straightforward function. As you can see CAD Views are queried via a separate function call and are not part of the main tree (add as a member function to ModelTree):
Now let's make sure that we call this new function when generating the tree:
The CAD Views get their own callback to handle selection:
In addition, they need their own variable to handle a previous selection state:
You now have created a rudimentary version of a bidirectional model tree that allows for highlighting from the tree, as well as the Web Viewer window. As mentioned earlier the HOOPS Web Viewer ships with a full UI which includes a much more sophisticated model tree. It is your decision if you want to use this model tree in your application or write your own. For more information on parsing the assembly tree, please consult the Programming Guide.