多模型场景下如何获取每个RootID? | Forge 查看器 API

问题描述 投票:0回答:1

目前我正在尝试从每个模型的所有对象中获取特定属性的值。例如。我需要从所有对象的属性“layer”中获取值,以便我可以自动创建一个列表来过滤模型。 我已经成功打印了所有不同的属性及其值,但仅限于加载的第一个模型。

在此代码段中,我迭代所有加载的模型,然后对于每个模型,我迭代搜索值。这些值是预定义的,例如是“层”。 之后,使用模型实例,我检索其对象树。使用这个对象树我得到了根 id。

async function getTreefromObject() {
    var modelInstance;
    var objectTree;
    var rootId;
    for(var urn of loadedModels) {
        for(var filterValue of FILTER_VALUES) {
            modelInstance = loadedModels[urn];
            console.log(urn);
            console.log(filterValue);
            objectTree = await new Promise((resolve)=> {
                modelInstance.getObjectTree(function(objectTree) {
                    resolve(objectTree);
                })
            });
            rootId = objectTree.getRootId();
            console.log('RootID: ', rootId);
            await extractAttributesRecursively(rootId, filterValue);
        }
    }
}

问题是,即使每个模型的对象树都不同,根 id 始终相同 (1)。

当我注意到这一点时,我尝试使用获取模型ID

viewer.model.getInstanceTree()
并手动输入这些,以便该函数将使用这些数字而不是 root id。

但这也不起作用,因为以下函数一直只搜索首先加载的模型。

此递归函数首先从提供的 dbId 获取属性。然后,如果找到某些属性,它将继续尝试查找指定的属性(“图层”)。当成功时,该值将添加到我的值矩阵中,如果没有成功,则该函数将深入到对象树中。

async function extractAttributesRecursively(dbId, filterValue, depth = 0) {
    return new Promise((resolve) => {
        viewer.getProperties(dbId, function(properties) {
            if (properties ) {
                const levelAttribute = properties.properties.find(prop => prop.displayName === filterValue);
                if (levelAttribute) {
                    if (!valueMatrix[filterValue]) {
                        valueMatrix[filterValue] = [];
                    }
                    valueMatrix[filterValue].push(levelAttribute.displayValue);
                } else if (depth < MAX_RECURSION_DEPTH){
                    viewer.model.getInstanceTree().enumNodeChildren(dbId, function(childId) {
                        extractAttributesRecursively(childId, filterValue, depth+1);
                    });
                }
            }
            resolve();    
        });
    });
}
javascript autodesk-forge autodesk-viewer autodesk
1个回答
0
投票

发现错误!

我不得不将 modelInstance 交给

extractAttributesRecursively()
,然后改为

viewer.getProperties(dbId, function(properties)

只是

modelInstance.getProperties(dbId, function(properties)

© www.soinside.com 2019 - 2024. All rights reserved.