Error on API documentation view.draw2d

Hi all,

I try to display an jpg image in Sketchup windows

When I copy and past the following code from the documentation I got an error:

module Example
  class MyTool

    def activate
      view = Sketchup.active_model.active_view
      image_rep = view.model.materials.current.texture.image_rep
      @texture_id = view.load_texture(image_rep)
    end

    def deactivate(view)
      view.release_texture(@texture_id)
    end

    def draw(view)
      points = [ [0, 0, 0], [9, 0, 0], [9, 9, 0], [0, 9, 0] ]
      uvs = [ [0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0] ]
      view.draw(points, texture_id: @texture_id, uvs: uvs)
    end

  end
end
Sketchup.active_model.select_tool(Example::MyTool.new)

=>

Error: #<TypeError: no implicit conversion of Array into Integer>
<main>:16:in `draw'
<main>:16:in `draw'

So I had the the openglenum and its start working, but I don’t know how to map texture. Wich openglnum should I use ? what shoud be the UV

module Example
  class MyTool

    def activate
      view = Sketchup.active_model.active_view
      image_rep = view.model.materials.current.texture.image_rep
      puts "image_rep#{image_rep}<-"

      @texture_id = view.load_texture(image_rep)
    end

    def deactivate(view)
      view.release_texture(@texture_id)
    end

    def draw(view)
    a = 100
      points = [ [a, a, 0], [a*2, a, 0], [a*2, a*2, 0], [a, a*2, 0] ]
      uvs = [ [0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0] ]
      #view.draw(GL_POLYGON,points, texture_id: @texture_id, uvs: uvs)
      view.draw2d(GL_QUADS,points, texture_id: @texture_id, uvs: uvs)
      #GL_POLYGON
      #GL_QUADS
      #GL_QUAD_STRIP
    end

  end
end
Sketchup.active_model.select_tool(Example::MyTool.new)

Thanks for your help

The erroneous documentation code example has already been reported in the official issue tracker …

There is also a code example error as the examples show the wrong options hash key for the texture id …

https://github.com/SketchUp/api-issue-tracker/issues/446

Okay, after testing the limited example in the docs (Sketchup::View#load_texture and Sketchup::View#release_texture) is not a full working example !

Here is an example that works (and uses better practice) …

module Example
  class MyTool

    def activate
      view = Sketchup.active_model.active_view
      @points = [ [0, 0, 0], [9, 0, 0], [9, 9, 0], [0, 9, 0] ]
      @uvs = [ [0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0] ]
      @extents = Sketchup.active_model.bounds.add(@points)
      @color = Sketchup::Color.new(0, 0, 255)
      matl = view.model.materials.current
      # The current material might be nil just after model load.
      # Also the current selected material may not have a texture:
      if matl && matl.texture
        image_rep = matl.texture.image_rep
        @texture_id = view.load_texture(image_rep)
      else
        @texture_id = nil
      end
    rescue => err
      puts err.inspect
    ensure
      view.invalidate # redraw the view immediately
    end

    def deactivate(view)
      view.release_texture(@texture_id) if @texture_id
      view.invalidate # redraw the view when done
    end

    def draw(view)
      view.line_stipple = '' # Solid line
      view.drawing_color = @color
      if @texture_id
        view.draw(GL_QUADS, @points, texture: @texture_id, uvs: @uvs)
      else
        view.draw(GL_QUADS, @points)
      end
    end

    def getExtents
      @extents
    end

    def onMouseMove(flags, x, y, view)
      view.invalidate # redraw the view on mousemove
    end

  end
end

Sketchup.active_model.select_tool(Example::MyTool.new)
1 Like

Thanks you @DanRathbun, your the best !
I just reverse the texture uv and it works ! perfectly !