sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ«› Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Ruby Plugin Help

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 4 Posters 298 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.
    • A Offline
      AlexS
      last edited by

      Hello,

      I've made a a plugin for Ruby before, but I'm writing another one and it requires something a bit tougher... πŸ˜„

      I want to gather all the groups that have the name that starts with "SPECIAL". In reality these groups will contain no faces, but simply curves. I want to get each "point" in the group and write it to a file (I know how to do that).

      Now question is, how do I get the points for a line segment?

      If I need to clarify more simply ask,

      Thanks,

      Alex

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        verts=[]
        group.entities.each{|e| verts << e.vertices if e.class==Sketchup::Edge }
        verts.flatten!

        each e.vertices returns a two element array, flatten explodes these

        inner arrays so that verts is then one array...

        verts.uniq!

        this removes duplicate vertices from verts, i.e. because in a curve

        most vertices get listed twice because they will have two edges...

        points=[]
        verts.each{|v| points << v.position }

        points is now a list of all points represented by

        all of the edges' vertices inside the given group...

        TIG

        1 Reply Last reply Reply Quote 0
        • A Offline
          AlexS
          last edited by

          Thanks, TIG!

          How can I separate the groups whos names start with SPECIAL and the ones that don't, though? Thanks for your help,

          Alex

          EDIT: And also, I'm trying to use the 'puts' function, but when I'm doing this: file.puts "<model/>", it thinks that when I use the < & > symbols that I'm trying to define a constant or something. This is the same for the 'print' function. How can I add the < > characters? Thanks for your patience with me. πŸ˜›

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

            Well since you use the group.name= method to set the group's name, you will use the group.name method to retrieve it. Use a comparison expression:

            verts=[

            Sketchup.active_model.entities.each {|ent|
            if ent.kind_of?(Sketchup::Group) && ent.name == 'SPECIAL']

            ... then iterate the group's entites ...

            ... then flatten and uniqify the verts array,
            ... which can be done in one line:
            %(#BF0000)[verts.flatten!.uniq!

            end # if SPECIAL group
            }
            points=[]]
            ... rest of code as shown above.

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • A Offline
              AlexS
              last edited by

              Sorry I wasn't clear enough... I mean to retrieve whatever groups whos name STARTS with SPECIAL... Is there is an equivalent to C's "StartsWith" in Ruby?

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

                You test for substrings using .include?

                if ent.kind_of?(Sketchup::Group) && ent.name.include?('_SPECIAL_')

                If you wish to really use starts with, then you can test a subrange of a string:

                if ent.kind_of?(Sketchup::Group) && ent.name[0..8] == '_SPECIAL_'

                or:
                if ent.kind_of?(Sketchup::Group) && ent.name[0,9] == '_SPECIAL_'


                You can look up Core methods online:
                http://www.ruby-doc.org/core-1.8.6/index.html

                .. or download the CHM for offline use, from my post here:
                http://forums.sketchucation.com/viewtopic.php?f=180&t=10142&p=266725#p266725

                I'm not here much anymore.

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

                  @alexs said:

                  Sorry I wasn't clear enough... I mean to retrieve whatever groups whos name STARTS with SPECIAL... Is there is an equivalent to C's "StartsWith" in Ruby?

                  You can use RegEx:
                  my_string.test(/^_SPECIAL_/)

                  Or simple text operation:
                  my_string[0,9] == '_SPECIAL_'

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

                  1 Reply Last reply Reply Quote 0
                  • TIGT Offline
                    TIG Moderator
                    last edited by

                    To explain a little more about 'patterns'...
                    /^_SPECIAL_/
                    The ^ at the start says 'starts_with'
                    A $ at the end says 'ends_with'
                    /_SPECIAL_$/
                    Using both is equivalent to 'is_equal_to'.
                    Using neither says 'contains_this_text_anywhere' in the string...

                    Note that the my_string.test(//) method is equivalent to my_string =~ // that you might also come across...

                    The whole pattern thing can be quite complex - e.g.
                    /^[^a-z0-9]/
                    says starts with any character (^ inside the [] reverses the match) that is NOT a lowercase letter or number.
                    With gsub you can use something like it to 'squeeze' everything out of say a file-name so it only uses letters, numbers, '-' or '_' - e.g. as used in OBJ-file naming conventions, thus:
                    filename.gsub!(/[^-_A-Za-z0-8]/, '_')

                    There are several websites that cover this in considerable detail.......

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • A Offline
                      AlexS
                      last edited by

                      Thank you guys, you've been a great help.

                      Now... just one LAST question. I'm trying to use the 'print' and 'puts' function, and I'm trying to put in these characters: < >

                      But when I load Sketchup, it gives me an error. I guess it thinks I'm trying to do something within the string when I put a < or >.

                      How can I fix this?

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

                        Those characters are both Ruby operators and instance method names.

                        Use the String concate method + to add those chars (which must be quoted.)

                        my_string = 'Some special string' + ' > ' + 'the last part of the string.'

                        or you can use replacement within double-quoted string:

                        my_string = "The value: #{biggernum.to_s} > #{smallernum.to_s}"
                        where biggernum and smallernum are numeric references.

                        The 3rd way is to use the Integer instance method chr if you know the character's ordinal within the UTF-8 set.
                        Since decimal 60 is the ordinal for "less than" ...
                        60.chr returns the single char string "<"

                        I'm not here much anymore.

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

                        Advertisement