# Using Selectionobserver to transfer the persistent\_id of selected face

**URL:** <https://forums.sketchup.com/t/using-selectionobserver-to-transfer-the-persistent-id-of-selected-face/180810>\
**Category:** Ruby API\
**Created:** [November 10, 2021, 7:55am UTC](https://forums.sketchup.com/t/using-selectionobserver-to-transfer-the-persistent-id-of-selected-face/180810 "2021-11-10T07:55:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![li\_jian](https://avatars.discourse-cdn.com/v4/letter/l/ecccb3/32.png) [@li\_jian](https://forums.sketchup.com/u/li_jian)\
**Post date:** [November 10, 2021, 7:55am UTC](https://forums.sketchup.com/t/using-selectionobserver-to-transfer-the-persistent-id-of-selected-face/180810/1 "2021-11-10T07:55:05Z")

</div>

Hello!  
I want to get the persistent\_id of a selected face,how to use Selectionobserver to loop through and return the persistent\_id of a face when it is selected with a mouse click.  
Thanks!

---

<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:** [November 10, 2021, 6:52pm UTC](https://forums.sketchup.com/t/using-selectionobserver-to-transfer-the-persistent-id-of-selected-face/180810/2 "2021-11-10T18:52:29Z")

</div>

Most all of the SketchUp API’s collection classes have the Core Ruby [`Enumerable`](https://ruby-doc.org/core-2.7.2/Enumerable.html) module mixed in.  
This library module provides many nifty search, sort and filtering methods.

So generally …

```ruby
model = Sketchup.active_model
sel = model.selection
face1 = sel.grep(Sketchup::Face)[0]
if face1
   id = face1.persistent_id
end

```

With a selection observer …

```ruby
module SomeAuthor
  module SomeExtension

    extend self

    class SelectionSpy
      def self.attach
        Sketchup.active_model.selection.add_observer(self.new)
      end
      def onSelectionBulkChange(selection)
        return if selection.empty?
        face = selection.grep(Sketchup::Face)[0]
        SomeAuthor::SomeExtension.store_id(face.persistent_id)
      end
    end

    def store_id(id)
      # Do something with face id
    end

    if !defined?(@loaded)
      SelectionSpy.attach
      @loaded = true
    end

  end
end

```

---

<div class="post-metadata">

**Author:** ![li\_jian](https://avatars.discourse-cdn.com/v4/letter/l/ecccb3/32.png) [@li\_jian](https://forums.sketchup.com/u/li_jian)\
**Post date:** [November 10, 2021, 11:58pm UTC](https://forums.sketchup.com/t/using-selectionobserver-to-transfer-the-persistent-id-of-selected-face/180810/3 "2021-11-10T23:58:46Z")

</div>

Thank you for solving so many problems for me, I would like to ask you how to learn ruby API systematically.

---

<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:** [November 11, 2021, 6:58pm UTC](https://forums.sketchup.com/t/using-selectionobserver-to-transfer-the-persistent-id-of-selected-face/180810/4 "2021-11-11T18:58:49Z")

</div>

> [@li\_jian](#):
>
> I would like to ask you how to learn ruby API systematically.

This is off-topic and has been covered in numerous other topics …

[https://forums.sketchup.com/search?q=learning%20Ruby%20%23developers%3Aruby-api](https://forums.sketchup.com/search?q=learning%20Ruby%20%23developers%3Aruby-api)
