Using Selectionobserver to transfer the persistent_id of selected face

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!

Most all of the SketchUp API’s collection classes have the Core Ruby Enumerable module mixed in.
This library module provides many nifty search, sort and filtering methods.

So generally …

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

With a selection observer …

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

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

1 Like

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

https://forums.sketchup.com/search?q=learning%20Ruby%20%23developers:ruby-api