|
SketchUp Ruby API Reference |
Because length units are used so often in SketchUp, a special class has been added to make it easier to work with length values. The Length class is derived from the Float class. You can use a Length object any place that you would use a Float.
Internally, all lengths in SketchUp are stored in inches. The Length class stores values in inches
A number of methods have been added to the Ruby Numeric class to do units conversions.
Parent: Object
Methods: <, <=, <=>, ==, >, >=, inspect, to_f, to_s
Example Code: lengthtests.rb
The < method is used to see if one length is less than another length.
status = length1 < length2
length1 - a length value
length2 - a length value
status - true if length1 is < length2; false if length1 is not < length2
For example, if l1 = 1.0.inch and l2 = 1.000001.inch then l1 == l2 so l1 < l2 should return false.
length1 = 11
length2 = 12
if length1 < length2
UI.messagebox "Less than"
else
UI.messagebox "Failure"
end
The <= method is used to see if one length is less than or equal to another length.
status = length1 <= length2
length1 - a length value
length2 - a length value
status - true if length1 is <= length2; false if length1 is not <= length2
For example, if l1 = 1.0.inch and l2 = 1.000001.inch then l1 == l2 so l1 <= l2 should return true.
length1 = 11
length2 = 12
if 11 <= 12
UI.messagebox "Less than or Equals"
else
UI.messagebox "Failure"
end
The <=> method is used to see if one length is less than equal or greater than another length.
status = length1 <=> length2
length1 - a length value
length2 - a length value
status -
length1 = 11
length2 = 12
if length1 <=> length2
UI.messagebox "Success"
else
UI.messagebox "Failure"
end
The == method is used to see if one length is equal to another length.
status = length1 == length2
length1 - a length value
length2 - a length value
status - true if length1 is == length2; false if length1 is not == length2
The > method is used to see if one length is greater than another length.
status = length1 > length2
length1 - a length value
length2 - a length value
status - true if length1 is > length2; false if length1 is not > length2
For example, if l1 = 1.0.inch and l2 = 1.000001.inch then l1 == l2 so l1 > l2 should return false.
length1 = 11
length2 = 12
if length2 > length1
UI.messagebox "Greater Than"
else
UI.messagebox "Failure"
end
The >= method is used to see if one length is greater than or equal to another length.
status = length1 < length2
length1 - a length value
length2 - a length value
status - true if length1 is >= length2; false if length1 is not >= length2
For example, if l1 = 1.0.inch and l2 = 1.000001.inch then l1 == l2 so l1 >= l2 should return true. Also L1 <= l2 would return true.
length1 = 11
length2 = 12
if length2 >= length1
UI.messagebox "Greater Than or Equal"
else
UI.messagebox "Failure"
end
The inspect method is used to retrieve an unformatted string for the length.
length = length.inspect
length - an unformatted length string
depth = 100
width = 100
model = Sketchup.active_model
entities = model.active_entities
pts = []
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]
# Add the face to the entities in the model
face = entities.add_face pts
length = entities.length
str = length.inspect
The to_f method is used to convert a length to a normal float.
value = length.to_f
value - the float length value
length = entities.length
f = length.to_f
The to_f method is used to convert a length to a string using current units.
length = length.to_s
length - a length string
length = entities.length
s = length.to_s