Selection error?? with Ruby API
-
Hi.
I was working on 'Selection' with Ruby API and have a problem with it.
entities=Sketchup.active_model.active_entities selection = Sketchup.active_model.selection a = entities.grep(Sketchup;;Group).each{|e|selection.add e if e.name.include?('aaa')}
This is the code selects groups of which name has 'aaa' and it looked like all 'aaa' were selected well.
The problem was the array 'a' contained other groups together not only 'aaa'.
Once I tried to do next work(ex :
a.hidden = true
), other unnecessary groups were worked together.The ruby code editor said
Done. Ruby says: [#Sketchup::Group:0x00000009e7c468, #Sketchup::Group:0x00000009e762c0, #Sketchup::Group:0x00000009e65ab0, #Sketchup::Group:0x00000009e5f868, #Sketchup::Group:0x00000009e3f040, #Sketchup::Group:0x00000009e06920, #Sketchup::Group:0x00000009e05368]in the array, only three things should have been selected(I put 7 objects in space. the name of 3 of 7 groups was 'aaa' and others had name of 'bbb')
How can I exactly select only what I want?
-
I think you were selected already what you want.
But the "a" array is contains all groups since you were grep-ed it. Inside the iteration you have a right selection aray. You have to look for that for example like this:entities=Sketchup.active_model.active_entities selection = Sketchup.active_model.selection a = entities.grep(Sketchup;;Group).each{|e|selection.add e if e.name.include?('aaa')} selection.each{|e| e.hidden=true}
So the "selection" will be your array of selected groups.
(Btw. I don't think you can apply s.hidden=true you heve to iterate through each element... )More simple:
entities=Sketchup.active_model.active_entities entities.grep(Sketchup;;Group).each{|e| e.hidden=true if e.name.include?('aaa')}
-
To make minor adjustments...
Typo in 'ents' referencing fixed, alternative [more flexible?] testing and clearing the selection before adding matches...ents = Sketchup.active_model.active_entities sel = Sketchup.active_model.selection a = ents.grep(Sketchup;;Group).find_all {|e| e.name =~ /aaa/ } sel.clear sel.add(a)
-
Note to add to selecting/deselecting - if you do multiple entities make sure you collected them in arrays and pass in the array to selection.add/remove. If you call .add/.remove on each entity it'll be many times slower.
-
Thank you for all kind replies. All my problems are solved except one :
is there a pattern which determines the order of elements in array? or are they designated randomly in array? If there is a pattern, It will be much easier to control objects
-
You need to use a filtering method. The
each
method always returns the entire collection object, regardless of what you do in the iteration block.ents = Sketchup.active_model.active_entities sel = Sketchup.active_model.selection a = ents.grep(Sketchup;;Group).find_all {|e| e.name.include?('aaa') } sel.add(a)
(Fixed: "
a = ents.grep
" was: "a = entities.grep
"Array and most array-like SketchUp API collections have Ruby's Enumerable library module mixed in. So you can use any of it's methods, like
find()
andfind_all()
, etc. -
No pattern. The various ways that the user can manipulate the selection set makes any order meaningless.
Ie: single pick, single pick + CTRL, single pick + SHIFT, Window Right-to-Left pick, Window Right-to-Left pick + CTRL, Window Right-to-Left pick + SHIFT, Window Left-to-Right pick, Window Left-to-Right pick + CTRL, and Window Left-to-Right pick + SHIFT.
Any of these, followed by any other number of them, in whatever order the user may use them, creates a selection set in which the pick order has no importance.
That said, ...
You question indicates you are perhaps trying to program a workflow that would better be implemented as a Ruby
Tool
, using aSketchup::PickHelper
.Another alternative might be a
Sketchup::SelectionObserver
subclass.
Advertisement