module Testing @@loaded ||= false class DragTool def activate @model = Sketchup.active_model @choice=Sketchup.active_model.selection[0]; if choice @cancelled = @placed = false @model.start_operation("DragInsert Component") else @model.select_tool(nil) end end def deactivate(view) if @placed && !@cancelled @model.commit_operation else @model.abort_operation end end def onCancel(reason, view) puts "Cancel reason: #{reason}" @cancelled = true @model.select_tool(nil) end def onLButtonDown( flags, x, y, view ) ip = view.inputpoint( x,y ) point = ip.position @choice.move!( Geom::Transformation::translation(point) ) view.invalidate @placed = true @model.select_tool(nil) end def onMouseMove( flags, x, y, view ) @ip = view.inputpoint( x,y ) @point = @ip.position @choice.move!( Geom::Transformation::translation(@point) ) view.invalidate plane = [Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,1)] projected_point = @point.project_to_plane(plane) #puts projected_point end def draw(view) draw_preview(view) #@ip.draw(view) if @ip.display? end def draw_preview(view) ipface = @ip.face if ipface box_points = ipface.vertices.map { |v| v.position } view.drawing_color = Sketchup::Color.new(0, 255, 255, 50) view.draw(GL_POLYGON, box_points) puts box_points puts 1 choicepoint = Geom::Point3d.new(@choice.transformation.origin) puts choicepoint boundingbox = ipface.bounds choicefacepoint = boundingbox.center #view.set_color_from_line(box_points[0], choicepoint) view.drawing_color = Sketchup::Color.new(0, 0, 0, 50) view.line_width = 2 view.line_stipple = '123' points = [Geom::Point3d.new(box_points[2]), Geom::Point3d.new(box_points[3])] view.draw(GL_LINES, points) distance = choicepoint.distance_to_line(points) puts distance end end end if !@@loaded UI.add_context_menu_handler {|popup| popup.add_item("DragInsert Component Tool") { Sketchup.active_model.select_tool(DragTool::new) } } @@loaded = true end end