Drawing a Face on the View with a Ruby Tool

Continuing the discussion from Highlighting a Bounding Box (of a Group):

YES. (But they are not really geometric faces.)

Try using one of the polygon-like GL constants of View#draw (the last 6 in the list.)

The Sketchup::Tool#draw() documentation actually shows an example
of drawing a pink filled quad with a crimson border.

Here is another example … paste into the console and type TestTool.go
then click with LMB (repeatedly) in the modeling area to see a “walking” polygon.

class TestTool

  def self.go
    Sketchup.active_model.select_tool(self.new)
  end

  def activate
    @start = 0
    @side = 10
    set_points()
  end

  def deactivate(view)
    view.drawing_color= Sketchup::Color.new("Black")
  end

  def draw(view)
    view.drawing_color= Sketchup::Color.new("Orange")
    view.draw(GL_POLYGON, @points)
    @start += @side
  end

  def getExtents()
    Sketchup.active_model.bounds.add(@points)
  end
  
  def onLButtonUp(flags, x, y, view)
    set_points()
    view.refresh # calls getExtents() then draw() method
  end

  def set_points()
    @points = [
      Geom::Point3d.new(@start, 0, 0),
      Geom::Point3d.new(@start+@side, 0, 0),
      Geom::Point3d.new(@start+@side, @side, 0),
      Geom::Point3d.new(@start, @side, 0)
    ]
  end
  
end

BTW, the quad walks out of bounds for me and disappears. You can only draw within the model bounds.
Somewhere there is a method that will recalculate the bounds after you add geometry. (Can’t find it right now.)
EDIT: Steve found it (below,) edited example.

I think what you want is the Tool’s getExtents method, which tells SketchUp to modify the model’s bounding box.

1 Like

Very interesting, I may have use for this.

Yes, that is the method I was thinking of.

Updated example above by adding a getExtents method which always
adds the polygon’s points to the draw bounding box.

Also note that the draw methods will not draw shaded polygons. It simply draws a polygon with the exact same color you provide it.

I had some experiment at one point that recreated the shading logic of SU… I think I posted it in a thread where Christina also was working on the same thing.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.