Problem adding polygons to a mesh


I’m trying to add a series of polygons to a mesh. I have defined num_pts and num_poly (around 87 in full) but get an error message I don’t understand when trying to add polygons from two predefined arrays of points, points1[ ] and points2[ ]

I believe the points are valid: before trying to make faces, I was able to draw and display curves in a component from the points arrays.

To simplify things, I only ran from i = 0 to 5, but there are 87 or 88 entries in points1 and points2 arrays, respectively. num_slices1 is defined earlier, and is 87.

#### Now we have the points, create a polygon mesh to add faces
num_pts = 2 * (num_slices1 + 1)
num_poly = num_slices1 + 1

mesh = num_pts, num_poly

i = 0
while i < 5  do
  mesh.add_polygon points1[i], points2[i], points2[i+1], points1[i+1]
  i += 1
ellipse_comp.definition.add_faces_from_mesh mesh

When run, this gives the error:
Error: #<ArgumentError: point index -9000 out of range> /Users/JohnWMcC/Downloads/RiverArch/RiverArch eliipses drawing.rb:154:inadd_polygon’
/Users/JohnWMcC/Downloads/RiverArch/RiverArch eliipses drawing.rb:154:in <top (required

Why is the point index negative?

PS. I have just realised that the above code doesn’t close the polygon, but this does, and still gives the same error.

mesh.add_polygon points1[i], points2[i], points2[i+1], 
  points1[i+1], points1[i]

And when I try again with just the first face of points, hard coded, like this:

puts "#{points1[0]}, #{points2[0]}, #{points2[1]}, #{points1[1]}, #{points1[0]}"
   mesh.add_polygon points1[0], points2[0], points2[1], points1[1], points1[0]

this outputs (from the puts statement)
[-9000.0, 0.0, 0.0], [-9150.0, 0.0, 0.0], [-9149.419144427724, 24.0, 120.0], [-8999.412225704702, 24.0, 120.0], [-9000.0, 0.0, 0.0]
and gives the same error message.

I see that the first value points1[0] is -9000 (inches) - why is this being treated as a point_index?

PPS: Should I just be iterating through each individual point to add, one at a time, using add_point?


PolygonMesh indexes start at 1 rather than 0.


AHA - that would likely be it. I’d read that recently, but forgotten.

But I’m not required to specify a point index for the added polygon, am I?

Current (SU 2017, which I’m using) Ruby API docs say:
The #add_polygon method is used for adding a polygon to a polygon mesh.


mesh =
point1 =, 1, 2)
point2 =, 0, 2)
point3 =, 0, 1)
index = mesh.add_polygon(point1, point2, point3)
So I need to redefine my addition like the last line of the example from the Ruby API docs?


But that still doesn’t work:

mesh = num_pts, num_poly

# i = 0
# while i < 1  do
puts "#{points1[0]}, #{points2[0]}, #{points2[1]}, #{points1[1]}, #{points1[0]}"
   index = mesh.add_polygon points1[0], points2[0], points2[1], points1[1], points1[0]

This still bombs with the same error.

I’m missing something here.

Must go back to earlier code I wrote for the Polyhedra plugin…

There I had the following, and that works. AND I didn’t need to close the polygon.

What’s different here?

# Create successive faces
  # Base - draw clockwise to face outside down
  mesh.add_polygon points_base[0], points_base[4], points_base[3], points_base[2], points_base[1]

  # First row of surrounding faces
  for i in 0..4
    mesh.add_polygon points_base[i], points_base[i+1], points_lower[i+1], points_upper[i+1], points_lower[i]

I still can’t see what I’m doing differently this time, or wrong.

Will try via add_point instead, and iterate through the points.


Only one difference I can see.

In the Polyhedra code, the points arrays are explicitly defined as

In my current code, they are just arrays. But that worked with add_curve.

I wonder if that matters here? Usually, they are equivalent, but perhaps not in this case??


I use a mesh for a curved stair case stringer. The key bit is to make sure that the points that make up a face are co planar. So I perform a test and if not co planar then I triangulate.


Here is a copy of the stringer that is smooth. They both are solids


Can you share the entire code?


Well, in case it mattered, I have tried again replacing all definitions of points1[i] = and points2[i] = [x,y,z] by points1[i] =,y,z).

I still have these lines earlier in the code:

points1 = []
points2 = []

Does that matter? Apparently yes, because without them, I get an undefined variable the first time I try to assign a value to points1[i].

This time, I get a different error message, which I still don’t understand, relating to the line:

 mesh.add_point points1[i]

Error: #<ArgumentError: Cannot convert argument to Sketchup::Point3d> /Users/JohnWMcC/Downloads/RiverArch/RiverArch ellipses drawing.rb:154:inadd_point’
but as far as I can see it IS a 3d point.

Tried once more using mesh.add_polygon and this time start to get sensible answers.

So I’ll go with that.


In the STL Importer, we are using Arrays to populate the PolygonMesh.


I thought my problem was not using Point3d, but I’ve found another bug in the code, where I was mistakenly writing:
num_slices1 += num_slices1 instead of num_slices1 += 1.

DOH! I still make dumb mistakes in Ruby.

I didn’t see it initially, since I was testing with a hard coded value for the upper limit of i.

So it probably would now work without explicitly setting the array values to Point3d objects.

Thanks for helping to point me in the right directions. Got there in the end.