Point3d#distance_to_line doesn't work according to the model units


#1

My model unit is metric. So the length result of the below transcript should have been 25.4mm. Note how to_f.to_l returns the right result

s = Geom::Point3d.new(0,0,0)
(0mm, 0mm, 0mm)
e = Geom::Point3d.new(1,0,0)
(25.4mm, 0mm, 0mm)
s.distance_to_line([e, [0,1,0]])
1.0
s.distance_to_line([e, [0,1,0]]).to_s
1.0
s.distance_to_line([e, [0,1,0]]).to_f
1.0
s.distance_to_line([e, [0,1,0]]).to_f.to_l
~ 25mm

#2

it is well documented that SU uses inch as it’s internal unit of measure…

it’s American after all…

but you missed some of the convenience methods that can help…

Sketchup.format_length(s.distance_to_line([e, [0,1,0]]))  #=> 25.4000

s.distance_to_line([e, [0,1,0]]).to_mm #=> 25.4

john


#3

All Floats (without specified unit) are consistently treated as inch. Length is a subclass of Numeric that provides explicit unit conversions, including the string conversion that implicitely defaults to model units.

1.0.to_l creates a length object of 1 inch (internal unit of Length), and printing it to the console calls to_s which actually converts the internal unit to your locally set millimeters.

I am even wondering whether Geom::Point#distance_to_line → Float is an API Bug and should rather return a Length?


#4

Sorry everyone, my mistake, while fiddling with distance_to_line I was sure it returned a Length instance (I was even sure I checked it). As a float it makes perfect sense to return 1.0 of course


#5

Toward the betterment of the general readership’s API knowledge:

One of the actual documentation references:

Length class’ introductory docstring:

Internally, all lengths in SketchUp are stored in inches. The Length class stores values in inches as well. A number of methods have been added to the Ruby Numeric class to do units conversion.
…
The setting for the Length format and Length unit can be retrieved from the model options by querying the “UnitsOptions” OptionsProvider for “LengthFormat” and “LengthUnit” respectively.

One of the best blog posts on lengths and units is Thomas Thomassen’s blog article:


#6

I looked at the API doc, and indeed it is incorrect, the actual class of the return value is not specified, and it erroneously says it returns in model units.

So, nice find. This should be reported in the API stubs repo.


#7

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.