Length method returns unexpected value
-
Can anyone tell me why when I do
Sketchup.active_model.entities.add_edges([0,0,0],[1,1,0]).length
it returns "1" instead of the expected "1.414..."?
It appears to return the number of edges rather than their sum length. Curve does the same.
-
Because you added 1 edge.
In other words,
add_edges
returned an Array containing 1 Edge object.[<Edge>]
Your example is asking the length of the Array.
You want to ask the length of the first Edge (index 0) in the Array:
Sketchup.active_model.entities.add_edges([0,0,0],[1,1,0])[0].length
-
Or:
Sketchup.active_model.entities.add_line([0,0,0],[1,1,0]).length
-
OK, .length returns the expected length for add_line, but for add_edges and add_curve it returns the array length. Not obvious, but I can live with that.
What I really want is to get the length of a curve, such as the Curve.length example code http://www.sketchup.com/intl/en/developer/docs/ourdoc/curve#length suggests. Stranger yet, their code returns 47.123... rather than the expected 62.652... (sum of segments) or 62.831... (ideal circle) Can anyone explain the 47.123... number? The workaround, which is working for me, is to iterate through the array and sum them:
edge.each { |x| $total_length+=x.length }
-
{ SNIP }
If you typed the example as it is, the radius will be 10 inches, regardless of the model units.
What do you have the model units set as ? -
I did a quick test, with a 10" radius circle, default 24 segments.
It does not matter what the model units are set to, the API'sedge.length()
andcircle.length()
return values in inches (even though the API doc says "current units".)It did not matter what the number of segments were, the value return by
e.curve.length
is always:
%(#004000)[62.8318530717959]
which is the perfect circumference, not the sum of the current number of display segments.So your summing the edges' lengths is appropriate.
You can do it without a global accumulator, thus:
clen = curve.edges.inject(0) {|sum, e| sum + e.length }
-
@orthogon said:
but for add_edges and add_curve it returns the array length. Not obvious, but I can live with that.
The API docs is for once correct, and the methods returns exactly what it says:
http://www.sketchup.com/intl/en/developer/docs/ourdoc/entities#add_edges
@unknownuser said:
an array of Edge objects if successful
http://www.sketchup.com/intl/en/developer/docs/ourdoc/entities#add_curve
@unknownuser said:
an array of Edges that make up the curve if successful
And I'd say it makes more sense to return the edges, like they currently do, instead of returning the combined geometric length of the edges. With the actual edges you can get the length and any other information you might need from the entities you just added. It's much more versatile.
-
@dan rathbun said:
I did a quick test, with a 10" radius circle, default 24 segments.
It does not matter what the model units are set to, the API'sedge.length()
andcircle.length()
return values in inches (even though the API doc says "current units".)"Current units" is a bit weak description, I agree. SketchUp uses inches at any given time. It's only when you convert
Length
objects to strings you get to see the unit in the format of the model's settings.http://www.sketchup.com/intl/en/developer/docs/ourdoc/length
@unknownuser said:
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 conversions.
-
On the topic of units in SketchUp: http://www.thomthom.net/thoughts/2012/08/dealing-with-units-in-sketchup/
Advertisement