[Code] Determining Angle Between View and Model's Axes

Continuing the discussion from Determining Angle Between View and Model's Axes:

Here’s a sample:
Axes_Cam_Angles.rb (1.1 KB)

Look’s like this for those who don’t wish to d/l:

# encoding: UTF-8
module Sample; end
module Sample::ShowAngles

  TITLE = "Angles ..."
  
  ITEM  = "Axes <-> Camera->Origin angles..."

  FMT = "\n Angle between cam eye -> ORIGIN vector, and ...\n\n"<<
        " X_AXIS:  %10.8f rad,  %12.8f deg\n\n"<<
        " Y_AXIS:  %10.8f rad,  %12.8f deg\n\n"<<
        " Z_AXIS:  %10.8f rad,  %12.8f deg\n\n"

  def show_dialog()
    mod = Sketchup.active_model
    cam = mod.active_view.camera
    vec = cam.eye.vector_to(ORIGIN)
    a = []
    a.x = vec.angle_between(X_AXIS)
    a.y = vec.angle_between(Y_AXIS)
    a.z = vec.angle_between(Z_AXIS)
    msg = format( FMT,
      a.x, a.x.radians,
      a.y, a.y.radians,
      a.z, a.z.radians 
    )
  rescue => e
    UI.messagebox( e.message )
  else
    UI.messagebox( msg, MB_MULTILINE, TITLE )
  end # show_dialog()
  
  module_function(:show_dialog)
  
  @cmd = UI::Command.new('Angles') { self::show_dialog() }
  @cmd.menu_text= ITEM
  
  UI.add_context_menu_handler do |popup|
    popup.add_item @cmd
  end # menu handler

end # module Sample::ShowAngles
1 Like

I changed the format string a bit to be sure to show the sign, but the values always seem to be positve.

  FMT = "\n Angle between cam eye -> ORIGIN vector, and ...\n\n"<<
        " X_AXIS:  %-+10.8f rad,  %-+12.8f deg\n\n"<<
        " Y_AXIS:  %-+10.8f rad,  %-+12.8f deg\n\n"<<
        " Z_AXIS:  %-+10.8f rad,  %-+12.8f deg\n\n"
1 Like

Thanks Dan, that’s an awesome introduction to Ruby scripts for SketchUp. I dropped it in my plugins folder and it appears to be loading because when I type Sample::ShowAngles::FMT in the Ruby console it echoes back the format, but otherwise how do I get it to work? From ITEM it looks like it should be a menu option (under Camera perhaps?) but I don’t actually see anything under any of the menus. This is after restarting SketchUp.

Edit: Doh, I see it now under a Right Click, I was looking in the drop down menus. Awesome, thanks!

Yes you can add an item to a dropdown menu, by adding a line after the @cmd instance variable is created and it’s text is set to the ITEM string constant. For example, adding it to the bottom of the “Camera” menu:

  UI.menu("Camera").add_item @cmd

Command instance references can also be used to make toolbar buttons.
Check out the API docs for the UI::Toolbar class. (But because they have extra image files for buttons, we usually package things up in a plugin sub-directory, and have a SketchupExtension registration script in the “Plugins” directory that loads the script in the subdir.)

Note that this gives the angle between the view and global model axes - not the drawing axes which the user might have set. Unfortunately the drawing axes isn’t exposed to the Ruby API yet.

Very cool, thanks! I have some customization to get working on.