Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

Caculate all volume of objects in viewer- Autodesk Forge

I want to caculate all volume of objects in viewer but return arrayVolume = [] and vol = 0; Where did I go wrong ? Thank in advanced for all

    $("#kl").click(function(){
viewer.model.getExternalIdMapping(data => kl(data));
function kl(data){
    var arrayVolume = [];
    var vol;
    for(var key in data){
        var dbId = data[key];
        viewer.getProperties(dbId, function(e){
            //console.log('Entire object response ',e);
            //console.log('Properties ',e.properties);
            var propertiesObj = e.properties;
            propertiesObj.forEach(obj => {
                if(obj.displayName === "Volume"){
                    var volume = obj.displayValue;
                    arrayVolume.push(volume);
                }
            });
        });
    }
    vol= arrayVolume.reduce(function(a,b){return a+ b;},0)
    alert('w: '+arrayVolume);
    alert('w: '+vol);
}
question from:https://stackoverflow.com/questions/66058260/caculate-all-volume-of-objects-in-viewer-autodesk-forge

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Be careful about synchronous vs. asynchronous code. The viewer.getProperties method is asynchronous, meaning that the callback you provided might be called later. In your code snippet the two alert calls are most likely called before the arrayVolume and vol properties are populated with your callback function.

Also, note that when you need to retrieve properties from more than one dbID, there's a better method for that: https://forge.autodesk.com/en/docs/viewer/v7/reference/Viewing/Model/#getbulkproperties-dbids-options-onsuccesscallback-onerrorcallback.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...