Here is a little code snippet that you may find helpful. In a recent thread, we were helping a developer who wanted to select a component in the viewer as soon as the model was loaded using viewer.select([nodeId])
It appears that you need to wait for two different events if you want to make sure the selection is going to work reliably:
Autodesk.Viewing.GEOMETRY_LOADED_EVENT
Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT
However there is no guarantee about which event will be fired first upon loading a model. So we could come up with various code constructs that allow to wait for both events but since this is a fairly standard situation in JavaScript asynchronous programming, it's nice to come up with a more generic solution.
I'm using here async, a very handy library that you can install using "bower install async" for example (available also for node npm install async) and which expose many useful methods to wait or delay execution of custom code, see the doc for more details, you'll probably need it at some point if you write enough JavaScript ;)
Here we go, the following sample illustrates how to wait for both viewer events and parse the model structure once they have been fired. As you can tell, it will be very straightforward to add some more events or change them if needed:
Comments
You can follow this conversation by subscribing to the comment feed for this post.