Ruby for sphere

488 views
Skip to first unread message

Gaieus

unread,
Nov 25, 2006, 4:12:28 PM11/25/06
to Google SketchUp Help - Ruby ideas
In the "How to draw a sphere"
(http://groups.google.com/group/sketchuphowto/tree/browse_frm/thread/dec99130eb6955f1/96b5ddc0d56e2f9f?rnum=1&_done=%2Fgroup%2Fsketchuphowto%2Fbrowse_frm%2Fthread%2Fdec99130eb6955f1%2F%3F#doc_27daef77ed306f25)
thread we have come to the pint that this most frequently asked
question could also be solved by a ruby script.

John posted a solution in the thread that we need to copy/paste into
the ruby console in SU, but there is also a handy ruby out there
(shapes.rb) that could even be modified with this addition.

SwaJime

unread,
Nov 26, 2006, 10:39:26 PM11/26/06
to Google SketchUp Help - Ruby ideas
Gaieus,

You can copy/paste the following into a new text file (sphere.rb for
example) with an .rb extension. Place the file in the same directory
with shapes.rb.

require 'shapes.rb'
class Sphere < Parametric
def create_entities(data, e)
r = data["r"].to_l; n = 24
c1 = e.add_circle(ORIGIN,Z_AXIS,r,n)
c2 = e.add_circle(ORIGIN,X_AXIS,2*r,n)
f = e.add_face(c1)
status = f.followme(c2)
status = c2.each {|edge| edge.erase!}
end
def default_parameters; {"r", 6.feet}; end
def translate_key(key); "Radius"; end
end
if $sphere_menu.nil? then sphere_menu =
UI.menu("Draw").add_item("Sphere") { Sphere.new }; end

Yafei Guan

unread,
Apr 23, 2016, 8:42:33 AM4/23/16
to SketchUp Ruby API, jo...@swajime.com
I used this code to draw and then rotate it/them to be multiple spheres on parallel rings. I can draw a sphere at any position and I can move it as a group and rotate it to generate more on a ring. but I still want to make all the spheres on the same ring to be a group or a component, then I can move or rotate them as a piece.
Could any body help me to figure it out how to make a group / component?
+++++++++++++++
c1 = group1.entities.add_circle(ball_center, main, ball_radius)
c2 = group1.entities.add_circle(ball_center, path_main, ball_radius+1)
f = group1.entities.add_face(c1)
f.material= ball_color
f.material.alpha = 10
ball0=f.followme(c2)
ball0=c2.each {|edge| edge.erase!}
new_comp_def=group1.to_component

range=5
n=29
#0 1 2 3 4 5,,,29
y_location=n*35 # 0 35 70 105,,,,,+35
componentinstance_ball=Array.new( number_of_ball_per_ring )

# generate next ball on same ring
for i in 0..(number_of_ball_per_ring-1)
transformation_ball = Geom::Transformation.new [0, 0, ring_high], [0, ring_high, 0], radians*i+rand()*range*Math::PI/180 
#componentinstance_ball[i] = entities.add_instance(new_comp_def.definition, transformation_ball)
componentinstance_ball[i] = Sketchup.active_model.active_entities.add_instance(new_comp_def.definition, transformation_ball) 

end

+++++++++++++++
Reply all
Reply to author
Forward
0 new messages