0

When the user clicks 'Set current view as Home' on the drop-down arrow to the bottom-right of the 3d Cube, the selected view is not preserved the next time the model is loaded into the viewer. It is only preserved for the currently loaded session in the viewer. How can we get this setting to be preserved between loads?

Don Porter
  • 45
  • 4
  • Could it be that 'Home' is the home orientation for the viewer, not the model? it's obvious that the viewer isn't preserving state between loads; maybe you need to install a cookie in the browser that notes the view from previously viewed models. – Jakob Lovern Jan 03 '18 at 22:22

2 Answers2

0

Something that you could do is to getState of the current view you wish your viewer to start at the time of reloading. When you load the next time, you can add the restoreState with the JSON object you obtain from the previous call and you can trigger this at the time of geometry_load_event.

Here are the steps of what I'm talking about.

Use viewer.getState function like this

JSON.stringify(viewer.getState())

If you console log or assign it to a variable this will have a complete JSON object with all the information of the current state of your model. If any object is hidden, if it is explode and so on.

After you have that you can test this by moving the camera again and later use the following to restore the state to the previous one you tried.

viewer.restoreState(YOURSTATE)

If you want your viewer to load at a specific state you will need to pass that restoreState function on the GEOMETRY_LOADED_EVENT

You can find the documentation about all 3 I just mentioned here https://developer.autodesk.com/en/docs/viewer/v2/reference/javascript/viewer3d/

halfer
  • 19,824
  • 17
  • 99
  • 186
Jaime Rosales
  • 1,106
  • 1
  • 6
  • 8
0

You could use the localStorage to store the view you define as "home" and reload that view upon a new session. Something along those lines... :

localStorage.setItem('homeView', JSON.stringify(viewer.getState({viewport:true})))

// ...

var state = localStorage.getItem('homeView')

// restoreState (state, filter, immediate)
viewer.restoreState(JSON.parse(state), {viewport:true}, true)

viewer.autocam.setHomeViewFrom(viewer.navigation.getCamera()) 

Alternatively you could of course use a more advanced storage strategy, for example using a database ...

Felipe
  • 4,325
  • 1
  • 14
  • 19
  • Thank you for the response. We were also wondering about the inconsistent behavior between Revit and CADmep models uploaded to BIM360. It seems that if the "Set this view as home" button is clicked in CADmep, after the model is published it loads that view initially into the Forge viewer, but when it is set in Revit, it does not exhibit this behavior. Wanted your thoughts on detecting when the user clicks the Set View as Home button in the forge viewer. My thought is maybe add a second click event to the DOM for that link, but didn't know if there was a built in hook already to do it. – Don Porter Jan 04 '18 at 14:23
  • Revit and Acad translators are implemented differently, so unfortunately the behaviour of the models in the viewer may change a bit. If you can control how the model are being uploaded to Forge - eg through an addin - then I would recommend you store the desired home view in a side database and restore it by default upon loading the model, I understand it might not be an easy workaround to put in place but I see no better solution at the moment. – Felipe Jan 04 '18 at 23:21
  • Concerning your second question - there is no API to hook into "Set this view as home" AFAIK. A little JS hack as you suggest to either replace the button by your own one or attach a second handler to the DOM element seems the most appropriate. – Felipe Jan 04 '18 at 23:22