【问题标题】:setThemingColor only working for leaf node dbIdssetThemingColor 仅适用于叶节点 dbIds
【发布时间】:2023-03-03 16:09:01
【问题描述】:

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

还有没有办法批量调用这个方法,还是一次只有一个叶子节点?我想将一个 dbId 数组传递给方法。

【问题讨论】:

    标签: autodesk-forge autodesk-viewer


    【解决方案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 ) );
    

    检索所有叶子 dbId 后,您可以简单地编写一个 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 2016-03-27
      • 2012-06-07
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多