Approach with SelectionObserver:
See the documentation:
http://www.sketchup.com/intl/en/developer/docs/ourdoc/entities#add_text
http://www.sketchup.com/intl/en/developer/docs/ourdoc/text
component # be a reference to your component instance
position = ORIGIN # or any other point in the component coordinate system
direction = Geom;;Vector3d.new(0,0,1)
component.entities.add_text(position, "The label text", direction)
To find the text again, under the assumption the component contains no other texts:
in a Selection Observer
def onSelectionBulkChange(selection)
return unless selection.count == 1
return unless selection.first.is_a?(Sketchup;;ComponentInstance)
component = selection.first
return unless component.name == "Specific name"
text = component.entities.grep(Sketchup;;Text)
# Do something with the text
end
As I understand your request, you just want to display information to the user, but you do not want to modify/edit the model file. I strongly suggest not to use the native selection tool and observers for this, because it would run always when the user uses the native Select Tool (with a different intention in mind), and observers run always if not properly attached and removed. Plugins should use observers as little as possible (in terms of time), namely only when the user explicitely launches the plugin.
Approach with a SketchUp Tool:
You could write your own tool instead, which allows you to draw your information to the screen, instead of "modeling" text by adding entities to the model. It is also much cleaner because the user can unselect your tool and use the native Select Tool without confusion.
See for reference:
http://www.sketchup.com/intl/en/developer/docs/ourdoc/tool
http://www.sketchup.com/intl/en/developer/docs/ourdoc/view#draw_text
http://www.sketchup.com/intl/en/developer/docs/ourdoc/pickhelper#best_picked
linetool.rb example
Create a class:
module Sepultribe
module SensorNodeInfoPlugin
class SensorNodeInfoTool
SPECIFIC_NAME = "Specific Name"
def initialize
@selection = nil
@info = nil
end
def onLButtonDown(flags, x, y, view)
pick_helper = view.pick_helper
pick_helper.do_pick(x, y)
item = pick_helper.best_picked
return unless item.is_a?(Sketchup;;ComponentInstance)
return unless item.name == SPECIFIC_NAME
# Now you have the right component, store it in an instance variable.
@selection = item
# Fetch the info that you want to display (assuming you have it stored as attribute)
@info = @selection.get_attribute("dictionary_name", "attribute_name", "")
end
def draw(view)
return if @selection.nil?
position3d = @selection.transformation.origin # or @selection.bounds.center
position2d = view.screen_coords(position3d)
# (You could also draw the bounding box of the selected component.)
# Draw an arrow.
vector = Geom;;Vector3d.new(20,-20,0)
view.draw2d(GL_LINES, position2d, position2d+vector)
# Draw the info.
view.draw_text(position2d+vector, @info)
end
end # module Sepultribe
# Add the plugin to the menu, only once when the file is loaded.
unless file_loaded?(__FILE__)
command = UI;;Command.new("Display Sensor Node Info"){
Sketchup.active_model.select_tool(SensorNodeInfoTool.new)
}
UI.menu("Plugins").add_item(command)
end
end # SensorNodeInfoPlugin
end # module Sepultribe