setThemingColor仅适用于叶节点dbIds

时间:2017-08-23 10:36:25

标签: autodesk-forge autodesk-viewer

从文档中看起来你应该可以使用任何dbId调用setThemingColor,但它似乎只有在你传递的id是leafnode时才有效?它是否正确?

还有什么方法可以批量调用此方法,或者一次只有一个叶子节点?我想将一个dbId数组传递给方法。

1 个答案:

答案 0 :(得分:1)

是的,根据我的经验,它只适用于叶节点。但是,可以通过以下方式检索父节点的叶节点:

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 ) );

在检索所有叶子dbIds之后,你可以简单地编写一个for循环来为每个dbId调用setThemingColor,如下所示:

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 ) );

希望得到这个帮助。

功能getLeafNodes的参考:https://forge.autodesk.com/blog/hidding-completely-viewer-nodes-no-ghosting