• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Changing scenes from Ruby

Scheduled Pinned Locked Moved Developers' Forum
5 Posts 3 Posters 605 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    Dhruv
    last edited by 9 Dec 2008, 00:30

    **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

    1 Reply Last reply Reply Quote 0
    • D Offline
      Dhruv
      last edited by 9 Dec 2008, 17:59

      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">

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by 10 Dec 2008, 02:52

        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}"
        ...
        
        

        Hi

        1 Reply Last reply Reply Quote 0
        • R Offline
          RickW
          last edited by 10 Dec 2008, 17:44

          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.

          RickW
          [www.smustard.com](http://www.smustard.com)

          1 Reply Last reply Reply Quote 0
          • D Offline
            Dhruv
            last edited by 11 Dec 2008, 03:42

            Thanks Rick and Jim 😄 that helped!

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            1 / 1
            • First post
              1/5
              Last post
            Buy SketchPlus
            Buy SUbD
            Buy WrapR
            Buy eBook
            Buy Modelur
            Buy Vertex Tools
            Buy SketchCuisine
            Buy FormFonts

            Advertisement