Coder Social home page Coder Social logo

sermonis / autodesk-forge-extensions-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from petrbroz/forge-extensions

0.0 0.0 0.0 25 MB

Autodesk Forge Viewer Extensions with loose coupling, so that it's easy to plug and play in other projects.

Home Page: https://forge-extensions.herokuapp.com/

License: MIT License

JavaScript 99.57% CSS 0.12% HTML 0.31%

autodesk-forge-extensions-1's Introduction

forge-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

Extensions were created using a Basic Skeleton.

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

Thumbnail

thumbnail

Live Demo

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

forge-extensions.autodesk.io

Setup

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.

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-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.

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 forge app

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:

  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 Forge 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 Forge Advocates team:

autodesk-forge-extensions-1's People

Contributors

augustogoncalves avatar libvarun avatar petrbroz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.