Search code examples
rubysketchup

How to create a click event on group object in sketchup?


I have cubes in sketchup.each cube is grouped from four faces like this:

    entities = model.active_entities
    pts = []
    pts[0] = [0, 0, 0]
    pts[1] = [width, 0, 0]
    pts[2] = [width, depth, 0]
    pts[3] = [0, depth, 0]
    # Add the group to the entities in the model
    group = entities.add_group

this creates a box.now how can i create click event on that box so that when clicked on box.it shows Group.name in messagebox?


Solution

  • The SketchUp API doesn't let you do this outside of a custom Tool: http://www.sketchup.com/intl/en/developer/docs/ourdoc/tool

    Edit: Seeing how you are querying the group name in a database then a custom query tool is the way to go. When your user activate your tool you have mouse and key events like onLButtonDown and OnLButtonUp. These event provide the x and y coordinate of where mouse is on the SketchUp viewport.

    Then you can use the PickHelper class to find what entity the user picked: http://www.sketchup.com/intl/en/developer/docs/ourdoc/pickhelper Most of the time you want to simply use the best_picked after doing your pick with do_pick. best_picked will return an entity which you can check if it's a group or component. This entity will be the same as what the Select tool would select.

    The only generic way outside of a custom tool would perhaps to user an SelectionObserver to detect when your group is selected. But I'd discourage against this as it's so easy that this will just interfere with the normal usage of SketchUp.