Get Current Units
-
I was wondering if there was a way to get (in a ruby script) the current units that are being used in the model. The reason is that I want to make a dialog box that asks for dimensions from the user and it would be nice if the user could simply enter a numerical value corresponding to the length in the model's current units set in Model Info>Units.
Someone please let me know if it is possible to do this or if it is somehow unnecessary.
-
Yes model.options
See:
http://code.google.com/apis/sketchup/docs/ourdoc/optionsprovider.htmlNote the code examples, and ThomThoms comments posted at the bottom of the page.
You will also need to be aware of the conversion methods that the API adds to the
Numeric
classes (because internally all distances are in inches.) -
Sketchup.active_model.options["UnitsOptions"]["LengthUnit"]
returns an integer - this corresponds to the units as follows:0 = "
1 = '
2 = mm
3 = cm
4 = m The whole set of UnitOptions are:
[["LengthPrecision", 0], ["LengthFormat", 0], ["LengthUnit", 2], ["LengthSnapEnabled", true], ["LengthSnapLength", 0.0393700787401575], ["AnglePrecision", 1], ["AngleSnapEnabled", true], ["SnapAngle", 15.0], ["SuppressUnitsDisplay", true], ["ForceInchDisplay", false]]
This .to_a is for a SKP with Model Info > Units set to display to nearest-unit, in decimal format, with no unit suffix, in 'mm' units, and snapping to nearest 1 unit, etc...
Always get values from an options type by 'name' like ["UnitsOptions"]["LengthUnit"], rather than the element 'index' like ["UnitsOptions"][2] because the order they are listed in can change between SUp versions etc... -
Thanks guys. I love how active this whole forum is.
-
If you convert a string number into a Length (
.to_l
) and the string contains no unit indication it assumes the units is the current model unit."50".to_l
Returns a Length:1.96850393700787
(assuming the model units where mm)"50m".to_l
Returns a Length:1968.50393700787
regardless of what the model units are because the unit is specified.
Advertisement