sketchucation logo sketchucation
    • Login
    1. Home
    2. s_k_e_t_c_h_y
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info
    Offline
    • Profile
    • Following 0
    • Followers 1
    • Topics 9
    • Posts 46
    • Groups 1

    s_k_e_t_c_h_y

    @s_k_e_t_c_h_y

    10
    Reputation
    10
    Profile views
    46
    Posts
    1
    Followers
    0
    Following
    Joined
    Last Online

    s_k_e_t_c_h_y Unfollow Follow
    registered-users

    Latest posts made by s_k_e_t_c_h_y

    • RE: Web dialog on a Mac

      Ok,
      I've got it!! I made a few design decisions which should be reasonably documented in the code, but I've enclosed a fully working example below for submitting forms that works on a Mac and a Windows system.

      The example below presents a single form with a radio buttons and checkboxes and after selecting them how you desire and hitting submit all of the settings are sent one by one. To get around the asynchronous/synchronous problem, I essentially have message passing between Ruby and JavaScript to force sending one option at a time (making both Operating Systems act asynchronously) from a options list. It should be quite easy to extend from here.
      

      Here is the HTML/JavaScript Code:

      
      <html>
          <body >
              Multi-Platform WebDialog form submission test<br><br>
      
              <form name="stuff">
                  Favorite Food;<br>
                  <input type="radio" name="food" value="Pizza">Pizza<br>
                  <input type="radio" name="food" value="Hotdogs">Hotdogs<br>
                  <br>Favorite Animals;<br>
                  <input type="checkbox" id="a_cat" value="a_cat">Cat<br>
                  <input type="checkbox" id="a_dog" value="a_dog">Dog<br>
                  <input type="checkbox" id="a_squirrel" value="a_squirrel">Squirrel<br>
                      
                  <input type="button" value="Submit" onclick="sendformdata()">
              </form>
              
              <script>
                  var options = {};   // object that stores all of our settings
                  
                  //Sends a mesage to SU / Ruby
                  function call_ruby( callback_name, message ) {
                      window.location = 'skp;' + callback_name + '@' + message;
                  }
              
                  //Add a checkbox value to the options object
                  function add_checkbox_data(var_str) {
                      if (document.getElementById(var_str).checked) {
                          options[var_str] = "1";
                      } else {
                          options[var_str] = "0";
                      }
                  }
              
                  //Add a radio button value to the options object
                  function add_radio_data(field) {
                      //Get the field data
                      var ps = document.getElementsByName(field);
                  
                      for (var i = 0, length = ps.length; i < length; i++) {
                          if (ps[i].checked) {
                              // store value in our object
                              options[field] = ps[i].value;
                              // only one radio can be logically checked, don't check the rest
                              break;
                          }
                      }
                  }
              
                  //Send a single setting to SU, callbacks will come and get the rest
                  function send_setting()
                  {
                      var size = 0, key;
                      var seting, value;
                      
                      for (key in options) {
                          if (options.hasOwnProperty(key)) size++;
                      }
                      
                      if (size > 0) {
                          for (setting in options) break; // Get the first setting
                          value = options[setting];
                          delete options[setting];
                          call_ruby(setting, value);
                      } else {
                          call_ruby("submit", 1);
                      }
                  }
              
                  function sendformdata()
                  {
                      add_radio_data("food");
                      
                      //The checkbox options all have unique names to simplify storage in 'options' receipt at the SU end (more callback handlers needed though)
                      //Could have concatenated all check options into a string, sent them as a single submit and parsed the string at the SU end too
                      add_checkbox_data("a_cat");
                      add_checkbox_data("a_dog");
                      add_checkbox_data("a_squirrel");
                      
                      //Send the first setting to SU, the callbacks will get the rest
                      send_setting();
                  }
      
              </script>
          </body>
      <html>
      
      

      Here is the Ruby Code:

      
      @d = nil
      @d = UI;;WebDialog.new("Test", true, "", 800, 800, 200, 200, true)
      
      htmlset = File.join( File.dirname(__FILE__), "test.html" )
      @d.set_file( htmlset )
      
      #set the callback for the radiobuttons
      @d.add_action_callback('food') { |dialog, params|
          puts '>> Food; ' + params
          @d.execute_script('send_setting();')    #check for more settings to send
      }
      
      @d.add_action_callback('a_cat') { |dialog, params|
          puts '>> Animal; Cat - ' + params
          @d.execute_script('send_setting();')    #check for more settings to send
      }
      
      @d.add_action_callback('a_dog') { |dialog, params|
          puts '>> Animal; Dog - ' + params
          @d.execute_script('send_setting();')    #check for more settings to send
      }
      
      @d.add_action_callback('a_squirrel') { |dialog, params|
          puts '>> Animal; Squirrel - ' + params
          @d.execute_script('send_setting();')    #check for more settings to send
      }
      
      #Make the window persistant on top of SU
      RUBY_PLATFORM =~ /(darwin)/ ? @d.show_modal() ; @d.show
      
      

      Thanks to all for the advice along the way.

      Sincerely, Paul

      posted in Developers' Forum
      s_k_e_t_c_h_yS
      s_k_e_t_c_h_y
    • RE: Web dialog on a Mac

      Thanks for the replies Dan,
      I'm still having issues, but I'll keep tinkering =P.

      posted in Developers' Forum
      s_k_e_t_c_h_yS
      s_k_e_t_c_h_y
    • RE: Web dialog on a Mac

      All,
      I'm still having trouble despite reading the articles and trying to incorporate the advice recommended. Most of the 'examples' seem to stop at demonstrating that the behavior is different between operating systems. I just want to have a form send back it's data. I'm presuming that this is to do with the asynchronicity on the Mac. The submit button in the form doesn't run make it back to Ruby at all and the normal button works for one option, but not multiple options.

      Here is the Ruby example code:

      @d = nil
      @d = UI;;WebDialog.new("Test", true, "", 800, 800, 200, 200, true)
      
      htmlset = File.join( File.dirname(__FILE__), "test.html" )
      @d.set_file( htmlset )
      
      #set the callback for the radio buttons
      @d.add_action_callback('food') { |dialog, params|
          puts '>> Food; ' + params
          @d.execute_script('send_radio_data();')    #return control back to JS?
      }
      
      @d.add_action_callback('animal') { |dialog, params|
          puts '>> Animal; ' + params
          @d.execute_script('send_radio_data();')    #return control back to JS?
      }
      
      RUBY_PLATFORM =~ /(darwin)/ ? @d.show_modal() ; @d.show
      

      Here is the HTML example code:

      <html>
          <body >
              Multi-Platform WebDialog form submission test<br>
              
              <form name="stuff" onsubmit="submitbutton();" method="post">
                  Favorite Food;<br>
                  <input type="radio" name="food" value="Pizza">Pizza<br>
                  <input type="radio" name="food" value="Hotdogs">Hotdogs<br>
                  Favorite Animal;<br>
                  <input type="radio" name="animal" value="Cat">Cat<br>
                  <input type="radio" name="animal" value="Dog">Dog<br>
                              
                  <input type="button" value="Submit by Button" onclick="buttonpress()">
                  <input type="submit" value="Submit by Submit">
              </form>
              
              <script>
                  function call_ruby( callback_name, message ) {
                      location = 'skp;' + callback_name + '@' + message
                  }
              
                  function send_radio_data(field) {
                      //Get the field data
                      var ps = document.getElementsByName(field);
                  
                      for (var i = 0, length = ps.length; i < length; i++) {
                          if (ps[i].checked) {
                              // Send the value back to SketchUp
                              call_ruby(field, ps[i].value);
                              // only one radio can be logically checked, don't check the rest
                              break;
                          }
                      }
                  }
              
                  function sendformdata()
                  {
                      send_radio_data("food")
                      send_radio_data("animal")
                  }
              
                  function buttonpress()
                  {
                      alert("Doing Button Press Send");
                      sendformdata();
                  }
              
                  function submitbutton()
                  {
                      alert("Doing Submit send")
                      sendformdata()    // Never gets ran
                  }
              
              </script>
          </body>
      <html>
      

      If someone could put me out of my misery and help me correct this it would be much appreciated.

      Sincerely, Paul.

      posted in Developers' Forum
      s_k_e_t_c_h_yS
      s_k_e_t_c_h_y
    • RE: Web dialog on a Mac

      Thanks for the feedback,
      I'll try some of your ideas. I did know about the unload, but thought I'd taken care of that with a message back to ruby to let it know the DOM was loaded.

      Paul.

      posted in Developers' Forum
      s_k_e_t_c_h_yS
      s_k_e_t_c_h_y
    • Web dialog on a Mac

      Hi,
      I have a plug-in I wrote working on Windows system, but have received feedback that is doesn't work on a Mac. I've borrowed a Mac and confirmed that the Web Dialog doesn't send the message back to the Ruby script. I tried a basic example (showing the console from a web dialog) and this also failed.

      I am new to using a Mac so I don't know if there is some option you need to set to permit web dialogs to operate? There doesn't appear to be any differentiation in the coding documentation required between the Operating Systems or their respective browsers.
      
      I assume this is a pretty basic fix, but can't seem to find the answer yet.
      

      Help appreciated. Happy Holidays.

      Paul.

      posted in Developers' Forum
      s_k_e_t_c_h_yS
      s_k_e_t_c_h_y
    • RE: Intersect_line_line question

      Ah,
      Good point sl. I used a similar test for a line-plane intersection test.

      posted in Developers' Forum
      s_k_e_t_c_h_yS
      s_k_e_t_c_h_y
    • RE: Intersect_line_line question

      Not the most mathematically efficient I think, but this will do for now...

      https://groups.google.com/forum/#!topic/sketchupruby/2y8fU2wMZYo

      posted in Developers' Forum
      s_k_e_t_c_h_yS
      s_k_e_t_c_h_y
    • RE: Intersect_line_line question

      Thanks again Dan,
      I'll see what I can come up with =).

      posted in Developers' Forum
      s_k_e_t_c_h_yS
      s_k_e_t_c_h_y
    • RE: Intersect_line_line question

      Thanks Dan,
      That makes sense. I see that the way the function operates however isn't what I was after. The function as it stands turns the lines into infinite rays defined by the line's two points and then checks if the infinite rays intersect. I just want to check if the line segments bounded by the pairs of points intersect.

      I'm muddling through implementing this now, unless you know of an existing coded examples. There are 5 cases to check for
      
      1. Parallel Lines
      2. Normal Intersection within line segments
      3. Intersection, but outside limits defined by line segment points
      4. No intersection at all
      5. Lines are coincident (intersection is a line, not a point)

      Sincerely, Paul.

      posted in Developers' Forum
      s_k_e_t_c_h_yS
      s_k_e_t_c_h_y
    • Intersect_line_line question

      Hi,
      I'm having trouble with Geom.intersect_line_line not giving the expected results so I was hoping someone could either tell me what I'm doing wrong or that the function is bugged so I know whether to rewrite it.

      According to the documentation in the API

      @unknownuser said:

      Geom.intersect_line_lineSketchUp 6.0+
      The intersect_line_line computes the intersection of two lines.

      Arguments:

      line1
      The first line to intersect.
      line2
      The second line to intersect.
      Returns:

      point
      The intersection point. Returns nil if they do not intersect.
      line1 = [Geom::Point3d.new(10,0,0), Geom::Vector3d.new(1,0,0)]
      line2 = [Geom::Point3d.new(0,10,0), Geom::Point3d.new(20,10,0)]
      pt = Geom.intersect_line_line(line1, line2)

      This will return the point (10,10,0).

      If you type in their example into the Ruby editor it doesn't actually work as advertised, it returns nil.

      Here is an example of two parallel lines returning a value...

      l1 = [[0,0,0], [10,0,0]]
      [[0, 0, 0], [10, 0, 0]]
      l2 = [[0,5,0], [10,5,0]]
      [[0, 5, 0], [10, 5, 0]]
      i = Geom.intersect_line_line(l1, l2)
      Point3d(-10, 0, 0)
      

      Thoughts?

      Sincerely, Paul.

      posted in Developers' Forum
      s_k_e_t_c_h_yS
      s_k_e_t_c_h_y