autodesk-viewerautodesk-tandem

How to skip displayFacility and start with setCurrentView in Autodesk Tandem API? Also, how to detect when the view is fully loaded?


I am working with Autodesk Tandem and have implemented a custom app where users can select a facility and its associated view. I currently use the following code to display the selected facility view inside the viewer:

private async displayFacilityView(sTwin: string, sViewId: string): Promise<void> {
  const app = this.oApp;
  const viewer = this.oViewer;

  try {
    const facilitiesSharedWithMe: Autodesk.Tandem.DtFacility[] = await app.getCurrentTeamsFacilities();
    const myFacilities: Autodesk.Tandem.DtFacility[] = await app.getUsersFacilities();
    const aFacilities: Autodesk.Tandem.DtFacility[] = [...facilitiesSharedWithMe, ...myFacilities];
    const matchingFacility = aFacilities.find(facility => facility.twinId === sTwin);
    const aViews = await app.views.fetchFacilityViews(matchingFacility);
    const matchingView = aViews.find(v => v.id === sViewId);
    let aModelIds;
    if (matchingView)
      aModelIds = new Set(matchingView?.facets?.filters?.models);
    await app.displayFacility(matchingFacility, aModelIds, viewer); // TODO: time consuming, possibility to skip?
    await app.views.setCurrentView(matchingFacility, matchingView);
  } catch (error) {
    console.error('Error displaying facility view:', error);
    throw error;
  }
}

Question 1: I would like to skip the displayFacility step and start directly with setCurrentView to reduce loading time. However, when I skip displayFacility, the viewer property inside DtFacility becomes undefined. What do I need to change in the code to achieve this?

Question 2: After setting the current view using setCurrentView, what is the correct event to listen for in order to detect when the view has been completely loaded? I need to run viewer.getVisibleDbIds() as the next step. How can I achieve this?

Additional Information:

I’m using Tandem's REST API to receive facilities and views. The viewer is undefined within DtFacility after skipping displayFacility.

Any help or guidance would be greatly appreciated!


Solution

  • Currently there is no single method which can be used to load facility and set current view. Tandem app is using DtApp.displayFacility and then DtFacility.goToView (which is also used internally by DtApp.views.setCurrentView).

    You can use DtModel.waitForLoad(true, true) to make sure that geometry is loaded. For example:

    const result = await Promise.all(facility.getModels().map(async (model) => {
      await model.waitForLoad(true, true);
      const dbIds = model.getVisibleDbIds();
      return {
        model,
        dbIds
      };
    }));
    

    You can also use DtFacility.getVisibleElementsByURN() to get visible elements grouped by model URN.

    As an alternative to REST API you can use DtFacility.getSavedViewsList(). Of course this is using same API under the hood.