import { assert , expect } from "chai" ; import { CheckpointConnection , IModelApp , IModelConnection , RealityDataSource , SpatialModelState , TileAdmin } from "@itwin/core-frontend" ; import { TestUsers } from "@itwin/oidc-signin-tool/lib/cjs/frontend" ; import { TestUtility } from "../TestUtility" ; import { RealityDataFormat , RealityDataProvider , RealityDataSourceKey } from "@itwin/core-common" ; import { Id64String } from "@itwin/core-bentley" ; import { ITwinRealityData , RealityDataAccessClient , RealityDataClientOptions , RealityDataQueryCriteria , RealityDataResponse } from "@itwin/reality-data-client" ; export interface IRealityDataModelInfo { key : RealityDataSourceKey ; modelId : Id64String ; attachmentUrl : string ; attachmentName : string ; } describe ( "RealityDataAccess (#integration)" , ( ) => { let imodel ; let iTwinId ; const realityDataClientOptions = { /** API Version. v1 by default */ /** API Url. Used to select environment. Defaults to "https://api.bentley.com/realitydata" */ baseUrl : `https://${ process . env . IMJS_URL_PREFIX }api.bentley.com/realitydata` } ; let realityDataAccess ; before ( async ( ) => { await TestUtility . shutdownFrontend ( ) ; realityDataAccess = new RealityDataAccessClient ( realityDataClientOptions ) ; const options = TestUtility . iModelAppOptions ; options . realityDataAccess = realityDataAccess ; const tileAdmin = { } ; tileAdmin . cesiumIonKey = process . env . IMJS_CESIUM_ION_KEY ; options . tileAdmin = tileAdmin ; await TestUtility . startFrontend ( { ... options } ) ; await TestUtility . initialize ( TestUsers . regular ) ; iTwinId = await TestUtility . queryITwinIdByName ( TestUtility . testITwinName ) ; const iModelId = await TestUtility . queryIModelIdByName ( iTwinId , TestUtility . testIModelNames . realityDataAccess ) ; imodel = await CheckpointConnection . openRemote ( iTwinId , iModelId ) ; } ) ; after ( async ( ) => { if ( imodel ) await imodel . close ( ) ; await TestUtility . shutdownFrontend ( ) ; } ) ; function getAttachmentURLFromModelProps ( modelProps ) { if ( modelProps . tilesetUrl !== undefined && modelProps . tilesetUrl . orbitGtBlob !== undefined ) { return { rdsUrl : modelProps . tilesetUrl . orbitGtBlob . rdsUrl , blobFilename : modelProps . tilesetUrl . orbitGtBlob . blobFileName } ; } else if ( modelProps . orbitGtBlob !== undefined && modelProps . orbitGtBlob . rdsUrl ) { return { rdsUrl : modelProps . orbitGtBlob . rdsUrl , blobFilename : modelProps . orbitGtBlob . blobFileName } ; } return { rdsUrl : modelProps . tilesetUrl , blobFilename : undefined } ; } async function getAttachedRealityDataModelInfoSet ( iModel ) { const modelRealityDataInfos = new Set < IRealityDataModelInfo > ( ) ; if ( iModel ) { const query = { from : SpatialModelState . classFullName , wantPrivate : false } ; const iModelProps = await iModel . models . queryProps ( query ) ; for ( const prop of iModelProps ) { if ( prop . jsonProperties !== undefined && ( prop . jsonProperties . tilesetUrl || prop . jsonProperties . orbitGtBlob ) && prop . id !== undefined && prop . name ) { const attachmentUrl = getAttachmentURLFromModelProps ( prop . jsonProperties ) ; if ( attachmentUrl !== undefined && attachmentUrl . rdsUrl !== undefined ) { let fileFormat = RealityDataFormat . ThreeDTile ; if ( prop . jsonProperties . orbitGtBlob ) fileFormat = RealityDataFormat . OPC ; const key = RealityDataSource . createKeyFromUrl ( attachmentUrl . rdsUrl , undefined , fileFormat ) ; modelRealityDataInfos . add ( { key , modelId : prop . id , attachmentUrl : attachmentUrl . rdsUrl , attachmentName : prop . name } ) ; } } } } return modelRealityDataInfos ; } enum RealityDataType { REALITYMESH3DTILES = "REALITYMESH3DTILES" , OSMBUILDINGS = "OSMBUILDINGS" , OPC = "OPC" , TERRAIN3DTILES = "TERRAIN3DTILES" , OMR = "OMR" , CESIUM3DTILES = "CESIUM3DTILES" , UNKNOWN = "UNKNOWN" } function isSupportedType ( type ) { if ( type === undefined ) return false ; switch ( type . toUpperCase ( ) ) { case RealityDataType . REALITYMESH3DTILES : return true ; case RealityDataType . CESIUM3DTILES : return true ; case RealityDataType . OPC : return true ; case RealityDataType . OSMBUILDINGS : return true ; case RealityDataType . TERRAIN3DTILES : return true ; case RealityDataType . OMR : return true ; } return false ; } function createRealityDataListKeyFromITwinRealityData ( iTwinRealityData ) { return { provider : RealityDataProvider . ContextShare , format : iTwinRealityData . type === RealityDataType . OPC ? RealityDataFormat . OPC : RealityDataFormat . ThreeDTile , id : iTwinRealityData . id } ; } function getOSMBuildingsKey ( ) { return { provider : RealityDataProvider . CesiumIonAsset , format : RealityDataFormat . ThreeDTile , id : "OSMBuildings" } ; } it ( "should get RealityDataSource for all supported reality data in itwin project" , async ( ) => { let projectRealityDatas = { realityDatas : [ ] } ; let continuationToken ; const top = 100 ; const criteria = { getFullRepresentation : true , top , continuationToken } ; do { criteria . continuationToken = projectRealityDatas . continuationToken ; const accessToken = await IModelApp . getAccessToken ( ) ; projectRealityDatas = await realityDataAccess . getRealityDatas ( accessToken , iTwinId , criteria ) ; for ( const rd of projectRealityDatas . realityDatas ) { if ( isSupportedType ( rd . type ) ) { const keyFromInput = createRealityDataListKeyFromITwinRealityData ( rd ) ; const rdSource = await RealityDataSource . fromKey ( keyFromInput , iTwinId ) ; expect ( rdSource ) . not . undefined ; expect ( rdSource ?. isContextShare ) . to . be . true ; } } } while ( projectRealityDatas . continuationToken ) ; } ) ; it ( "should get RealityDataSource for reality data attachment in iModel" , async ( ) => { assert . isTrue ( imodel !== undefined ) ; const modelRealityDataInfos = await getAttachedRealityDataModelInfoSet ( imodel ) ; expect ( modelRealityDataInfos . size ) . to . equal ( 3 ) ; for ( const entry of modelRealityDataInfos ) { const rdSource = await RealityDataSource . fromKey ( entry . key , iTwinId ) ; expect ( rdSource ) . not . undefined ; expect ( rdSource ?. isContextShare ) . to . be . true ; } } ) ; it . skip ( "should get RealityDataSource for Open Street Map Building (OSM)" , async ( ) => { assert . isTrue ( imodel !== undefined ) ; const rdSourceKey = getOSMBuildingsKey ( ) ; const rdSource = await RealityDataSource . fromKey ( rdSourceKey , iTwinId ) ; expect ( rdSource ) . not . undefined ; expect ( rdSource ?. isContextShare ) . to . be . false ; } ) ; } ) ;