Removing duplicat Component Instances from an array
-
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 -
['a','a','b','c'].uniq ["a", "b", "c"]
doesn't uniq work?
-
Because each instance has its own unique reference you'll need to use their actual definitions to get a reduced list.
So let's assumeiarray
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 nameddefns
!
Process that... -
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
-
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
Advertisement