Utility Class for loading models in sequence for Forge Viewer.
This video shows core concepts of this tool: https://youtu.be/nfHZLlWtQ-0?t=16
A reversion of this official blog: Aggregate multi models in sequence in Forge Viewer.
- Include libraries
<linkrel="stylesheet" href="https://developer.api.autodesk.com/viewingservice/v1/viewers/7.*/style.min.css" type="text/css"><scriptsrc="https://developer.api.autodesk.com/viewingservice/v1/viewers/7.*/viewer3D.js"></script><scriptsrc="MultipleModelUtil.js"></script>- Create instance of the
MultipleModelUtilinside theAutodesk.Viewing.Initializercallback, then callMultipleModelUtil#processModelsto load all models
Autodesk.Viewing.Initializer(options,function(){//get the viewer divconstviewerDiv=document.getElementById('viewer');//initialize the viewer objectconstviewer=newAutodesk.Viewing.Private.GuiViewer3D(viewerDiv);//load model one by one in sequenceconstutil=newMultipleModelUtil(viewer);util.processModels(models);});functionfetchForgeToken(callback){fetch('https://127.0.0.1/api/forge/oauth/token',{method: 'get',headers: newHeaders({'Content-Type': 'application/json'})}).then((response)=>{if(response.status===200){returnresponse.json();}else{returnPromise.reject(newError(`Failed to fetch token from server (status: ${response.status}, message: ${response.statusText})`));}}).then((data)=>{if(!data)returnPromise.reject(newError('Empty token response'));callback(data.access_token,data.expires_in);}).catch((error)=>console.error(error));}functionlaunchViewer(models){if(!models||models.length<=0)returnconsole.error('Empty model input');constoptions={env: 'AutodeskProduction',getAccessToken: fetchForgeToken//accessToken: 'eyJhbGciOiJIUzI1NiIs....'};Autodesk.Viewing.Initializer(options,function(){//get the viewer divconstviewerDiv=document.getElementById('viewer');//initialize the viewer objectconstviewer=newAutodesk.Viewing.Private.GuiViewer3D(viewerDiv);//load model one by one in sequenceconstutil=newMultipleModelUtil(viewer);util.processModels(models);});}constmodels=[{name: '1.nwc',urn: 'urn:dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLlNpaHgxOTVuUVJDMHIyWXZUSVRuZFE/dmVyc2lvbj0x'},{name: '2.nwc',urn: 'urn:dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLldVRHJ4ajZ6UTBPLTRrbWZrZ3ZoLUE/dmVyc2lvbj0x'},{name: '3.nwc',urn: 'urn:dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLjRyZW5HRTNUU25xNHhYaW5xdWtyaWc/dmVyc2lvbj0x'}];launchViewer(models.concat());This utility supports 4 kinds alignments as the below:
Center to center: the default way of the viewer.
constutil=newMultipleModelUtil(viewer);util.options={alignment: MultipleModelAlignmentType.CenterToCenter};util.processModels(models);
Origin to origin: Apply the globalOffset of the 1st model to others. (This is the default alignment method of this utility)
constutil=newMultipleModelUtil(viewer);util.options={alignment: MultipleModelAlignmentType.OriginToOrigin};util.processModels(models);
By Revit shared coordinates: Set up
applyRefpoint: trueand make theglobalOffsetto therefPoint. (AEC model data is required)constutil=newMultipleModelUtil(viewer);util.options={alignment: MultipleModelAlignmentType.ShareCoordinates};util.processModels(models);
Custom alignment: If the above alignments don't match your need, you can use this option to set up custom alignments.
constutil=newMultipleModelUtil(viewer);util.options={alignment: MultipleModelAlignmentType.Custom,getCustomLoadOptions: (bubble,data)=>{console.log(bubble,data);consttx=newTHREE.Matrix4();tx.setPosition({x:1,y:100,z:1}).scale({x:2,y:2,z:2});return{placementTransform: tx};}};util.processModels(models);
Note. Forge Viewer supports 3 kinds of Revit link methods as I shared here:
- Origin to origin
- Center to center
- By shared coordinate
This sample is licensed under the terms of the MIT License. Please see the LICENSE file for full details.
Eason Kang @yiskang, Forge Partner Development