setThemingColor only working for leaf node dbIds

2019-03-02 07:30发布

From the documentation it looks like you should be able to call setThemingColor with any dbId, but it seems to only work if the id that you pass is a leafnode? Is this correct?

Also is there any way to bulk call this method, or is it only one single leaf node at a time? I would like to pass an array of dbId's into the method.

1条回答
该账号已被封号
2楼-- · 2019-03-02 08:11

Yes, it's only working with the leaf nodes in my experience. However, leaf nodes of a parent node can be retrieved in this way:

getLeafNodes( model, dbIds ) {

      return new Promise( ( resolve, reject ) => {

        try {

          const instanceTree = model.getData().instanceTree

          dbIds = dbIds || instanceTree.getRootId();

          const dbIdArray = Array.isArray( dbIds ) ? dbIds : [dbIds]
          let leafIds = [];

          const getLeafNodesRec = ( id ) => {
            let childCount = 0;

            instanceTree.enumNodeChildren( id, ( childId ) => {
                getLeafNodesRec( childId );

                ++childCount;
              })

            if( childCount == 0 ) {
              leafIds.push( id );
            }
          }

          for( let i = 0; i < dbIdArray.length; ++i ) {
            getLeafNodesRec( dbIdArray[i] );
          }

          return resolve( leafIds );

        } catch (ex) {

          return reject(ex)
        }
    })
}

getLeafNodes( viewer.model, [1] )
    .then( ( leafNodes ) => {
      // All leaf dbIds under the dbId 1.
      console.log( leafNodes );
    })
    .catch( ( error ) => console.warn( error ) );

After retrieving all leaf dbIds, you can simply write a for loop to call setThemingColor for every dbIds like this way:

const color = new THREE.Vector4( 255/255, 0, 0, 1 );

getLeafNodes( viewer.model, [1] )
    .then( ( leafNodes ) => {

      // Call setThemingColor for every leaf node.
      for( let i = 0; i < leafNodes.length; i++ ) {
          viewer.setThemingColor( leafNodes[i], color );
      }

    })
    .catch( ( error ) => console.warn( error ) );

Hope this help.

Ref of the function getLeafNodes: https://forge.autodesk.com/blog/hidding-completely-viewer-nodes-no-ghosting

查看更多
登录 后发表回答