# Copyright 2005, @Last Software, Inc. # This software is provided as an example of using the Ruby interface # to SketchUp. # Permission to use, copy, modify, and distribute this software for # any purpose and without fee is hereby granted, provided that the above # copyright notice appear in all copies. # THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. #----------------------------------------------------------------------------- require 'sketchup.rb' # Th PickHelper class has methods that are generally used within the context of a Tool # that you create by implementing the Tool interface. Basically, you need screen # coordinates when doing a pick and those are passed to the various Tool methods, such # onLButtonDown. class PickHelperToolTests # Called when you select the tool def activate puts "activate called" @status = 0 depth = 100 width = 100 model = Sketchup.active_model 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 face to the entities in the model face = entities.add_face pts face.pushpull 100 end # Called when I select another tool or quit SketchUp def deactivate(view) puts "deactivate called" end def onLButtonDown(flags, x, y, view) case @status # First left click we test PickHelper.do_pick. Click # on one of the corners of the box and you will get 6 or 7 items # picked. when 0 ph = view.pick_helper num = ph.do_pick x,y if (num) UI.messagebox num else UI.messagebox "Failure" end @status +=1 when 1 ph = view.pick_helper ph.do_pick x,y picked = ph.all_picked if (picked) UI.messagebox picked else UI.messagebox "Failure" end @status +=1 when 2 ph = view.pick_helper ph.do_pick x,y best = ph.best_picked if (best) UI.messagebox best else UI.messagebox "Failure" end @status +=1 when 3 ph = view.pick_helper ph.do_pick x,y num = ph.count if (num) UI.messagebox num else UI.messagebox "Failure" end @status +=1 when 4 ph = view.pick_helper ph.do_pick x,y # Returns a depth of 1 for any entity outside of a # component. Unlike InputPoint.depth which returns 0. I think this # is broken. depth = ph.depth_at 0 if (depth) UI.messagebox depth else UI.messagebox "Failure" end @status +=1 when 5 ph = view.pick_helper ph.do_pick x,y # Returns the element at a specific spot in the list of # elements picked. element = ph.element_at 0 if (element) UI.messagebox element else UI.messagebox "Failure" end @status +=1 when 6 ph = view.pick_helper phnew = ph.init x, y if (phnew) UI.messagebox phnew else UI.messagebox "Failure" end @status +=1 # The leaf is the last element in the list. Not sure how # this works because we pass it an index and it returns a leaf # based on that index. when 7 ph = view.pick_helper # Returns nil phnew = ph.leaf_at 1 if (phnew) UI.messagebox phnew else UI.messagebox "Failure" end @status +=1 when 8 ph = view.pick_helper # Should return the pick path. Must have a depth of 1 or greater. According # to depth_at, an index of 1 returns 1 so the depth should be fine. # Returns nil path = ph.path_at 1 if (path) UI.messagebox path else UI.messagebox "Failure" end @status +=1 when 9 point1 = Geom::Point3d.new 0,0,0 point2 = Geom::Point3d.new 10,0,0 ph = view.pick_helper # Should take an array of points, a series of points, screen coordinates (x,y), # and an apature. Only the points are required. Code says if none of the points # were picked, then try picking one of the segments connecting # the points. If a segment is picked, return a negative number which is the # negative of the index of the end of the segement. # Returns -1 or 0 path = ph.pick_segment point1, point2 if (path) UI.messagebox path else UI.messagebox "Failure" end @status +=1 when 10 ph = view.pick_helper ph.do_pick x,y best = ph.picked_edge if (best) UI.messagebox best else UI.messagebox "Failure" end @status +=1 when 11 ph = view.pick_helper ph.do_pick x,y best = ph.picked_element if (best) UI.messagebox best else UI.messagebox "Failure" end @status +=1 when 12 ph = view.pick_helper ph.do_pick x,y best = ph.picked_face if (best) UI.messagebox best else UI.messagebox "Failure" end @status +=1 when 13 point = Geom::Point3d.new 0,0,0 ph = view.pick_helper ph.do_pick x,y # returns nil status = ph.test_point point if (status) UI.messagebox status else UI.messagebox "Failure" end @status +=1 when 14 ph = view.pick_helper ph.do_pick x,y t = ph.transformation_at 1 if (t) UI.messagebox t else UI.messagebox "Failure" end @status +=1 when 15 ph = view.pick_helper ph.do_pick x,y v = ph.view if (v) UI.messagebox v else UI.messagebox "Failure" end @status +=1 when 16 ph = view.pick_helper ph.do_pick x,y t = ph.transformation_at 1 if (t) UI.messagebox t else UI.messagebox "Failure" end @status +=1 end end end # end of class PickHelperToolTests # Add a menu choice for viewtooltests if( not file_loaded?("pickhelpertooltests.rb") ) plugins_menu = UI.menu("Plugins") plugins_menu.add_item("PickHelperToolTests") { Sketchup.active_model.select_tool PickHelperToolTests.new } end