Unit UI issue?

Hello yall :v:

I was updating an extension code but then realized a UI and/or UX problem:
Units aren’t being displaced nicely on input box.

It does correct for mm but not for degrees:

Description:
→ 5.mm displays as 5.mm
→ 360.degrees displays as 6.28 (rad)

Im using SU18 on win10

Is it an old now fixed issue?
Am i correctly following the guideline?
Is there a workaround to make it work (at least su16 and above)
Any advice or similar issues on this matter to be aware of?

Thanks!

No. Post code not images.

This is because the Numeric#degrees method converts to radians.

If you want degrees to be displayed then use a Float360.0
When you get the value back from the user, convert to radians when API methods require radians.
some_method(@Total_Rotation.degrees)

UI::inputbox displays Length strings in the user’s model units.
Afterward it runs the strings through String#to_l to convert the input back into a Length object.

Read this …

Hey Dan! Im ur fan :smiley:
U have no idea how you, tig, thom (others also) helped me learning SUapi on sketchucation forums.

Thanks and Im sorry for the img

So lengths and angles units are treated in diferent ways…

For lengths:

  • Internally, “.mm .cm .m .inch” methods converts to internal unit (inches)
  • And “.to_l” deals with ins and outs of inputbox

For angles:

  • Internally, “.degrees” converts to internal unit (rad) but “.radians” converts back (deg)
  • But theres no in-out handling of inputbox for it

I see its more practical for development, and its not a big point anyways… the concern is from a user perspective view witch is limited to use the stated unit (angles only).
Even tought it may be very very unusual for a user to preffer using radians :grin:

Ok. I’ll stay with it.
Display Float 360.0 and convert to radians internally.

Thx again :v:

1 Like

Start your dialog with the angle data defaulting to a float - e.g. 0.0 or 360.0
Then in your code read that float and apply .radians to it, so that the API uses it as an angle in radians in its processes.
Later, if you rerun the tool and want that current angle to be used in the dialog, just save the previous input as a @float so you can then use it directly - it’ll appear as a user-friendly float in the dialog…
So input and display [and save] as a @float, convert to radians for calculations…

1 Like

Thx Tig! :blush:

That should be the best balance.
(Less work and correct display)

Think u just inverted here “.radians” converts to degrees; “.degrees” converts to radians.
Its tricky to remember, I got confused most of time.

But I got it, thx :v:

Yes we have long asked for method aliases #to_radians for #degrees and #to_degrees for #radians. They’ve not been added to the API.

You can write your own method wrappers within your own module(s) …

def deg_to_rad(num)
  num.degrees
end

def rad_to_deg(num)
  num.radians
end
1 Like

Use Sketchup.format_angle to display an angle to the user. It honors the model angular precision and the system decimal separator.

Sadly there is no corresponding parse_angle method, so in this specific case you have to manually handle the decimal sign when parsing.

2 Likes

Ok, I merged both 3 pro answers and it got pretty well.
just use “to_angle(input)” method after getting the string from inputbox.
As I see, it works just like .to_l but for angles.

Description:
if input ends with radian unit indicator it leaves as it is, otherwise it converts to radians using .degrees method.

Code:

def to_angle(input) #Converts anything to radians
@String = input.to_s.tr(",",".")

	if @String.end_with?("radians") || @String.end_with?("rad")
		@Angle = @String.to_f #Leave it as is and will work as radians
	else
		@Angle = @String.to_f.degrees #converts from deg to rad
	end

return @Angle #Float radians output
end

#-----------------------------------------------------------

@prompts = ["Length value: ", "Angle value: "]
@defaults = [5.0.mm, "360.0.degrees"]
@list = ["", ""]
@input = UI.inputbox(@prompts, @defaults, @list, "UI unit test")

if @input then @Length, @Angle = @input; end

@Angle = to_angle(@Angle) #Converts anything to radians

#Internal API processes using radians

print ("Angle (internal unit): " + @Angle.to_s)

@msg = "Input values" + "\n\n"
@msg += "Length: " + @Length.to_s + "\n"
@msg += "Angle (model unit): " + Sketchup.format_angle(@Angle).to_s + "degrees"
UI.messagebox(@msg, type = MB_OK)

Thanks everyone :v:

As a user you wouldn’t type “17.radians” but rather “17 radians”. The “.” notation is because it is a method call in a programming language. That said, you don’t really need to have radian support in the user input. Radians are useful in math (often in expressions involving Pi) but in architecture or design you don’t say “the angle of this wall is half Pi radians”, you simply call it 90 degrees. On the other hand, you do need to support “,” as decimal separator for continental Europe, Quebec and a number of other places.

Here’s the method I’ve been using.

      # Parse angle from text.
      #
      # @param text [String]
      #
      # @return [Numeric] Angle in radians.
      def self.parse_angle(text)
        # In continental Europe, South America and other places,
        # a comma is used as decimal sign.
        text.tr(",", ".").to_f.degrees
      end
3 Likes

Thanks again

I’ve removed the dots on sufix and added ur parse solution.
How could I forget the comma problem, I use it everyday :smile:

Agreed its not common at all…
Its kind of a “just in case”

Got 4 solutions out from here :100: