6 Jul 2020

Get dbId from externalId

When working inside the Forge Viewer you rely on dbId's to interact with the objects in it: getSelection() returns a list of dbIds, getProperties() expects dbId as input, etc

The values of dbId's are not persistent: i.e. in different versions of the same model the dbId for the same object might be different. Therefore they are not ideal for associating data to objects in an external database. For that we can use the externalId's that come from the program where the original model was created e.g. Revit 
You can find more info about id's for architectural models here: https://thebuildingcoder.typepad.com/blog/2019/07/element-identifiers-in-rvt-ifc-nw-and-forge.html

It's very easy to get the externalId for an object once you have its dbId by using the getProperties() function of the Viewer
If you wanted to test this function in the Developer Console of the browser (as shown here) you could just run this after selecting an object in the Viewer:

NOP_VIEWER.getProperties(NOP_VIEWER.getSelection(), data => console.log(data))

// reply 
// {dbId: Array(1), properties: Array(0), externalId: "a6aa132d-ccd7-408f-b2f9-ed67350c8c3a-0003b64a"}

There is also a nice blog post showing how you could display both the dbId and externalId for the selected object in the Property Palette of the Viewer:
https://forge.autodesk.com/blog/adding-custom-properties-property-panel 

You might also need to go the other way though, from externalId to dbId when e.g. you want to select an object based on its externalId.
That's what the getExternalIdMapping() function is for. In case of v7 of the Viewer it takes two callback functions as input. The first one will be called with the externalId->dbId map that we need.
You can test this function as well in the Developer Console of the browser:

NOP_VIEWER.model.getExternalIdMapping(data => console.log(data))

// reply
// {doc_131133be-fca6-4a87-a6be-0bc0b1ff4d6d: 1, e3e052f9-0156-11d5-9301-0000863f27ad-00000137: 2, …}

 

Related Article