Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...