Selection from array

Hi everyone,

i’m having some problems with selections using ruby
i want to use a loop to go trough an array, but somehow it doesn’t want to excecute the code in the loop

this is the code:

if finalselection contains the entities you want to add to the selection, do this:

sel.add finalselection

If you want to replace the current selection you would need to do this:

sel.clear
sel.add finalselection

Hey Neil,

I just tried it, but no luck :/.
could it be that it doesnt work because the array contains dynamic components?

I assume sel references the selection?

sel = Sketchup.active_model.selection
finalselection = [window1,window2,door]
sel.add finalselection

Works for me with three dynamic components.

If you still have problems, please post the error results from the ruby console. There could be other things in your array that are not a SketchUp::Entity. To sort out only items that are entities you could try this:

sel = Sketchup.active_model.selection
finalselection = [window1,window2,door, 'some text']
sel.add finalselection.grep(Sketchup::Entity)

hi neil thanks for helping me

but some how it doesnt do the trick, im not using the standard ruby console, but the ruby editor plugin, since this is part of a bigger plugin i’m writing.
if i run it the ruby editor plugin gives me no errors at all.

if you were interested, here is the complete script:

the selection part is located at the very bottom.

In your initial code you are looping over integer i and trying to add it to the selection. It would be easier to just use # each on your array. Each iterates over the contents versus the index.

If finalselection is an array, then length is a scalar indicating the number of items. The times should return 1. Try leaving out the length and use finalselection.times do |i| instead.

to use .each to put the m in an array, beacaust thats what i initially wrote,

finalslection.each do |i|
sel.add i
end

hey jimhami,

i just tested it, and it didnt do it aswel :confused:

this is what i wrote:
finalselection.times do |i|
sel.add i
end

I’m not sure why any of the approaches outlined above don’t work. If @Neil_Burkholder’s approach doesn’t work for you, then I don’t believe your finalselection is actually an array. Do you have any way to put the array itself to see what’s what?

BTW, I looked at your full code and see a number of potential issues. I’m not sure if placing the require ‘sketchup.rb’ after the first few lines matters, but I’ve always included it as the first line.

Provided finalselection is an Array (or other enumerable) of DrawingElements and sel refers to the active model’s selection, each should work fine. From your snippet I can’t tell whether either of those is true.

I think this is the culprit:

for iv in 1..vertical
    for ih in 1..horizontal  
      #add instances of definition
      model.active_entities.add_instance(defn,[_width ,0, _height])
      finalselection << model.active_entities
      ih+1    
      _width = _width + frame_width.mm # needs to be replaced by component LenX
    end  
   _height = _height+ frame_height.mm  
   _width = 0.mm
end  

The “<<” operator, I believe, is the binary left shift operator. Normally, I would use something like finalselection.push(model.active_entities) to build an array from finalselection = [].

It seems to me I had some problems when I tried to use the Ruby Editor plugin. Maybe I didn’t try it out enough. I’ve had good luck with the Ruby console +. Although it doesn’t have the code saving features for projects.

You could try to see if your array contains the right type of data.

puts finalselection.grep(SketchUp::Entity).length
puts finalselection.grep(SketchUp::Entity).to_a

I stand corrected … Ruby gives this example:

[ 1, 2 ] << “c” << “d” << [ 3, 4 ]
#=> [ 1, 2, “c”, “d”, [ 3, 4 ] ]

Clearly, the “<<” can also be used to add to an array like you want to. However I think you are creating an array of arrays.

It shouldn’t matter. This works.

windows = [window1,window2]
doors = [door1,door2]
openings = [windows,doors]
sel.add openings

However this would only return the door because windows is an array, not a SketchUp::Entity

windows = [window1,window2]
door = door1
openings = [windows,door]
sel.add openings.grep(SketchUp::Entity)

I used that exact example to learn how ruby and array’s work.

Well the main thing about this script is that i want to draw a certain amount of frames (components) into a blanco file, this all works, but the client wants that when the frames are drawn, all must be selected, so i don’t really know how to test if the components are “active” (as slbaumgartner mentionned)

Is there maybe a less confusing approuch to tackle this?

For starters, something like this would save many lines of code (you should use forward slashes):

path = 'C:/Users/sande/Desktop/Bematrix skp plugin/606 ++++ ++++ 30/606 ' +
frame_width.to_f.to_s + ' ' +
frame_height.to_f.to_s +
' 30.skp'

When you’re done creating your instances (it appears you are creating instances from only one definition, which you have a handle to ‘defn’), simply do this.

sel.add defn.instances

EDIT:

Sorry. The .to_f.to_s isn’t necessary. I failed to notice you are using a list for your input.

2 Likes

Hey Neil,

I was thinking to change that big cluster to something similar.
i’ll try this right now and let you know if it works :slight_smile:

Wow it worked like a charm.

Thanks neil! not only is my code way more readable, but it actually works :smiley: