[Q] on mouse over component - boundingbox lightup
-
Greetings out there...
I'm trying to make a snippet where a component's bounding box lights up, when the mouse is pointing at the object. I have looked around for a while at different classes, for instance the oberserver class and the UI class - pickhelper, but nothing seems to do the job.
Can anyone tell me in which direction to look, or even better show me a rubyexample I can look in?
thx in advance
-Rasmus
-
The way I would do it is to make a tool. Then use the pickhelper class to track what the mouse is hovering over. THen anytime it hovers over a component, add the component to the selection set. When the cursor is no longer over the component, clear the selection set.
This obviously has the limitation of clearing the selection set, which might not be idea for your plugin's needs. So instead of actually adding it to the selection set, instead you could use the draw method to highlight the bounding box.
This is a fun exercise. I'll see if I can do a quick sample script.
Chris
-
Here is a simple example of making a tool that automatically adds a component instance to the selection set and clears the selection set if the cursor is not hovering over a component. Biggest downfalls of this code probably are that everytime the mouse moves, it either re-adds a component to the selection - even if it already in it! Or it clears the selection set, even if it is already clear. Ideally you would do something to track from mouse movement to mouse movement if the pickhelper has changed (instead of just adding and clearing the selection blindly).
Also as I noted before, this clears the users selection set when the tool is activated, and it clears it repeatedly throughoutt he script, which generally is not a good idea. But in some scripts, it is exactly what you want.
` class Clf_highlight_bb
def activate
Sketchup.active_model.selection.clear
@@ph= Sketchup.active_model.active_view.pick_helper
end #activatedef onMouseMove(flags, x, y, view)
@@ph.do_pick x,y
best = @@ph.best_picked
if best.is_a? Sketchup::ComponentInstance
Sketchup.active_model.selection.add best
else
Sketchup.active_model.selection.clear
end
end #onMouseMoveend #class
Sketchup.active_model.select_tool( Clf_highlight_bb.new )`
-
And here's a quick example of how to do it using the draw method in the tool. This does not mess with the selectio set at all. So if you want the user to be able to actualy select the object, you will need to add an onLClick method that adds the highlighted object to the selection set.
Again, this code is not very optimized. It is calling the draw method (by calling
view.invalidate!
) each pixel that the mouse moves. You would be wise to make sure it only calls the draw method when it needs to.` class Clf_highlight_bb
def activate
Sketchup.active_model.selection.clear
@@ph= Sketchup.active_model.active_view.pick_helper
@@best = []
end #activatedef onMouseMove(flags, x, y, view)
@@ph.do_pick x,y
@@best = @@ph.best_picked
view.invalidate
end #onMouseMovedef draw(view)
if @@best
view.drawing_color = "red"
bb = @@best.bounds
pts = [bb.corner(0), bb.corner(1), bb.corner(2), bb.corner(3), bb.corner(4), bb.corner(5), bb.corner(6), bb.corner(7), bb.corner(0), bb.corner(2), bb.corner(1), bb.corner(3), bb.corner(4), bb.corner(6), bb.corner(5), bb.corner(7), bb.corner(0), bb.corner(4), bb.corner(1), bb.corner(5), bb.corner(2), bb.corner(6), bb.corner(3), bb.corner(7)]
view.draw GL_LINES, pts
end
endend #class
Sketchup.active_model.select_tool( Clf_highlight_bb.new )`
Hope these examples help figure it out,
Chris
-
Hi Chris
Thank you so much!
This is exactly what I was looking for. Once again this forum has helped me out!
I'm trying to make a wall tool, and have an idea that this will be very usefull.-Rasmus
Advertisement