looping over all dbids stops my viewer

95 views Asked by At

So I have a fairly big revit file with 137 MB and I need to get all dbIds so I can filter them by a certain properties, but due to the number of dbIds it takes too long. Is it possible to filter the dbids beforehand or a really fast way of getting the dbids. Currently I use this piece of code for dbid extraction.

  getDbIds() {
    let dictionary = this.instancetree.nodeAccess.dbIdToIndex
    let dbids = []
    for (let i = 0; i < Object.keys(dictionary).length; i++) {
      let nochildCondition = this.instancetree.getChildCount(dictionary[i]);
      if (nochildCondition !== 0) {
        continue
      }
      dbids.push(dictionary[i])
    }
    return dbids
  }

Is there a better way of extracting dbids fast ?

1

There are 1 answers

0
Eason Kang On BEST ANSWER

It's recommended to use TnstanceTree#enumNodeChildren() to obtain children nodes in the node tree instead. The following code snippet is used in our demo projects, hope it helps.

function 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 ) => {
         console.log( leafNodes );
    })
    .catch( ( error ) => console.warn( error ) );