Autodesk View and Data API NPM Package
Description
An NPM package for Autodesk View & Data API.
Setup
-
As usual:
$ npm install view-and-data
-
Request your own API keys from our developer portal developer.autodesk.com.
-
Replace the credentials placeholders with your own keys in
config-view-and-data.js
or use ENV variables:ConsumerKey: process.env.LMV_CONSUMERKEY || '<replace with your consumer key>', ConsumerSecret: process.env.LMV_CONSUMERSECRET || '<replace with your consumer secret>'
-
Set up the default bucket name defined by the
defaultBucketKey
variable. -
Copy the file
config-view-and-data.js
to your server config directory.
Test
This test will upload a sample file (/test/data/test.dwf) and translate, then show the URN.
Make sure to set up your consumer key and secret and the default bucket name in config-view-and-data.js
as described above (the test will look for this config file!), Change the bucket name to a bucket you actually own in test/test.js
, then run the following commands in the node_modules/view-and-data/
folder:
$ npm install
$ npm test
Usage
Here is a simple example on how to use the library. It will retrieve or create the specified bucket (you will need to modify that value in config-view-and-data.js or provide directly a unique bucket name, see that article for more details). Then it will upload the test.dwf file, monitor it's translation status and get the thumbnail of model if the translation is successful.
//Make sure config-view-and-data.js is copied at indicated location //in your server and that you filed up the API credentials as indicated above var config = ; var Lmv = ; var lmv = config; //you probably want a more specific error handler... { console; } //wrapper is initialized. Token refreshment will happen automatically //no need to worry about it { var createIfNotExists = true; var bucketCreationData = bucketKey: configdefaultBucketKey servicesAllowed: {} policy: 'transient' //['temporary', 'transient', 'persistent'] ; lmv; } //bucket retrieved or created successfully { //see resumableUpload instead for large files lmv; } //upload complete { var fileId = responseobjects0id; urn = lmv; lmv; } //registration complete but may have failed //need to check result { if responseResult === "Success" console; lmv; else console; } //optional translation progress callback //may be used to display progress to user { console; } //file ready for viewing { console; lmv; } //thumbnail retrieved successfully { //response: base64 encoded thumbnail data //ex: var imgsrc = "data:image/png;base64," + response; //<img src=imgsrc> } //start the test lmv;
Offline
The package now allows you to download the full viewable package on your disk to run the viewer completely offline. Here is an example that illustrates downloading a model packge from its URN:
var lmv = config; { ; } { // downloads package to target directory, // creates recursively if not exists lmv; } { console; var path3d = items; console; console; var path2d = items; console; console; } //start the test lmv;
To load the model from downloaded package:
- In your html:
Grab the zip in "lmv" directory and unzip its content in your client lib folder. Then reference style.css and viewer3d.js (or style.min.css/viewer3d.min.js for production).
<link rel="stylesheet" type="text/css" href="lib/lmv-local/version/style.css">
<script src="lib/lmv-local/version/viewer3D.js"></script>
- In your javascript:
Load an available viewable path obtained as output of the download.
//<div id="viewerContainer"></div> in your html
var viewer = new Autodesk.Viewing.Private.GuiViewer3D(
document.getElementById('viewerContainer'));
var options = {
path: viewablePath[0].path,
env: 'Local'
};
Autodesk.Viewing.Initializer (options, function () {
viewer.initialize();
viewer.load(options.path);
});
License
This sample is licensed under the terms of the MIT License. Please see the LICENSE file for full details. The Autodesk Viewer is not under MIT License but copyright by Autodesk, Inc.
Written by
Autodesk Forge, 2016