Checking Template Units
-
I'm sure this has been asked before but I'm not seeming to find it so I will ask again. I want to be able to detect whether the user is using a metric or imperial template so I can customize my plugin user interface to the correct units. What is the suggested way to check for metric vs. imperial units of the template being used?
-
Btw, I wrote a general article on how to deal with units in SketchUp: http://www.thomthom.net/thoughts/2012/08/dealing-with-units-in-sketchup/
-
You don't really check the templates - you just ask the current model the user is using. The current model might be of a different unit than their selected template.
At my previous job we switched a lot between mm and m in our project files.You use model.options to inspect the current model's options:
options_manager = Sketchup.active_model.options options_manager.keys.each { |options_provider_name| puts options_provider_name options_provider = options_manager[options_provider_name] options_provider.each { |key, value| puts "> #{key} - #{value}" } }
Output from that snippet:
PageOptions > ShowTransition - false > TransitionTime - 2.0 UnitsOptions > LengthPrecision - 0 > LengthFormat - 0 > LengthUnit - 2 > LengthSnapEnabled - false > LengthSnapLength - 0.03937007874015748 > AnglePrecision - 1 > AngleSnapEnabled - true > SnapAngle - 15.0 > SuppressUnitsDisplay - false > ForceInchDisplay - false SlideshowOptions > LoopSlideshow - true > SlideTime - 1.0 NamedOptions PrintOptions > PrintWidth - 8.5 > PrintHeight - 11.0 > ComputeSizeFromScale - false > SizeInPrint - 1.0 > SizeInModel - 1.0 > VectorMode - false > FitToPage - true > NumberOfPages - 1 > LineWeight - 0.5 > PixelsPerInch - 150.0 > SectionSlice - false > ModelExtents - true > PrintQuality - 0 > ScaleAdjustment - 1.0 > QualityAdjustment - 1.0
So in this case you want to inspect UnitOptions:
model = Sketchup.active_model model.options["UnitsOptions"]["LengthUnit"]
Note that you want to use the constants in the class
Length
against the values returned here.Length;;Decimal Length;;Architectural Length;;Engineering Length;;Fractional Length;;Inches Length;;Feet Length;;Millimeter Length;;Centimeter Length;;Meter
So something like this:
model = Sketchup.active_model unit_type = model.options["UnitsOptions"]["LengthUnit"] metric = case unit_type when Length;;Millimeter, Length;;Centimeter, Length;;Meter true else false end
I'm doing this check because LengthFormat got options that can be used with both imperial and metric units.
Advertisement