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?
This is because the Numeric#degrees method converts to radians.
If you want degrees to be displayed then use a Float … 360.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.
Hey Dan! Im ur fan
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
Ok. I’ll stay with it.
Display Float 360.0 and convert to radians internally.
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…
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)
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