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

    Removing duplicat Component Instances from an array

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 4 Posters 718 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.
    • K Offline
      ktkoh
      last edited by

      I am having a problem trying to eliminate duplicate Component Instances from an the selection array. If the user selects more than one instance of a component I want to eliminate that from the array before operating on the selection. Suggestions would be appreciated.

      Thanks
      Keith

      1 Reply Last reply Reply Quote 0
      • D Offline
        driven
        last edited by

        ['a','a','b','c'].uniq
        ["a", "b", "c"]
        

        doesn't uniq work?

        learn from the mistakes of others, you may not live long enough to make them all yourself...

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

          Because each instance has its own unique reference you'll need to use their actual definitions to get a reduced list.
          So let's assume iarray is an array of your selected instances [up to you how you compile this...]
          Then use something like this...
          defns = [] iarray.each{|i| defns << i.definition unless defns.include?(i.definition) }
          Now you have an array of all selected definitions named defns !
          Process that...

          TIG

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

            Or use Enumerable#grep ...

            defns = selection.grep(Sketchup::ComponentInstance){|i| i.definition }.uniq

            But, if you've already got the instances filtered then:

            defns = iarray.map{|i| i.definition }.uniq

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • K Offline
              ktkoh
              last edited by

              Thanks for the help I just got to part of the program where this is needed. I visulized 6 to10 lines of code knowing that unique would work directly.

              Keith

              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