在ViewingApplication方法中使用modelOptions

时间:2019-02-13 09:40:37

标签: typescript autodesk-forge autodesk-viewer

我目前只能使用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.selectItemsee doc)。但是此方法没有像Viewer3D(see doc)的modelOptions方法那样的参数loadModel

所以这是我的问题: 我正在查看器实例中加载多个模型,并希望将每个模型的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方法并加载第一个模型。这就是我被困住的地方。

2 个答案:

答案 0 :(得分:0)

另一种方法是使用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

请参见full sample和文档here

答案 1 :(得分:0)

我找到了解决方案。不知道这是否正确,但是对我有用:

我通过调用var viewer = viewerApp.getViewer()获得了一个新的Viewer3D实例。之后,我通过调用viewer.start(svfUrl, modelOptions, this.onItemLoadSuccess, this.onItemLoadFail)

手动启动查看器
var svfUrl = doc.getViewablePath(viewables[0].data);
var modelOptions = {
  sharedPropertyDbPath: doc.getPropertyDbPath(),
  globalOffset: { x: 0, y: 0, z: 0 }
};

if (!!this.viewerApp.getCurrentViewer()) {
  this.viewerApp.getCurrentViewer().loadModel(svfUrl, modelOptions, this.onItemLoadSuccess, this.onItemLoadFail)
} else {
  // Get a new Viewer3D instance by calling viewerApp.getViewer() with an empty object {}
  var viewer = this.viewerApp.getViewer({})
  // Start the viewer manually by passing the url of the svf model and the desired model options
  viewer.start(svfUrl, modelOptions, this.onItemLoadSuccess, this.onItemLoadFail)
}