Lengths and Units in SketchUp
This article will hopefully shed some light on the nuances of lengths and units in SketchUp, how to understand them, and how to create better extensions, with less work!
The Theory
In most 3D modelers, lengths are represented by abstract numbers and the unit that translates these numbers into a physical distance in space is stored as metadata. In SketchUp, lengths and units work a bit differently. Internally, SketchUp always uses inches. The unit setting of a model is used to translate between inches and the unit of the user's choice, in the display layer only.
For end users it may make little difference, but it can be useful to know as developers. Thinking in inches can be upsetting for people outside of the USA. Luckily, you don't actually need to think of inches. The SketchUp length class encapsulates this well, and SketchUp's internal unit can be thought of largely as an implementation detail.
In practice
In SketchUp extensions, prefer defining distances using the Length class, not as floats or integers. The length class has a bunch of nice behaviors we'll get into in this article. It also makes the intent of the code clearer when you spell out a distance - such as the thickness of your lumber - as 2 inches and not 2 abstract units.
The API offers several methods for converting Numerics to Lengths.
# Bad
length1 = 1
length2 = 0.2
# Good
length1 = 1.feet
length2 = 0.2.mWhen showing the values to the user, rely on the Length#to_s method. This will automatically format the value according to the model's unit settings as well as the system's decimal separator.
# Bad
length = 200
length_string = length.to_s + " mm"
# Good
length =
