资讯专栏INFORMATION COLUMN

使用 Forge Viewer 在序列中聚合多模型

graf / 1671人阅读

摘要:但模型载入程序并不是同步执行的载入文档和几何等动作在里都是异步的,我们没办法知道哪一个模型是第一个被完整载入,和下个一个完全载入的是谁而在一些应用场景里是有可能需要在一个序列聚合多个模型。

此篇博客原著为 Autodesk ADN 的梁晓冬,以下以我简称。

我的同事创作了一些关于如何在一个 Forge Viewer 实例里聚合多模型的博客,例如:

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

这些示例多半是一次聚合一个模型到 viewer 里,照这里面的思路是很容易将其改写成透过一个回圈(Loop)在 Viewer 载入任意个数模型的。但模型载入程序并不是同步执行的(载入文档、viewable和几何等动作在 Viewer
里都是异步的),我们没办法知道哪一个模型是第一个被完整载入,和下个一个完全载入的是谁;而在一些应用场景里是有可能需要在一个序列(Sequeuence)聚合多个模型。

所以我花了一些时间研究如何使用 JavaScript Promise 这个机制,基本上 Promise 提供了一个灵活的机制可以用来管理这些单元程序。为了建立一个 Promise 的序列,我找到了一篇有用的讨论串和他的测试例子:

https://stackoverflow.com/que...

根据这个基础,我写了一个测试工程 https://jsfiddle.net/xiaodong...,这个工程主要是用来展示如何透过一个序列将各楼层的模型一个一个载入,这工程的部份代码如下所示:

//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();

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/91851.html

相关文章

  • Forge Viewer 里切换模型视图(Viewables)

    摘要:有提供类似的功能,但这并不包含在里头。条列清单或是切换视图是非常容易的,你主要是要建立一个使用者介面让使用者去选取他们想观看的内容。我使用了来确保当前载入模型占用的内存可以都被释出。 此篇文章原作是 Autodesk ADN Philippe Leefsma,以下以我简称。 这有一个简易的博客用来说明一个我刚加入 https://forge-rcdb.autodesk.io 的一个新功...

    BlackHole1 评论0 收藏0
  • 「翻译」Forge Viewer上实作简易的模型版本比较

    摘要:现在让我们修改这个示例让他可以展示两个同项目但不同版号的模型及。示例执行结果如下这边是这个比较模型的括展代码英文原文 showImg(https://segmentfault.com/img/bVOmjp?w=1542&h=925); 熟悉 BIM360 Team 的朋友可能知道他有一个很牛的模型文档版本比较的功能,但如果模型是放在 Google 云盘或是百度云盘上有可能做到吗? Au...

    JowayYoung 评论0 收藏0
  • Autodesk Forge Viewer 信息本地化技术分析

    摘要:默认情况下,是英文环境,调取的是的资源其实无需翻译。但是,如前面提到的,语言包只是包含了部分常规字串的翻译,如果遇到没有包含的常规字串怎么办呢例如,本例中的语言包并没有对,进行翻译,所以即使切换了语言,它们仍旧是英文。 注:本文是个人调试分析所得,非官方文档,请酌情选用参考。文中分析的数据由https://extract.autodesk.io转换下载而来。 谈到信息本地化,个人觉得包...

    littleGrow 评论0 收藏0
  • Forge Viewer 里显示 Revit 格子线 (grids)

    最近一些Forge客户都在询问我同一个的问题,他们希望将Revit的网格呈现在viewer中,藉此让我有机会来完成这件事,并将它记录在本文章里,就让我们开始吧! 在开始之前,有件事你必须先知道: 由于在Revit里格子线只能在2D视图(例如平面图、立面图、表单等等)中显示,并不会在3D视图中被看见。因此,我们也无法在ForgeViewer的3D视图中看到这些格子线,网格会在模型转文件时被忽略。据我...

    Yangyang 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<