首页 > 解决方案 > 在 ViewingApplication 方法中使用 modelOptions

问题描述

我目前正忙于使用 GlobalOffset 加载模型。我已按照以下步骤操作:https ://forge.autodesk.com/en/docs/viewer/v6/tutorials/basic-application/

这是他们对 DocumentLoaded-event 的实现:

function onDocumentLoadSuccess(doc) {
    // We could still make use of Document.getSubItemsWithProperties()
    // However, when using a ViewingApplication, we have access to the **bubble** attribute,
    // which references the root node of a graph that wraps each object from the Manifest JSON.
    var viewables = viewerApp.bubble.search({'type':'geometry'});
    if (viewables.length === 0) {
        console.error('Document contains no viewables.');
        return;
    }
    // Choose any of the avialble viewables
    viewerApp.selectItem(viewables[0].data, onItemLoadSuccess, onItemLoadFail);
}

此事件处理程序的最后一步是调用viewerApp.selectItem请参阅 doc)。但是这个方法没有modelOptionsloadModelViewer3D 的方法那样的参数(参见文档)。

所以现在我的问题是:我在查看器实例中加载多个模型,并希望将每个模型的 GlobalOffset 设置为globalOffset: {x: 0, y:0, z:0}. 但我无法modelOptions为第一个加载的模型设置viewerApp.selectItem. 有没有其他方法可以使用这种方法加载模型?

顺便说一句,这是我的实现(我正在使用 TypeScript):

private onDocumentLoaded(doc: Autodesk.Viewing.Document) {
    if (!this.viewerApp.bubble) return;
    var viewables = this.viewerApp.bubble.search(Autodesk.Viewing.BubbleNode.MODEL_NODE);
    if (viewables.length === 0) {
        console.error('Document contains no viewables.');
        return;
    }
    if (!!this.viewerApp.getCurrentViewer()) {
        var svfUrl = doc.getViewablePath(viewables[0].data);
        var modelOptions = {
            sharedPropertyDbPath: doc.getPropertyDbPath(),
            globalOffset: { x: 0, y: 0, z: 0 }
        };
        this.viewerApp.getCurrentViewer().loadModel(svfUrl, modelOptions, this.onItemLoadSuccess, this.onItemLoadFail);
    } else {
        this.viewerApp.setCurrentViewer(this.viewerApp.getViewer({}));
        this.viewerApp.selectItem(viewables[0].data, this.onItemLoadSuccess.bind(this), this.onItemLoadFail.bind(this));
    }
}

当我的 DocumentLoaded-event 被调用时,我首先检查一个查看器是否已经被实例化,如果没有调用该selectItem方法并加载第一个模型。这就是我卡住的地方。

标签: typescriptautodesk-forgeautodesk-viewer

解决方案


另一种方法是使用 Viewer3D 而不是 ViewerApplication:

Autodesk.Viewing.Initializer(options, function onInitialized(){
            Autodesk.Viewing.Document.load(urn, function(doc){
            ...
            var svfUrl = doc.getViewablePath(initGeom);        
            var modelOptions={
               globalOffsets: ...
            }
            viewer.start(svfUrl, modelOptions); // load your first model
});
});

...
viewer.loadModel(svfUrl, modelOpts) //second model

在此处查看完整示例和文档


推荐阅读