Search code examples
autodesk-forgeautodesk-viewer

Forge APS Viewer - Revit Color Scheme - How to get room color from 2D Viewer


We are trying to retrieve the Room colors (based on Views with Revit Color Scheme) from Sheets viewed in the 2D Viewer:

Sheet in Viewer 2D

Color Scheme in Revit

Color Scheme

For this purpose I retrieve all the rooms and get their materials:

const tree = model.getInstanceTree();
const frags = model.getFragmentList();

const getRoomsId = new Promise((resolve, reject) => {
    obj.model.search('Rooms', function (dbIds) {
        dbIds.some(dbId => {
            if (tree.getNodeName(dbId) === 'Rooms') {
                resolve(dbId)
                return true
            }
        })
    })
})

getRoomsId.then(function (roomsId) {
    tree.enumNodeChildren(roomsId,
      function (childId) {
        tree.enumNodeFragments(childId, function (fragid) {
            const material = frags.getMaterial(fragid);
            console.log('Room: ', childId, ' / ', tree.getNodeName(childId), ' Fragment: ', fragid, ' Material: ', material.uuid)
        })
       }
    )
})

Unfortunately they all seem to have the same material as shown in the log below, so I have no idea where the colors are coming from:

Console log

tree.enumNodeChildren(childId)

doesn't retrieve anything so I wonder if somehow it is using an overlay to display the colors.

Any help is very much appreciated.


Solution

  • Instead of using Material the 2D Viewer uses Vertex colors.

        const mesh = frags.getVizmesh(fragId);
        const vbr = new Autodesk.Viewing.Private.VertexBufferReader(mesh.geometry, viewer.impl.use2dInstancing);
    
        var vertexCount = vbr.vcount;
    
        for (var i = 0; i < vertexCount; i++) {
            // Each vertex knows its related dbId
            var dbId = vbr.getDbIdAt(i);
            // sign extend the upper byte to get back negative numbers (since per-vertex ids are clamped from 32 bit to 24 bit)
            dbId = dbId << 8 >> 8;
            var color = vbr.getColorAt(i)
            color = color >>> 0     // Signed to Unsigned 32
            color &= 0x00FFFFFF     // Getting rid of opacity
        }
    

    Note: The color is stored as GBR instead of RGB