Search code examples
javascriptautodesk-forgeautodesk-viewerautodesk-model-derivative

Autodesk Forge DWG layout pre selection


I'm using Autodesk Forge to view DWG file in browser. It was successful. One thing I'm trying to figure out right now is how to do pre selection on which layout to view in Forge viewer. When I upload DWG into the bucket, there are several layouts available in document browser

Autodesk Forge document browser

I have a lot of drawings and some of them didn't display 'Layout2' (which I wanna display in viewer) on first load. I need to open the document browser and select 'Layout2' manually.

How do I do like pre select on 'Layout2' every time the viewer load.


Solution

  • Found the solution for this. Here's how I did it.

    Launch the viewer as usual

    Autodesk.Viewing.Document.load(documentId, this.onDocumentLoadSuccess, this.onDocumentLoadFailure);

    And then in "onDocumentLoadSuccess" method, get the guid for 'Layout2':

    let layoutList = doc.getRoot().search({ 'type': 'geometry' });
    let selectedItem = layoutList[0]
    let guid;
    
    //Iterate to find guid for Layout2
    layoutList.forEach((o, i) => {
      guid = (o.data.viewableID == 'Layout2') ? o.data.guid : selectedItem.data.guid
    })
    
    //instead of Load the document using this
    var viewables = doc.getRoot().getDefaultGeometry();
    
    //Load document using function => findByGuid(guid)
    viewer.loadDocumentNode(doc, doc.getRoot().findByGuid(guid)).then(i => {
    //this just to add mouse event on viewer
      $('#forgeViewer').mousemove(onMouseMove);
    });

    Here is the reference => Github