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.
var config = require('your-server-config/config-view-and-data');
var Lmv = require('view-and-data');
var lmv = new Lmv(config);
function onError(error) {
console.log(error);
}
function onInitialized(response) {
var createIfNotExists = true;
var bucketCreationData = {
bucketKey: config.defaultBucketKey,
servicesAllowed: {},
policy: 'transient'
};
lmv.getBucket(config.defaultBucketKey,
createIfNotExists,
bucketCreationData).then(
onBucketCreated,
onError);
}
function onBucketCreated(response) {
lmv.upload(path.join(__dirname, './data/test.dwf'),
config.defaultBucketKey,
'test.dwf').then(onUploadCompleted, onError);
}
function onUploadCompleted(response) {
var fileId = response.objects[0].id;
urn = lmv.toBase64(fileId);
lmv.register(urn, true).then(onRegister, onError);
}
function onRegister(response) {
if (response.Result === "Success") {
console.log('Translating file...');
lmv.checkTranslationStatus(
urn, 1000 * 60 * 5, 1000 * 10,
progressCallback).then(
onTranslationCompleted,
onError);
}
else {
console.log(response);
}
}
function progressCallback(progress) {
console.log(progress);
}
function onTranslationCompleted(response) {
console.log('URN: ' + response.urn);
lmv.getThumbnail(urn).then(onThumbnail, onError);
}
function onThumbnail(response) {
}
lmv.initialize().then(onInitialized, onError);
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 = new Lmv(config);
function onError(error) {
done(error);
}
function onInitialized(response) {
lmv.download(urn, '.test/data/download').then(
onDataDownloaded,
onError
);
}
function onDataDownloaded(items) {
console.log('Model downloaded successfully');
var path3d = items.filter(function(item){
return item.type === '3d';
});
console.log('3D Viewable path:');
console.log(path3d);
var path2d = items.filter(function(item){
return item.type === '2d';
});
console.log('2D Viewable path:');
console.log(path2d);
}
lmv.initialize().then(onInitialized, onError);
To load the model from downloaded package:
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>
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