2

guys wonder if someone can help with a Sketchup ruby question. I am trying to differentiate the difference between an internal corner and an external corner to get two different angles as shown in the below image.

Sketchup example

This is the code I am using, which is returning the same angle for all?

puts (sel[0].faces[0].normal.angle_between (sel[0].faces[1].normal)).radians
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
rinse04
  • 47
  • 3
  • Can you post the values of `sel[0].faces[0].normal` and `sel[0].faces[1].normal`? (use `p` for printing with inspection). Also, please add a third face, let's say `sel[0].faces[2].normal`. Better if you can associate each face (index) with the faces of the image you posted. – iGian Sep 25 '19 at 17:05

1 Answers1

0

Below is a code example for verifying if a selected edge is either 'Concave' or 'Convex'.

How to use:

  1. Create .rb file (name it 'concave-convex.rb' for example) and copy & paste the code below. Then place the file in the SketchUp Plugins folder.

  2. Activate the tool inside Sketchup by going to Plugins or Extension Menu -> Selected Edge Concave or Convex?

  3. Done! If you selected a single SketchUp edge that was used by two edges a message will be prompted saying if the edge is concave or convex.

Also, remember to re-name the modules to make them unique so that your ruby script plays nice with other scripts in the Plugins folder.

module DevName
  module ToolName
    class Main
      def activate
        model = Sketchup.active_model
        selection = model.selection
        edges = selection.grep(Sketchup::Edge)
        first_edge = edges[0]

        if first_edge.nil?
          line1 = "Select a 'single edge' used by two faces\n"
          line2 = 'to determine if the edges is concave or convex.'
          msg = line1 + line2
          UI.messagebox(msg, MB_OK)
          Sketchup.active_model.select_tool(nil)
          return
        end

        msg = if concave?(first_edge)
                'Selected Edge is concave'
              else
                'Selected Edge is convex'
              end

        UI.messagebox(msg, MB_OK)
        Sketchup.active_model.select_tool(nil)
      end

      # Return true if edge is concave and returns false if convex
      def concave?(edge)
        faces = edge.faces
        if faces.length != 2
          line1 = "Select a 'single edge' used by two faces\n"
          line2 = 'to determine if the edges is concave or convex.'
          msg = line1 + line2
          UI.messagebox(msg, MB_OK)
          Sketchup.active_model.select_tool(nil)
          return
        end

        fn1 = faces.first.normal
        fn2 = faces.last.normal
        if !fn1.parallel?(fn2)
          vector = fn1 * fn2
          vector.samedirection?(edge.line[1]) == edge.reversed_in?(faces[0])
        end
      end
      # # #
    end
    unless defined?(@loaded)
      UI.menu('Plugins').add_item('Selected Edge Concave or Convex?') do
        Sketchup.active_model.select_tool(DevName::ToolName::Main.new)
      end
      @loaded = true
    end
  end
end