Inche fraction in ruby script
-
hi guys, here's my code
centerpoint = Geom::Point3d.new
Create a circle perpendicular to the normal or Z axis
vector = Geom::Vector3d.new 0,1,0
vector2 = vector.normalize!
model = Sketchup.active_model
entities = model.active_entities
edges = entities.add_circle centerpoint, vector2, 2.inch
faceCircle = entities.add_face(edges)
faceCircle.pushpull heighthowever, I'd like to have 2 1/8 inches in the radius ... can I write it directly in the code and if so, how ? Otherwise, I guess I need to convert into a float number ? thanks
-
2 1/8 can easily be re-written as 2.125, so why not just write it like that? I'm guessing this is a question that is actually larger than just the example provided?
Also, SU ruby works in inches, so there is no need to add the
.inches
to the end of the2
. But if you'd really like it to accept a fraction, you could write 2 1/8 as(2.0)+(1.0/8.0)
. That should be a workable solution. Good luck,Chris
-
And also, of course, you may set your units in the Model Info dialog, Units page, to either Architectural or Fractional, (and choose precision,) if you wish the app UI to run in fractions.
-
@nics said:
however, I'd like to have 2 1/8 inches in the radius ... can I write it directly in the code and if so, how ?
Yes. In Ruby (and in Math,) fractions are really just division.
@nics said:
Otherwise, I guess I need to convert into a float number ?
Again yes, you need to convert using the .to_f method, otherwise Ruby will truncate the fractional part and just return the Integer part (if both the numerator and denominator are Integer.)
Here's an example:
` mod=Sketchup.active_model#Sketchup::Model:0x60aeb04
ents=mod.entities
#Sketchup::Entities:0x60a3434
pt1=[12+1/8.to_f,14+5/8.to_f,0+7/8.to_f]
[12.125, 14.625, 0.875]
pt2=[33+1/2.to_f,67+3/4.to_f,47+3/8.to_f]
[33.5, 67.75, 47.375]
line=ents.add_line( pt1, pt2 )
#Sketchup::Edge:0x602d1bc
p line.start.position
Point3d(12.125, 14.625, 0.875)
p line.end.position
Point3d(33.5, 67.75, 47.375)` -
You can also use strings, and apply the Sketchup extension to the String class, which adds the .to_l method.
Example:
` length = '5 3/8"'.to_l5.375`
See the API (Base Classes): String
http://code.google.com/apis/sketchup/docs/ourdoc/string.html -
Hi nics.
You can use fractions pretty easily.
entities = Sketchup.active_model.active_entities ; face = entities.add_face(entities.add_circle(ORIGIN, Y_AXIS, 2.125.inch)).pushpull 4.inch ; or face = entities.add_face(entities.add_circle(ORIGIN, Y_AXIS, '2 1/8"'.to_l)).pushpull 4.inch ;
Note that there are many predefined constants for common locations/vectors you can use, and when it's not too cryptic for you, you can string commands together as well. Paste the above 2 lines into the Ruby Console and you'll get the same result.
Todd
-
wow thank you guys its really appreciated
Advertisement