Skip to content

Instantly share code, notes, and snippets.

@yiskang
Created July 3, 2020 08:22
Show Gist options
  • Save yiskang/f33249d0c787ec1b964b8cd7b5b0d2d7 to your computer and use it in GitHub Desktop.
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
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