Setting a certain unit as standard
-
I created a small plugin that calculates the reinforcement for concrete beams and columns. I gave it to my friends to show off, but unfortunately, he was using inches and I was using mm and it was quiet the embarrassing moment . Is there a way to set the unit as mm in the start of the program. Is this permitted by sketchup? or is there is way to not convert the units every time?
-
SU always uses inches internally. The Numeric and Length class has methods to deal with this. You must always work around inches.
http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/numeric.html
-
@meeta said:
Is there a way to set the unit as mm in the start of the program.
That's a darn good question. Anyone?
-
so says we ask the user to input this
'number'
this is in inches automatically?
and say i ask the user to input
'number' and then put 'number'.to_mm
does that mean it is converting inches in to mm?
-
This won't set units, but will tell you what is set:
def units x = '120'.to_l.inspect.to_i if x == 120 return 'in' elsif x == 1440 return 'ft' elsif x == 4 return 'mm' elsif x == 47 return 'cm' elsif x == 4724 return 'm' else throw "Unknown units" end end
I wouldn't trust this code until a bunch more people test it.
-
If you take an input number, a pure integer - then SU will assume it's in inches.
If the input that's an string that has a unit notation, like "20mm" or "30'" -then you can use String.to_l (http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/string.html#to_l) to convert it into a Length. (Length is the internal unit of SU - in inches.)If you take a Length unit and use the
.to_s
method - then it will print out the value in the unit of the current Model.If you have some fixed formulas you type in the code you find easier to type in mm then use the methods of the Numeric class.
100.mm + 320.mm
So in overview:
Ensure input is converted into SU's internal unit (inches). Make use of the Length class.
When you need to output the values to the user you use the.to_s
method of theLength
class which makes SU do all the dirty work of formatting the value to fit the current model's settings. -
@meeta said:
so says we ask the user to input this 'number' this is in inches automatically?
No. Number is in user's units. If it's a length, SketchUp stores it internally in inches, displays it in user's units.
And I apologize on behalf of myself and the rest of the citizens of my country for causing this confusion. Maybe some day we'll join the rest of the world. Maybe.
-
@martinrinehart said:
This won't set units, but will tell you what is set:
> def units > x = '120'.to_l.inspect.to_i > if x == 120 > return 'in' > elsif x == 1440 > return 'ft' > elsif x == 4 > return 'mm' > elsif x == 47 > return 'cm' > elsif x == 4724 > return 'm' > else > throw "Unknown units" > end > end >
I wouldn't trust this code until a bunch more people test it.
'120'.to_l
This will convert theString
into aLength
unit, assuming the value is in the current model Unit. So if your model unit is mm - then it will produce 4.7244094488189 inches.120.to_l
This will convert theInteger
into aLength
unit - assuming the value is inches. -
Best option for me, I reckon ask the user to input in his or her own units and then convert to mm for my calculations..
no offense to anyone.. but s ketchup should've been based on the metric system.. -
@meeta said:
no offense to anyone.. but s ketchup should've been based on the metric system..
Agree with you there. Then again - I'm a metric user.
@meeta said:
Best option for me, I reckon ask the user to input in his or her own units and then convert to mm for my calculations..
Why do you have to do this? With the helper methods of the Numeric class that let you write stuff like
100.mm
in your code? surely it's better to let the code do the work instead of the user? -
Because my constants are based on eurocode 3.. Which I definitely cannot change. I have constant such as this 25 kN/mm.. which I cannot avoid..
I reckon.. I will put something like this every time I multiply
h.to_mm*250
-
Yes, that's how you need to treat units in SU.
-
Thank you Thom and Martin.. I understand better now :
Advertisement