By Philippe Leefsma (@F3lipek)
Whether you are a frequent traveler or not, if you are developing a Web application using the Forge Viewer chances are that at some point you may be interested in doing offline viewing, that is loading your models not from the Autodesk Cloud but from the local machine. I can see various advantages of doing that: the first one obviously is being able to develop and work on your app with no internet connection at all, but even if you do have a connection, you may enjoy faster loading time of your model when you have to repeatedly reload the scene to see your code modifications when working on a viewer plugin. Other customers may be interested in running a viewer application in a closed environment or behind firewalls of their company with no http calls to the outside world. The offline approach applies to this type of requirement as well...
My application is using the following technologies: NodeJS server, mongoDB database, RESTful API, React and ES6 for the building the frontend. The details I'm exposing here relate to that specific stack, however the concepts I am discussing in that post should easily apply to any other backend/frontend stack combination you may use on your side.
Here is the general idea of the workflow: you first need to upload and translate the models you want to view offline. Typically when you develop your application, one or a few different models can be used for testing purpose, so you won't have to store locally all the designs your app is working with. Once you have your local models, they can be served as static resources by your server. You use the database to store information about each model, including a specific field that indicates if the model is local or Cloud-based. The bootstrapping JavaScript code that initializes the viewer will first look at the info in the database prior to load the model and either perform local or Cloud initialization. That way when using the local database in dev, models will be loaded from the local files and when your app is connected to the production database, models will be loaded from the Autodesk Cloud. All of that being transparent to the JavaScript code in your app.
Here are the detailed steps you can follow to get that set up:
1/ Upload your design to Autodesk Cloud and use the Model Derivatives API to generate the svf viewable format: see our Prepare a File for the Viewer step-by-step tutorial
2/ Upon successful translation, download the design package locally. You can use the following npm package for that: view-and-data-npm. Even if it is still relying on the V1 API, it does the job. See the "Offline" section in the readme
3/ Populate your mongoDB database with the correct records. In the case of my app, it is using a models collection with the following schema for each record:
1 {
2 "_id" : ObjectId("57efaf0377c8eb0a560ef467"),
3 "urn" : "dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6bGVlZnNtcC1mb3JnZS1kZXYvZW5naW5lLmR3Zg",
4 "name" : "Engine",
5 "path" : "resources/models/engine/0.svf",
6 "env" : "Local",
7 "options" : {
8 //some app specific options
9 },
10 "thumbnail" : "... base64 encoded thumbnail of the model ... "
11 }
The path field has to point to the location of the .svf file in the static resources route server by your server
the env field has to be set to Local for offline viewing and AutodeskProduction for Cloud-based viewing. That is the only field that is changed between my development and production databases
4/ You also need a complete local copy of the Viewer API files. There is more than the .css and .js you would reference in an online setup. The Viewer will dynamically fetch the extra scripts and resources needed once it gets loaded. It will look for those file relatively to the viewer3D.js or viewer3D.min.js that you loaded in your app, so if you keep the file hierarchy of the lib, things should be ok. You can find all the required files for the v 2.10 of the API in my project.
5/ The rest of the job will be handled by the JavaScript code initializing your viewer application based on the env field set in the DB when loading a specific model. You will pass that value to Autodesk.Viewing.Initializer and let the viewer lib do the initialization. Below is the code that I'm using to seamlessly load a design offline or online. You can notice that in the offline case, we already know the viewable path that we want to load. In online case you need to first load all viewable path and select the one you want to load - in case your document has multiple views - my code here is simply loading the first viewable path, but you can tweak that based on your needs:
6/ I am using an .ejs template to generate my main html layout, this way when creating a dev build of my app, it will use the local viewer3D.js and when creating a prod build, it will reference the online version. The template is being generated by the webpack build, you can see my build config there.
Having worked with the viewer API for a while now, I came to use that setup by refining my experience with those tools and I find it pretty convenient so far.
The complete project applying all those concept is located at:
https://github.com/Autodesk-Forge/forge-rcdb.nodejs