rubylinesketchup

SketchUp Ruby - Display a message box to user if they clicked on a line in SketchUp


I am trying to develop a custom tool which will allow the user to select a line and they will be given the slope distance, azimuth and zenith angle of the line. A small table with all that info will then appear on their screen.

I currently have a little bit of code written which you can see below:

class MySpecialLineTool
    def onLButtonDown(flags, x, y, view)
        puts "onMButtonDoubleClick: flags = #{flags}"
        puts "                          x = #{x}"
        puts "                          y = #{y}"
        puts "                       view = #{view}"
        UI.messagebox("You clicked somewhere in SketchUp.")
    end
end

def self.activate_special_tool
    Sketchup.active_model.select_tool(MySpecialLineTool.new)
end

unless file_loaded?(__FILE__)
    menu = UI.menu('Plugins')
        menu.add_item("Darrian's Special Line Tool") {
        self.activate_special_tool
    }
    file_loaded(__FILE__)
end

This will add a tool under the 'Extensions' tab at the top of SketchUp called 'Darrian's Special Line Tool'. When you click on this tool, then click anywhere in SketchUp, a message box appears which says, "You clicked somewhere in SketchUp." But that's about it, I've hit a brick wall so far.

Below is a picture of what 'should' happen if the whole thing worked as intended. https://i.sstatic.net/DPmdF.png

I'm quite okay with the calculations. It's just being able to get the code to know that a line has been clicked and to retrieve the Delta X, Y and Z of that line in order to be able to calculate the slope distance, azimuth and zenith angle.

I'm not too concerned about starting and ending point. I am aware this will have an affect on the azimuth and zenith angle, but I was already thinking of providing information from both directions to the user.

I can provide more info if required.

Thanks for your help!


Solution

  • The code below might help you with creating class variables of the selected edge's vertex start & end positions of X, Y, Z coordinates. These values might be needed to calculate some math formulas. Let me know if it helped or not.

    I recommend encapsulating your code with Ruby modules for variable or method names not clashing with other SketchUp scripts on the Plugins folder.

    module CodeZealot
      # # #
      module MySpecialLineTool
        # # #
        class Main
          def initialize
            @model = Sketchup.active_model
            @selection = @model.selection
            @edges = @selection.grep(Sketchup::Edge)
            @edge = @edges[0]
          end
    
          def activate
            if @edges.empty?
              msg = 'Select edge before using this tool.'
              UI.messagebox(msg)
              return
            end
    
            # # # XYZ Cordinate values of first edge vertex
            @edge_start_x = @edge.start.position.x
            @edge_start_y = @edge.start.position.y
            @edge_start_z = @edge.start.position.z
            # # # XYZ Cordinate values of other edge vertex
            # These XYZ might be used for MATH formulas maybe?
            @edge_end_x = @edge.end.position.x
            @edge_end_y = @edge.end.position.y
            @edge_end_z = @edge.end.position.z
            # call math calculation method here...
            math_calculation
          end
    
          def math_calculation
            # Use class variables for you Math formulas here.
            # Example...
            result = @edge_start_x + @edge_end_x
            msg = "The result = #{result}"
            UI.messagebox(msg, MB_OK)
          end
        end
        # # #
        def self.activate_special_tool
          Sketchup.active_model.select_tool(Main.new)
        end
    
        unless file_loaded?(__FILE__)
          menu = UI.menu('Plugins')
          menu.add_item("Darrian's Special Line Tool") { self.activate_special_tool }
          file_loaded(__FILE__)
        end
        # # #
      end
    end