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.
- Camera Rotation
- Icon Markup
- Nested Viewer
- Transform
- GoogleMapsLocator
- Draw Tool Extension
- Custom Properties Extension
- XLS Extension
Extensions were created using a Basic Skeleton.
This sample is based on the Learn Forge tutorials in the section View modelss.
Extensions are dynamically loaded and unloaded for testing on the live version.
To use this sample, you will need Autodesk developer credentials. Visit the Forge Developer Portal, sign up for an account, then create an app. For this new app, use http://localhost:3000/api/forge/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.
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-forge/forge-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 FORGE_CLIENT_ID=<<YOUR CLIENT ID FROM DEVELOPER PORTAL>>
export FORGE_CLIENT_SECRET=<<YOUR CLIENT SECRET>>
npm start
Windows (use Node.js command line from the Start menu)
npm install
set FORGE_CLIENT_ID=<<YOUR CLIENT ID FROM DEVELOPER PORTAL>>
set FORGE_CLIENT_SECRET=<<YOUR CLIENT SECRET>>
npm start
Open the browser: http://localhost:3000.
- 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.
- 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.
The way loose coupling between extensions and forge app is achived is with custom event, if you want to use extensionloader in your forge app, follow the three steps:
-
Copy paste the extensions in public folder of your app or in the folder where the index file resides.
-
Include below script in index.html file
<script src="/extensions/extensionloader.js"></script>
- 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
The Autodesk Forge packages are included by default. Some other non-Autodesk packages are used, including express and multer for upload.
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.
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"
This sample is licensed under the terms of the MIT License. Please see the LICENSE file for full details.
The Forge Advocates team:
- Varun Patil @VarunPatil578
- Petr Broz @ipetrbroz
- Augusto Goncalves @augustomaia