Hi,
I face problem when I try to get return value from an action callback and the value that is read by Javascript is always undefined. I already read some posts that discuss the same issues like here and here and also read docs about add_action_callback but unfortunately I failed to implement it to my code.
Here is my code in Ruby side:
# add callback to calculate QTO
@dlg.add_action_callback("calculate") { |action_context|
self.export_qto()
}
def self.export_qto()
# grab active model
model = Sketchup.active_model
# initiate selected entities total volume
message = "Volume of all selected entities: \n"
vol = 0.0
# select entities
selected = model.selection
if (selected.length > 0)
selected.each { |item|
if (item.typename == 'Group')
if (item.volume != -1)
vol = vol + item.volume
message = message + item.definition.name + '(' + item.typename + ')' + ': ' + format_volume_in_model_units(item.volume, 2) + "\n"
end
end
}
puts message
message
else
UI.messagebox('No entities selected. Select entities and try again!', type=MB_OK)
end
end
And here is my code in Javascript side:
function calculate() {
let hasil = []
let result = sketchup.calculate({
onCompleted: function (data) {
console.log(data)
hasil.push(data)
}
})
if (result) {
document.getElementById('hasilhitungan').innerHTML = hasil[0];
console.log('Hasil: ', hasil[0]);
}
}
However the resulting value is always undefined.
Do I have to always use execute_script on the Ruby side to be able to send the return value back to Javascript? Or I could still use this approach instead?
Thanks… really appreciate your helps…
7 posts - 3 participants