Search code examples
powerbiautodesk-forgepowerbi-datasource

Forge Viewer in PowerBi unload and load models


I have extracted all the document URNs i needed and currently have them in an excel sheet that I have imported into PowerBi. So essentially I am able to create a list with slicers that organizes each file into its specific section within the list in PowerBi.

For Example:

  • Folder 1
    • URN 1
    • URN 2
  • Folder 2
    • URN 3
    • URN 4
  • Etc...

I am currently using a tutorial I found to put the Forge Viewer into PowerBi. ANd in this the URN is hard coded. Now I am either trying to build a Forge Tree within the viewer or just use the list I have in PowerBi, which ever is possible. I have got the viewer to work from the tutorial and am using a 2-legged authentication.

My next steps are as follows:

  • unload current model
  • Load new model (from the tree or list in powerbi)
  • No longer need to initialize the viewer anymore

to unload a document i have created this code in the visual.ts file.

         this.forge_viewer.unloadDocumentNode = function (manifestNode){
         //if model is in memory, just unload it.

         let model = this.impl.findModel(manifestNode, true);
         if (model){
             this.impl.unloadModel(model);
             return true;
         }
     }

I'm not sure how to test this to see if it works if I don't know how to load another file from the list I have in PowerBi. So my question is, is this correct on unloading the model and how do I let the code in the tutorial know if I am selecting a different URN in PowerBi?

Thank you in advance


Solution

  • Now, loadDocumentNode will also take care of unloading current model, so you can just call it directly with new document. e.g. In the code below, launchViewer loads the first model. Then, loadModel1 can load model 1, and loadModel2 will load model 2.

     var viewer;
    
     function launchViewer(urn, vid) {
        var options = {
        env: 'AutodeskProduction',
        getAccessToken: getForgeToken
      };
    
     Autodesk.Viewing.Initializer(options, () => {
       viewer = new Autodesk.Viewing.GuiViewer3D(document.getElementById('forgeViewer'));
       viewer.start();
       var documentId = 'urn:' + urn;
       Autodesk.Viewing.Document.load(documentId, onDocumentLoadSuccess, 
      onDocumentLoadFailure);
     }); 
    }
    
    function getForgeToken(callback) {
     fetch('/api/forge/oauth/token').then(res => {
       res.json().then(data => {
        callback(data.access_token, data.expires_in);
      });
    });
    }
    
    function onDocumentLoadSuccess(doc) {
      var viewables = (viewableId ? doc.getRoot().findByGuid(viewableId) : 
        doc.getRoot().getDefaultGeometry());
       viewer.loadDocumentNode(doc, viewables).then(i => {
      });
    }
    
    function onDocumentLoadFailure(viewerErrorCode) {
     console.error('onDocumentLoadFailure() - errorCode:' + viewerErrorCode);
    }
    
     function loadModel1(){
         Autodesk.Viewing.Document.load('urn:<model1 urn>', onDocumentLoadSuccess, 
       onDocumentLoadFailure);
    }
    
     function loadModel2(){
             Autodesk.Viewing.Document.load('urn:<model2 urn>', onDocumentLoadSuccess, 
       onDocumentLoadFailure);
    }