Highlight the selection of a face

Hello everyone,
I would like to highlight the selection of a face, with the mouse click on the face,
while I use “Class: Sketchup::Tool”, you can give me some advice, thank you.

Joseph.

You could use the .selection method to clear and add the face to the current_selection.
OR entirely within your own tool you could use the various draw method to add a temporary highlighted edge and/or a face.
Class: Sketchup::View — SketchUp Ruby API Documentation
Class: Sketchup::View — SketchUp Ruby API Documentation
etc…

Thanks for the answer, I tried to look at the code, but I could not understand much, you can further help more specific… thank you very much.

Joseph.

# Inside your tool class ...

  def onLButtonUp(flags, x, y, view)
    model = view.model
    ph = view.pick_helper
    ph.do_pick(x, y)
    face = ph.picked_face
    model.selection.add(face) unless face.nil?
  end
1 Like

Just a quick snippet.

class Testtool
  def initialize
    @face = nil
    @tr = IDENTITY
  end

  def get_trans(ph, obj)
    ph.count.times{|i|
      if ph.leaf_at(i) == obj
        return ph.transformation_at(i)
      end
    }
    IDENTITY
  end

  def onMouseMove(flags, x, y, view)
    ph_fe = view.pick_helper
    ph_fe.do_pick(x, y, 0)
    face = ph_fe.picked_face
    @face = face ? face : nil
    @tr = get_trans( ph_fe, face ) if face
    view.invalidate
  end

  def onLButtonUp(flags, x, y, view)
    if @face
      @c_face = @face 
      @c_tr = @tr
      puts "#{@c_face} outer loop is highlighted."
    end
    view.invalidate
  end

  def draw(view)
    if @face
      loop = @face.outer_loop.vertices.map{|v|
        v.position.transform( @tr )
      }
      view.drawing_color = [0,0,256, 64] 
      view.draw(GL_POLYGON, loop)
    end
    if @c_face
      c_loop = @c_face.outer_loop.vertices.map{|v|
        v.position.transform( @c_tr )
      }
      view.line_width = 3
      view.drawing_color ="blue" 
      view.draw(GL_LINE_LOOP, c_loop)
    end
  end
end
Sketchup.active_model.select_tool(Testtool.new)
2 Likes

Dan, Can you please explain what is a drawback if I just use unless face instead?

Many thanks to all the participants, to the discussion, for their help…

Joseph.

None. No drawback. (In fact, likely faster as method calls take time.)
Just me being “picky”. (pun intended)

The docs actually do not say what is returned when the pickhelper cannot pick a face. (I assumed it would be nil.)

Thanks Dan, You reassured me.

In this case yes, it says : “Returns nil if there were no faces picked.
However it is not in the right place (in Returns field), but above the example :confused:

1 Like

I stand corrected. :slight_smile:

1 Like

6 posts were split to a new topic: Transforming picked face