sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Where to start?

    Scheduled Pinned Locked Moved Developers' Forum
    44 Posts 4 Posters 772 Views 4 Watching
    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.
    • R Offline
      rumcajs
      last edited by

      I want to dig in this stuff. Can you post links where to go, when I want to learn about SU structure? I need some site where I can learn a bit about basics (SU model structure, types, variables, how to debug etc), but would be best if you know about some site for programmers, which want to start learn ruby to program plugins for SU. I am not gonna do crazy things, just want to know how basic things work like, drawing of lines, reading/saving of files, working with arrays, objects. Also some forum where I can ask how to do things...

      1 Reply Last reply Reply Quote 0
      • Chris FullmerC Offline
        Chris Fullmer
        last edited by

        I'm sure everyone here will have a different answer to your question. But here goes mine.

        Right here in this forum is by far the best place to ask questions and typically get answers. Often answers come within minutes, or a few hours. None of this wait around a month for someone to get back to you. Its an active community.

        Also in this forum, there is a thread called SketchUp-Ruby resources. Look through that. Lots of important links and info. Make sure to scroll all the way though, Dan Rathbun posted a comprehensive list of links to important resources:
        http://forums.sketchucation.com/viewtopic.php?f=180&t=10142

        #1 most important link for SketchUp programming though has to be the SketchUp API:
        https://developers.google.com/sketchup/

        Imprtant Ruby programming resource, is the Pickaxe, available online:
        http://www.ruby-doc.org/docs/ProgrammingRuby/

        As for getting started, and just being able to fiddle around with ruby inside of Sketchup, to get a feel for how it works, I 100% hands down prefer Jim Foltz's Ruby Web console:
        http://sketchuptips.blogspot.com/2007/08/plugin-webconsolerb.html

        Use that to enter multi line snippets of code to test. very helpful for trying to figure out specific methods of the SketchUp API.

        And one thing I've put together, for better or for worse, is a series of YouTube videos where I show how to begin writing code to do specific things. Typically its simple, but important things like add entities to the selection via ruby, or clear the selection set. How to pushpull things. There are just a few videos, most about 5 minutes each. Here's some links

        http://www.youtube.com/watch?v=vTQWl4vLAOU&feature=plcp - Getting Started
        http://www.youtube.com/watch?v=K4mjZeEOgJE&feature=plcp - Using the Selection Set
        http://www.youtube.com/watch?v=lPGIqpWsSSQ&feature=plcp - Remove edges from the Selection Set
        http://www.youtube.com/watch?v=3P5xPwQdD7w&feature=plcp - Erase Faces
        http://www.youtube.com/watch?v=Aq1YEaI2XlA&feature=plcp - Multiple Pushpull Tutorial

        Lately you've been tan, suspicious for the winter.
        All my Plugins I've written

        1 Reply Last reply Reply Quote 0
        • R Offline
          rumcajs
          last edited by

          Thanks for links. I'm impatient coz the language looks very close to my programming knowledges.

          I will read the links tommorow, right now few things had surprised me:

          For example:

          
          def file_loaded?(filename)
              $loaded_files.include? filename.downcase
          end
          
          

          No return value? Does the "?" on end mean that it returns bolean?

          What does mean "e" and "|e|" in

          
          entities.each do |e|
                faces.push e if e.is_a? Sketchup;;Face
          end
          
          

          Just impatience

          1 Reply Last reply Reply Quote 0
          • Chris FullmerC Offline
            Chris Fullmer
            last edited by

            ruby always returns something, even if its "nil". methods always return whatever is on the last line of the method. So whatever $loaded_files.include? filename.downcase returns is what that method will return. If your uncomfortable with that, you can always put a return in front of it for fun - return $loaded_files.include? filename.downcase

            No, the question mark does not mean boolean. It is just another character you can use in a method name. So it just helps make it read a little bit more like english.

            In the e and |e| question, "e" is variable that represents the object that is being processed. So entities.each is going to process each object in the entities collection. Each time, the object that is being processed will be called e. So essentially the line of code is saying push (as in push/pull) the sketchup object (e) if that object (e) is a face. If its not a face, it moves on to the next object in the entities collection.

            Lately you've been tan, suspicious for the winter.
            All my Plugins I've written

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              If you're new to Ruby I recommend "Ruby in Twenty Minutes": http://www.ruby-lang.org/en/documentation/quickstart/

              As for SketchUp API, have a look at the sticky forum posts in this Developers section.

              Thomas Thomassen — SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • R Offline
                rumcajs
                last edited by

                Yup. So it is similar to foreach (PHP and other languages that use that syntax like foreach ($arr as $key => $val ) {} ). Ruby is very similar to JQuery and Javascript.

                1 Reply Last reply Reply Quote 0
                • thomthomT Offline
                  thomthom
                  last edited by

                  for key, value in array
                    puts "#{key} - #{value}"
                  end
                  
                  array.each { |key,value|
                    http://www.thomthom.net/software/vertex_tools/manual
                  }
                  

                  Thomas Thomassen — SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 Reply Last reply Reply Quote 0
                  • R Offline
                    rumcajs
                    last edited by

                    puts "Hello #{name.capitalize}!"
                    

                    You can "add" any method to string inside string? That is awesome.

                    Class syntax:

                    class Greeter
                    def initialize(name = "World")
                      @name = name
                    end
                    end
                    

                    Does the class can have something like:

                    class Greeter
                      @name = ""
                      @myname = 
                    end
                    

                    Or you cannot define variables outside method?

                    It looks like I have use initialize method to define instances before all

                    Is this @ used only in classed? I have seen using

                    @@time_start = Time.now
                    

                    what does mean the @@?

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      @rumcajs said:

                      puts "Hello #{name.capitalize}!"
                      

                      You can "add" any method to string inside string? That is awesome.

                      That's string interpolation / it's the most efficient way to concatenate strings.

                      @rumcajs said:

                      what does mean the @@?

                      Class variable - single @ is instance variables. Have a look at the Ruby in Twenty minutes link. It goes through all the basics like this.

                      Thomas Thomassen — SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • R Offline
                        rumcajs
                        last edited by

                        @thomthom said:

                        Have a look at the Ruby in Twenty minutes link. It goes through all the basics like this.

                        Yeah, I've just finished.

                        Is it only way of detecting if variable is array or do you have more ways? (inside class)

                        
                        if @names.respond_to?("each")
                        end
                        
                        1 Reply Last reply Reply Quote 0
                        • R Offline
                          rumcajs
                          last edited by

                          I'm having a fun with this:
                          http://tryruby.org/levels/1/challenges/0#levels/1/challenges

                          This interactive guide for ruby is really cool. Somebody could make one for ruby beginners in SU, don't you think? It looks simple to do something like that.

                          1 Reply Last reply Reply Quote 0
                          • thomthomT Offline
                            thomthom
                            last edited by

                            @rumcajs said:

                            @thomthom said:

                            Have a look at the Ruby in Twenty minutes link. It goes through all the basics like this.

                            Yeah, I've just finished.

                            Is it only way of detecting if variable is array or do you have more ways? (inside class)

                            
                            > if @names.respond_to?("each")
                            > end
                            

                            @names.is_a?( Enumerable )

                            Btw, - strings are slow, avoid them if you can. For respond_to?("each") use Symbols instead - like so: respond_to?(:each)

                            Thomas Thomassen — SketchUp Monkey & Coding addict
                            List of my plugins and link to the CookieWare fund

                            1 Reply Last reply Reply Quote 0
                            • R Offline
                              rumcajs
                              last edited by

                              @thomthom said:

                              Btw, - strings are slow, avoid them if you can. For respond_to?("each") use Symbols instead - like so: respond_to?(:each)

                              Yes, I read it just now in the interactive guide 😉 I create my first hash. But don't know what is it hash, looks like an object... Why they call it hash?

                              1 Reply Last reply Reply Quote 0
                              • thomthomT Offline
                                thomthom
                                last edited by

                                I take it you come from Javascript background?

                                Object have methods - Hashes has data.
                                A hash is an associative array - you specify the key and value. While with an array you just insert or remove values. (PHP and Javascript blends the difference between Hashes and Arrays. In Ruby they are distinct. ) Note that the order of the elements in arrays is based on the order they where added. When you iterate a Hash the values and keys is returned in a different order from how they where inserted.

                                Thomas Thomassen — SketchUp Monkey & Coding addict
                                List of my plugins and link to the CookieWare fund

                                1 Reply Last reply Reply Quote 0
                                • R Offline
                                  rumcajs
                                  last edited by

                                  OK. So summary.

                                  Objects can keep properties (@name...) and methods.
                                  Arrays [] are only non-associative.
                                  Hashes {} are associative arrays, not ordered.

                                  Can you print function or method just like in JS? I know more scripting languages not just JS. Last one is AHK.

                                  1 Reply Last reply Reply Quote 0
                                  • R Offline
                                    rumcajs
                                    last edited by

                                    I tried this (interactive guide):

                                    ratings = Hash.­new(0)
                                    => {}
                                    Success!
                                    > books.valu­es.each { |rate­| ratin­gs[rate] += 1 }
                                    => [;splendid, ;quite_good, ;mediocre, ;quite_not_good, ;abysmal]
                                    > ratings
                                    => {;splendid=>1, ;quite_good=>1, ;mediocre=>1, ;quite_not_good=>1, ;abysmal=>1}
                                    >  
                                    

                                    And it is not clear to me, hoq the value 1 is assigned to the values. I guess that first command created new hash ratings. Then I go through each value of the hash and set the ratings... But how is it possible, that ratings hash overwrite the book.values? As I know this is problem in PHP. If you know something like

                                    foreach(books->valu­es as $val){ 
                                    ; this will not work;
                                    val="new value";
                                    }
                                    

                                    It will not overwrite the for values of "values" property. Maybe the relation between ratings and books.valu­es was already defined somewhere in a class, which is not visible to me?

                                    This command does not work to me as expected:

                                    .. File.Open(­"/Home/com­ics.txt", "a") do |f|
                                    .... jjj
                                    .. ss
                                    

                                    I can write in the file but how to close it and to keep data in it? There should be some other stuff in the file when I write File.Open(­"/Home/com­ics.txt", "a") . Once I succeeded there were some links and they say, that is should add some data in file...

                                    1 Reply Last reply Reply Quote 0
                                    • Dan RathbunD Offline
                                      Dan Rathbun
                                      last edited by

                                      @rumcajs said:

                                      I tried this (interactive guide):

                                      ratings = Hash.­new(0)
                                      > => {}
                                      > Success!
                                      > > books.valu­es.each { |rate­| ratin­gs[rate] += 1 }
                                      > => [;splendid, ;quite_good, ;mediocre, ;quite_not_good, ;abysmal]
                                      > > ratings
                                      > => {;splendid=>1, ;quite_good=>1, ;mediocre=>1, ;quite_not_good=>1, ;abysmal=>1}
                                      > >  
                                      

                                      And it is not clear to me, how the value 1 is assigned to the values.

                                      Because the Ruby interpreter changes the shorthand a += b, to a = a + b

                                      So your statement:
                                      books.valu­es.each { |rate­| ratin­gs[rate] += 1 }

                                      is actually executed as:
                                      books.valu­es.each { |rate­| ratin­gs[rate]= ratin­gs[rate] + 1 }

                                      😄

                                      I'm not here much anymore.

                                      1 Reply Last reply Reply Quote 0
                                      • Dan RathbunD Offline
                                        Dan Rathbun
                                        last edited by

                                        Method names begin with a lower case character (by convention.)

                                        So it is:

                                        ` File.open("path/to/file","w") do |f|

                                        end`

                                        The block form of IO::open with automatically close the file handle.

                                        I'm not here much anymore.

                                        1 Reply Last reply Reply Quote 0
                                        • R Offline
                                          rumcajs
                                          last edited by

                                          Something else confused me. But i think I had overlooked something. The ratings are not saved in values, but in ratings...

                                          Things are going bad, I cannot move on.

                                          
                                          #<SyntaxError; Invalid char "\xC2" in expression. near line 1; "\xADread(\"/Hom\xC2\xADe/comics.t\xC2\xADxt\")">
                                          > print File.­­read("/co­mics.t­xt"­)
                                          =>
                                          #<SyntaxError; Invalid char "\xC2" in expression. near line 1; "\xADread(\"/comics.t\xC2\xADxt\")">
                                          > print File.­read("comi­cs.txt")
                                          =>
                                          #<Errno;;ENOENT; No such file or directory - comics.txt>
                                          > Dir["/*"]
                                          => []
                                          Open up a new BlogEntry class, pretty pretty please.
                                          >  
                                          
                                          

                                          How can I print what is in /home/comics.txt and /comics.txt and if they exists and why this error happens...

                                          1 Reply Last reply Reply Quote 0
                                          • R Offline
                                            rumcajs
                                            last edited by

                                            Whatever i write it jumps into input mode:

                                            comics.is_­a
                                            .. end
                                            > comics.is_­a?
                                            ..  
                                            
                                            

                                            Damn, that's not 15 minutes tutorial, but 15 hours (I'm in half but cannot move on).

                                            
                                            > Popup.make­ do
                                            .... h1 "Header"
                                            .... list do
                                            ...... comics.eac­h do |name­, url|
                                            ........ link name,­ url
                                            ........ end
                                            ...... end
                                            .... end
                                            ..  
                                            
                                            

                                            Should generate web page, but nothing popups. And I cannot check if the array exists, what is in array, what is in directory or if the file exist... Look like not realy ruby console (running in browser). 👎

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

                                            Advertisement