Changing units with one click

As a design-builder in the US using a European product, I need to be able to easily switch between metric (Format:Decimal, Length: Millimeters, Display Precision: 0 mm) and standard (Format: Architectural, Length: Inches, Display Precision: 1/16").

Is there anyone who could help me adapt the script by @McGordon to fit this purpose. I have tried using Matt’s Toggle Units plugin from this link but it doesn’t work for me (SU 2021 Pro, Mac). Any would be much appreciated and hopefully helpful to others. Thank you in advance. Also, please advise if its best to fork this thread or start a new one for this specific task.

A quick one…
UnitSwitch_D-mm_A-inch.rbz (575 Bytes)

I tested on windows only but should work on Mac too…

It is in a Menu>Tools
image

You can set a shortcuts as above…
image

Code
# By Gordon McIntyre 4th July 2018
# Adapted to Decimal 0 mm <> Architectural 1/16 inch by dezmo 24th March 2021
# UnitSwitch_D-mm_A-inch.rb

module Dezmo
  module UnitSwitchDez
    @@loaded = false unless defined?(@@loaded)
    extend self
    
    def set_d_mm_0
      manager = Sketchup.active_model.options
      provider = manager["UnitsOptions"]
      provider["LengthUnit"] = Length::Millimeter
      provider["LengthFormat"] = Length::Decimal
      provider["LengthPrecision"] = 0
    end #set_mm

    def set_a_inch_116
      manager = Sketchup.active_model.options
      provider = manager["UnitsOptions"]
      provider["LengthUnit"] = Length::Inches
      provider["LengthFormat"] = Length::Architectural
      provider["LengthPrecision"] = 4
    end #set_m

    unless @@loaded
      menu = UI.menu("Tools").add_submenu("Unit Switch D_mm-A_inch")
      menu.add_item("D-mm-0") {set_d_mm_0}
      menu.add_item("A-inch-1/16") {set_a_inch_116}
      @@loaded = true
    end

  end #UnitSwitchDez
end #Dezmo

Fantastic @dezmo ! I have loaded it succesfully in SU Pro 2021 for Mac and it works as a menu item excactly as I had hoped. Nice work. :grinning:
For some reason I am not able to get it to work with keyboard shortcuts despite trying a couple of different ones. It just beeps. :pensive:

This is strange…

I made a new version which will have a toolbar and wrapped as “real” Extension (however still unsigned):
image
The toolbar and menus are also indicating if the relevant unit and precision is selected… :wink:

UnitSwitch_D-mm_A-inch_E.rbz (5.3 KB)

Before install, please remove the previous version and restart SU to get it work…

Thanks @TIG as of Oct 2021 it is still working!

@dezmo I love the simplicity of your solution! Is there any way you’d be open to editing the ruby script to switch between Architectural and Fractional? Aka 12’ 6 1/4" > 150 1/4". Or if I’m able to do that with some guidance in case I want to switch between different formats in the future? Thanks!

Note: replaced by new, Mac compatible version (I hope…)

Dezmo_LUnitswitch_mm-A-F.rbz (9.4 KB)

This is an independent version, you can install it beside the previous version, but perhaps better to remove the old… since the new heve the the functions of previous one.
You will get a toolbar and menu in Tools. (You can assign shortcuts too.)

Does not really tested, but it should work.
No warranties, use at your own risk!

2 Likes

@dezmo This is absolutely fantastic, thank you so much! The shortcuts don’t seem to work but the toolbar does the trick! I so appreciate you helping me out–and so quickly!!! :pray: :hugs:

That is strange, I don’t know how it is in Mac but I just checked the shortcut assignments in two version of SU on Windows and its okay. (The recording is on SU 2017 make):

scutassign

(You need to click on a screen to “refresh” the new unit settings)

1 Like

The reason that shortcuts won’t work for your extension on a Mac is that you have the / character in various things that are taken as menu paths, e.g. 1/16 and A/F appear in several places. On a Mac, the / is the path separator used to look up the menu commands for shortcuts. Those slashes cause the path to lead to a non-existent menu item! Replace them in the code with, e.g., _ and shortcuts will work.

Edit: IMO, since these things are just Strings, this might be considered a bug - or at least a poor design that needs clear documentation!

2 Likes

@aldencreative

Thanks to @slbaumgartner << :beers: , I updated my original post #27 above:

1 Like

I can confirm that this version works on my Mac, including all three ways to launch: menu, toolbar, and shortcuts. One thing to know, which is mentioned in the Ruby API documentation, is that for some longstanding reason, on Mac the validation procs aren’t called “as often as they should be”. This means that the button state on the toolbar isn’t usually correct though the buttons themselves work correctly.

2 Likes

There is another problem, at least on Mac. After a menu change, tool button change, or keyboard shortcut change, the viewport has to be refreshed to see the change of units.

1 Like

Yes, I noticed (and mentioned it above) too.
Will check later, to make refresh happen automatically…

1 Like

Confirmed the shortcuts now work on my Mac, even better! :raised_hands: Thanks @dezmo and @slbaumgartner for the insight. :100: