Search code examples
autodesk-forgeautodesk-viewerautodeskforge

Adding buttons to the Forge Viewer


I am a developer who's currently trying to add another button to the Autodesk Forge Viewer. I can only find one example on the Autodesk website which explains this and I feel like I would understand more through a discussion or example.

I currently have a very basic viewer, which asks for a bucket name and model then once it uploads the model it shows the model in the viewer with all the generic buttons, how do I go about inserting my own button?

Below is the viewer.html used in my basic viewer:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script src="https://developer.api.autodesk.com/viewingservice/v1/viewers/three.min.css"></script>
    <link rel="stylesheet" href="https://developer.api.autodesk.com/viewingservice/v1/viewers/style.min.css" />
    <script src="https://developer.api.autodesk.com/viewingservice/v1/viewers/viewer3D.min.js"></script>
</head>
<body onload="initialize()">
<div id="viewer" style="position:absolute; width:90%; height:90%;"></div>
<script>
    function authMe () { return ('eyJhbGciOiJIUzI1NiIsImtpZCI6Imp3dF9zeW1tZXRyaWNfa2V5In0.eyJjbGllbnRfaWQiOiJJTzY5cVJGYk5kNGlVcWlnS3FnR2xzREdzSk1zQVNBYyIsImV4cCI6MTUzNjMzMDYwMiwic2NvcGUiOlsiZGF0YTpyZWFkIiwiZGF0YTp3cml0ZSIsImJ1Y2tldDpjcmVhdGUiLCJidWNrZXQ6cmVhZCJdLCJhdWQiOiJodHRwczovL2F1dG9kZXNrLmNvbS9hdWQvand0ZXhwNjAiLCJqdGkiOiJ6Q0U5R0FMOGFHNWhOdlFFSjhqeExFZDVqTjc1MXV1UjA0UENIME9mYzRRT0k5b2pkRXhyeUE1MmJwdHdsdU5XIn0.FM1P50Ldu1H1LygYXwtP77Kr3128xFZKMgoaBmtXqF0') ; }

    function initialize () {
        var options ={
            'document' : "urn:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6Zm9yZ2UtY3NoYXJwLXNhbXBsZS1hcHAtaW82OXFyZmJuZDRpdXFpZ2txZ2dsc2Rnc2ptc2FzYWMvbXktZWxlcGhhbnQub2Jq",
            'env': 'AutodeskProduction',
            'getAccessToken': authMe
        } ;
        var viewerElement =document.getElementById ('viewer') ;
        //var viewer =new Autodesk.Viewing.Viewer3D (viewerElement, {}) ; / No toolbar
        var viewer =new Autodesk.Viewing.Private.GuiViewer3D (viewerElement, {}) ; // With toolbar
        Autodesk.Viewing.Initializer (options, function () {
            viewer.initialize () ;
            loadDocument (viewer, options.document) ;
        }) ;
    }
    function loadDocument (viewer, documentId) {
        // Find the first 3d geometry and load that.
        Autodesk.Viewing.Document.load (
            documentId,
            function (doc) { // onLoadCallback
                var geometryItems =[] ;
                geometryItems =Autodesk.Viewing.Document.getSubItemsWithProperties (
                    doc.getRootItem (),
                    { 'type' : 'geometry', 'role' : '3d' },
                    true
                ) ;
                if ( geometryItems.length <= 0 ) {
                    geometryItems =Autodesk.Viewing.Document.getSubItemsWithProperties (
                        doc.getRootItem (),
                        { 'type': 'geometry', 'role': '2d' },
                        true
                    ) ;
                }
                if ( geometryItems.length > 0 )
                    viewer.load (
                        doc.getViewablePath (geometryItems [0])//,
                        //null, null, null,
                        //doc.acmSessionId /*session for DM*/
                    ) ;
            },
            function (errorMsg) { // onErrorCallback
                alert("Load Error: " + errorMsg) ;
            }//,
            //{
            //  'oauth2AccessToken': authMe (),
            //  'x-ads-acm-namespace': 'WIPDM',
            //  'x-ads-acm-check-groups': 'true',
            //}
        ) ;
    }
</script>
</body>
</html>

Solution

  • Here you can find an example of creating the basic skeleton we use in most of our samples to add buttons to the UI toolbar of the viewer.

    http://learnforge.autodesk.io/#/viewer/extensions/skeleton