摘要:但模型載入程序并不是同步執(zhí)行的載入文檔和幾何等動(dòng)作在里都是異步的,我們沒辦法知道哪一個(gè)模型是第一個(gè)被完整載入,和下個(gè)一個(gè)完全載入的是誰而在一些應(yīng)用場景里是有可能需要在一個(gè)序列聚合多個(gè)模型。
此篇博客原著為 Autodesk ADN 的梁曉冬,以下以我簡稱。
我的同事創(chuàng)作了一些關(guān)于如何在一個(gè) Forge Viewer 實(shí)例里聚合多模型的博客,例如:
Aggregating multiple models in the Viewer
Preparing your viewing application for multi-model workflows
Preparing your viewing application for multi-model workflows - Part 2: Model Loader
這些示例多半是一次聚合一個(gè)模型到 viewer 里,照這里面的思路是很容易將其改寫成透過一個(gè)回圈(Loop)在 Viewer 載入任意個(gè)數(shù)模型的。但模型載入程序并不是同步執(zhí)行的(載入文檔、viewable和幾何等動(dòng)作在 Viewer
里都是異步的),我們沒辦法知道哪一個(gè)模型是第一個(gè)被完整載入,和下個(gè)一個(gè)完全載入的是誰;而在一些應(yīng)用場景里是有可能需要在一個(gè)序列(Sequeuence)聚合多個(gè)模型。
所以我花了一些時(shí)間研究如何使用 JavaScript Promise 這個(gè)機(jī)制,基本上 Promise 提供了一個(gè)靈活的機(jī)制可以用來管理這些單元程序。為了建立一個(gè) Promise 的序列,我找到了一篇有用的討論串和他的測試?yán)樱?/p>
https://stackoverflow.com/que...
根據(jù)這個(gè)基礎(chǔ),我寫了一個(gè)測試工程 https://jsfiddle.net/xiaodong...,這個(gè)工程主要是用來展示如何透過一個(gè)序列將各樓層的模型一個(gè)一個(gè)載入,這工程的部份代碼如下所示:
//replace with your own urns this.urn_model1 =; this.urn_model2 = ; this.urn_model3 = ; this.urn_model4 = ; //model info array this.modelArray = [{ modelName: "urn_model1", urn: urn_model1, modelObj: null }, { modelName: "urn_model2", urn: urn_model2, modelObj: null }, { modelName: "urn_model3", urn: urn_model3, modelObj: null }, { modelName: "urn_model4", urn: urn_model4, modelObj: null } ]; //viewer object this.viewer = null; function start() { //replace with your own token var token = < your token > ; //option to initialize viewer. var options = { env: "AutodeskProduction", accessToken: token }; //It looks the static function of Viewer does not support ES6 //still use ES5 Autodesk.Viewing.Initializer(options, function onInitialized() { //get the viewer div var viewerDiv = document.getElementById("myViewer"); //initialize the viewer object viewer = new Autodesk.Viewing.Private.GuiViewer3D(viewerDiv, {}); //load model one by one in sequence globalPromise(modelArray); }); } //load model by promise globalPromise = (modelArray) => { var _this = this; //each promise function //input the index of model array function promiseEachModel(index) { return new Promise((resolve, reject) => { var modelName = modelArray[index].modelName; var _index = index; //when the document is loaded function _onDocumentLoadSuccess(doc) { console.log(modelName + ": Document Load Succeeded!"); _this.globalDocumentLoad(doc, _onLoadModelSuccess, _onLoadModelError); }; //when the document failed to be loaded function _onDocumentLoadFailure(viewerErrorCode) { console.error(modelName + ": Document Load Failure, errorCode:" + viewerErrorCode); } //when the model is loaded function _onLoadModelSuccess(model) { console.log(modelName + ": Load Model Succeeded!"); //delegate geometry loaded event _this.viewer.addEventListener( Autodesk.Viewing.GEOMETRY_LOADED_EVENT, _onGeometryLoaded); //map this item with the corresponding model in case of use modelArray[index].modelObj = model }; function _onLoadModelError(viewerErrorCode) { console.error(modelName + ": Load Model Error, errorCode:" + viewerErrorCode); //any error reject(modelName + " Loading Failed!" + viewerErrorCode); } function _onGeometryLoaded(evt) { //_this.globalGeometryLoaded(modelName,evt.model); _this.viewer.removeEventListener( Autodesk.Viewing.GEOMETRY_LOADED_EVENT, _onGeometryLoaded); console.log(modelName + " Geometry Loaded!"); resolve(modelName + " Geometry Loaded!"); } //load the model Autodesk.Viewing.Document.load( modelArray[index].urn, _onDocumentLoadSuccess, _onDocumentLoadFailure); }); //end of new promise } //end of each promise function //build the index array var indexArr = [0, 1, 2, 3]; //proces each promise //refer to http://jsfiddle.net/jfriend00/h3zaw8u8/ function processArray(array, fn) { var results = []; return array.reduce(function(p, item) { return p.then(function() { return fn(item).then(function(data) { results.push(data); return results; }); }); }, Promise.resolve()); } //start to process processArray(indexArr, promiseEachModel).then(function(result) { console.log(result); }, function(reason) { console.log(reason); }); } //end of function globalPromise //when document is being loaded globalDocumentLoad = (doc, _onLoadModelSuccess, _onLoadModelError) => { //get available viewables var viewables = Autodesk.Viewing.Document.getSubItemsWithProperties( doc.getRootItem(), { "type": "geometry" }, true); if (viewables.length === 0) { console.error("Document contains no viewables."); return; } // Choose the first avialble viewables var initialViewable = viewables[0]; var svfUrl = doc.getViewablePath(initialViewable); var mat = new THREE.Matrix4(); //input the transformation var loadOptions = { placementTransform: mat, globalOffset: { x: 0, y: 0, z: 0 }, // to align the models sharedPropertyDbPath: doc.getPropertyDbPath() }; //if this is the first model if (doc.myPath == this.urn_model1) { //load the first model this.viewer.start(svfUrl, loadOptions, _onLoadModelSuccess, _onLoadModelError); } else { //other models this.viewer.loadModel(svfUrl, loadOptions, _onLoadModelSuccess, _onLoadModelError); } } start();
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/91851.html
摘要:有提供類似的功能,但這并不包含在里頭。條列清單或是切換視圖是非常容易的,你主要是要建立一個(gè)使用者介面讓使用者去選取他們想觀看的內(nèi)容。我使用了來確保當(dāng)前載入模型占用的內(nèi)存可以都被釋出。 此篇文章原作是 Autodesk ADN Philippe Leefsma,以下以我簡稱。 這有一個(gè)簡易的博客用來說明一個(gè)我剛加入 https://forge-rcdb.autodesk.io 的一個(gè)新功...
摘要:現(xiàn)在讓我們修改這個(gè)示例讓他可以展示兩個(gè)同項(xiàng)目但不同版號(hào)的模型及。示例執(zhí)行結(jié)果如下這邊是這個(gè)比較模型的括展代碼英文原文 showImg(https://segmentfault.com/img/bVOmjp?w=1542&h=925); 熟悉 BIM360 Team 的朋友可能知道他有一個(gè)很牛的模型文檔版本比較的功能,但如果模型是放在 Google 云盤或是百度云盤上有可能做到嗎? Au...
摘要:默認(rèn)情況下,是英文環(huán)境,調(diào)取的是的資源其實(shí)無需翻譯。但是,如前面提到的,語言包只是包含了部分常規(guī)字串的翻譯,如果遇到?jīng)]有包含的常規(guī)字串怎么辦呢例如,本例中的語言包并沒有對(duì),進(jìn)行翻譯,所以即使切換了語言,它們?nèi)耘f是英文。 注:本文是個(gè)人調(diào)試分析所得,非官方文檔,請(qǐng)酌情選用參考。文中分析的數(shù)據(jù)由https://extract.autodesk.io轉(zhuǎn)換下載而來。 談到信息本地化,個(gè)人覺得包...
最近一些Forge客戶都在詢問我同一個(gè)的問題,他們希望將Revit的網(wǎng)格呈現(xiàn)在viewer中,藉此讓我有機(jī)會(huì)來完成這件事,并將它記錄在本文章里,就讓我們開始吧! 在開始之前,有件事你必須先知道: 由于在Revit里格子線只能在2D視圖(例如平面圖、立面圖、表單等等)中顯示,并不會(huì)在3D視圖中被看見。因此,我們也無法在ForgeViewer的3D視圖中看到這些格子線,網(wǎng)格會(huì)在模型轉(zhuǎn)文件時(shí)被忽略。據(jù)我...
閱讀 3937·2021-11-24 09:39
閱讀 3842·2021-11-22 12:07
閱讀 1181·2021-11-04 16:10
閱讀 921·2021-09-07 09:59
閱讀 1966·2019-08-30 15:55
閱讀 1008·2019-08-30 15:54
閱讀 794·2019-08-29 14:06
閱讀 2539·2019-08-27 10:54