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

    Posts

    Recent Best Controversial
    • RE: Ruby Docs

      @dan rathbun said:

      How about a Ruby Cheatsheet?

      Ruby QuickRef
      http://www.zenspider.com/Languages/Ruby/QuickRef.html

      Excellent!

      I took the liberty of converting the cheatsheet to lowercase tags and attributes, then fiddled with the TOC a bit. I've written the author requesting permission to continue fiddling.


      ruby_quickref.zip

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: Control animation from own software?

      The camera is easy to position using Ruby and the API. Watch the first 30 seconds of my Airshow! movie for a good example.

      What is the requirement for a separate C++ program?

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: Ruby Docs

      @honoluludesktop said:

      I often need a more baby one. Any suggestions?

      Link Preview Image
      Learn to Program, by Chris Pine

      favicon

      (pine.fm)

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: They say Ruby is slow...

      @adamb said:

      Strength Reduction is the transformation that would turn these loops into do_nothing loops.

      Yes, but that's for really sharp compilers. I was just expecting func( 1.0 + 2.0 ) to be turned into func( 3.0 ).

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: Sketchup.write_default needs escaping?

      @thomthom said:

      Looks like it evaluates the string and treats the backslashes as special characters.

      I had no end of troubles with slashes. Finally implemented this none-too-clever code and all the bugs went away:

      =begin
      qq() and qq!() do these replacements;
          "'" -> "qq1"
          '"' -> "qq2"
          '/' -> "qq3"
          '\' -> "qq4"
          '\n' -> "qq5"
          '\r' -> "qq6"
      =end
      
      def self.qq( str )
          return str.gsub( 
              /\'/, 'qq1' ).gsub( 
              /\"/, 'qq2' ).gsub( 
              /\//, 'qq3' ).gsub( 
              /\\/, 'qq4' ).gsub(
              /\n/, 'qq5' ).gsub(
              /\r/, 'qq6' )
      end # of qq()
      
      def self.qq!( str )
          str.gsub!( /\'/, 'qq1' )    
          str.gsub!( /\"/, 'qq2' )    
          str.gsub!( /\//, 'qq3' )    
          str.gsub!( /\\/, 'qq4' )
          str.gsub!( /\n/, 'qq5' )
          str.gsub!( /\r/, 'qq6' )
      end # of qq!()
      
      

      These reversers complete the Ruby:

      
      def self.unqq( str ) # reverses qq()
          return str.gsub( 
              /qq1/, '\'' ).gsub( 
              /qq2/, '"' ).gsub( 
              /qq3/, '/' ).gsub( 
              /qq4/, '\\' ).gsub(
              /qq5/, '\n' ).gsub(
              /qq6/, '\r' )
      end # of unqq()
      
      def self.unqq!( str ) # reverses qq!()
          str.gsub!( /qq1/, '\'' )
          str.gsub!( /qq2/, '"' )
          str.gsub!( /qq3/, '/' )
          str.gsub!( /qq4/, '\\' )
          str.gsub!( /qq5/, '\n' )
          str.gsub!( /qq6/, '\r' )
      end # of unqq!()
      
      

      These are the reversers in JavaScript:

      
          single_quote = String.fromCharCode( 39 );
          double_quote = String.fromCharCode( 34 );
          ford_slash = String.fromCharCode( 47 );
          back_slash = String.fromCharCode( 92 );
          
      
          function unqq( msg ) {
              s = msg.replace( /qq1/g, single_quote );
              s = s.replace( /qq2/g, double_quote );
              s = s.replace( /qq3/g, ford_slash );
              s = s.replace( /qq4/g, back_slash );
              s = s.replace( /qq5/g, '\\n' );
              s = s.replace( /qq6/g, '\\r' );
              return s;
          }
      
      
      posted in Developers' Forum
      M
      MartinRinehart
    • RE: They say Ruby is slow...

      @thomthom said:

      ...and I believed that. It is after all a scripting language we're talking about. But wow! - it's slow!

      
      > > t=Time.now; 1000000.times { 1.0 }; puts Time.now - t
      > 0.1
      > nil
      > 
      > > t=Time.now; 1000000.times { 1.0 + 2.0 }; puts Time.now - t
      > 0.985
      > nil
      > 
      > > t=Time.now; 1000000.times { 1.0 + 2.0 + 3.0 }; puts Time.now - t
      > 1.85
      > nil
      > 
      > > t=Time.now; 1000000.times { 1.0 + 2.0 + 3.0 + 4.0 }; puts Time.now - t
      > 2.76
      > nil
      > 
      > > t=Time.now; 1000000.times { 1.0 + 2.0 + 3.0 + 4.0 + 5.0 }; puts Time.now - t
      > 3.685
      > nil
      > 
      

      If I remember correctly, compilers started adding constant folding in the '80s. This is truly brain dead.

      On the other hand, Ruby allows 1_000_000.times ... when counting zeros gets annoying. A peculiar mixture of great stuff and trash.

      posted in Developers' Forum
      M
      MartinRinehart
    • Ruby Docs

      In my tutorial I suggest using a spare browser as a reference guide. One tab for the SketchUp Ruby API, another for Ruby, more for JavaScript, CSS and HTML.

      I use this setup and it's very helpful, except for Ruby. My tab is http://ruby-doc.org/core/. In practice I find it's faster to do a Google search.

      For the rest I've found references that I'm happy with, but not for Ruby. Suggestions?

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: 3D Engine

      Take a look at my 3-minute movie to see SketchUp in its full-4D glory.

      Then use the code in my tutorial's Chapter 16 for classes that let you do all this without need for the APIs Transformation class.

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: [help]Group component instances

      In addition to TIG's info, you should move all the one-time code outside the loop.

      
      model = Sketchup.active_model
      entities = model.active_entities
      path = Sketchup.find_support_file "mymodel.skp" ,"Components/"
      . . .
      
      

      I also heartily recommend that you use four-space indentation in spite of the fact that two spaces is a Ruby convention. Four is much more readable which is why it is mandatory in Python, convention in C++, Java, JavaScript, ... (every language I know, except Ruby).

      New languages are always a PITA. Fortunately, quite a small subset of Ruby is all you really need to create SketchUp plugins.

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: Untrapped Error

      @unknownuser said:

      rescue Exception => e

      to catch everything.

      I don't want to seem ungrateful, but that upgraded me to Bug Splat. Is there a

      rescue BugSplat => e

      ???

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: Open URL on local disk

      I've set single click to open. An HTM opens in Chrome.

      posted in Developers' Forum
      M
      MartinRinehart
    • Untrapped Error

      I'm writing a console. You input 2+2 and it outputs 2+2 # 4. Problem is, your input may not be perfect, so a little error trapping is needed.

      In this part of the process() function (line #s added):

      
      59    results = []
      60    for line in lines do
      61        rslt = line.chomp() + ' # '
      62        begin
      63            rslt += eval(line).to_s().chomp()
      64        rescue => e
      65            rslt += e.to_s().chomp()
      66        end
      67    
      68        results.push( qq(rslt) ) 
      69    end # loop over lines
      
      

      I'm thinking that an error at line 63 gets caught and reported at line 65. It usually does. Now, however, I've got this:

      process.jpg

      Any ideas why this isn't caught by the rescue statement? Any ideas on how to catch it?

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: Optimization Tips

      Let's see - for performance I'm going to avoid iterations, arrays, hashes and objects.

      What's left?

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: [CExt] C array to Ruby Array?

      Ruby has its wonderful numbers (integers when possible, floats as needed) but they aren't C's much simpler (and faster) plain numbers. Cs numbers can be used directly by the CPU/FPU. Ruby's numbers are object instances.

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: Open URL on local disk

      @dan rathbun said:

      You should have instead had the &Edit association pointing to Notepad++

      Folder Options has disabled what I need:

      folder_options.jpg

      I gave the user admin privileges, but that didn't help. I right-clicked in Explorer, Properties, and was able to change the default Open to a browser, getting openURL() working but that doesn't allow setting the Edit default.

      Can I go back to KDE now?

      posted in Developers' Forum
      M
      MartinRinehart
    • Yet Another WebDialog Gotcha

      Ruby handles text files correctly on both Windows and Mac. The line separator is the newline character (10, '\n'), regardless of platform.

      This does not extend to text retrieved from a WebDialog textarea widget, which will be delimited with newlines on a Mac, with return/newline pairs on a PC. This little bit of extra code is needed:

      text = web_dialog.get_element_value( 'textarea_id' ) text.gsub!( /\r\n/, "\n" ) # needed extra!

      Thank Todd Burch for his Mac support.

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: Textarea line separators on Mac

      @unknownuser said:

      I personally see no productivity or functionality gains from bothering with each platforms newline patterns, unless you just want to.

      This isn't the file I want given the input textarea:

      two.jpg

      You have to convert crlf to newline because Ruby converts newline to crlf as it writes the file on a PC. Ugh.

      
      # ta_sep.rb 
      
      require 'sketchup'
      
      html="
      <html> <body>
      
      <textarea id='ta'>one
      two</textarea>
      
      <script>
          onload = function() { location = 'skp;go'; }
      </script>
      
      </body> </html>"
      
      wd = UI;;WebDialog.new( "Test", true, "test", 400, 300, 100, 100, true )
      wd.set_html( html )
      
      wd.add_action_callback( "go" ) do | dlg, msg |
          val = wd.get_element_value( 'ta' )
          # val.gsub!( /\r\n/, "\n" )
          pn = UI.savepanel( 
              "Save As ...", File.dirname(__FILE__), 't.txt' )
          file = File.new( pn, 'w' )
          file.puts( val )
          file.close()
      end
      
      wd.show()
      
      
      

      I think this is correct cross-platform code if you uncomment the gsub!(). For this I be thanking you and Notepad++ (View/Show Symbol/Show All Characters).

      Ruby's lovely ability to handle everything with newlines, platform-independently, does not extend to text read from a browser's textarea widgets.

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: Textarea line separators on Mac

      @unknownuser said:

      You're welcome!

      Thank you!

      @unknownuser said:

      Why are you coding at that level?

      Stupidity or ignorance. Maybe both.

      @unknownuser said:

      Why not just bust the input data up into ruby "newlines"?

      Hmmm. This is the code that gets a WebDialog <textarea>'s data into a disk file:

      
          # write the file
          str = wd_console.get_element_value( 'console' )
          str.gsub!( /\r\n/, "\n" ) # why? doesn't look PC, but it works
          file = File.new( pathname, 'w' )
          file.puts( str )
          file.close()
      
      

      I'm thinking that will work fine on a Mac, because the gsub!() will find zero occurrences. Now the other direction: you read the file as a string and pass the string to the textarea and it will be Win/Mac compatible.

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: Open URL on local disk

      @cjthompson said:

      even just UI.openURL("notepad") will also work.

      You never know when one little factoid will solve a problem.

      I tried notepad (it works), then notepad++ (also works) and then an HTML file. It opened the HTML, but it opened it in Notepad++.

      Now if only I can figure out how to specify both program and file ...

      posted in Developers' Forum
      M
      MartinRinehart
    • RE: Python/Ruby vs. Ruby/Python

      Don't think I want to dust off Decaf until I've got time to go after it. I forget where I put my mains in Java. There's tons of my Java on my site: http://www.MartinRinehart.com , including the original Decaf tokenizer.

      There's a whole new vocabulary (words, not tokens; phrases, not expressions; sentences, not statements; etc.) that I'll need to document before anyone could realize that I wasn't just babbling.

      See "The Project", my site, for more.

      posted in Developers' Forum
      M
      MartinRinehart
    • 1
    • 2
    • 6
    • 7
    • 8
    • 9
    • 10
    • 38
    • 39
    • 8 / 39