根据我在其他一个问题(Get a list of all objects from loaded model)上给出的建议,我在我的应用程序中添加了此代码以从所有对象中检索所有属性:
function onGeometryLoadedEvent(viewer) {
var dbIds = Object.keys(
viewer.model.getData().instanceTree.nodeAccess.dbIdToIndex
);
this.viewer.model.getBulkProperties(
dbIds,
{
propFilter: false,
ignoreHidden: true
},
(objects) => console.log(objects),
() => console.log('error')
)
}
它正确显示所有对象。问题是需要很多时间才能完成(+1分钟),即使对于只有10,000个物体的模型也是如此。
这是正常的吗?
我真的需要带有类别的对象列表,我必须在让它们向用户呈现所有可用类别和属性的列表之后对它们进行排序。
我知道我可以使用Model Derivatives API,但如果可能的话我想避免使用它。
请注意,当您列出模型上的所有元素时,它将包括那些不可见的元素,例如类别。如果您只需要模型上的元素,那么您需要模型的叶子。这个article described it和源代码如下:
function getAllLeafComponents(viewer, callback) {
var cbCount = 0; // count pending callbacks
var components = []; // store the results
var tree; // the instance tree
function getLeafComponentsRec(parent) {
cbCount++;
if (tree.getChildCount(parent) != 0) {
tree.enumNodeChildren(parent, function (children) {
getLeafComponentsRec(children);
}, false);
} else {
components.push(parent);
}
if (--cbCount == 0) callback(components);
}
viewer.getObjectTree(function (objectTree) {
tree = objectTree;
var allLeafComponents = getLeafComponentsRec(tree.getRootId());
});
}
现在使用.getBulkProperties
你可以指定你想要的属性,所以类似于类别,对吗?所以指定,as shown at this article并与上面的.getAllLeafComponent
函数一起使用:
getAllLeafComponents(viewer, function (dbIds) {
viewer.model.getBulkProperties(dbIds, ['Category'],
function(elements){
// ToDo here
// this will only include leaf with Category property
})
})
我希望这比原始代码更快,因为我正在过滤元素和属性。