# Which Observer to use, when a locked group is selected?

**URL:** <https://forums.sketchup.com/t/which-observer-to-use-when-a-locked-group-is-selected/134237>\
**Category:** Developers\
**Created:** [August 23, 2020, 11:58am UTC](https://forums.sketchup.com/t/which-observer-to-use-when-a-locked-group-is-selected/134237 "2020-08-23T11:58:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rvs1977](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/rvs1977/32/15210_2.png) [@rvs1977](https://forums.sketchup.com/u/rvs1977)\
**Post date:** [August 23, 2020, 11:58am UTC](https://forums.sketchup.com/t/which-observer-to-use-when-a-locked-group-is-selected/134237/1 "2020-08-23T11:58:47Z")

</div>

Hi

Do Ruby Sketchup API have an observer which fire, when selecting a locked group?

I have tried with Class: Sketchup::SelectionObserver, which dont seem to work in this case.

thanks in advance…

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [August 23, 2020, 1:08pm UTC](https://forums.sketchup.com/t/which-observer-to-use-when-a-locked-group-is-selected/134237/2 "2020-08-23T13:08:38Z")

</div>

You need to edit the standard SelectionObserver example class, to filter the selection for groups that are locked and then call some action[s] you want…

```auto
class MySelectionObserver < Sketchup::SelectionObserver
  def onSelectionBulkChange(ss)
    locked = ss.grep(Sketchup::Group).find_all{|e| e.locked? }
    if locked[0]
      p locked ### To see the selection
      ### Do your stuff...
    end
  end
end
### Attach the observer to the model's selection.
Sketchup.active_model.selection.add_observer(MySelectionObserver.new)

```

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [August 24, 2020, 1:34pm UTC](https://forums.sketchup.com/t/which-observer-to-use-when-a-locked-group-is-selected/134237/3 "2020-08-24T13:34:30Z")

</div>

I would tweak that slightly to not iterate the whole selection (`grep`) first to get all group and then collect all locked groups into another temporary array when you only care if there is or is not _any_ locked groups.

```rb
locked = ss.any? { |e| e.is_a?(Sketchup::Group) && e.locked? }

```

It’s in particular in observer events, tool events and command procs that it’d good to minimize the amount of work done. Just a general guideline.

---

<div class="post-metadata">

**Author:** ![rvs1977](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/rvs1977/32/15210_2.png) [@rvs1977](https://forums.sketchup.com/u/rvs1977)\
**Post date:** [August 29, 2020, 5:18pm UTC](https://forums.sketchup.com/t/which-observer-to-use-when-a-locked-group-is-selected/134237/4 "2020-08-29T17:18:54Z")

</div>

Thank you both of you.

I have been working around with your solution for some time now.

TIG’s solution seems to work, with a but. And tt\_su’s solution I cant get to work.

In this sequence there seems to be a problem:

1. ruby is loading and instantiate MySelectionObserver
2. ruby generates a face inside a locked group
3. I try to select it, but no observer fires
4. I draw a line/face manually, and now the observer fires
5. I select a locked group, and the observer fire.

The problem is that I have to draw an entity (line/face) manually before i works. Is there a way to work around that problem?

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [August 29, 2020, 10:16pm UTC](https://forums.sketchup.com/t/which-observer-to-use-when-a-locked-group-is-selected/134237/5 "2020-08-29T22:16:23Z")

</div>

> [@tt\_su](#):
>
> It’s in particular in observer events, tool events and command procs that it’d good to minimize the amount of work done. Just a general guideline.

I think in the past we’ve tested and found that [`#grep`](https://ruby-doc.org/core-2.5.5/Enumerable.html#method-i-grep) is very fast compared to repeated calls to [`#is_a?`](https://ruby-doc.org/core-2.5.5/Object.html#method-i-is_a-3F).

So I also favor first filtering with [`#grep`](https://ruby-doc.org/core-2.5.5/Enumerable.html#method-i-grep).

> [@rvs1977](#):
>
> And tt\_su’s solution I cant get to work.

It **wasn’t** a solution, … it was a “nitpick” that uses the wrong reference name.  
Ie, TT’s `"locked"` is not an array of locked objects, it a boolean state of whether there are `"any_locked"`.

He meant that it would be inserted as the first line of TIG’s method (and let’s assume the reference name is `any_locked` instead of `locked`,) and then another line added afterward, thus …

```ruby
return [] unless any_locked

```

* * *

But, … TT’s idea, means that if indeed there are any locked objects, the collection needs to be iterated **again** to select the locked objects.

SO … I must **favor TIG’s solution** as either way you’d end up iterating twice if there are locked objects.

---

<div class="post-metadata">

**Author:** ![rvs1977](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/rvs1977/32/15210_2.png) [@rvs1977](https://forums.sketchup.com/u/rvs1977)\
**Post date:** [August 31, 2020, 6:42pm UTC](https://forums.sketchup.com/t/which-observer-to-use-when-a-locked-group-is-selected/134237/6 "2020-08-31T18:42:42Z")

</div>

Thank you, for clearing that up for me.

Do you have any idea why the selection observer don’t fire when a group has been added by a ruby script, unless you draw eg. a line or a face?

EDIT: It didnt fire, because of a bug in my script. The observer works fine now!

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [August 31, 2020, 8:05pm UTC](https://forums.sketchup.com/t/which-observer-to-use-when-a-locked-group-is-selected/134237/7 "2020-08-31T20:05:56Z")

</div>

> [@rvs1977](#):
>
> Do you have any idea why the selection observer don’t fire when a group has been added by a ruby script, unless you draw eg. a line or a face?

For component and group instances, the callback that gets called is [`onSelectionAdded`](https://ruby.sketchup.com/Sketchup/SelectionObserver.html#onSelectionAdded-instance_method) …

```ruby
  def onSelectionAdded(selection, entity)
    puts "onSelectionAdded: #{entity}"
  end

```
