Understanding how overlay works

I copied this example from Ruby API

class ExampleOverlay < Sketchup::Overlay

  def initialize
    super('example_inc.my_overlay', 'Example Overlay')
  end

  def draw(view)
    # Draw a square.
    points = [
      Geom::Point3d.new(0, 0, 0),
      Geom::Point3d.new(9, 0, 0),
      Geom::Point3d.new(9, 9, 0),
      Geom::Point3d.new(0, 9, 0)
    ]
    # Fill
    view.drawing_color = Sketchup::Color.new(255, 128, 128)
    view.draw(GL_QUADS, points)
    # Outline
    view.line_stipple = '' # Solid line
    view.drawing_color = Sketchup::Color.new(64, 0, 0)
    view.draw(GL_LINE_LOOP, points)
  end

end

If I run this code

firstOverlay = ExampleOverlay.new
firstOverlay.draw(Sketchup.active_model.active_view)

What it’s supposed to happen? Should I see something in my screen?

You need to add it to the model using [OverlaysManager]

OverlaysManager #add-instance_method.

firstOverlay = ExampleOverlay.new
Sketchup.active_model.overlays.add(firstOverlay)

Then the user (you) can activate it via Overlays Tray.
(or by Overlay #enabled= -instance_method )

(You do not need to call .draw method, it is called by SketchUp whenever the view is refreshed to allow the tool/overlay to do its own drawing. You can force to call by e.g. view.invalidate )

1 Like

Reread the Overlay class introduction. It shows how to add an overlay to a model.

But it also lacks the statement to enable it. Overlays will be disabled by default.

# Using an observer to create a new overlay per model.
class ExampleAppObserver < Sketchup::AppObserver

  def expectsStartupModelNotifications
    true
  end

  def register_overlay(model)
    overlay = ExampleOverlay.new
    model.overlays.add(overlay)
    overlay.enabled= true
  end
  alias_method :onNewModel, :register_overlay
  alias_method :onOpenModel, :register_overlay

end

observer = ExampleAppObserver.new
Sketchup.add_observer(observer)
2 Likes

It’s worth noting that the Overlay#enabled= method is exactly equivalent to the checkbox in the SketchUp Overlays panel (checked => enabled = true, unchecked => enabled = false). One might want to leave the overlay disabled until the user clicks that box so that it doesn’t immediately start drawing onto every model, which could be annoying.

2 Likes

Thanks @dezmo and @DanRathbun.
I will consider your recommendations too @slbaumgartner