How to round the corners of a tabletop in Sketchup using a Ruby script. I saw a youtube clip showing how to do that manually with creating an arc in a tabletop group and using pushpull to remove the excess of the material. Can anybody show me an example how to make it done with Ruby API.
Basically:
The Entities #add_arc method is used to create an arc curve segment.
The Face #pushpull method is used to perform a push/pull on a face.
You may need to do some other steps before, in between, and after so may need to learn the : https://ruby.sketchup.com/index.html
It’s even better if you’re familiar with Sketchup, first. There are better ways to do a tabletop, beside this video you linked.
Well, I am new to Sketchup but I am a software engineer and I tried some scripting using the API documentation already . It does not work as expected though. Here the tabletop is vertical, but that is not a problem.
model = Sketchup.active_model
entities = model.active_entities
model.start_operation('Create Wall and Arc', true)
width = 50.cm
height = 90.cm
thickness = 2.cm
radius = 2.cm
rect_face = entities.add_face([0, 0, 0], [width, 0, 0], [width, 0, height], [0, 0, height])
rect_face.pushpull(-thickness)
center1 = Geom::Point3d.new(0 + radius, 0, height - radius)
arc = entities.add_arc(center1, X_AXIS.reverse, Y_AXIS, radius, 0.degrees, 90.degrees, 12)
arc_face = entities.add_face(arc)
arc_face.pushpull(-thickness)
model.commit_operation
There are serval problems:
These coordinates not in a same plane.
it should be
[0, 0, 0], [width, 0, 0], [width, height, 0], [0, height, 0]
Again a coordinates are wrong for the centre, first for simplicity, I would define like:
center1 = Geom::Point3d.new(radius, radius, 0)
The normal of the arc also wrong.
The #add_arc
method Returns:
- (Array<Sketchup::Edge>) —
an array of Edge objects that define the arc.
You can not add a face to that, you need to give a Array of Sketchup::Vertex
or Point3d
You can get the vertices of the curve, if you take the first edge of arc
array, retrieve its curve and get its vertices.
Then you can map the vertices position and need to add an ORIGIN for start and end point of loop to be able to create a face.
model = Sketchup.active_model
entities = model.active_entities
model.start_operation('Create Wall and Arc', true)
width = 50.cm
height = 90.cm
thickness = 2.cm
radius = 2.cm
rect_face = entities.add_face([0, 0, 0], [width, 0, 0], [width, height, 0], [0, height, 0])
rect_face.pushpull(-thickness)
center1 = Geom::Point3d.new(radius, radius, 0)
arc = entities.add_arc(center1, X_AXIS.reverse, Z_AXIS, radius, 0.degrees, 90.degrees, 12)
vertices = arc.first.curve.vertices
arc_face_points = vertices.map(&:position).unshift(ORIGIN)<<ORIGIN
arc_face = entities.add_face(arc_face_points)
arc_face.pushpull(-thickness)
model.commit_operation
You are well ahead of the game then. Especially if you have had exposure to Object Oriented Programming in weakly typed languages (Python for example.)
In my Ruby Learning Resources lists there is a link to primers for programmers coming from other languages.
It explains the similarities and differences. Re: Ruby From Other Languages
FYI, the snippets in the docs are notorious for errors. We report them but, correcting them is low priority.
Issues · SketchUp/api-issue-tracker
Error:
#<NoMethodError: undefined method `pushpull' for nil:NilClass>
This comes from not verifying that an entities.add
call has resulted in a valid Drawinglement
object.
In this scenario, the add_face
call using an ArcCurve
as the object. The result is nil
because the arc is not a closed curve object, … or some other error cannot create the arc.
You could instead perhaps use arc.edges.first.find_faces
, see: Edge#find_faces
This method returns an integer. To get the new face you must use the snapshot subtract pattern:
before = entities.grep(SketchUp::Face)
arc.edges.first.find_faces
new_faces = entities.grep(SketchUp::Face) - before
face = new_faces[0]
if face # nil check
# use the face reference
end
Actually they are in the XZ plane. If you paste just down to the first pushpull
statement the table top is created fine.
You saved my day. Thank you!
You are right, actually I wanted to say it is in a wrong plane, because normally the tabletop should be horizontal… (for me much easier to determine the coordinates)