Changing scenes from Ruby
-
**What I am trying to do,
I am trying to change scenes through web-dialogue, Inside web dialogue I have buttons upon which clicken on , scenes will change.
Here is part of my ruby code.**
@@xzybit_dlg.add_action_callback("get_data") do |web_dialog,action_name|
if action_name=="selected_page"
#For scene 1
pages = Sketchup.active_model.pages
pages.selected_page = pages["Scene 1"]
#js_command = "passFromRubyToJavascript("+ selected_page.to_s + ")"
web_dialog.execute_script(js_command)
end# For scene 2 if action_name="selected_page" pages1 = Sketchup.active_model.pages pages1.selected_page = pages1["Scene 2"] #js_command = "passFromRubyToJavascript("+ selected_page.to_s + ")" web_dialog.execute_script(js_command) end # For scene 3 , bug for now, it goes to scene 2 when clicked upon scene 3 if action_name="selected_page" pages2 = Sketchup.active_model.pages pages2.selected_page = pages2["Scene 3"] #js_command = "passFromRubyToJavascript("+ selected_page.to_s + ")" web_dialog.execute_script(js_command) end end
Here is part of my javascript code
function callRuby(actionName) {
query = 'skp:get_data@' + actionName;
window.location.href = query;
}
<input type="button" onclick="callRuby('selected_page')">
<input type="button" onclick="callRuby('pages1.selected_page')">
<input type="button" onclick="callRuby('pages2.selected_page')" value="Scene 3">scene 1 and 2 are working fine. But when I click on Scene 3 it goes to scene 2 and not scene 3, which needs to be fixed.
Thank you,
Dhruv -
Here is the corrected part of javascript. But still I am facing the same problem. Actually that was a typo in my previous post.
<input type="button" onclick="callRuby('selected_page') value ="Scene1">
<input type="button" onclick="callRuby('pages1.selected_page') value = "Scene2">
<input type="button" onclick="callRuby('pages2.selected_page')" value="Scene 3"> -
Dhruv,
Add some 'puts debugging' in your callback to see what strings are being passed by the Javascript:
???.add_action_callback("get_data") do |web_dialog,action_name| puts "action_name=#{action_name}" ...
-
First off, action_name will only equal "selected_page" for the first button. The second button will return "pages1.selected_page" for action_name, and the third button will return "pages2.selected_page", so the equality check needs help. Second, there's probably an easier way to accomplish things.
The general form for callbacks is
add_action_callback(callback_name) {|dialog,parameters| action}
callback_name is the name of the callback. In your case, a good callback name might be "show_scene".
dialog will return the active dialog object.
parameters will return any parameters you send, and can be used just like the GET method for submitting forms. Of course, you could send a simple parameter like "scene1".
action should ideally call a method in your class, rather than trying to handle everything right there, unless it's a really short action.... @@xzybit_dlg.add_action_callback("select_page") {|web_dialog,page| show_page(page)} ... def show_page(page) pages = Sketchup.active_model.pages pages.selected_page = pages[page] end ... #javascript function showPage(page) { query = 'skp;show_page@' + page; window.location.href = query; } ... #html <input type="button" onclick="showPage('Scene1') value = "Scene1"> <input type="button" onclick="showPage('Scene2') value = "Scene2"> <input type="button" onclick="showPage('Scene3') value = "Scene3">
Now, you may be trying to accomplish other things besides just activating a scene. Just add more callbacks and methods. It's often easier than trying to create a do-everything callback, unless the actions are very similar.
-
Thanks Rick and Jim that helped!
Advertisement