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

    Uniq! not working ?

    Scheduled Pinned Locked Moved Developers' Forum
    18 Posts 7 Posters 1.3k Views 7 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
      remus
      last edited by

      im using this:
      ` model = Sketchup.active_model
      entities = model.entities
      selection = model.selection

      faces = []

      face = selection[0]

      face.edges.each{|e| faces.push(e.faces)}

      faces.uniq!

      puts faces`

      to try and create an array of the faces connected to a face, but for whatever reason im getting lots of duplicates of the selected face. I tried writing my own mini version of uniq and that didnt do the trick either, so im at a bit of a loss.

      http://remusrendering.wordpress.com/

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

        You are inserting arrays of faces, so you have an array of arrays of faces. try to flatten the array first.

        faces.flatten!.uniq!

        Sidenote: you might find the Set class faster to keep a collection of unique objects. Array isn't very efficient for large collections - especially the .include? method....

        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
          remus
          last edited by

          yay, thanks. I thought it was going to be something obvious but had a complete mental blank.

          with regards to using sets, its only a small array (max 20-30 elements) so it shouldnt matter.

          http://remusrendering.wordpress.com/

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

            @remus said:

            with regards to using sets, its only a small array (max 20-30 elements) so it shouldnt matter.

            Then Arrays might be faster.

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

            1 Reply Last reply Reply Quote 0
            • J Offline
              Jim
              last edited by

              You can also use Array.concat to append new arrays as elements instead of a single array.

              
              a = []
              a.push([1, 2, 3])
              ==> [[1, 2, 3]]
              
              a = []
              a.concat([1, 2, 3])
              ==> [1, 2, 3]
              
              

              Hi

              1 Reply Last reply Reply Quote 0
              • fredo6F Offline
                fredo6
                last edited by

                @remus said:

                face.edges.each{|e| faces.push(e.faces)}

                The correct code should be something like:
                face.edges.each { |e| faces += e.faces }

                For small numbers of elements, uniq! should be OK. Otherwise, you might consider using a Hash or a Set to avoid redundant elements.

                Fredo

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

                  Fredo, it gives me a syntax error when i try that, bear in mind faces is an array (and i dont think += is defined for arrays.)

                  http://remusrendering.wordpress.com/

                  1 Reply Last reply Reply Quote 0
                  • mitcorbM Offline
                    mitcorb
                    last edited by

                    Excuse me for stepping in here.
                    Out of curiosity, are there a lot of terms like "uniq" in the ruby language? Couldn't this get confusing? Or can you use interchangeably "unique"?

                    I take the slow, deliberate approach in my aimless wandering.

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

                      I dont think you can use "unique" interchangeably but its not too confusing really, if anything i find the differences help you remember the methods.

                      http://remusrendering.wordpress.com/

                      1 Reply Last reply Reply Quote 0
                      • mitcorbM Offline
                        mitcorb
                        last edited by

                        Thanks, Remus.
                        Again, please excuse the intrusion.

                        I take the slow, deliberate approach in my aimless wandering.

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

                          SUp's own .make_unique method only applies to groups and instances of component definitions.
                          Its .unique_name methods applies to definitions and layers name creation.
                          Ruby's own .uniq method returns an array (etc) of unique items ('cloned'), missing out duplicates,,,
                          array1=[1,1,2,3]; array2=array1.uniq >>> array2 returns [1,2,3] but array1 still returns [1,1,2,3]
                          whereas using .uniq! changes the original array to a unique one...
                          array1=[1,1,2,3]; array1.uniq! >>> array1 now returns [1,2,3]

                          All very useful in there own ways...

                          TIG

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

                            @remus said:

                            ...to try and create an array of the faces connected to a face...

                            EdgeUse.partners
                            http://code.google.com/apis/sketchup/docs/ourdoc/edgeuse.html#partners
                            @unknownuser said:

                            The partners method is used to retrieve all of the partner edge uses. This method allows you to find all Faces that use an edge.

                            EDIT: Never-mind. I think the flatten! array means by ThomThom is much simpler. Compact code, let ruby do the hard work.

                            I'm not here much anymore.

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

                              @mitcorb said:

                              Excuse me for stepping in here.
                              Out of curiosity, are there a lot of terms like "uniq" in the ruby language? Couldn't this get confusing?

                              Many of the method names came from Unix and Perl, so they are familiar to people with that background. Well there are some 'shortened nicknames', such as: assoc, eql, attr, dup, eval, stat, sync, const, undef,... etc. sprinkled throughout ruby modules, but No not a majority. A very small minority in fact. But you get used to them.

                              @mitcorb said:

                              Or can you use interchangeably "unique"?

                              You could (if you felt more comfortable,) create singleton methods that provide an alias.

                              
                              arr = [1,2,1,3,4]
                              
                              def arr.unique!
                                self.uniq!
                              end
                              
                              arr.unique!
                               >> [1,2,3,4]
                              
                              arr2=[5,6,7,5]
                              arr2.unique!
                               >> Error; #<NoMethodError; undefined method `unique!' for [5, 6, 7, 5];Array>
                              
                              
                              

                              So it only works for the single instance 'arr'.
                              This avoids globally changing Base classes, which most people get upset about.

                              I'm not here much anymore.

                              1 Reply Last reply Reply Quote 0
                              • fredo6F Offline
                                fredo6
                                last edited by

                                @remus said:

                                Fredo, it gives me a syntax error when i try that, bear in mind faces is an array (and I dont think += is defined for arrays.)

                                It must be something else (do you have a precise description of the error).
                                + is defined for Arrays (it's equivalent to concat), so += is meaningful (I use it frequently).

                                Fredo

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

                                  @unknownuser said:

                                  @remus said:

                                  Fredo, it gives me a syntax error when i try that, bear in mind faces is an array (and I dont think += is defined for arrays.)

                                  It must be something else (do you have a precise description of the error).
                                  + is defined for Arrays (it's equivalent to concat), so += is meaningful (I use it frequently).

                                  Fredo

                                  I get a syntax error as well:

                                  
                                  [1,2,3] += [4,5]
                                  Error; #<SyntaxError; (eval);894; compile error
                                  (eval);894; syntax error
                                  [1,2,3] += [4,5]
                                            ^>
                                  (eval);894
                                  
                                  

                                  Is it not that you have some code that has added this as extra functionality?

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

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

                                    hm... when I defined the variables face and faces - and ran the code - it worked....
                                    face.edges.each { |e| faces += e.faces }

                                    now I'm confused

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

                                    1 Reply Last reply Reply Quote 0
                                    • fredo6F Offline
                                      fredo6
                                      last edited by

                                      Tom,

                                      It only works on variables, because you actually modify the contnet of the left-side array (and I guess [3,5] is considered as a constant).

                                      I tried in the console:

                                      
                                      a = [1,2]
                                      [1, 2]
                                      b = [3, 4]
                                      [3, 4]
                                      a += b
                                      [1, 2, 3, 4]
                                      
                                      [1,2] += b
                                      Error; #<SyntaxError; (eval);130; compile error
                                      (eval);130; syntax error
                                      [1,2] += b
                                              ^>
                                      (eval);130
                                      
                                      

                                      Fred

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

                                        Working now 😳 Must have missed something last time i tried it.

                                        http://remusrendering.wordpress.com/

                                        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