How to draw a cylinder by ruby?
-
group=Sketchup.active_model.active_entities.add_group() ents=group.entities edges=ents.add_circle(center_point, normal_vector, radius, segments) face=edges[0].face face.pushpull(height)
center_point
is the point at which the center of the cylinder's bottom face sits...
normal_vector
is the direction of the circular face [which will become the cylinder's bottom face] - typicallyZ_AXIS
...
radius
the circular face's and therefore the cylinder's radius...
segments
the number of segments in the circle and thereby the cylinder's 'side' faces...
height
is the cylinder's height.When you have made the cylinder you can use
group.explode
to return the cylinder's geometry to the active context if desired... -
Thank you very much! You provided just enough for me create my first Ruby SketchUp script (it makes cylinders) and I figured out how to integrate it with SketchUp..all in just a few hours..lol! It was an ideal challenge--I feel like I'm off and running! I should mention, for the sake of anyone reading this, that some background I had in O-O programming and some reading I did about Ruby, were invaluable too. I just registered at this site and I am looking forward to learning more about programming for SketchUp here.
Bill
-
Great, welcome to the site. Good luck with your next Ruby endevor!
-
@tig said:
group=Sketchup.active_model.active_entities.add_group() > ents=group.entities > edges=ents.add_circle(center_point, normal_vector, radius, segments) > face=edges[0].face > face.pushpull(height)
center_point
is the point at which the center of the cylinder's bottom face sits...
normal_vector
is the direction of the circular face [which will become the cylinder's bottom face] - typicallyZ_AXIS
...
radius
the circular face's and therefore the cylinder's radius...
segments
the number of segments in the circle and thereby the cylinder's 'side' faces...
height
is the cylinder's height.When you have made the cylinder you can use
group.explode
to return the cylinder's geometry to the active context if desired...Sorry, but that doesn´t works:
center_point = Geom;;Point3d.new(10, 10, 10) => Point3d(10, 10, 10) vector = Geom;;Vector3d.new(0,0,1) => Vector3d(0, 0, 1) => nil radius = 5 => 5 segments = 10 => 10 height = 2 => 2 => nil group=Sketchup.active_model.active_entities.add_group() => #<Sketchup;;Group;0x821f824> ents=group.entities => #<Sketchup;;Entities;0x821f298> => nil edges=ents.add_circle(center_point, Z_AXIS, radius, segments) => [#<Sketchup;;Edge;0x821e190>, #<Sketchup;;Edge;0x821e168>, #<Sketchup;;Edge;0x821e154>, #<Sketchup;;Edge;0x821e140>, #<Sketchup;;Edge;0x821e12c>, #<Sketchup;;Edge;0x821e118>, #<Sketchup;;Edge;0x821e104>, #<Sketchup;;Edge;0x821e0f0>, #<Sketchup;;Edge;0x821e0dc>, #<Sketchup;;Edge;0x821e0c8>] => nil face=edges[0].face NoMethodError; undefined method `face' for #<Sketchup;;Edge;0x821e190>
-
face=edges[0].faces[0]
-
@thomthom said:
face=edges[0].faces[0]
There is also an error:
center_point = Geom;;Point3d.new(10, 10, 10) => Point3d(10, 10, 10) vector = Geom;;Vector3d.new(0,0,1) => Vector3d(0, 0, 1) => nil radius = 5 => 5 segments = 10 => 10 height = 2 => 2 => nil group=Sketchup.active_model.active_entities.add_group() => #<Sketchup;;Group;0x82075d0> ents=group.entities => #<Sketchup;;Entities;0x8206d60> => nil edges=ents.add_circle(center_point, Z_AXIS, radius, segments) => [#<Sketchup;;Edge;0x8205adc>, #<Sketchup;;Edge;0x8205ac8>, #<Sketchup;;Edge;0x8205a64>, #<Sketchup;;Edge;0x8205a50>, #<Sketchup;;Edge;0x82059ec>, #<Sketchup;;Edge;0x82059d8>, #<Sketchup;;Edge;0x82059b0>, #<Sketchup;;Edge;0x820599c>, #<Sketchup;;Edge;0x8205988>, #<Sketchup;;Edge;0x8205974>] face=edges[0].faces[0] => nil face.pushpull(height) NoMethodError; undefined method `pushpull' for nil;NilClass
-
edges=ents.add_circle(center_point, Z_AXIS, radius, segments) edges[0].find_faces face=edges[0].faces[0]
or (maybe better option)
edges=ents.add_circle(center_point, Z_AXIS, radius, segments) face=ents.add_face( edges )
-
We have all gone adrift here...
You have a set ofedges
forming a circular 'ring' - nothing more.
We've missing a line of code!
To make a face from the edges use
face=ents.add_face(edges)
then to ensure it's the inner face that the edges delineate use
face=edges[0].faces[0]
NOW face will be the inner face of the circle - this is useful in case the 'ring' is already drawn over a face etc and you might end up with two faces touching an edge...EDIT: I see Thomthom beat me to it!
-
Hi guys,
I use the following code to set the center point = position of the mouse click:def onLButtonDown(flags, x, y, view)
puts " x = "
puts " y = "
puts "--------------------- "
centerpoint = Geom::Point3d.new x,y,0However, the (x,y) seems not to be my mouse's click position. It is just random.
How could I set the center point to be my mouse click position? -
hi
have a look atlinetool.rb
andclass CylTool < LineTool
in the Example scripts folder in ../SketchUp/Plugins/examples/linetool.rb, or download it if it's not......john
Advertisement