draw_text - not displaying text?


#1

Hello all,

I am trying to do some programmatic rendering of text. Right now, I try to render text pretty simply in the interactive Ruby box:

view = Sketchup.active_model.active_view
point16 = Geom::Point3d.new 0,0,0
status = view.draw_text point16, "This is a test"

(Note: this is taken directly from the API docs)

As far as I can see, this does nothing. If it renders text, it’s being rendered invisibly. I poked around on the web and found:

http://sketchucation.com/forums/viewtopic.php?f=180&t=44650

Which suggested setting either the view’s drawing model or the model’s rendering color itself:

model = Sketchup.active_model
model.rendering_options['ForegroundColor'] = Sketchup::Color.new(255, 0, 0)

or:

view.drawing_color= Sketchup::Color.new(255, 0, 0)

Subsequently trying to draw_text after each of these operations has resulted in no text being drawn to the screen. What am I doing wrong?


#2

Notice that what you draw on the screen lives only until the screen is redrawn with the next frame (camera or anything else changed). That’s why only tools have control over drawing to the viewport (mentioned in the intro of View). A tool is like the line tool and the push pull tool: only one of them can be active.

A minimal tool to draw on the viewport:

class MyTool
  def draw(view) # SketchUp calls the method when a redraw is required, and gives it the view to draw to.
    point16 = Geom::Point3d.new(0, 0, 0)
    status = view.draw_text(point16, "This is a test")
  rescue Exception => e # Tool methods are invoked dispatched from the Ruby Console / Ruby loading, so exceptions wouldn't go to the console but stay silent and unnoticed.
    puts e.message
    puts e.backtrace
  end
end

Learn more about tools here:

API Documentation: Tool class
Official LineTool example
Forum example for a tool

The SketchUcation topic mentions that the text color is a model setting (currently). It does however not suggest to modify the model setting, because that would not only upset your plugin users, but also add infinite operations to the undo stack, and trigger a viewport refresh, causing an infinite loop.


#3

Thanks for the very helpful advice. Something I’m noticing:

As soon as I change tools, e.g. to the pan tool, the text disappears. So, it sounds like this only gets drawn if it’s the active tool. Is this correct? Is there any way to set a tool as a default tool?


#4

That’s correct, there is no way to draw unless you have the tool that is active.

You could make your tool temporarily the selected tool as long as you need:

model.tools.push_tool(MyTool.new)

later (best from within one of the tool’s methods, to ensure you only pop if the user hasn’t selected another tool):

model.tools.pop_tool

… and the user’s previously selected tool is restored.


#5

Okay - so if I’m understanding correctly, I can activate and deactive tools no problem through an operation, but there’s no way to have the system automatically turn on the tool when the user is in selection mode, right?

If so that’s no problem, I can just have them select a “display text” tool, but I just want to be sure.


#6

“Selection mode” is the built in selection tool. Modifying the behavior of other tools (especially of built-in tools) is not possible and probably not desired.

I have no idea what you want to achieve, but is it that you want to design a workflow that includes several steps of which the first is selecting entities, and once they are selected display properties? You can not control the built-in selection tool (eg. get a callback once entities have been selected), but you rebuild the selection tool step into your own tool (using a pickhelper) – if you really need to.


#7

If you just want to display text on the viewport and let the user change the camera beneath it, you need to use a view-based model note.

But it’s features are very basic.

See Sketchup::Model#add_note()

The objects are “special” instances of Sketchup::Text that have no leader nor arrow, and are anchored to the model view.

More info here at:
Is there a screen text method?