Search code examples
node.jsautodesk-forgeautodesk-viewerautodeskautodesk-model-derivative

How to Download Object Derivatives as SVF in Node.js Using Autodesk APS API?


I am trying to download object derivatives as SVF using Autodesk's Platform Services (APS) API in a Node.js application. I can retrieve the manifest and identify SVF-related derivatives, but I am struggling to programmatically download all the required files (e.g., .svf, .sdb, .json, etc.) to a specified local directory.

In Visual Studio Code, the Autodesk APS extension allows me to right-click a model and select "Download Model Derivatives as SVF," which works perfectly. I want to replicate this functionality in Node.js.

Here is what I have done so far:

Authenticated using the @aps/node SDK to retrieve the access token. Fetched the object manifest using the DerivativesApi.getManifest method. Attempted to download derivative files using the getDerivativeManifest method. However, I am unsure how to properly download and save all related files in a way that matches the VS Code extension's behavior.

const fs = require('fs');
const path = require('path');
const { AuthClientTwoLegged, DerivativesApi } = require('@aps/node');

const CLIENT_ID = process.env.APS_CLIENT_ID;
const CLIENT_SECRET = process.env.APS_CLIENT_SECRET;
const OBJECT_URN = 'your-object-urn'; // Base64 encoded URN
const OUTPUT_DIR = './downloads'; // Directory to save files

async function downloadSVF() {
    const authClient = new AuthClientTwoLegged(CLIENT_ID, CLIENT_SECRET, ['data:read'], true);
    const token = await authClient.authenticate();
    const derivativesApi = new DerivativesApi();

    // Get manifest
    const manifest = await derivativesApi.getManifest(OBJECT_URN, {}, { authorization: `Bearer ${token.access_token}` });
    const derivatives = manifest.body.derivatives;

    for (const derivative of derivatives) {
        if (derivative.outputType === 'svf') {
            for (const child of derivative.children) {
                const fileUrl = child.urn;
                const fileName = path.basename(fileUrl);
                const filePath = path.join(OUTPUT_DIR, fileName);

                console.log(`Downloading: ${fileUrl} -> ${filePath}`);

                const response = await derivativesApi.getDerivativeManifest(OBJECT_URN, fileUrl, {}, { authorization: `Bearer ${token.access_token}` });
                fs.writeFileSync(filePath, response.body);
            }
        }
    }
}

downloadSVF().catch(console.error);

How can I ensure that all related files (e.g., .svf, .sdb, .json) are downloaded as expected, similar to the VS Code extension? Is there a specific API endpoint or workflow to mimic the VS Code extension's "Download Model Derivatives as SVF" functionality? Are there any best practices for handling large derivative files or ensuring file integrity during download? Any guidance, code examples, or references would be greatly appreciated!


Solution

  • Have a look at this utility: https://github.com/petrbroz/svf-utils

    This part shows how you can use it to download SVF content: https://github.com/petrbroz/svf-utils/blob/develop/samples/download-svf.js