Skip to content

This sample is a collection of extensions ready to be reused. Just add reference to the required files, load and use.

License

Notifications You must be signed in to change notification settings

autodesk-platform-services/aps-extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aps-extensions

Node.js npm Platforms License

oAuth2 Data-Management Model-Derivative Viewer

Level

Description

This sample is a collection of extensions ready to be reused. Just add reference to the required files, load and use. Check each extension documentation for details.

  1. Camera Rotation
  2. Icon Markup
  3. Nested Viewer
  4. Transform
  5. GoogleMapsLocator
  6. Draw Tool Extension
  7. Custom Properties Extension
  8. XLS Extension
  9. Edit2D Extension
  10. Phasing Extension

Extensions were created using a Basic Skeleton.

This sample is based on the Learn APS tutorials in the section View modelss.

Thumbnail

thumbnail

Live Demo

Extensions are dynamically loaded and unloaded for testing on the live version.

aps-extensions.autodesk.io

Setup

To use this sample, you will need Autodesk developer credentials. Visit the APS Developer Portal, sign up for an account, then create an app. For this new app, use http://localhost:3000/api/aps/callback/oauth as the Callback URL, although it is not used on a 2-legged flow. Finally, take note of the Client ID and Client Secret.

Run locally

Install NodeJS.

Clone this project or download it. It's recommended to install GitHub Desktop. To clone it via command line, use the following (Terminal on MacOSX/Linux, Git Shell on Windows):

git clone https://github.com/autodesk-platform-services/aps-extensions.git

To run it, install the required packages, set the enviroment variables with your client ID & Secret and finally start it. Via command line, navigate to the folder where this repository was cloned to and use the following commands:

Mac OSX/Linux (Terminal)

npm install
export APS_CLIENT_ID=<<YOUR CLIENT ID FROM DEVELOPER PORTAL>>
export APS_CLIENT_SECRET=<<YOUR CLIENT SECRET>>
npm start

Windows (use Node.js command line from the Start menu)

npm install
set APS_CLIENT_ID=<<YOUR CLIENT ID FROM DEVELOPER PORTAL>>
set APS_CLIENT_SECRET=<<YOUR CLIENT SECRET>>
npm start

Open the browser: http://localhost:3000.

Steps to plug in new extension

  1. Create folder in public/extensions with same name as extension name. Structure of the extension folder is as shown below:
ExtensionName[Folder]
        | 
        |->contents
        |     |
        |     |->main.js
        |     |->main.css
        |     |->assets[folder]
        |->config.json

Refer the BasicSkeleton Extension for boilerplate code.

  1. Each extension folder should be self-contained code, so that it's easily shareable between projects. Extension[Folder]/config.json is meant for keeping the config of an extension and for sharing.

Extension config schema:

{
    "name":"extension name registered",
    "displayname": "display name for the extension in list",
    "description": "description for the extension",
    "options":{model specific information array to pass on to extension constructor},
    "viewerversion":"viewer version",
    "loadonstartup": "true or false",
    "filestoload":{
        "cssfiles":["css file(s)"],
        "jsfiles":["js file(s)"]
    },
    "bloglink":"Blog link for working explanation of the extension (optional)",
    "includeinlist":"true or false"
}

Example: IconMarkupExtension config.json

Note: If your extension relies on event Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT to load, in load function check if the data is already loaded, if not only then add the event listener, below code shows the structure.

class MyExtension extends Autodesk.Viewing.Extension {
    ...
    load() {
        ...
        if (this.viewer.model.getInstanceTree()) {
            this.onTreeReady();
        } else {
            this.viewer.addEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT, this.onTreeReady.bind(this));
        }
        ...
    }
    ...
    onTreeReady() {
        const tree = this.viewer.model.getInstanceTree();
        ...
    }
    ...
}

Example: IconMarkupExtension load function

Note: After the new extension is added, please restart the server.

Understanding extensionloader and using it in aps app

The way loose coupling between extensions and aps app is achived is with custom event, if you want to use extensionloader in your aps app, follow the three steps:

  1. Copy paste the extensions in public folder of your app or in the folder where the index file resides.

  2. Include below script in index.html file

<script src="/extensions/extensionloader.js"></script>
  1. Here's the linking part between the app and the extensionloader, in viewer onDocumentLoadSuccess function, emit an event to inform the extensionloader that viewer has loaded the model with the below code:
var ViewerInstance = new CustomEvent("viewerinstance", {detail: {viewer: viewer}});      
document.dispatchEvent(ViewerInstance);

To load an extension programmatically, emit the below event.

 var LoadExtensionEvent = new CustomEvent("loadextension", {
              detail: {
                extension: "Extension1",
                viewer: viewer
             }
         });
 document.dispatchEvent(LoadExtensionEvent);
 

To unload extension:

 var UnloadExtensionEvent = new CustomEvent("unloadextension", {
              detail: {
                extension: "Extension1",
                viewer: viewer
             }
         });
 document.dispatchEvent(UnloadExtensionEvent);

Note: If the extension needs additional UI elements, first option we suggest is use the viewer UI Autodesk.Viewing.UI.DockingPanel

Packages used

The Autodesk Platform Services packages are included by default. Some other non-Autodesk packages are used, including express and multer for upload.

Tips & tricks

For local development/ testing, consider using the nodemon package, which auto-restarts your node application after any modification to your code. To install it, use:

sudo npm install -g nodemon

Then, instead of npm run dev, use the following:

npm run nodemon

Which executes nodemon server.js --ignore www/, where the --ignore parameter indicates that the app should not restart if files under the www folder are modified.

Troubleshooting

After installing GitHub Desktop for Windows, on the Git Shell, if you see the error setting certificate verify locations error, then use the following command:

git config --global http.sslverify "false"

License

This sample is licensed under the terms of the MIT License. Please see the LICENSE file for full details.

Written by

The APS Advocates team:

About

This sample is a collection of extensions ready to be reused. Just add reference to the required files, load and use.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages