Forge查看器Autodesk.Viewing.GEOMETRY_LOADED_EVENT未被触发为pdf文件

时间:2017-02-02 07:27:10

标签: javascript autodesk autodesk-forge autodesk-viewer

在我的代码中,我将监听器附加到Autodesk.Viewing.GEOMETRY_LOADED_EVENT事件,以确保只有在查看器完成加载模型文件后才会运行。我已经关注了this的例子。

// Attach event handlers
viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, () => $timeout(handleViewerGeometryLoaded));         
viewer.addEventListener(Autodesk.Viewing.SELECTION_CHANGED_EVENT, () => $timeout(handleViewerSelectionChanged));


function handleViewerGeometryLoaded() {
   // Perform some actions here after everything is loaded.
}

只要在查看器完成加载文件时触发GEOMETRY_LOADED_EVENT get,这就完全可以正常工作。但我最近发现的一件事是,对于像pdf这样的文件类型,这个事件不会被触发。对于所有其他文件,如3d文件,rvt,dwg文件等,这完全正常。但是对于pdf文件,这不起作用。

这是否正确,这不适用于pdf文件?我该怎么做才能知道观众何时加载这些文件呢?我可以在这里使用其他类似的活动吗?

在我们的应用程序中,我们需要支持从3d模型到2d文件的所有类型的文件,甚至包括pdf文件。所以,我需要某种事件,当为观众支持的所有文件类型加载时,会触发这种事件。

感谢。

1 个答案:

答案 0 :(得分:0)

无论如何,我尝试了一切,但最终我不得不做这个黑客让它在我的应用程序中工作。但它完成了这项工作:)

let modelCompletelyLoaded === false;

function ensureModelsWithoutGeometryLoaded(viewer) {
    const CHECK_INTERVAL = 1000;

    return new Promise(resolve => {
        // This is a dirty hack we need to do to ensure pdf files
        // to know when pdf files are loaded. We had to do this since
        // the GEOMETRY_LOADED_EVENT won't get triggered for the pdf files and
        // the files that doesn't have any geometry so we need to poll continously
        // to know if the model is fully loaded.
        let loadChecker = setInterval(() => {
            let hasMyData = viewer.model && viewer.model.myData;
            let loaded = hasMyData && viewer.model.myData.loadDone;
            let is2d = hasMyData && viewer.model.myData.is2d;
            let hasInstanceTree = hasMyData && (typeof viewer.model.myData.instanceTree === 'object');

            // It's not a 2d model, or the instanceTree has been loaded i.e the it has geometry
            // it implies that this couldn't be a pdf file, just skip it.
            if (is2d === false || hasInstanceTree) {
                clearInterval(loadChecker);
            } else if (loaded) {
                // Loaded now. Okay, great trigger the event finally.
                resolve();
                clearInterval(loadChecker);
            }
        }, CHECK_INTERVAL);
    });
}

function handleObjectLoaded() {
    if (modelCompletelyLoaded === true) {
        return
    }

    modelCompletelyLoaded === true;

    // Perform some actions here after everything is loaded.
    // ...
}

// Attach event handlers (this would work for all the files except those that doesn't have geometry data).
viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, handleObjectLoaded);

// For pdf files and those that don't contain geometry do this
ensureModelsWithoutGeometryLoaded(viewer).then(handleObjectLoaded);