Created
July 3, 2020 08:22
-
-
Save yiskang/f33249d0c787ec1b964b8cd7b5b0d2d7 to your computer and use it in GitHub Desktop.
Forge Viewer function to separate host or linked elements in the composite Revit model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
}) | |
} | |
function userFunction( pdb, dbIds ) { | |
const idsLoaded = pdb.externalIdsLoaded(); | |
if( !idsLoaded ) | |
return null; | |
const hostElemetns = [], linkedElements = []; | |
const idsMap = pdb.getExternalIdMapping(); | |
const ids = Object.keys( idsMap ); | |
for( let i = 0; i < dbIds.length; i++ ) { | |
const dbId = dbIds[i]; | |
const externalId = pdb.getIdAt( dbId ); | |
if( externalId.lastIndexOf( '/' ) !== -1 ) { | |
linkedElements.push( dbId ); | |
} else { | |
hostElemetns.push( dbId ); | |
} | |
} | |
return { | |
hostElemetns, | |
linkedElements | |
}; | |
} | |
await viewer.model.getPropertyDb().executeUserFunction( userFunction, await getLeafNodes() ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment